blob: 970103f2cacd90d17fe02b6dcd7bc115db3beb2f [file] [log] [blame]
Chris Masone20d96d2007-03-22 12:13:20 -04001#include <linux/module.h>
2#include <linux/fs.h>
Chris Masond98237b2007-03-28 13:57:48 -04003#include <linux/blkdev.h>
Chris Masoneb60cea2007-02-02 09:18:22 -05004#include "ctree.h"
5#include "disk-io.h"
Chris Masone089f052007-03-16 16:20:31 -04006#include "transaction.h"
Chris Masoneb60cea2007-02-02 09:18:22 -05007
Chris Masond98237b2007-03-28 13:57:48 -04008
Chris Masone20d96d2007-03-22 12:13:20 -04009static int check_tree_block(struct btrfs_root *root, struct buffer_head *buf)
Chris Masoneb60cea2007-02-02 09:18:22 -050010{
Chris Masone20d96d2007-03-22 12:13:20 -040011 struct btrfs_node *node = btrfs_buffer_node(buf);
Chris Masond98237b2007-03-28 13:57:48 -040012 if (buf->b_blocknr != btrfs_header_blocknr(&node->header)) {
Chris Mason9a8dd152007-02-23 08:38:36 -050013 BUG();
Chris Masond98237b2007-03-28 13:57:48 -040014 }
Chris Masone20d96d2007-03-22 12:13:20 -040015 if (root->node && btrfs_header_parentid(&node->header) !=
Chris Masondf2ce342007-03-23 11:00:45 -040016 btrfs_header_parentid(btrfs_buffer_header(root->node))) {
Chris Mason7f5c1512007-03-23 15:56:19 -040017 BUG();
Chris Masondf2ce342007-03-23 11:00:45 -040018 }
Chris Mason9a8dd152007-02-23 08:38:36 -050019 return 0;
Chris Masoneb60cea2007-02-02 09:18:22 -050020}
21
Chris Masond98237b2007-03-28 13:57:48 -040022struct buffer_head *btrfs_find_tree_block(struct btrfs_root *root, u64 blocknr)
Chris Masoned2ff2c2007-03-01 18:59:40 -050023{
Chris Masond98237b2007-03-28 13:57:48 -040024 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;
Chris Masoned2ff2c2007-03-01 18:59:40 -050053}
54
Chris Masond98237b2007-03-28 13:57:48 -040055struct buffer_head *btrfs_find_create_tree_block(struct btrfs_root *root,
56 u64 blocknr)
Chris Masoneb60cea2007-02-02 09:18:22 -050057{
Chris Masond98237b2007-03-28 13:57:48 -040058 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;
Chris Masone20d96d2007-03-22 12:13:20 -040093}
Chris Mason123abc82007-03-14 14:14:43 -040094
Chris Masond98237b2007-03-28 13:57:48 -040095static 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)
130{
131 return block_write_full_page(page, btree_get_block, wbc);
132}
133
134static int btree_readpage(struct file * file, struct page * page)
135{
136 return block_read_full_page(page, btree_get_block);
137}
138
139static struct address_space_operations btree_aops = {
140 .readpage = btree_readpage,
141 .writepage = btree_writepage,
142 .sync_page = block_sync_page,
143};
144
Chris Masone20d96d2007-03-22 12:13:20 -0400145struct buffer_head *read_tree_block(struct btrfs_root *root, u64 blocknr)
146{
Chris Masond98237b2007-03-28 13:57:48 -0400147 struct buffer_head *bh = NULL;
Chris Masone20d96d2007-03-22 12:13:20 -0400148
Chris Masond98237b2007-03-28 13:57:48 -0400149 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))
Chris Masoncfaa7292007-02-21 17:04:57 -0500164 BUG();
Chris Masond98237b2007-03-28 13:57:48 -0400165 return bh;
166fail:
167 brelse(bh);
168 return NULL;
169
Chris Masoneb60cea2007-02-02 09:18:22 -0500170}
171
Chris Masone089f052007-03-16 16:20:31 -0400172int dirty_tree_block(struct btrfs_trans_handle *trans, struct btrfs_root *root,
Chris Masone20d96d2007-03-22 12:13:20 -0400173 struct buffer_head *buf)
Chris Masoned2ff2c2007-03-01 18:59:40 -0500174{
Chris Masone20d96d2007-03-22 12:13:20 -0400175 mark_buffer_dirty(buf);
Chris Masoned2ff2c2007-03-01 18:59:40 -0500176 return 0;
177}
178
Chris Masone089f052007-03-16 16:20:31 -0400179int clean_tree_block(struct btrfs_trans_handle *trans, struct btrfs_root *root,
Chris Masone20d96d2007-03-22 12:13:20 -0400180 struct buffer_head *buf)
Chris Masoned2ff2c2007-03-01 18:59:40 -0500181{
Chris Masone20d96d2007-03-22 12:13:20 -0400182 clear_buffer_dirty(buf);
Chris Masoned2ff2c2007-03-01 18:59:40 -0500183 return 0;
184}
185
Chris Mason123abc82007-03-14 14:14:43 -0400186static int __setup_root(struct btrfs_super_block *super,
Chris Mason9f5fae22007-03-20 14:38:32 -0400187 struct btrfs_root *root,
188 struct btrfs_fs_info *fs_info,
Chris Masone20d96d2007-03-22 12:13:20 -0400189 u64 objectid)
Chris Masond97e63b2007-02-20 16:40:44 -0500190{
Chris Masoncfaa7292007-02-21 17:04:57 -0500191 root->node = NULL;
Chris Masona28ec192007-03-06 20:08:01 -0500192 root->commit_root = NULL;
Chris Mason123abc82007-03-14 14:14:43 -0400193 root->blocksize = btrfs_super_blocksize(super);
194 root->ref_cows = 0;
Chris Mason9f5fae22007-03-20 14:38:32 -0400195 root->fs_info = fs_info;
Chris Mason3768f362007-03-13 16:47:54 -0400196 memset(&root->root_key, 0, sizeof(root->root_key));
197 memset(&root->root_item, 0, sizeof(root->root_item));
198 return 0;
199}
200
Chris Mason123abc82007-03-14 14:14:43 -0400201static int find_and_setup_root(struct btrfs_super_block *super,
Chris Mason9f5fae22007-03-20 14:38:32 -0400202 struct btrfs_root *tree_root,
203 struct btrfs_fs_info *fs_info,
204 u64 objectid,
Chris Masone20d96d2007-03-22 12:13:20 -0400205 struct btrfs_root *root)
Chris Mason3768f362007-03-13 16:47:54 -0400206{
207 int ret;
208
Chris Masone20d96d2007-03-22 12:13:20 -0400209 __setup_root(super, root, fs_info, objectid);
Chris Mason3768f362007-03-13 16:47:54 -0400210 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));
Chris Mason3768f362007-03-13 16:47:54 -0400216 BUG_ON(!root->node);
Chris Masond97e63b2007-02-20 16:40:44 -0500217 return 0;
218}
219
Chris Masone20d96d2007-03-22 12:13:20 -0400220struct btrfs_root *open_ctree(struct super_block *sb,
221 struct buffer_head *sb_buffer,
222 struct btrfs_super_block *disk_super)
Chris Masoneb60cea2007-02-02 09:18:22 -0500223{
Chris Masone20d96d2007-03-22 12:13:20 -0400224 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);
Chris Masoneb60cea2007-02-02 09:18:22 -0500234 int ret;
235
Chris Masone20d96d2007-03-22 12:13:20 -0400236 if (!btrfs_super_root(disk_super))
237 return NULL;
Chris Mason8ef97622007-03-26 10:15:30 -0400238 init_bit_radix(&fs_info->pinned_radix);
239 init_bit_radix(&fs_info->pending_del_radix);
Chris Masond98237b2007-03-28 13:57:48 -0400240 sb_set_blocksize(sb, sb_buffer->b_size);
Chris Mason9f5fae22007-03-20 14:38:32 -0400241 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;
Chris Masone20d96d2007-03-22 12:13:20 -0400248 fs_info->disk_super = disk_super;
Chris Masone20d96d2007-03-22 12:13:20 -0400249 fs_info->sb = sb;
Chris Masond98237b2007-03-28 13:57:48 -0400250 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
Chris Mason79154b12007-03-22 15:59:16 -0400256 mutex_init(&fs_info->trans_mutex);
Chris Masond561c022007-03-23 19:47:49 -0400257 mutex_init(&fs_info->fs_mutex);
Chris Mason9f5fae22007-03-20 14:38:32 -0400258 memset(&fs_info->current_insert, 0, sizeof(fs_info->current_insert));
259 memset(&fs_info->last_insert, 0, sizeof(fs_info->last_insert));
Chris Mason3768f362007-03-13 16:47:54 -0400260
Chris Masone20d96d2007-03-22 12:13:20 -0400261 __setup_root(disk_super, tree_root, fs_info, BTRFS_ROOT_TREE_OBJECTID);
Chris Masond98237b2007-03-28 13:57:48 -0400262
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
Chris Masone20d96d2007-03-22 12:13:20 -0400273 tree_root->node = read_tree_block(tree_root,
274 btrfs_super_root(disk_super));
Chris Mason3768f362007-03-13 16:47:54 -0400275 BUG_ON(!tree_root->node);
276
Chris Masone20d96d2007-03-22 12:13:20 -0400277 ret = find_and_setup_root(disk_super, tree_root, fs_info,
278 BTRFS_EXTENT_TREE_OBJECTID, extent_root);
Chris Mason3768f362007-03-13 16:47:54 -0400279 BUG_ON(ret);
280
Chris Masone20d96d2007-03-22 12:13:20 -0400281 ret = find_and_setup_root(disk_super, tree_root, fs_info,
282 BTRFS_INODE_MAP_OBJECTID, inode_root);
Chris Mason9f5fae22007-03-20 14:38:32 -0400283 BUG_ON(ret);
284
Chris Masone20d96d2007-03-22 12:13:20 -0400285 ret = find_and_setup_root(disk_super, tree_root, fs_info,
286 BTRFS_FS_TREE_OBJECTID, root);
Chris Mason3768f362007-03-13 16:47:54 -0400287 BUG_ON(ret);
Chris Masona28ec192007-03-06 20:08:01 -0500288 root->commit_root = root->node;
Chris Masone20d96d2007-03-22 12:13:20 -0400289 get_bh(root->node);
Chris Mason3768f362007-03-13 16:47:54 -0400290 root->ref_cows = 1;
Chris Mason293ffd52007-03-20 15:57:25 -0400291 root->fs_info->generation = root->root_key.offset + 1;
Chris Masoneb60cea2007-02-02 09:18:22 -0500292 return root;
293}
294
Chris Masone089f052007-03-16 16:20:31 -0400295int write_ctree_super(struct btrfs_trans_handle *trans, struct btrfs_root
Chris Mason79154b12007-03-22 15:59:16 -0400296 *root)
Chris Masoncfaa7292007-02-21 17:04:57 -0500297{
Chris Masond5719762007-03-23 10:01:08 -0400298 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;
Chris Masoncfaa7292007-02-21 17:04:57 -0500310 }
311 return 0;
312}
313
Chris Masone20d96d2007-03-22 12:13:20 -0400314int close_ctree(struct btrfs_root *root)
Chris Masoneb60cea2007-02-02 09:18:22 -0500315{
Chris Mason3768f362007-03-13 16:47:54 -0400316 int ret;
Chris Masone089f052007-03-16 16:20:31 -0400317 struct btrfs_trans_handle *trans;
318
Chris Mason79154b12007-03-22 15:59:16 -0400319 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);
Chris Mason9f5fae22007-03-20 14:38:32 -0400325 BUG_ON(ret);
Chris Mason79154b12007-03-22 15:59:16 -0400326 write_ctree_super(NULL, root);
Chris Masoned2ff2c2007-03-01 18:59:40 -0500327
Chris Masoneb60cea2007-02-02 09:18:22 -0500328 if (root->node)
Chris Mason234b63a2007-03-13 10:46:10 -0400329 btrfs_block_release(root, root->node);
Chris Mason9f5fae22007-03-20 14:38:32 -0400330 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);
Chris Mason234b63a2007-03-13 10:46:10 -0400339 btrfs_block_release(root, root->commit_root);
Chris Masone20d96d2007-03-22 12:13:20 -0400340 btrfs_block_release(root, root->fs_info->sb_buffer);
Chris Masond98237b2007-03-28 13:57:48 -0400341 iput(root->fs_info->btree_inode);
Chris Masone20d96d2007-03-22 12:13:20 -0400342 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);
Chris Masoneb60cea2007-02-02 09:18:22 -0500347 return 0;
348}
349
Chris Masone20d96d2007-03-22 12:13:20 -0400350void btrfs_block_release(struct btrfs_root *root, struct buffer_head *buf)
Chris Masoneb60cea2007-02-02 09:18:22 -0500351{
Chris Masone20d96d2007-03-22 12:13:20 -0400352 brelse(buf);
Chris Masoneb60cea2007-02-02 09:18:22 -0500353}
354