Btrfs: use a btree inode instead of sb_getblk
[linux-2.6-block.git] / fs / btrfs / disk-io.c
CommitLineData
e20d96d6
CM
1#include <linux/module.h>
2#include <linux/fs.h>
d98237b3 3#include <linux/blkdev.h>
eb60ceac
CM
4#include "ctree.h"
5#include "disk-io.h"
e089f05c 6#include "transaction.h"
eb60ceac 7
d98237b3 8
e20d96d6 9static int check_tree_block(struct btrfs_root *root, struct buffer_head *buf)
eb60ceac 10{
e20d96d6 11 struct btrfs_node *node = btrfs_buffer_node(buf);
d98237b3 12 if (buf->b_blocknr != btrfs_header_blocknr(&node->header)) {
9a8dd150 13 BUG();
d98237b3 14 }
e20d96d6 15 if (root->node && btrfs_header_parentid(&node->header) !=
df2ce34c 16 btrfs_header_parentid(btrfs_buffer_header(root->node))) {
7f5c1516 17 BUG();
df2ce34c 18 }
9a8dd150 19 return 0;
eb60ceac
CM
20}
21
d98237b3
CM
22struct buffer_head *btrfs_find_tree_block(struct btrfs_root *root, u64 blocknr)
23{
24 struct address_space *mapping = root->fs_info->btree_inode->i_mapping;
25 int blockbits = root->fs_info->sb->s_blocksize_bits;
26 unsigned long index = blocknr >> (PAGE_CACHE_SHIFT - blockbits);
27 struct page *page;
28 struct buffer_head *bh;
29 struct buffer_head *head;
30 struct buffer_head *ret = NULL;
31
32 page = find_lock_page(mapping, index);
33 if (!page)
34 return NULL;
35
36 if (!page_has_buffers(page))
37 goto out_unlock;
38
39 head = page_buffers(page);
40 bh = head;
41 do {
42 if (buffer_mapped(bh) && bh->b_blocknr == blocknr) {
43 ret = bh;
44 get_bh(bh);
45 goto out_unlock;
46 }
47 bh = bh->b_this_page;
48 } while (bh != head);
49out_unlock:
50 unlock_page(page);
51 page_cache_release(page);
52 return ret;
53}
54
55struct buffer_head *btrfs_find_create_tree_block(struct btrfs_root *root,
56 u64 blocknr)
57{
58 struct address_space *mapping = root->fs_info->btree_inode->i_mapping;
59 int blockbits = root->fs_info->sb->s_blocksize_bits;
60 unsigned long index = blocknr >> (PAGE_CACHE_SHIFT - blockbits);
61 struct page *page;
62 struct buffer_head *bh;
63 struct buffer_head *head;
64 struct buffer_head *ret = NULL;
65 u64 first_block = index << (PAGE_CACHE_SHIFT - blockbits);
66 page = grab_cache_page(mapping, index);
67 if (!page)
68 return NULL;
69
70 wait_on_page_writeback(page);
71 if (!page_has_buffers(page))
72 create_empty_buffers(page, root->fs_info->sb->s_blocksize, 0);
73 head = page_buffers(page);
74 bh = head;
75 do {
76 if (!buffer_mapped(bh)) {
77 bh->b_bdev = root->fs_info->sb->s_bdev;
78 bh->b_blocknr = first_block;
79 set_buffer_mapped(bh);
80 }
81 if (bh->b_blocknr == blocknr) {
82 ret = bh;
83 get_bh(bh);
84 goto out_unlock;
85 }
86 bh = bh->b_this_page;
87 first_block++;
88 } while (bh != head);
89out_unlock:
90 unlock_page(page);
91 page_cache_release(page);
92 return ret;
93}
94
95static sector_t max_block(struct block_device *bdev)
96{
97 sector_t retval = ~((sector_t)0);
98 loff_t sz = i_size_read(bdev->bd_inode);
99
100 if (sz) {
101 unsigned int size = block_size(bdev);
102 unsigned int sizebits = blksize_bits(size);
103 retval = (sz >> sizebits);
104 }
105 return retval;
106}
107
108static int btree_get_block(struct inode *inode, sector_t iblock,
109 struct buffer_head *bh, int create)
110{
111 if (iblock >= max_block(inode->i_sb->s_bdev)) {
112 if (create)
113 return -EIO;
114
115 /*
116 * for reads, we're just trying to fill a partial page.
117 * return a hole, they will have to call get_block again
118 * before they can fill it, and they will get -EIO at that
119 * time
120 */
121 return 0;
122 }
123 bh->b_bdev = inode->i_sb->s_bdev;
124 bh->b_blocknr = iblock;
125 set_buffer_mapped(bh);
126 return 0;
127}
128
129static int btree_writepage(struct page *page, struct writeback_control *wbc)
ed2ff2cb 130{
d98237b3 131 return block_write_full_page(page, btree_get_block, wbc);
ed2ff2cb
CM
132}
133
d98237b3 134static int btree_readpage(struct file * file, struct page * page)
eb60ceac 135{
d98237b3 136 return block_read_full_page(page, btree_get_block);
eb60ceac
CM
137}
138
d98237b3
CM
139static struct address_space_operations btree_aops = {
140 .readpage = btree_readpage,
141 .writepage = btree_writepage,
142 .sync_page = block_sync_page,
143};
144
e20d96d6 145struct buffer_head *read_tree_block(struct btrfs_root *root, u64 blocknr)
eb60ceac 146{
d98237b3 147 struct buffer_head *bh = NULL;
eb60ceac 148
d98237b3
CM
149 bh = btrfs_find_create_tree_block(root, blocknr);
150 if (!bh)
151 return bh;
152 lock_buffer(bh);
153 if (!buffer_uptodate(bh)) {
154 get_bh(bh);
155 bh->b_end_io = end_buffer_read_sync;
156 submit_bh(READ, bh);
157 wait_on_buffer(bh);
158 if (!buffer_uptodate(bh))
159 goto fail;
160 } else {
161 unlock_buffer(bh);
162 }
163 if (check_tree_block(root, bh))
cfaa7295 164 BUG();
d98237b3
CM
165 return bh;
166fail:
167 brelse(bh);
168 return NULL;
169
eb60ceac
CM
170}
171
e089f05c 172int dirty_tree_block(struct btrfs_trans_handle *trans, struct btrfs_root *root,
e20d96d6 173 struct buffer_head *buf)
ed2ff2cb 174{
e20d96d6 175 mark_buffer_dirty(buf);
ed2ff2cb
CM
176 return 0;
177}
178
e089f05c 179int clean_tree_block(struct btrfs_trans_handle *trans, struct btrfs_root *root,
e20d96d6 180 struct buffer_head *buf)
ed2ff2cb 181{
e20d96d6 182 clear_buffer_dirty(buf);
ed2ff2cb
CM
183 return 0;
184}
185
123abc88 186static int __setup_root(struct btrfs_super_block *super,
9f5fae2f
CM
187 struct btrfs_root *root,
188 struct btrfs_fs_info *fs_info,
e20d96d6 189 u64 objectid)
d97e63b6 190{
cfaa7295 191 root->node = NULL;
a28ec197 192 root->commit_root = NULL;
123abc88
CM
193 root->blocksize = btrfs_super_blocksize(super);
194 root->ref_cows = 0;
9f5fae2f 195 root->fs_info = fs_info;
3768f368
CM
196 memset(&root->root_key, 0, sizeof(root->root_key));
197 memset(&root->root_item, 0, sizeof(root->root_item));
198 return 0;
199}
200
123abc88 201static int find_and_setup_root(struct btrfs_super_block *super,
9f5fae2f
CM
202 struct btrfs_root *tree_root,
203 struct btrfs_fs_info *fs_info,
204 u64 objectid,
e20d96d6 205 struct btrfs_root *root)
3768f368
CM
206{
207 int ret;
208
e20d96d6 209 __setup_root(super, root, fs_info, objectid);
3768f368
CM
210 ret = btrfs_find_last_root(tree_root, objectid,
211 &root->root_item, &root->root_key);
212 BUG_ON(ret);
213
214 root->node = read_tree_block(root,
215 btrfs_root_blocknr(&root->root_item));
3768f368 216 BUG_ON(!root->node);
d97e63b6
CM
217 return 0;
218}
219
e20d96d6
CM
220struct btrfs_root *open_ctree(struct super_block *sb,
221 struct buffer_head *sb_buffer,
222 struct btrfs_super_block *disk_super)
2e635a27 223{
e20d96d6
CM
224 struct btrfs_root *root = kmalloc(sizeof(struct btrfs_root),
225 GFP_NOFS);
226 struct btrfs_root *extent_root = kmalloc(sizeof(struct btrfs_root),
227 GFP_NOFS);
228 struct btrfs_root *tree_root = kmalloc(sizeof(struct btrfs_root),
229 GFP_NOFS);
230 struct btrfs_root *inode_root = kmalloc(sizeof(struct btrfs_root),
231 GFP_NOFS);
232 struct btrfs_fs_info *fs_info = kmalloc(sizeof(*fs_info),
233 GFP_NOFS);
eb60ceac
CM
234 int ret;
235
e20d96d6
CM
236 if (!btrfs_super_root(disk_super))
237 return NULL;
8ef97622
CM
238 init_bit_radix(&fs_info->pinned_radix);
239 init_bit_radix(&fs_info->pending_del_radix);
d98237b3 240 sb_set_blocksize(sb, sb_buffer->b_size);
9f5fae2f
CM
241 fs_info->running_transaction = NULL;
242 fs_info->fs_root = root;
243 fs_info->tree_root = tree_root;
244 fs_info->extent_root = extent_root;
245 fs_info->inode_root = inode_root;
246 fs_info->last_inode_alloc = 0;
247 fs_info->last_inode_alloc_dirid = 0;
e20d96d6 248 fs_info->disk_super = disk_super;
e20d96d6 249 fs_info->sb = sb;
d98237b3
CM
250 fs_info->btree_inode = new_inode(sb);
251 fs_info->btree_inode->i_ino = 1;
252 fs_info->btree_inode->i_size = sb->s_bdev->bd_inode->i_size;
253 fs_info->btree_inode->i_mapping->a_ops = &btree_aops;
254 mapping_set_gfp_mask(fs_info->btree_inode->i_mapping, GFP_NOFS);
255
79154b1b 256 mutex_init(&fs_info->trans_mutex);
d561c025 257 mutex_init(&fs_info->fs_mutex);
9f5fae2f
CM
258 memset(&fs_info->current_insert, 0, sizeof(fs_info->current_insert));
259 memset(&fs_info->last_insert, 0, sizeof(fs_info->last_insert));
3768f368 260
e20d96d6 261 __setup_root(disk_super, tree_root, fs_info, BTRFS_ROOT_TREE_OBJECTID);
d98237b3
CM
262
263 fs_info->sb_buffer = read_tree_block(tree_root, sb_buffer->b_blocknr);
264
265 if (!fs_info->sb_buffer)
266 return NULL;
267
268 brelse(sb_buffer);
269 sb_buffer = NULL;
270 disk_super = (struct btrfs_super_block *)fs_info->sb_buffer->b_data;
271 fs_info->disk_super = disk_super;
272
e20d96d6
CM
273 tree_root->node = read_tree_block(tree_root,
274 btrfs_super_root(disk_super));
3768f368
CM
275 BUG_ON(!tree_root->node);
276
e20d96d6
CM
277 ret = find_and_setup_root(disk_super, tree_root, fs_info,
278 BTRFS_EXTENT_TREE_OBJECTID, extent_root);
3768f368
CM
279 BUG_ON(ret);
280
e20d96d6
CM
281 ret = find_and_setup_root(disk_super, tree_root, fs_info,
282 BTRFS_INODE_MAP_OBJECTID, inode_root);
9f5fae2f
CM
283 BUG_ON(ret);
284
e20d96d6
CM
285 ret = find_and_setup_root(disk_super, tree_root, fs_info,
286 BTRFS_FS_TREE_OBJECTID, root);
3768f368 287 BUG_ON(ret);
a28ec197 288 root->commit_root = root->node;
e20d96d6 289 get_bh(root->node);
3768f368 290 root->ref_cows = 1;
293ffd5f 291 root->fs_info->generation = root->root_key.offset + 1;
eb60ceac
CM
292 return root;
293}
294
e089f05c 295int write_ctree_super(struct btrfs_trans_handle *trans, struct btrfs_root
79154b1b 296 *root)
eb60ceac 297{
d5719762
CM
298 struct buffer_head *bh = root->fs_info->sb_buffer;
299 btrfs_set_super_root(root->fs_info->disk_super,
300 root->fs_info->tree_root->node->b_blocknr);
301 lock_buffer(bh);
302 clear_buffer_dirty(bh);
303 bh->b_end_io = end_buffer_write_sync;
304 get_bh(bh);
305 submit_bh(WRITE, bh);
306 wait_on_buffer(bh);
307 if (!buffer_uptodate(bh)) {
308 WARN_ON(1);
309 return -EIO;
cfaa7295
CM
310 }
311 return 0;
312}
313
e20d96d6 314int close_ctree(struct btrfs_root *root)
cfaa7295 315{
3768f368 316 int ret;
e089f05c
CM
317 struct btrfs_trans_handle *trans;
318
79154b1b
CM
319 trans = btrfs_start_transaction(root, 1);
320 btrfs_commit_transaction(trans, root);
321 /* run commit again to drop the original snapshot */
322 trans = btrfs_start_transaction(root, 1);
323 btrfs_commit_transaction(trans, root);
324 ret = btrfs_write_and_wait_transaction(NULL, root);
3768f368 325 BUG_ON(ret);
79154b1b 326 write_ctree_super(NULL, root);
ed2ff2cb 327
cfaa7295 328 if (root->node)
234b63a0 329 btrfs_block_release(root, root->node);
9f5fae2f
CM
330 if (root->fs_info->extent_root->node)
331 btrfs_block_release(root->fs_info->extent_root,
332 root->fs_info->extent_root->node);
333 if (root->fs_info->inode_root->node)
334 btrfs_block_release(root->fs_info->inode_root,
335 root->fs_info->inode_root->node);
336 if (root->fs_info->tree_root->node)
337 btrfs_block_release(root->fs_info->tree_root,
338 root->fs_info->tree_root->node);
234b63a0 339 btrfs_block_release(root, root->commit_root);
e20d96d6 340 btrfs_block_release(root, root->fs_info->sb_buffer);
d98237b3 341 iput(root->fs_info->btree_inode);
e20d96d6
CM
342 kfree(root->fs_info->extent_root);
343 kfree(root->fs_info->inode_root);
344 kfree(root->fs_info->tree_root);
345 kfree(root->fs_info);
346 kfree(root);
eb60ceac
CM
347 return 0;
348}
349
e20d96d6 350void btrfs_block_release(struct btrfs_root *root, struct buffer_head *buf)
eb60ceac 351{
e20d96d6 352 brelse(buf);
eb60ceac
CM
353}
354