blob: df2061a735cd5601b15db9918b7855c012bfc457 [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) !=
13 btrfs_header_parentid(btrfs_buffer_header(root->node)))
Chris Mason9a8dd152007-02-23 08:38:36 -050014 BUG();
15 return 0;
Chris Masoneb60cea2007-02-02 09:18:22 -050016}
17
Chris Masone20d96d2007-03-22 12:13:20 -040018struct buffer_head *alloc_tree_block(struct btrfs_root *root, u64 blocknr)
Chris Masoned2ff2c2007-03-01 18:59:40 -050019{
Chris Masone20d96d2007-03-22 12:13:20 -040020 return sb_getblk(root->fs_info->sb, blocknr);
Chris Masoned2ff2c2007-03-01 18:59:40 -050021}
22
Chris Masone20d96d2007-03-22 12:13:20 -040023struct buffer_head *find_tree_block(struct btrfs_root *root, u64 blocknr)
Chris Masoneb60cea2007-02-02 09:18:22 -050024{
Chris Masone20d96d2007-03-22 12:13:20 -040025 return sb_getblk(root->fs_info->sb, blocknr);
26}
Chris Mason123abc82007-03-14 14:14:43 -040027
Chris Masone20d96d2007-03-22 12:13:20 -040028struct buffer_head *read_tree_block(struct btrfs_root *root, u64 blocknr)
29{
30 struct buffer_head *buf = sb_bread(root->fs_info->sb, blocknr);
31
Chris Masoneb60cea2007-02-02 09:18:22 -050032 if (!buf)
33 return buf;
Chris Mason9a8dd152007-02-23 08:38:36 -050034 if (check_tree_block(root, buf))
Chris Masoncfaa7292007-02-21 17:04:57 -050035 BUG();
Chris Masoneb60cea2007-02-02 09:18:22 -050036 return buf;
37}
38
Chris Masone089f052007-03-16 16:20:31 -040039int dirty_tree_block(struct btrfs_trans_handle *trans, struct btrfs_root *root,
Chris Masone20d96d2007-03-22 12:13:20 -040040 struct buffer_head *buf)
Chris Masoned2ff2c2007-03-01 18:59:40 -050041{
Chris Masone20d96d2007-03-22 12:13:20 -040042 mark_buffer_dirty(buf);
Chris Masoned2ff2c2007-03-01 18:59:40 -050043 return 0;
44}
45
Chris Masone089f052007-03-16 16:20:31 -040046int clean_tree_block(struct btrfs_trans_handle *trans, struct btrfs_root *root,
Chris Masone20d96d2007-03-22 12:13:20 -040047 struct buffer_head *buf)
Chris Masoned2ff2c2007-03-01 18:59:40 -050048{
Chris Masone20d96d2007-03-22 12:13:20 -040049 clear_buffer_dirty(buf);
Chris Masoned2ff2c2007-03-01 18:59:40 -050050 return 0;
51}
52
Chris Masone089f052007-03-16 16:20:31 -040053int write_tree_block(struct btrfs_trans_handle *trans, struct btrfs_root *root,
Chris Masone20d96d2007-03-22 12:13:20 -040054 struct buffer_head *buf)
Chris Masoneb60cea2007-02-02 09:18:22 -050055{
Chris Masone20d96d2007-03-22 12:13:20 -040056 mark_buffer_dirty(buf);
Chris Masoneb60cea2007-02-02 09:18:22 -050057 return 0;
58}
59
Chris Masone089f052007-03-16 16:20:31 -040060static int __commit_transaction(struct btrfs_trans_handle *trans, struct
61 btrfs_root *root)
Chris Masoned2ff2c2007-03-01 18:59:40 -050062{
Chris Masone20d96d2007-03-22 12:13:20 -040063 filemap_write_and_wait(root->fs_info->sb->s_bdev->bd_inode->i_mapping);
64 return 0;
Chris Masoned2ff2c2007-03-01 18:59:40 -050065}
66
Chris Mason9f5fae22007-03-20 14:38:32 -040067static int commit_tree_roots(struct btrfs_trans_handle *trans,
68 struct btrfs_fs_info *fs_info)
Chris Mason3768f362007-03-13 16:47:54 -040069{
70 int ret;
71 u64 old_extent_block;
Chris Mason9f5fae22007-03-20 14:38:32 -040072 struct btrfs_root *tree_root = fs_info->tree_root;
73 struct btrfs_root *extent_root = fs_info->extent_root;
74 struct btrfs_root *inode_root = fs_info->inode_root;
Chris Mason3768f362007-03-13 16:47:54 -040075
Chris Mason9f5fae22007-03-20 14:38:32 -040076 btrfs_set_root_blocknr(&inode_root->root_item,
Chris Masone20d96d2007-03-22 12:13:20 -040077 inode_root->node->b_blocknr);
Chris Mason9f5fae22007-03-20 14:38:32 -040078 ret = btrfs_update_root(trans, tree_root,
79 &inode_root->root_key,
80 &inode_root->root_item);
81 BUG_ON(ret);
Chris Mason3768f362007-03-13 16:47:54 -040082 while(1) {
83 old_extent_block = btrfs_root_blocknr(&extent_root->root_item);
Chris Masone20d96d2007-03-22 12:13:20 -040084 if (old_extent_block == extent_root->node->b_blocknr)
Chris Mason3768f362007-03-13 16:47:54 -040085 break;
86 btrfs_set_root_blocknr(&extent_root->root_item,
Chris Masone20d96d2007-03-22 12:13:20 -040087 extent_root->node->b_blocknr);
Chris Masone089f052007-03-16 16:20:31 -040088 ret = btrfs_update_root(trans, tree_root,
Chris Mason3768f362007-03-13 16:47:54 -040089 &extent_root->root_key,
90 &extent_root->root_item);
91 BUG_ON(ret);
92 }
Chris Mason3768f362007-03-13 16:47:54 -040093 return 0;
94}
95
Chris Masone089f052007-03-16 16:20:31 -040096int btrfs_commit_transaction(struct btrfs_trans_handle *trans, struct
97 btrfs_root *root, struct btrfs_super_block *s)
Chris Masoned2ff2c2007-03-01 18:59:40 -050098{
Chris Masona28ec192007-03-06 20:08:01 -050099 int ret = 0;
Chris Masone20d96d2007-03-22 12:13:20 -0400100 struct buffer_head *snap = root->commit_root;
Chris Mason3768f362007-03-13 16:47:54 -0400101 struct btrfs_key snap_key;
Chris Masona28ec192007-03-06 20:08:01 -0500102
Chris Mason3768f362007-03-13 16:47:54 -0400103 if (root->commit_root == root->node)
104 return 0;
105
106 memcpy(&snap_key, &root->root_key, sizeof(snap_key));
107 root->root_key.offset++;
108
Chris Masone20d96d2007-03-22 12:13:20 -0400109 btrfs_set_root_blocknr(&root->root_item, root->node->b_blocknr);
Chris Mason9f5fae22007-03-20 14:38:32 -0400110 ret = btrfs_insert_root(trans, root->fs_info->tree_root,
111 &root->root_key, &root->root_item);
Chris Mason3768f362007-03-13 16:47:54 -0400112 BUG_ON(ret);
113
Chris Mason9f5fae22007-03-20 14:38:32 -0400114 ret = commit_tree_roots(trans, root->fs_info);
115 BUG_ON(ret);
116
117 ret = __commit_transaction(trans, root);
Chris Mason3768f362007-03-13 16:47:54 -0400118 BUG_ON(ret);
119
Chris Masone089f052007-03-16 16:20:31 -0400120 write_ctree_super(trans, root, s);
Chris Mason9f5fae22007-03-20 14:38:32 -0400121 btrfs_finish_extent_commit(trans, root->fs_info->extent_root);
122 btrfs_finish_extent_commit(trans, root->fs_info->tree_root);
Chris Mason3768f362007-03-13 16:47:54 -0400123
124 root->commit_root = root->node;
Chris Masone20d96d2007-03-22 12:13:20 -0400125 get_bh(root->node);
Chris Masone089f052007-03-16 16:20:31 -0400126 ret = btrfs_drop_snapshot(trans, root, snap);
Chris Mason3768f362007-03-13 16:47:54 -0400127 BUG_ON(ret);
128
Chris Mason9f5fae22007-03-20 14:38:32 -0400129 ret = btrfs_del_root(trans, root->fs_info->tree_root, &snap_key);
Chris Mason3768f362007-03-13 16:47:54 -0400130 BUG_ON(ret);
Chris Mason293ffd52007-03-20 15:57:25 -0400131 root->fs_info->generation = root->root_key.offset + 1;
Chris Mason3768f362007-03-13 16:47:54 -0400132
Chris Masoned2ff2c2007-03-01 18:59:40 -0500133 return ret;
134}
135
Chris Mason123abc82007-03-14 14:14:43 -0400136static int __setup_root(struct btrfs_super_block *super,
Chris Mason9f5fae22007-03-20 14:38:32 -0400137 struct btrfs_root *root,
138 struct btrfs_fs_info *fs_info,
Chris Masone20d96d2007-03-22 12:13:20 -0400139 u64 objectid)
Chris Masond97e63b2007-02-20 16:40:44 -0500140{
Chris Masoncfaa7292007-02-21 17:04:57 -0500141 root->node = NULL;
Chris Masona28ec192007-03-06 20:08:01 -0500142 root->commit_root = NULL;
Chris Mason123abc82007-03-14 14:14:43 -0400143 root->blocksize = btrfs_super_blocksize(super);
144 root->ref_cows = 0;
Chris Mason9f5fae22007-03-20 14:38:32 -0400145 root->fs_info = fs_info;
Chris Mason3768f362007-03-13 16:47:54 -0400146 memset(&root->root_key, 0, sizeof(root->root_key));
147 memset(&root->root_item, 0, sizeof(root->root_item));
148 return 0;
149}
150
Chris Mason123abc82007-03-14 14:14:43 -0400151static int find_and_setup_root(struct btrfs_super_block *super,
Chris Mason9f5fae22007-03-20 14:38:32 -0400152 struct btrfs_root *tree_root,
153 struct btrfs_fs_info *fs_info,
154 u64 objectid,
Chris Masone20d96d2007-03-22 12:13:20 -0400155 struct btrfs_root *root)
Chris Mason3768f362007-03-13 16:47:54 -0400156{
157 int ret;
158
Chris Masone20d96d2007-03-22 12:13:20 -0400159 __setup_root(super, root, fs_info, objectid);
Chris Mason3768f362007-03-13 16:47:54 -0400160 ret = btrfs_find_last_root(tree_root, objectid,
161 &root->root_item, &root->root_key);
162 BUG_ON(ret);
163
164 root->node = read_tree_block(root,
165 btrfs_root_blocknr(&root->root_item));
Chris Mason3768f362007-03-13 16:47:54 -0400166 BUG_ON(!root->node);
Chris Masond97e63b2007-02-20 16:40:44 -0500167 return 0;
168}
169
Chris Masone20d96d2007-03-22 12:13:20 -0400170struct btrfs_root *open_ctree(struct super_block *sb,
171 struct buffer_head *sb_buffer,
172 struct btrfs_super_block *disk_super)
Chris Masoneb60cea2007-02-02 09:18:22 -0500173{
Chris Masone20d96d2007-03-22 12:13:20 -0400174 struct btrfs_root *root = kmalloc(sizeof(struct btrfs_root),
175 GFP_NOFS);
176 struct btrfs_root *extent_root = kmalloc(sizeof(struct btrfs_root),
177 GFP_NOFS);
178 struct btrfs_root *tree_root = kmalloc(sizeof(struct btrfs_root),
179 GFP_NOFS);
180 struct btrfs_root *inode_root = kmalloc(sizeof(struct btrfs_root),
181 GFP_NOFS);
182 struct btrfs_fs_info *fs_info = kmalloc(sizeof(*fs_info),
183 GFP_NOFS);
Chris Masoneb60cea2007-02-02 09:18:22 -0500184 int ret;
185
Chris Masone20d96d2007-03-22 12:13:20 -0400186 /* FIXME: don't be stupid */
187 if (!btrfs_super_root(disk_super))
188 return NULL;
Chris Mason9f5fae22007-03-20 14:38:32 -0400189 INIT_RADIX_TREE(&fs_info->pinned_radix, GFP_KERNEL);
Chris Mason9f5fae22007-03-20 14:38:32 -0400190 fs_info->running_transaction = NULL;
191 fs_info->fs_root = root;
192 fs_info->tree_root = tree_root;
193 fs_info->extent_root = extent_root;
194 fs_info->inode_root = inode_root;
195 fs_info->last_inode_alloc = 0;
196 fs_info->last_inode_alloc_dirid = 0;
Chris Masone20d96d2007-03-22 12:13:20 -0400197 fs_info->disk_super = disk_super;
198 fs_info->sb_buffer = sb_buffer;
199 fs_info->sb = sb;
Chris Mason9f5fae22007-03-20 14:38:32 -0400200 memset(&fs_info->current_insert, 0, sizeof(fs_info->current_insert));
201 memset(&fs_info->last_insert, 0, sizeof(fs_info->last_insert));
Chris Mason3768f362007-03-13 16:47:54 -0400202
Chris Masone20d96d2007-03-22 12:13:20 -0400203 __setup_root(disk_super, tree_root, fs_info, BTRFS_ROOT_TREE_OBJECTID);
204 tree_root->node = read_tree_block(tree_root,
205 btrfs_super_root(disk_super));
Chris Mason3768f362007-03-13 16:47:54 -0400206 BUG_ON(!tree_root->node);
207
Chris Masone20d96d2007-03-22 12:13:20 -0400208 ret = find_and_setup_root(disk_super, tree_root, fs_info,
209 BTRFS_EXTENT_TREE_OBJECTID, extent_root);
Chris Mason3768f362007-03-13 16:47:54 -0400210 BUG_ON(ret);
211
Chris Masone20d96d2007-03-22 12:13:20 -0400212 ret = find_and_setup_root(disk_super, tree_root, fs_info,
213 BTRFS_INODE_MAP_OBJECTID, inode_root);
Chris Mason9f5fae22007-03-20 14:38:32 -0400214 BUG_ON(ret);
215
Chris Masone20d96d2007-03-22 12:13:20 -0400216 ret = find_and_setup_root(disk_super, tree_root, fs_info,
217 BTRFS_FS_TREE_OBJECTID, root);
Chris Mason3768f362007-03-13 16:47:54 -0400218 BUG_ON(ret);
219
Chris Masona28ec192007-03-06 20:08:01 -0500220 root->commit_root = root->node;
Chris Masone20d96d2007-03-22 12:13:20 -0400221 get_bh(root->node);
Chris Mason3768f362007-03-13 16:47:54 -0400222 root->ref_cows = 1;
Chris Mason293ffd52007-03-20 15:57:25 -0400223 root->fs_info->generation = root->root_key.offset + 1;
Chris Masoneb60cea2007-02-02 09:18:22 -0500224 return root;
225}
226
Chris Masone089f052007-03-16 16:20:31 -0400227int write_ctree_super(struct btrfs_trans_handle *trans, struct btrfs_root
228 *root, struct btrfs_super_block *s)
Chris Masoncfaa7292007-02-21 17:04:57 -0500229{
Chris Masone20d96d2007-03-22 12:13:20 -0400230 return 0;
231#if 0
Chris Masoncfaa7292007-02-21 17:04:57 -0500232 int ret;
Chris Masone20d96d2007-03-22 12:13:20 -0400233 btrfs_set_super_root(s, root->fs_info->tree_root->node->b_blocknr);
234
Chris Mason9f5fae22007-03-20 14:38:32 -0400235 ret = pwrite(root->fs_info->fp, s, sizeof(*s),
Chris Mason123abc82007-03-14 14:14:43 -0400236 BTRFS_SUPER_INFO_OFFSET);
Chris Masoncfaa7292007-02-21 17:04:57 -0500237 if (ret != sizeof(*s)) {
238 fprintf(stderr, "failed to write new super block err %d\n", ret);
239 return ret;
240 }
241 return 0;
Chris Masone20d96d2007-03-22 12:13:20 -0400242#endif
Chris Masoncfaa7292007-02-21 17:04:57 -0500243}
244
Chris Mason234b63a2007-03-13 10:46:10 -0400245static int drop_cache(struct btrfs_root *root)
Chris Masoned2ff2c2007-03-01 18:59:40 -0500246{
Chris Masone20d96d2007-03-22 12:13:20 -0400247 return 0;
248#if 0
Chris Mason9f5fae22007-03-20 14:38:32 -0400249 while(!list_empty(&root->fs_info->cache)) {
Chris Masone20d96d2007-03-22 12:13:20 -0400250 struct buffer_head *b = list_entry(root->fs_info->cache.next,
251 struct buffer_head,
Chris Mason9f5fae22007-03-20 14:38:32 -0400252 cache);
Chris Masoned2ff2c2007-03-01 18:59:40 -0500253 list_del_init(&b->cache);
Chris Mason234b63a2007-03-13 10:46:10 -0400254 btrfs_block_release(root, b);
Chris Masoned2ff2c2007-03-01 18:59:40 -0500255 }
256 return 0;
Chris Masone20d96d2007-03-22 12:13:20 -0400257#endif
Chris Masoned2ff2c2007-03-01 18:59:40 -0500258}
Chris Masone20d96d2007-03-22 12:13:20 -0400259
260int close_ctree(struct btrfs_root *root)
Chris Masoneb60cea2007-02-02 09:18:22 -0500261{
Chris Mason3768f362007-03-13 16:47:54 -0400262 int ret;
Chris Masone089f052007-03-16 16:20:31 -0400263 struct btrfs_trans_handle *trans;
264
Chris Mason9f5fae22007-03-20 14:38:32 -0400265 trans = root->fs_info->running_transaction;
Chris Masone20d96d2007-03-22 12:13:20 -0400266 btrfs_commit_transaction(trans, root, root->fs_info->disk_super);
Chris Mason9f5fae22007-03-20 14:38:32 -0400267 ret = commit_tree_roots(trans, root->fs_info);
268 BUG_ON(ret);
269 ret = __commit_transaction(trans, root);
Chris Mason3768f362007-03-13 16:47:54 -0400270 BUG_ON(ret);
Chris Masone20d96d2007-03-22 12:13:20 -0400271 write_ctree_super(trans, root, root->fs_info->disk_super);
Chris Masoned2ff2c2007-03-01 18:59:40 -0500272 drop_cache(root);
Chris Masoned2ff2c2007-03-01 18:59:40 -0500273
Chris Masoneb60cea2007-02-02 09:18:22 -0500274 if (root->node)
Chris Mason234b63a2007-03-13 10:46:10 -0400275 btrfs_block_release(root, root->node);
Chris Mason9f5fae22007-03-20 14:38:32 -0400276 if (root->fs_info->extent_root->node)
277 btrfs_block_release(root->fs_info->extent_root,
278 root->fs_info->extent_root->node);
279 if (root->fs_info->inode_root->node)
280 btrfs_block_release(root->fs_info->inode_root,
281 root->fs_info->inode_root->node);
282 if (root->fs_info->tree_root->node)
283 btrfs_block_release(root->fs_info->tree_root,
284 root->fs_info->tree_root->node);
Chris Mason234b63a2007-03-13 10:46:10 -0400285 btrfs_block_release(root, root->commit_root);
Chris Masone20d96d2007-03-22 12:13:20 -0400286 btrfs_block_release(root, root->fs_info->sb_buffer);
287 kfree(root->fs_info->extent_root);
288 kfree(root->fs_info->inode_root);
289 kfree(root->fs_info->tree_root);
290 kfree(root->fs_info);
291 kfree(root);
Chris Masoneb60cea2007-02-02 09:18:22 -0500292 return 0;
293}
294
Chris Masone20d96d2007-03-22 12:13:20 -0400295void btrfs_block_release(struct btrfs_root *root, struct buffer_head *buf)
Chris Masoneb60cea2007-02-02 09:18:22 -0500296{
Chris Masone20d96d2007-03-22 12:13:20 -0400297 brelse(buf);
Chris Masoneb60cea2007-02-02 09:18:22 -0500298}
299