blob: 9cacca0c525cbcc18f0fb8e4bb01b51989cc4ea5 [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) !=
13 btrfs_header_parentid(btrfs_buffer_header(root->node)))
Chris Mason9a8dd152007-02-23 08:38:36 -050014 BUG();
15 return 0;
Chris Masoneb60cea2007-02-02 09:18:22 -050016}
17
Chris Masone20d96d2007-03-22 12:13:20 -040018struct buffer_head *alloc_tree_block(struct btrfs_root *root, u64 blocknr)
Chris Masoned2ff2c2007-03-01 18:59:40 -050019{
Chris Masone20d96d2007-03-22 12:13:20 -040020 return sb_getblk(root->fs_info->sb, blocknr);
Chris Masoned2ff2c2007-03-01 18:59:40 -050021}
22
Chris Masone20d96d2007-03-22 12:13:20 -040023struct buffer_head *find_tree_block(struct btrfs_root *root, u64 blocknr)
Chris Masoneb60cea2007-02-02 09:18:22 -050024{
Chris Masone20d96d2007-03-22 12:13:20 -040025 return sb_getblk(root->fs_info->sb, blocknr);
26}
Chris Mason123abc82007-03-14 14:14:43 -040027
Chris Masone20d96d2007-03-22 12:13:20 -040028struct buffer_head *read_tree_block(struct btrfs_root *root, u64 blocknr)
29{
30 struct buffer_head *buf = sb_bread(root->fs_info->sb, blocknr);
31
Chris Masoneb60cea2007-02-02 09:18:22 -050032 if (!buf)
33 return buf;
Chris Mason9a8dd152007-02-23 08:38:36 -050034 if (check_tree_block(root, buf))
Chris Masoncfaa7292007-02-21 17:04:57 -050035 BUG();
Chris Masoneb60cea2007-02-02 09:18:22 -050036 return buf;
37}
38
Chris Masone089f052007-03-16 16:20:31 -040039int dirty_tree_block(struct btrfs_trans_handle *trans, struct btrfs_root *root,
Chris Masone20d96d2007-03-22 12:13:20 -040040 struct buffer_head *buf)
Chris Masoned2ff2c2007-03-01 18:59:40 -050041{
Chris Masone20d96d2007-03-22 12:13:20 -040042 mark_buffer_dirty(buf);
Chris Masoned2ff2c2007-03-01 18:59:40 -050043 return 0;
44}
45
Chris Masone089f052007-03-16 16:20:31 -040046int clean_tree_block(struct btrfs_trans_handle *trans, struct btrfs_root *root,
Chris Masone20d96d2007-03-22 12:13:20 -040047 struct buffer_head *buf)
Chris Masoned2ff2c2007-03-01 18:59:40 -050048{
Chris Masone20d96d2007-03-22 12:13:20 -040049 clear_buffer_dirty(buf);
Chris Masoned2ff2c2007-03-01 18:59:40 -050050 return 0;
51}
52
Chris Mason123abc82007-03-14 14:14:43 -040053static int __setup_root(struct btrfs_super_block *super,
Chris Mason9f5fae22007-03-20 14:38:32 -040054 struct btrfs_root *root,
55 struct btrfs_fs_info *fs_info,
Chris Masone20d96d2007-03-22 12:13:20 -040056 u64 objectid)
Chris Masond97e63b2007-02-20 16:40:44 -050057{
Chris Masoncfaa7292007-02-21 17:04:57 -050058 root->node = NULL;
Chris Masona28ec192007-03-06 20:08:01 -050059 root->commit_root = NULL;
Chris Mason123abc82007-03-14 14:14:43 -040060 root->blocksize = btrfs_super_blocksize(super);
61 root->ref_cows = 0;
Chris Mason9f5fae22007-03-20 14:38:32 -040062 root->fs_info = fs_info;
Chris Mason3768f362007-03-13 16:47:54 -040063 memset(&root->root_key, 0, sizeof(root->root_key));
64 memset(&root->root_item, 0, sizeof(root->root_item));
65 return 0;
66}
67
Chris Mason123abc82007-03-14 14:14:43 -040068static int find_and_setup_root(struct btrfs_super_block *super,
Chris Mason9f5fae22007-03-20 14:38:32 -040069 struct btrfs_root *tree_root,
70 struct btrfs_fs_info *fs_info,
71 u64 objectid,
Chris Masone20d96d2007-03-22 12:13:20 -040072 struct btrfs_root *root)
Chris Mason3768f362007-03-13 16:47:54 -040073{
74 int ret;
75
Chris Masone20d96d2007-03-22 12:13:20 -040076 __setup_root(super, root, fs_info, objectid);
Chris Mason3768f362007-03-13 16:47:54 -040077 ret = btrfs_find_last_root(tree_root, objectid,
78 &root->root_item, &root->root_key);
79 BUG_ON(ret);
80
81 root->node = read_tree_block(root,
82 btrfs_root_blocknr(&root->root_item));
Chris Mason3768f362007-03-13 16:47:54 -040083 BUG_ON(!root->node);
Chris Masond97e63b2007-02-20 16:40:44 -050084 return 0;
85}
86
Chris Masone20d96d2007-03-22 12:13:20 -040087struct btrfs_root *open_ctree(struct super_block *sb,
88 struct buffer_head *sb_buffer,
89 struct btrfs_super_block *disk_super)
Chris Masoneb60cea2007-02-02 09:18:22 -050090{
Chris Masone20d96d2007-03-22 12:13:20 -040091 struct btrfs_root *root = kmalloc(sizeof(struct btrfs_root),
92 GFP_NOFS);
93 struct btrfs_root *extent_root = kmalloc(sizeof(struct btrfs_root),
94 GFP_NOFS);
95 struct btrfs_root *tree_root = kmalloc(sizeof(struct btrfs_root),
96 GFP_NOFS);
97 struct btrfs_root *inode_root = kmalloc(sizeof(struct btrfs_root),
98 GFP_NOFS);
99 struct btrfs_fs_info *fs_info = kmalloc(sizeof(*fs_info),
100 GFP_NOFS);
Chris Masoneb60cea2007-02-02 09:18:22 -0500101 int ret;
102
Chris Masone20d96d2007-03-22 12:13:20 -0400103 /* FIXME: don't be stupid */
104 if (!btrfs_super_root(disk_super))
105 return NULL;
Chris Mason9f5fae22007-03-20 14:38:32 -0400106 INIT_RADIX_TREE(&fs_info->pinned_radix, GFP_KERNEL);
Chris Mason9f5fae22007-03-20 14:38:32 -0400107 fs_info->running_transaction = NULL;
108 fs_info->fs_root = root;
109 fs_info->tree_root = tree_root;
110 fs_info->extent_root = extent_root;
111 fs_info->inode_root = inode_root;
112 fs_info->last_inode_alloc = 0;
113 fs_info->last_inode_alloc_dirid = 0;
Chris Masone20d96d2007-03-22 12:13:20 -0400114 fs_info->disk_super = disk_super;
115 fs_info->sb_buffer = sb_buffer;
116 fs_info->sb = sb;
Chris Mason79154b12007-03-22 15:59:16 -0400117 mutex_init(&fs_info->trans_mutex);
Chris Mason9f5fae22007-03-20 14:38:32 -0400118 memset(&fs_info->current_insert, 0, sizeof(fs_info->current_insert));
119 memset(&fs_info->last_insert, 0, sizeof(fs_info->last_insert));
Chris Mason3768f362007-03-13 16:47:54 -0400120
Chris Masone20d96d2007-03-22 12:13:20 -0400121 __setup_root(disk_super, tree_root, fs_info, BTRFS_ROOT_TREE_OBJECTID);
122 tree_root->node = read_tree_block(tree_root,
123 btrfs_super_root(disk_super));
Chris Mason3768f362007-03-13 16:47:54 -0400124 BUG_ON(!tree_root->node);
125
Chris Masone20d96d2007-03-22 12:13:20 -0400126 ret = find_and_setup_root(disk_super, tree_root, fs_info,
127 BTRFS_EXTENT_TREE_OBJECTID, extent_root);
Chris Mason3768f362007-03-13 16:47:54 -0400128 BUG_ON(ret);
129
Chris Masone20d96d2007-03-22 12:13:20 -0400130 ret = find_and_setup_root(disk_super, tree_root, fs_info,
131 BTRFS_INODE_MAP_OBJECTID, inode_root);
Chris Mason9f5fae22007-03-20 14:38:32 -0400132 BUG_ON(ret);
133
Chris Masone20d96d2007-03-22 12:13:20 -0400134 ret = find_and_setup_root(disk_super, tree_root, fs_info,
135 BTRFS_FS_TREE_OBJECTID, root);
Chris Mason3768f362007-03-13 16:47:54 -0400136 BUG_ON(ret);
137
Chris Masona28ec192007-03-06 20:08:01 -0500138 root->commit_root = root->node;
Chris Masone20d96d2007-03-22 12:13:20 -0400139 get_bh(root->node);
Chris Mason3768f362007-03-13 16:47:54 -0400140 root->ref_cows = 1;
Chris Mason293ffd52007-03-20 15:57:25 -0400141 root->fs_info->generation = root->root_key.offset + 1;
Chris Masoneb60cea2007-02-02 09:18:22 -0500142 return root;
143}
144
Chris Masone089f052007-03-16 16:20:31 -0400145int write_ctree_super(struct btrfs_trans_handle *trans, struct btrfs_root
Chris Mason79154b12007-03-22 15:59:16 -0400146 *root)
Chris Masoncfaa7292007-02-21 17:04:57 -0500147{
Chris Masone20d96d2007-03-22 12:13:20 -0400148 return 0;
149#if 0
Chris Masoncfaa7292007-02-21 17:04:57 -0500150 int ret;
Chris Masone20d96d2007-03-22 12:13:20 -0400151 btrfs_set_super_root(s, root->fs_info->tree_root->node->b_blocknr);
152
Chris Mason9f5fae22007-03-20 14:38:32 -0400153 ret = pwrite(root->fs_info->fp, s, sizeof(*s),
Chris Mason123abc82007-03-14 14:14:43 -0400154 BTRFS_SUPER_INFO_OFFSET);
Chris Masoncfaa7292007-02-21 17:04:57 -0500155 if (ret != sizeof(*s)) {
156 fprintf(stderr, "failed to write new super block err %d\n", ret);
157 return ret;
158 }
159 return 0;
Chris Masone20d96d2007-03-22 12:13:20 -0400160#endif
Chris Masoncfaa7292007-02-21 17:04:57 -0500161}
162
Chris Masone20d96d2007-03-22 12:13:20 -0400163int close_ctree(struct btrfs_root *root)
Chris Masoneb60cea2007-02-02 09:18:22 -0500164{
Chris Mason3768f362007-03-13 16:47:54 -0400165 int ret;
Chris Masone089f052007-03-16 16:20:31 -0400166 struct btrfs_trans_handle *trans;
167
Chris Mason79154b12007-03-22 15:59:16 -0400168 trans = btrfs_start_transaction(root, 1);
169 btrfs_commit_transaction(trans, root);
170 /* run commit again to drop the original snapshot */
171 trans = btrfs_start_transaction(root, 1);
172 btrfs_commit_transaction(trans, root);
173 ret = btrfs_write_and_wait_transaction(NULL, root);
Chris Mason9f5fae22007-03-20 14:38:32 -0400174 BUG_ON(ret);
Chris Mason79154b12007-03-22 15:59:16 -0400175 write_ctree_super(NULL, root);
Chris Masoned2ff2c2007-03-01 18:59:40 -0500176
Chris Masoneb60cea2007-02-02 09:18:22 -0500177 if (root->node)
Chris Mason234b63a2007-03-13 10:46:10 -0400178 btrfs_block_release(root, root->node);
Chris Mason9f5fae22007-03-20 14:38:32 -0400179 if (root->fs_info->extent_root->node)
180 btrfs_block_release(root->fs_info->extent_root,
181 root->fs_info->extent_root->node);
182 if (root->fs_info->inode_root->node)
183 btrfs_block_release(root->fs_info->inode_root,
184 root->fs_info->inode_root->node);
185 if (root->fs_info->tree_root->node)
186 btrfs_block_release(root->fs_info->tree_root,
187 root->fs_info->tree_root->node);
Chris Mason234b63a2007-03-13 10:46:10 -0400188 btrfs_block_release(root, root->commit_root);
Chris Masone20d96d2007-03-22 12:13:20 -0400189 btrfs_block_release(root, root->fs_info->sb_buffer);
190 kfree(root->fs_info->extent_root);
191 kfree(root->fs_info->inode_root);
192 kfree(root->fs_info->tree_root);
193 kfree(root->fs_info);
194 kfree(root);
Chris Masoneb60cea2007-02-02 09:18:22 -0500195 return 0;
196}
197
Chris Masone20d96d2007-03-22 12:13:20 -0400198void btrfs_block_release(struct btrfs_root *root, struct buffer_head *buf)
Chris Masoneb60cea2007-02-02 09:18:22 -0500199{
Chris Masone20d96d2007-03-22 12:13:20 -0400200 brelse(buf);
Chris Masoneb60cea2007-02-02 09:18:22 -0500201}
202