blob: 758a62aba063d3f00102e88c8f674140b6ac5b66 [file] [log] [blame]
Chris Masone20d96d2007-03-22 12:13:20 -04001#include <linux/module.h>
2#include <linux/fs.h>
Chris Masoneb60cea2007-02-02 09:18:22 -05003#include "ctree.h"
4#include "disk-io.h"
Chris Masone089f052007-03-16 16:20:31 -04005#include "transaction.h"
Chris Masoneb60cea2007-02-02 09:18:22 -05006
Chris Masone20d96d2007-03-22 12:13:20 -04007static int check_tree_block(struct btrfs_root *root, struct buffer_head *buf)
Chris Masoneb60cea2007-02-02 09:18:22 -05008{
Chris Masone20d96d2007-03-22 12:13:20 -04009 struct btrfs_node *node = btrfs_buffer_node(buf);
10 if (buf->b_blocknr != btrfs_header_blocknr(&node->header))
Chris Mason9a8dd152007-02-23 08:38:36 -050011 BUG();
Chris Masone20d96d2007-03-22 12:13:20 -040012 if (root->node && btrfs_header_parentid(&node->header) !=
Chris Masondf2ce342007-03-23 11:00:45 -040013 btrfs_header_parentid(btrfs_buffer_header(root->node))) {
Chris Mason7f5c1512007-03-23 15:56:19 -040014 BUG();
Chris Masondf2ce342007-03-23 11:00:45 -040015 }
Chris Mason9a8dd152007-02-23 08:38:36 -050016 return 0;
Chris Masoneb60cea2007-02-02 09:18:22 -050017}
18
Chris Masone20d96d2007-03-22 12:13:20 -040019struct buffer_head *alloc_tree_block(struct btrfs_root *root, u64 blocknr)
Chris Masoned2ff2c2007-03-01 18:59:40 -050020{
Chris Masone20d96d2007-03-22 12:13:20 -040021 return sb_getblk(root->fs_info->sb, blocknr);
Chris Masoned2ff2c2007-03-01 18:59:40 -050022}
23
Chris Masone20d96d2007-03-22 12:13:20 -040024struct buffer_head *find_tree_block(struct btrfs_root *root, u64 blocknr)
Chris Masoneb60cea2007-02-02 09:18:22 -050025{
Chris Masone20d96d2007-03-22 12:13:20 -040026 return sb_getblk(root->fs_info->sb, blocknr);
27}
Chris Mason123abc82007-03-14 14:14:43 -040028
Chris Masone20d96d2007-03-22 12:13:20 -040029struct 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 Masoneb60cea2007-02-02 09:18:22 -050033 if (!buf)
34 return buf;
Chris Mason9a8dd152007-02-23 08:38:36 -050035 if (check_tree_block(root, buf))
Chris Masoncfaa7292007-02-21 17:04:57 -050036 BUG();
Chris Masoneb60cea2007-02-02 09:18:22 -050037 return buf;
38}
39
Chris Masone089f052007-03-16 16:20:31 -040040int dirty_tree_block(struct btrfs_trans_handle *trans, struct btrfs_root *root,
Chris Masone20d96d2007-03-22 12:13:20 -040041 struct buffer_head *buf)
Chris Masoned2ff2c2007-03-01 18:59:40 -050042{
Chris Masone20d96d2007-03-22 12:13:20 -040043 mark_buffer_dirty(buf);
Chris Masoned2ff2c2007-03-01 18:59:40 -050044 return 0;
45}
46
Chris Masone089f052007-03-16 16:20:31 -040047int clean_tree_block(struct btrfs_trans_handle *trans, struct btrfs_root *root,
Chris Masone20d96d2007-03-22 12:13:20 -040048 struct buffer_head *buf)
Chris Masoned2ff2c2007-03-01 18:59:40 -050049{
Chris Masone20d96d2007-03-22 12:13:20 -040050 clear_buffer_dirty(buf);
Chris Masoned2ff2c2007-03-01 18:59:40 -050051 return 0;
52}
53
Chris Mason123abc82007-03-14 14:14:43 -040054static int __setup_root(struct btrfs_super_block *super,
Chris Mason9f5fae22007-03-20 14:38:32 -040055 struct btrfs_root *root,
56 struct btrfs_fs_info *fs_info,
Chris Masone20d96d2007-03-22 12:13:20 -040057 u64 objectid)
Chris Masond97e63b2007-02-20 16:40:44 -050058{
Chris Masoncfaa7292007-02-21 17:04:57 -050059 root->node = NULL;
Chris Masona28ec192007-03-06 20:08:01 -050060 root->commit_root = NULL;
Chris Mason123abc82007-03-14 14:14:43 -040061 root->blocksize = btrfs_super_blocksize(super);
62 root->ref_cows = 0;
Chris Mason9f5fae22007-03-20 14:38:32 -040063 root->fs_info = fs_info;
Chris Mason3768f362007-03-13 16:47:54 -040064 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 Mason123abc82007-03-14 14:14:43 -040069static int find_and_setup_root(struct btrfs_super_block *super,
Chris Mason9f5fae22007-03-20 14:38:32 -040070 struct btrfs_root *tree_root,
71 struct btrfs_fs_info *fs_info,
72 u64 objectid,
Chris Masone20d96d2007-03-22 12:13:20 -040073 struct btrfs_root *root)
Chris Mason3768f362007-03-13 16:47:54 -040074{
75 int ret;
76
Chris Masone20d96d2007-03-22 12:13:20 -040077 __setup_root(super, root, fs_info, objectid);
Chris Mason3768f362007-03-13 16:47:54 -040078 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 Mason3768f362007-03-13 16:47:54 -040084 BUG_ON(!root->node);
Chris Masond97e63b2007-02-20 16:40:44 -050085 return 0;
86}
87
Chris Masone20d96d2007-03-22 12:13:20 -040088struct btrfs_root *open_ctree(struct super_block *sb,
89 struct buffer_head *sb_buffer,
90 struct btrfs_super_block *disk_super)
Chris Masoneb60cea2007-02-02 09:18:22 -050091{
Chris Masone20d96d2007-03-22 12:13:20 -040092 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 Masoneb60cea2007-02-02 09:18:22 -0500102 int ret;
103
Chris Masone20d96d2007-03-22 12:13:20 -0400104 /* FIXME: don't be stupid */
105 if (!btrfs_super_root(disk_super))
106 return NULL;
Chris Mason8ef97622007-03-26 10:15:30 -0400107 init_bit_radix(&fs_info->pinned_radix);
108 init_bit_radix(&fs_info->pending_del_radix);
Chris Mason9f5fae22007-03-20 14:38:32 -0400109 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 Masone20d96d2007-03-22 12:13:20 -0400116 fs_info->disk_super = disk_super;
117 fs_info->sb_buffer = sb_buffer;
118 fs_info->sb = sb;
Chris Mason79154b12007-03-22 15:59:16 -0400119 mutex_init(&fs_info->trans_mutex);
Chris Masond561c022007-03-23 19:47:49 -0400120 mutex_init(&fs_info->fs_mutex);
Chris Mason9f5fae22007-03-20 14:38:32 -0400121 memset(&fs_info->current_insert, 0, sizeof(fs_info->current_insert));
122 memset(&fs_info->last_insert, 0, sizeof(fs_info->last_insert));
Chris Mason3768f362007-03-13 16:47:54 -0400123
Chris Masone20d96d2007-03-22 12:13:20 -0400124 __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 Mason3768f362007-03-13 16:47:54 -0400127 BUG_ON(!tree_root->node);
128
Chris Masone20d96d2007-03-22 12:13:20 -0400129 ret = find_and_setup_root(disk_super, tree_root, fs_info,
130 BTRFS_EXTENT_TREE_OBJECTID, extent_root);
Chris Mason3768f362007-03-13 16:47:54 -0400131 BUG_ON(ret);
132
Chris Masone20d96d2007-03-22 12:13:20 -0400133 ret = find_and_setup_root(disk_super, tree_root, fs_info,
134 BTRFS_INODE_MAP_OBJECTID, inode_root);
Chris Mason9f5fae22007-03-20 14:38:32 -0400135 BUG_ON(ret);
136
Chris Masone20d96d2007-03-22 12:13:20 -0400137 ret = find_and_setup_root(disk_super, tree_root, fs_info,
138 BTRFS_FS_TREE_OBJECTID, root);
Chris Mason3768f362007-03-13 16:47:54 -0400139 BUG_ON(ret);
140
Chris Masona28ec192007-03-06 20:08:01 -0500141 root->commit_root = root->node;
Chris Masone20d96d2007-03-22 12:13:20 -0400142 get_bh(root->node);
Chris Mason3768f362007-03-13 16:47:54 -0400143 root->ref_cows = 1;
Chris Mason293ffd52007-03-20 15:57:25 -0400144 root->fs_info->generation = root->root_key.offset + 1;
Chris Masoneb60cea2007-02-02 09:18:22 -0500145 return root;
146}
147
Chris Masone089f052007-03-16 16:20:31 -0400148int write_ctree_super(struct btrfs_trans_handle *trans, struct btrfs_root
Chris Mason79154b12007-03-22 15:59:16 -0400149 *root)
Chris Masoncfaa7292007-02-21 17:04:57 -0500150{
Chris Masond5719762007-03-23 10:01:08 -0400151 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 Masoncfaa7292007-02-21 17:04:57 -0500163 }
164 return 0;
165}
166
Chris Masone20d96d2007-03-22 12:13:20 -0400167int close_ctree(struct btrfs_root *root)
Chris Masoneb60cea2007-02-02 09:18:22 -0500168{
Chris Mason3768f362007-03-13 16:47:54 -0400169 int ret;
Chris Masone089f052007-03-16 16:20:31 -0400170 struct btrfs_trans_handle *trans;
171
Chris Mason79154b12007-03-22 15:59:16 -0400172 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 Mason9f5fae22007-03-20 14:38:32 -0400178 BUG_ON(ret);
Chris Mason79154b12007-03-22 15:59:16 -0400179 write_ctree_super(NULL, root);
Chris Masoned2ff2c2007-03-01 18:59:40 -0500180
Chris Masoneb60cea2007-02-02 09:18:22 -0500181 if (root->node)
Chris Mason234b63a2007-03-13 10:46:10 -0400182 btrfs_block_release(root, root->node);
Chris Mason9f5fae22007-03-20 14:38:32 -0400183 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 Mason234b63a2007-03-13 10:46:10 -0400192 btrfs_block_release(root, root->commit_root);
Chris Masone20d96d2007-03-22 12:13:20 -0400193 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 Masoneb60cea2007-02-02 09:18:22 -0500199 return 0;
200}
201
Chris Masone20d96d2007-03-22 12:13:20 -0400202void btrfs_block_release(struct btrfs_root *root, struct buffer_head *buf)
Chris Masoneb60cea2007-02-02 09:18:22 -0500203{
Chris Masone20d96d2007-03-22 12:13:20 -0400204 brelse(buf);
Chris Masoneb60cea2007-02-02 09:18:22 -0500205}
206