blob: 2f1eb4ae4e1d2f9c31b5b71e20e06a1e299a30f8 [file] [log] [blame]
Chris Masone20d96d2007-03-22 12:13:20 -04001#include <linux/module.h>
2#include <linux/fs.h>
Chris Masoneb60cea2007-02-02 09:18:22 -05003#include "ctree.h"
4#include "disk-io.h"
Chris Masone089f052007-03-16 16:20:31 -04005#include "transaction.h"
Chris Masoneb60cea2007-02-02 09:18:22 -05006
Chris Masone20d96d2007-03-22 12:13:20 -04007static int check_tree_block(struct btrfs_root *root, struct buffer_head *buf)
Chris Masoneb60cea2007-02-02 09:18:22 -05008{
Chris Masone20d96d2007-03-22 12:13:20 -04009 struct btrfs_node *node = btrfs_buffer_node(buf);
10 if (buf->b_blocknr != btrfs_header_blocknr(&node->header))
Chris Mason9a8dd152007-02-23 08:38:36 -050011 BUG();
Chris Masone20d96d2007-03-22 12:13:20 -040012 if (root->node && btrfs_header_parentid(&node->header) !=
Chris Masondf2ce342007-03-23 11:00:45 -040013 btrfs_header_parentid(btrfs_buffer_header(root->node))) {
14 printk("block %Lu parentids don't match buf %Lu, root %Lu\n",
15 buf->b_blocknr,
16 btrfs_header_parentid(&node->header),
17 btrfs_header_parentid(btrfs_buffer_header(root->node)));
18 WARN_ON(1);
19 }
Chris Mason9a8dd152007-02-23 08:38:36 -050020 return 0;
Chris Masoneb60cea2007-02-02 09:18:22 -050021}
22
Chris Masone20d96d2007-03-22 12:13:20 -040023struct buffer_head *alloc_tree_block(struct btrfs_root *root, u64 blocknr)
Chris Masoned2ff2c2007-03-01 18:59:40 -050024{
Chris Masone20d96d2007-03-22 12:13:20 -040025 return sb_getblk(root->fs_info->sb, blocknr);
Chris Masoned2ff2c2007-03-01 18:59:40 -050026}
27
Chris Masone20d96d2007-03-22 12:13:20 -040028struct buffer_head *find_tree_block(struct btrfs_root *root, u64 blocknr)
Chris Masoneb60cea2007-02-02 09:18:22 -050029{
Chris Masone20d96d2007-03-22 12:13:20 -040030 return sb_getblk(root->fs_info->sb, blocknr);
31}
Chris Mason123abc82007-03-14 14:14:43 -040032
Chris Masone20d96d2007-03-22 12:13:20 -040033struct buffer_head *read_tree_block(struct btrfs_root *root, u64 blocknr)
34{
35 struct buffer_head *buf = sb_bread(root->fs_info->sb, blocknr);
36
Chris Masoneb60cea2007-02-02 09:18:22 -050037 if (!buf)
38 return buf;
Chris Mason9a8dd152007-02-23 08:38:36 -050039 if (check_tree_block(root, buf))
Chris Masoncfaa7292007-02-21 17:04:57 -050040 BUG();
Chris Masoneb60cea2007-02-02 09:18:22 -050041 return buf;
42}
43
Chris Masone089f052007-03-16 16:20:31 -040044int dirty_tree_block(struct btrfs_trans_handle *trans, struct btrfs_root *root,
Chris Masone20d96d2007-03-22 12:13:20 -040045 struct buffer_head *buf)
Chris Masoned2ff2c2007-03-01 18:59:40 -050046{
Chris Masone20d96d2007-03-22 12:13:20 -040047 mark_buffer_dirty(buf);
Chris Masoned2ff2c2007-03-01 18:59:40 -050048 return 0;
49}
50
Chris Masone089f052007-03-16 16:20:31 -040051int clean_tree_block(struct btrfs_trans_handle *trans, struct btrfs_root *root,
Chris Masone20d96d2007-03-22 12:13:20 -040052 struct buffer_head *buf)
Chris Masoned2ff2c2007-03-01 18:59:40 -050053{
Chris Masone20d96d2007-03-22 12:13:20 -040054 clear_buffer_dirty(buf);
Chris Masoned2ff2c2007-03-01 18:59:40 -050055 return 0;
56}
57
Chris Mason123abc82007-03-14 14:14:43 -040058static int __setup_root(struct btrfs_super_block *super,
Chris Mason9f5fae22007-03-20 14:38:32 -040059 struct btrfs_root *root,
60 struct btrfs_fs_info *fs_info,
Chris Masone20d96d2007-03-22 12:13:20 -040061 u64 objectid)
Chris Masond97e63b2007-02-20 16:40:44 -050062{
Chris Masoncfaa7292007-02-21 17:04:57 -050063 root->node = NULL;
Chris Masona28ec192007-03-06 20:08:01 -050064 root->commit_root = NULL;
Chris Mason123abc82007-03-14 14:14:43 -040065 root->blocksize = btrfs_super_blocksize(super);
66 root->ref_cows = 0;
Chris Mason9f5fae22007-03-20 14:38:32 -040067 root->fs_info = fs_info;
Chris Mason3768f362007-03-13 16:47:54 -040068 memset(&root->root_key, 0, sizeof(root->root_key));
69 memset(&root->root_item, 0, sizeof(root->root_item));
70 return 0;
71}
72
Chris Mason123abc82007-03-14 14:14:43 -040073static int find_and_setup_root(struct btrfs_super_block *super,
Chris Mason9f5fae22007-03-20 14:38:32 -040074 struct btrfs_root *tree_root,
75 struct btrfs_fs_info *fs_info,
76 u64 objectid,
Chris Masone20d96d2007-03-22 12:13:20 -040077 struct btrfs_root *root)
Chris Mason3768f362007-03-13 16:47:54 -040078{
79 int ret;
80
Chris Masone20d96d2007-03-22 12:13:20 -040081 __setup_root(super, root, fs_info, objectid);
Chris Mason3768f362007-03-13 16:47:54 -040082 ret = btrfs_find_last_root(tree_root, objectid,
83 &root->root_item, &root->root_key);
84 BUG_ON(ret);
85
86 root->node = read_tree_block(root,
87 btrfs_root_blocknr(&root->root_item));
Chris Mason3768f362007-03-13 16:47:54 -040088 BUG_ON(!root->node);
Chris Masond97e63b2007-02-20 16:40:44 -050089 return 0;
90}
91
Chris Masone20d96d2007-03-22 12:13:20 -040092struct btrfs_root *open_ctree(struct super_block *sb,
93 struct buffer_head *sb_buffer,
94 struct btrfs_super_block *disk_super)
Chris Masoneb60cea2007-02-02 09:18:22 -050095{
Chris Masone20d96d2007-03-22 12:13:20 -040096 struct btrfs_root *root = kmalloc(sizeof(struct btrfs_root),
97 GFP_NOFS);
98 struct btrfs_root *extent_root = kmalloc(sizeof(struct btrfs_root),
99 GFP_NOFS);
100 struct btrfs_root *tree_root = kmalloc(sizeof(struct btrfs_root),
101 GFP_NOFS);
102 struct btrfs_root *inode_root = kmalloc(sizeof(struct btrfs_root),
103 GFP_NOFS);
104 struct btrfs_fs_info *fs_info = kmalloc(sizeof(*fs_info),
105 GFP_NOFS);
Chris Masoneb60cea2007-02-02 09:18:22 -0500106 int ret;
107
Chris Masone20d96d2007-03-22 12:13:20 -0400108 /* FIXME: don't be stupid */
109 if (!btrfs_super_root(disk_super))
110 return NULL;
Chris Mason9f5fae22007-03-20 14:38:32 -0400111 INIT_RADIX_TREE(&fs_info->pinned_radix, GFP_KERNEL);
Chris Mason9f5fae22007-03-20 14:38:32 -0400112 fs_info->running_transaction = NULL;
113 fs_info->fs_root = root;
114 fs_info->tree_root = tree_root;
115 fs_info->extent_root = extent_root;
116 fs_info->inode_root = inode_root;
117 fs_info->last_inode_alloc = 0;
118 fs_info->last_inode_alloc_dirid = 0;
Chris Masone20d96d2007-03-22 12:13:20 -0400119 fs_info->disk_super = disk_super;
120 fs_info->sb_buffer = sb_buffer;
121 fs_info->sb = sb;
Chris Mason79154b12007-03-22 15:59:16 -0400122 mutex_init(&fs_info->trans_mutex);
Chris Mason9f5fae22007-03-20 14:38:32 -0400123 memset(&fs_info->current_insert, 0, sizeof(fs_info->current_insert));
124 memset(&fs_info->last_insert, 0, sizeof(fs_info->last_insert));
Chris Mason3768f362007-03-13 16:47:54 -0400125
Chris Masone20d96d2007-03-22 12:13:20 -0400126 __setup_root(disk_super, tree_root, fs_info, BTRFS_ROOT_TREE_OBJECTID);
127 tree_root->node = read_tree_block(tree_root,
128 btrfs_super_root(disk_super));
Chris Mason3768f362007-03-13 16:47:54 -0400129 BUG_ON(!tree_root->node);
130
Chris Masone20d96d2007-03-22 12:13:20 -0400131 ret = find_and_setup_root(disk_super, tree_root, fs_info,
132 BTRFS_EXTENT_TREE_OBJECTID, extent_root);
Chris Mason3768f362007-03-13 16:47:54 -0400133 BUG_ON(ret);
134
Chris Masone20d96d2007-03-22 12:13:20 -0400135 ret = find_and_setup_root(disk_super, tree_root, fs_info,
136 BTRFS_INODE_MAP_OBJECTID, inode_root);
Chris Mason9f5fae22007-03-20 14:38:32 -0400137 BUG_ON(ret);
138
Chris Masone20d96d2007-03-22 12:13:20 -0400139 ret = find_and_setup_root(disk_super, tree_root, fs_info,
140 BTRFS_FS_TREE_OBJECTID, root);
Chris Mason3768f362007-03-13 16:47:54 -0400141 BUG_ON(ret);
142
Chris Masona28ec192007-03-06 20:08:01 -0500143 root->commit_root = root->node;
Chris Masone20d96d2007-03-22 12:13:20 -0400144 get_bh(root->node);
Chris Mason3768f362007-03-13 16:47:54 -0400145 root->ref_cows = 1;
Chris Mason293ffd52007-03-20 15:57:25 -0400146 root->fs_info->generation = root->root_key.offset + 1;
Chris Masoneb60cea2007-02-02 09:18:22 -0500147 return root;
148}
149
Chris Masone089f052007-03-16 16:20:31 -0400150int write_ctree_super(struct btrfs_trans_handle *trans, struct btrfs_root
Chris Mason79154b12007-03-22 15:59:16 -0400151 *root)
Chris Masoncfaa7292007-02-21 17:04:57 -0500152{
Chris Masond5719762007-03-23 10:01:08 -0400153 struct buffer_head *bh = root->fs_info->sb_buffer;
154 btrfs_set_super_root(root->fs_info->disk_super,
155 root->fs_info->tree_root->node->b_blocknr);
156 lock_buffer(bh);
157 clear_buffer_dirty(bh);
158 bh->b_end_io = end_buffer_write_sync;
159 get_bh(bh);
160 submit_bh(WRITE, bh);
161 wait_on_buffer(bh);
162 if (!buffer_uptodate(bh)) {
163 WARN_ON(1);
164 return -EIO;
Chris Masoncfaa7292007-02-21 17:04:57 -0500165 }
166 return 0;
167}
168
Chris Masone20d96d2007-03-22 12:13:20 -0400169int close_ctree(struct btrfs_root *root)
Chris Masoneb60cea2007-02-02 09:18:22 -0500170{
Chris Mason3768f362007-03-13 16:47:54 -0400171 int ret;
Chris Masone089f052007-03-16 16:20:31 -0400172 struct btrfs_trans_handle *trans;
173
Chris Mason79154b12007-03-22 15:59:16 -0400174 trans = btrfs_start_transaction(root, 1);
175 btrfs_commit_transaction(trans, root);
176 /* run commit again to drop the original snapshot */
177 trans = btrfs_start_transaction(root, 1);
178 btrfs_commit_transaction(trans, root);
179 ret = btrfs_write_and_wait_transaction(NULL, root);
Chris Mason9f5fae22007-03-20 14:38:32 -0400180 BUG_ON(ret);
Chris Mason79154b12007-03-22 15:59:16 -0400181 write_ctree_super(NULL, root);
Chris Masoned2ff2c2007-03-01 18:59:40 -0500182
Chris Masoneb60cea2007-02-02 09:18:22 -0500183 if (root->node)
Chris Mason234b63a2007-03-13 10:46:10 -0400184 btrfs_block_release(root, root->node);
Chris Mason9f5fae22007-03-20 14:38:32 -0400185 if (root->fs_info->extent_root->node)
186 btrfs_block_release(root->fs_info->extent_root,
187 root->fs_info->extent_root->node);
188 if (root->fs_info->inode_root->node)
189 btrfs_block_release(root->fs_info->inode_root,
190 root->fs_info->inode_root->node);
191 if (root->fs_info->tree_root->node)
192 btrfs_block_release(root->fs_info->tree_root,
193 root->fs_info->tree_root->node);
Chris Mason234b63a2007-03-13 10:46:10 -0400194 btrfs_block_release(root, root->commit_root);
Chris Masone20d96d2007-03-22 12:13:20 -0400195 btrfs_block_release(root, root->fs_info->sb_buffer);
196 kfree(root->fs_info->extent_root);
197 kfree(root->fs_info->inode_root);
198 kfree(root->fs_info->tree_root);
199 kfree(root->fs_info);
200 kfree(root);
Chris Masoneb60cea2007-02-02 09:18:22 -0500201 return 0;
202}
203
Chris Masone20d96d2007-03-22 12:13:20 -0400204void btrfs_block_release(struct btrfs_root *root, struct buffer_head *buf)
Chris Masoneb60cea2007-02-02 09:18:22 -0500205{
Chris Masone20d96d2007-03-22 12:13:20 -0400206 brelse(buf);
Chris Masoneb60cea2007-02-02 09:18:22 -0500207}
208