1 #include <linux/module.h>
3 #include <linux/blkdev.h>
6 #include "transaction.h"
9 static int check_tree_block(struct btrfs_root *root, struct buffer_head *buf)
11 struct btrfs_node *node = btrfs_buffer_node(buf);
12 if (buf->b_blocknr != btrfs_header_blocknr(&node->header)) {
15 if (root->node && btrfs_header_parentid(&node->header) !=
16 btrfs_header_parentid(btrfs_buffer_header(root->node))) {
22 struct buffer_head *btrfs_find_tree_block(struct btrfs_root *root, u64 blocknr)
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);
28 struct buffer_head *bh;
29 struct buffer_head *head;
30 struct buffer_head *ret = NULL;
32 page = find_lock_page(mapping, index);
36 if (!page_has_buffers(page))
39 head = page_buffers(page);
42 if (buffer_mapped(bh) && bh->b_blocknr == blocknr) {
51 page_cache_release(page);
55 struct buffer_head *btrfs_find_create_tree_block(struct btrfs_root *root,
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);
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);
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);
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);
81 if (bh->b_blocknr == blocknr) {
91 page_cache_release(page);
95 static sector_t max_block(struct block_device *bdev)
97 sector_t retval = ~((sector_t)0);
98 loff_t sz = i_size_read(bdev->bd_inode);
101 unsigned int size = block_size(bdev);
102 unsigned int sizebits = blksize_bits(size);
103 retval = (sz >> sizebits);
108 static int btree_get_block(struct inode *inode, sector_t iblock,
109 struct buffer_head *bh, int create)
111 if (iblock >= max_block(inode->i_sb->s_bdev)) {
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
123 bh->b_bdev = inode->i_sb->s_bdev;
124 bh->b_blocknr = iblock;
125 set_buffer_mapped(bh);
129 static int btree_writepage(struct page *page, struct writeback_control *wbc)
131 return block_write_full_page(page, btree_get_block, wbc);
134 static int btree_readpage(struct file * file, struct page * page)
136 return block_read_full_page(page, btree_get_block);
139 static struct address_space_operations btree_aops = {
140 .readpage = btree_readpage,
141 .writepage = btree_writepage,
142 .sync_page = block_sync_page,
145 struct buffer_head *read_tree_block(struct btrfs_root *root, u64 blocknr)
147 struct buffer_head *bh = NULL;
149 bh = btrfs_find_create_tree_block(root, blocknr);
153 if (!buffer_uptodate(bh)) {
155 bh->b_end_io = end_buffer_read_sync;
158 if (!buffer_uptodate(bh))
163 if (check_tree_block(root, bh))
172 int dirty_tree_block(struct btrfs_trans_handle *trans, struct btrfs_root *root,
173 struct buffer_head *buf)
175 mark_buffer_dirty(buf);
179 int clean_tree_block(struct btrfs_trans_handle *trans, struct btrfs_root *root,
180 struct buffer_head *buf)
182 clear_buffer_dirty(buf);
186 static int __setup_root(struct btrfs_super_block *super,
187 struct btrfs_root *root,
188 struct btrfs_fs_info *fs_info,
192 root->commit_root = NULL;
193 root->blocksize = btrfs_super_blocksize(super);
195 root->fs_info = fs_info;
196 memset(&root->root_key, 0, sizeof(root->root_key));
197 memset(&root->root_item, 0, sizeof(root->root_item));
201 static int find_and_setup_root(struct btrfs_super_block *super,
202 struct btrfs_root *tree_root,
203 struct btrfs_fs_info *fs_info,
205 struct btrfs_root *root)
209 __setup_root(super, root, fs_info, objectid);
210 ret = btrfs_find_last_root(tree_root, objectid,
211 &root->root_item, &root->root_key);
214 root->node = read_tree_block(root,
215 btrfs_root_blocknr(&root->root_item));
220 struct btrfs_root *open_ctree(struct super_block *sb,
221 struct buffer_head *sb_buffer,
222 struct btrfs_super_block *disk_super)
224 struct btrfs_root *root = kmalloc(sizeof(struct btrfs_root),
226 struct btrfs_root *extent_root = kmalloc(sizeof(struct btrfs_root),
228 struct btrfs_root *tree_root = kmalloc(sizeof(struct btrfs_root),
230 struct btrfs_root *inode_root = kmalloc(sizeof(struct btrfs_root),
232 struct btrfs_fs_info *fs_info = kmalloc(sizeof(*fs_info),
236 if (!btrfs_super_root(disk_super))
238 init_bit_radix(&fs_info->pinned_radix);
239 init_bit_radix(&fs_info->pending_del_radix);
240 sb_set_blocksize(sb, sb_buffer->b_size);
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;
248 fs_info->disk_super = disk_super;
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);
256 mutex_init(&fs_info->trans_mutex);
257 mutex_init(&fs_info->fs_mutex);
258 memset(&fs_info->current_insert, 0, sizeof(fs_info->current_insert));
259 memset(&fs_info->last_insert, 0, sizeof(fs_info->last_insert));
261 __setup_root(disk_super, tree_root, fs_info, BTRFS_ROOT_TREE_OBJECTID);
263 fs_info->sb_buffer = read_tree_block(tree_root, sb_buffer->b_blocknr);
265 if (!fs_info->sb_buffer)
270 disk_super = (struct btrfs_super_block *)fs_info->sb_buffer->b_data;
271 fs_info->disk_super = disk_super;
273 tree_root->node = read_tree_block(tree_root,
274 btrfs_super_root(disk_super));
275 BUG_ON(!tree_root->node);
277 ret = find_and_setup_root(disk_super, tree_root, fs_info,
278 BTRFS_EXTENT_TREE_OBJECTID, extent_root);
281 ret = find_and_setup_root(disk_super, tree_root, fs_info,
282 BTRFS_INODE_MAP_OBJECTID, inode_root);
285 ret = find_and_setup_root(disk_super, tree_root, fs_info,
286 BTRFS_FS_TREE_OBJECTID, root);
288 root->commit_root = root->node;
291 root->fs_info->generation = root->root_key.offset + 1;
295 int write_ctree_super(struct btrfs_trans_handle *trans, struct btrfs_root
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);
302 clear_buffer_dirty(bh);
303 bh->b_end_io = end_buffer_write_sync;
305 submit_bh(WRITE, bh);
307 if (!buffer_uptodate(bh)) {
314 int close_ctree(struct btrfs_root *root)
317 struct btrfs_trans_handle *trans;
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);
326 write_ctree_super(NULL, root);
329 btrfs_block_release(root, root->node);
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);
339 btrfs_block_release(root, root->commit_root);
340 btrfs_block_release(root, root->fs_info->sb_buffer);
341 iput(root->fs_info->btree_inode);
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);
350 void btrfs_block_release(struct btrfs_root *root, struct buffer_head *buf)