Mountable btrfs, with readdir
[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
e089f05c 53int write_tree_block(struct btrfs_trans_handle *trans, struct btrfs_root *root,
e20d96d6 54 struct buffer_head *buf)
eb60ceac 55{
e20d96d6 56 mark_buffer_dirty(buf);
eb60ceac
CM
57 return 0;
58}
59
e089f05c
CM
60static int __commit_transaction(struct btrfs_trans_handle *trans, struct
61 btrfs_root *root)
ed2ff2cb 62{
e20d96d6
CM
63 filemap_write_and_wait(root->fs_info->sb->s_bdev->bd_inode->i_mapping);
64 return 0;
ed2ff2cb
CM
65}
66
9f5fae2f
CM
67static int commit_tree_roots(struct btrfs_trans_handle *trans,
68 struct btrfs_fs_info *fs_info)
3768f368
CM
69{
70 int ret;
71 u64 old_extent_block;
9f5fae2f
CM
72 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;
75
76 btrfs_set_root_blocknr(&inode_root->root_item,
e20d96d6 77 inode_root->node->b_blocknr);
9f5fae2f
CM
78 ret = btrfs_update_root(trans, tree_root,
79 &inode_root->root_key,
80 &inode_root->root_item);
81 BUG_ON(ret);
3768f368
CM
82 while(1) {
83 old_extent_block = btrfs_root_blocknr(&extent_root->root_item);
e20d96d6 84 if (old_extent_block == extent_root->node->b_blocknr)
3768f368
CM
85 break;
86 btrfs_set_root_blocknr(&extent_root->root_item,
e20d96d6 87 extent_root->node->b_blocknr);
e089f05c 88 ret = btrfs_update_root(trans, tree_root,
3768f368
CM
89 &extent_root->root_key,
90 &extent_root->root_item);
91 BUG_ON(ret);
92 }
3768f368
CM
93 return 0;
94}
95
e089f05c
CM
96int btrfs_commit_transaction(struct btrfs_trans_handle *trans, struct
97 btrfs_root *root, struct btrfs_super_block *s)
ed2ff2cb 98{
a28ec197 99 int ret = 0;
e20d96d6 100 struct buffer_head *snap = root->commit_root;
3768f368 101 struct btrfs_key snap_key;
a28ec197 102
3768f368
CM
103 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
e20d96d6 109 btrfs_set_root_blocknr(&root->root_item, root->node->b_blocknr);
9f5fae2f
CM
110 ret = btrfs_insert_root(trans, root->fs_info->tree_root,
111 &root->root_key, &root->root_item);
112 BUG_ON(ret);
113
114 ret = commit_tree_roots(trans, root->fs_info);
3768f368
CM
115 BUG_ON(ret);
116
9f5fae2f 117 ret = __commit_transaction(trans, root);
3768f368
CM
118 BUG_ON(ret);
119
e089f05c 120 write_ctree_super(trans, root, s);
9f5fae2f
CM
121 btrfs_finish_extent_commit(trans, root->fs_info->extent_root);
122 btrfs_finish_extent_commit(trans, root->fs_info->tree_root);
3768f368
CM
123
124 root->commit_root = root->node;
e20d96d6 125 get_bh(root->node);
e089f05c 126 ret = btrfs_drop_snapshot(trans, root, snap);
3768f368
CM
127 BUG_ON(ret);
128
9f5fae2f 129 ret = btrfs_del_root(trans, root->fs_info->tree_root, &snap_key);
3768f368 130 BUG_ON(ret);
293ffd5f 131 root->fs_info->generation = root->root_key.offset + 1;
3768f368 132
ed2ff2cb
CM
133 return ret;
134}
135
123abc88 136static int __setup_root(struct btrfs_super_block *super,
9f5fae2f
CM
137 struct btrfs_root *root,
138 struct btrfs_fs_info *fs_info,
e20d96d6 139 u64 objectid)
d97e63b6 140{
cfaa7295 141 root->node = NULL;
a28ec197 142 root->commit_root = NULL;
123abc88
CM
143 root->blocksize = btrfs_super_blocksize(super);
144 root->ref_cows = 0;
9f5fae2f 145 root->fs_info = fs_info;
3768f368
CM
146 memset(&root->root_key, 0, sizeof(root->root_key));
147 memset(&root->root_item, 0, sizeof(root->root_item));
148 return 0;
149}
150
123abc88 151static int find_and_setup_root(struct btrfs_super_block *super,
9f5fae2f
CM
152 struct btrfs_root *tree_root,
153 struct btrfs_fs_info *fs_info,
154 u64 objectid,
e20d96d6 155 struct btrfs_root *root)
3768f368
CM
156{
157 int ret;
158
e20d96d6 159 __setup_root(super, root, fs_info, objectid);
3768f368
CM
160 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));
3768f368 166 BUG_ON(!root->node);
d97e63b6
CM
167 return 0;
168}
169
e20d96d6
CM
170struct btrfs_root *open_ctree(struct super_block *sb,
171 struct buffer_head *sb_buffer,
172 struct btrfs_super_block *disk_super)
2e635a27 173{
e20d96d6
CM
174 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);
eb60ceac
CM
184 int ret;
185
e20d96d6
CM
186 /* FIXME: don't be stupid */
187 if (!btrfs_super_root(disk_super))
188 return NULL;
9f5fae2f 189 INIT_RADIX_TREE(&fs_info->pinned_radix, GFP_KERNEL);
9f5fae2f
CM
190 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;
e20d96d6
CM
197 fs_info->disk_super = disk_super;
198 fs_info->sb_buffer = sb_buffer;
199 fs_info->sb = sb;
9f5fae2f
CM
200 memset(&fs_info->current_insert, 0, sizeof(fs_info->current_insert));
201 memset(&fs_info->last_insert, 0, sizeof(fs_info->last_insert));
3768f368 202
e20d96d6
CM
203 __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));
3768f368
CM
206 BUG_ON(!tree_root->node);
207
e20d96d6
CM
208 ret = find_and_setup_root(disk_super, tree_root, fs_info,
209 BTRFS_EXTENT_TREE_OBJECTID, extent_root);
3768f368
CM
210 BUG_ON(ret);
211
e20d96d6
CM
212 ret = find_and_setup_root(disk_super, tree_root, fs_info,
213 BTRFS_INODE_MAP_OBJECTID, inode_root);
9f5fae2f
CM
214 BUG_ON(ret);
215
e20d96d6
CM
216 ret = find_and_setup_root(disk_super, tree_root, fs_info,
217 BTRFS_FS_TREE_OBJECTID, root);
3768f368
CM
218 BUG_ON(ret);
219
a28ec197 220 root->commit_root = root->node;
e20d96d6 221 get_bh(root->node);
3768f368 222 root->ref_cows = 1;
293ffd5f 223 root->fs_info->generation = root->root_key.offset + 1;
eb60ceac
CM
224 return root;
225}
226
e089f05c
CM
227int write_ctree_super(struct btrfs_trans_handle *trans, struct btrfs_root
228 *root, struct btrfs_super_block *s)
eb60ceac 229{
e20d96d6
CM
230 return 0;
231#if 0
eb60ceac 232 int ret;
e20d96d6
CM
233 btrfs_set_super_root(s, root->fs_info->tree_root->node->b_blocknr);
234
9f5fae2f 235 ret = pwrite(root->fs_info->fp, s, sizeof(*s),
123abc88 236 BTRFS_SUPER_INFO_OFFSET);
cfaa7295
CM
237 if (ret != sizeof(*s)) {
238 fprintf(stderr, "failed to write new super block err %d\n", ret);
eb60ceac 239 return ret;
cfaa7295
CM
240 }
241 return 0;
e20d96d6 242#endif
cfaa7295
CM
243}
244
234b63a0 245static int drop_cache(struct btrfs_root *root)
ed2ff2cb 246{
e20d96d6
CM
247 return 0;
248#if 0
9f5fae2f 249 while(!list_empty(&root->fs_info->cache)) {
e20d96d6
CM
250 struct buffer_head *b = list_entry(root->fs_info->cache.next,
251 struct buffer_head,
9f5fae2f 252 cache);
ed2ff2cb 253 list_del_init(&b->cache);
234b63a0 254 btrfs_block_release(root, b);
ed2ff2cb
CM
255 }
256 return 0;
e20d96d6 257#endif
ed2ff2cb 258}
e20d96d6
CM
259
260int close_ctree(struct btrfs_root *root)
cfaa7295 261{
3768f368 262 int ret;
e089f05c
CM
263 struct btrfs_trans_handle *trans;
264
9f5fae2f 265 trans = root->fs_info->running_transaction;
e20d96d6 266 btrfs_commit_transaction(trans, root, root->fs_info->disk_super);
9f5fae2f
CM
267 ret = commit_tree_roots(trans, root->fs_info);
268 BUG_ON(ret);
269 ret = __commit_transaction(trans, root);
3768f368 270 BUG_ON(ret);
e20d96d6 271 write_ctree_super(trans, root, root->fs_info->disk_super);
ed2ff2cb 272 drop_cache(root);
ed2ff2cb 273
cfaa7295 274 if (root->node)
234b63a0 275 btrfs_block_release(root, root->node);
9f5fae2f
CM
276 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);
234b63a0 285 btrfs_block_release(root, root->commit_root);
e20d96d6
CM
286 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);
eb60ceac
CM
292 return 0;
293}
294
e20d96d6 295void btrfs_block_release(struct btrfs_root *root, struct buffer_head *buf)
eb60ceac 296{
e20d96d6 297 brelse(buf);
eb60ceac
CM
298}
299