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