blob: e32ddff55b0ebb25d5cfddf05eaf57a532a633bb [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 Mason9f5fae22007-03-20 14:38:32 -0400107 INIT_RADIX_TREE(&fs_info->pinned_radix, GFP_KERNEL);
Chris Mason9f5fae22007-03-20 14:38:32 -0400108 fs_info->running_transaction = NULL;
109 fs_info->fs_root = root;
110 fs_info->tree_root = tree_root;
111 fs_info->extent_root = extent_root;
112 fs_info->inode_root = inode_root;
113 fs_info->last_inode_alloc = 0;
114 fs_info->last_inode_alloc_dirid = 0;
Chris Masone20d96d2007-03-22 12:13:20 -0400115 fs_info->disk_super = disk_super;
116 fs_info->sb_buffer = sb_buffer;
117 fs_info->sb = sb;
Chris Mason79154b12007-03-22 15:59:16 -0400118 mutex_init(&fs_info->trans_mutex);
Chris Masond561c022007-03-23 19:47:49 -0400119 mutex_init(&fs_info->fs_mutex);
Chris Mason9f5fae22007-03-20 14:38:32 -0400120 memset(&fs_info->current_insert, 0, sizeof(fs_info->current_insert));
121 memset(&fs_info->last_insert, 0, sizeof(fs_info->last_insert));
Chris Mason3768f362007-03-13 16:47:54 -0400122
Chris Masone20d96d2007-03-22 12:13:20 -0400123 __setup_root(disk_super, tree_root, fs_info, BTRFS_ROOT_TREE_OBJECTID);
124 tree_root->node = read_tree_block(tree_root,
125 btrfs_super_root(disk_super));
Chris Mason3768f362007-03-13 16:47:54 -0400126 BUG_ON(!tree_root->node);
127
Chris Masone20d96d2007-03-22 12:13:20 -0400128 ret = find_and_setup_root(disk_super, tree_root, fs_info,
129 BTRFS_EXTENT_TREE_OBJECTID, extent_root);
Chris Mason3768f362007-03-13 16:47:54 -0400130 BUG_ON(ret);
131
Chris Masone20d96d2007-03-22 12:13:20 -0400132 ret = find_and_setup_root(disk_super, tree_root, fs_info,
133 BTRFS_INODE_MAP_OBJECTID, inode_root);
Chris Mason9f5fae22007-03-20 14:38:32 -0400134 BUG_ON(ret);
135
Chris Masone20d96d2007-03-22 12:13:20 -0400136 ret = find_and_setup_root(disk_super, tree_root, fs_info,
137 BTRFS_FS_TREE_OBJECTID, root);
Chris Mason3768f362007-03-13 16:47:54 -0400138 BUG_ON(ret);
139
Chris Masona28ec192007-03-06 20:08:01 -0500140 root->commit_root = root->node;
Chris Masone20d96d2007-03-22 12:13:20 -0400141 get_bh(root->node);
Chris Mason3768f362007-03-13 16:47:54 -0400142 root->ref_cows = 1;
Chris Mason293ffd52007-03-20 15:57:25 -0400143 root->fs_info->generation = root->root_key.offset + 1;
Chris Masoneb60cea2007-02-02 09:18:22 -0500144 return root;
145}
146
Chris Masone089f052007-03-16 16:20:31 -0400147int write_ctree_super(struct btrfs_trans_handle *trans, struct btrfs_root
Chris Mason79154b12007-03-22 15:59:16 -0400148 *root)
Chris Masoncfaa7292007-02-21 17:04:57 -0500149{
Chris Masond5719762007-03-23 10:01:08 -0400150 struct buffer_head *bh = root->fs_info->sb_buffer;
151 btrfs_set_super_root(root->fs_info->disk_super,
152 root->fs_info->tree_root->node->b_blocknr);
153 lock_buffer(bh);
154 clear_buffer_dirty(bh);
155 bh->b_end_io = end_buffer_write_sync;
156 get_bh(bh);
157 submit_bh(WRITE, bh);
158 wait_on_buffer(bh);
159 if (!buffer_uptodate(bh)) {
160 WARN_ON(1);
161 return -EIO;
Chris Masoncfaa7292007-02-21 17:04:57 -0500162 }
163 return 0;
164}
165
Chris Masone20d96d2007-03-22 12:13:20 -0400166int close_ctree(struct btrfs_root *root)
Chris Masoneb60cea2007-02-02 09:18:22 -0500167{
Chris Mason3768f362007-03-13 16:47:54 -0400168 int ret;
Chris Masone089f052007-03-16 16:20:31 -0400169 struct btrfs_trans_handle *trans;
170
Chris Mason79154b12007-03-22 15:59:16 -0400171 trans = btrfs_start_transaction(root, 1);
172 btrfs_commit_transaction(trans, root);
173 /* run commit again to drop the original snapshot */
174 trans = btrfs_start_transaction(root, 1);
175 btrfs_commit_transaction(trans, root);
176 ret = btrfs_write_and_wait_transaction(NULL, root);
Chris Mason9f5fae22007-03-20 14:38:32 -0400177 BUG_ON(ret);
Chris Mason79154b12007-03-22 15:59:16 -0400178 write_ctree_super(NULL, root);
Chris Masoned2ff2c2007-03-01 18:59:40 -0500179
Chris Masoneb60cea2007-02-02 09:18:22 -0500180 if (root->node)
Chris Mason234b63a2007-03-13 10:46:10 -0400181 btrfs_block_release(root, root->node);
Chris Mason9f5fae22007-03-20 14:38:32 -0400182 if (root->fs_info->extent_root->node)
183 btrfs_block_release(root->fs_info->extent_root,
184 root->fs_info->extent_root->node);
185 if (root->fs_info->inode_root->node)
186 btrfs_block_release(root->fs_info->inode_root,
187 root->fs_info->inode_root->node);
188 if (root->fs_info->tree_root->node)
189 btrfs_block_release(root->fs_info->tree_root,
190 root->fs_info->tree_root->node);
Chris Mason234b63a2007-03-13 10:46:10 -0400191 btrfs_block_release(root, root->commit_root);
Chris Masone20d96d2007-03-22 12:13:20 -0400192 btrfs_block_release(root, root->fs_info->sb_buffer);
193 kfree(root->fs_info->extent_root);
194 kfree(root->fs_info->inode_root);
195 kfree(root->fs_info->tree_root);
196 kfree(root->fs_info);
197 kfree(root);
Chris Masoneb60cea2007-02-02 09:18:22 -0500198 return 0;
199}
200
Chris Masone20d96d2007-03-22 12:13:20 -0400201void btrfs_block_release(struct btrfs_root *root, struct buffer_head *buf)
Chris Masoneb60cea2007-02-02 09:18:22 -0500202{
Chris Masone20d96d2007-03-22 12:13:20 -0400203 brelse(buf);
Chris Masoneb60cea2007-02-02 09:18:22 -0500204}
205