disk-io.c 8.0 KB
Newer Older
C
Chris Mason 已提交
1 2
#include <linux/module.h>
#include <linux/fs.h>
3 4
#include "ctree.h"
#include "disk-io.h"
5
#include "transaction.h"
6

C
Chris Mason 已提交
7
static int check_tree_block(struct btrfs_root *root, struct buffer_head *buf)
8
{
C
Chris Mason 已提交
9 10
	struct btrfs_node *node = btrfs_buffer_node(buf);
	if (buf->b_blocknr != btrfs_header_blocknr(&node->header))
11
		BUG();
C
Chris Mason 已提交
12 13
	if (root->node && btrfs_header_parentid(&node->header) !=
	    btrfs_header_parentid(btrfs_buffer_header(root->node)))
14 15
		BUG();
	return 0;
16 17
}

C
Chris Mason 已提交
18
struct buffer_head *alloc_tree_block(struct btrfs_root *root, u64 blocknr)
19
{
C
Chris Mason 已提交
20
	return sb_getblk(root->fs_info->sb, blocknr);
21 22
}

C
Chris Mason 已提交
23
struct buffer_head *find_tree_block(struct btrfs_root *root, u64 blocknr)
24
{
C
Chris Mason 已提交
25
	return sb_getblk(root->fs_info->sb, blocknr);
26 27
}

C
Chris Mason 已提交
28
struct buffer_head *read_tree_block(struct btrfs_root *root, u64 blocknr)
29
{
C
Chris Mason 已提交
30
	struct buffer_head *buf = sb_bread(root->fs_info->sb, blocknr);
31

C
Chris Mason 已提交
32 33
	if (!buf)
		return buf;
34
	if (check_tree_block(root, buf))
C
Chris Mason 已提交
35
		BUG();
36 37 38
	return buf;
}

39
int dirty_tree_block(struct btrfs_trans_handle *trans, struct btrfs_root *root,
C
Chris Mason 已提交
40
		     struct buffer_head *buf)
41
{
C
Chris Mason 已提交
42
	mark_buffer_dirty(buf);
43 44 45
	return 0;
}

46
int clean_tree_block(struct btrfs_trans_handle *trans, struct btrfs_root *root,
C
Chris Mason 已提交
47
		     struct buffer_head *buf)
48
{
C
Chris Mason 已提交
49
	clear_buffer_dirty(buf);
50 51 52
	return 0;
}

53
int write_tree_block(struct btrfs_trans_handle *trans, struct btrfs_root *root,
C
Chris Mason 已提交
54
		     struct buffer_head *buf)
55
{
C
Chris Mason 已提交
56
	mark_buffer_dirty(buf);
57 58 59
	return 0;
}

60 61
static int __commit_transaction(struct btrfs_trans_handle *trans, struct
				btrfs_root *root)
62
{
C
Chris Mason 已提交
63 64
	filemap_write_and_wait(root->fs_info->sb->s_bdev->bd_inode->i_mapping);
	return 0;
65 66
}

67 68
static int commit_tree_roots(struct btrfs_trans_handle *trans,
			     struct btrfs_fs_info *fs_info)
69 70 71
{
	int ret;
	u64 old_extent_block;
72 73 74 75 76
	struct btrfs_root *tree_root = fs_info->tree_root;
	struct btrfs_root *extent_root = fs_info->extent_root;
	struct btrfs_root *inode_root = fs_info->inode_root;

	btrfs_set_root_blocknr(&inode_root->root_item,
C
Chris Mason 已提交
77
			       inode_root->node->b_blocknr);
78 79 80 81
	ret = btrfs_update_root(trans, tree_root,
				&inode_root->root_key,
				&inode_root->root_item);
	BUG_ON(ret);
82 83
	while(1) {
		old_extent_block = btrfs_root_blocknr(&extent_root->root_item);
C
Chris Mason 已提交
84
		if (old_extent_block == extent_root->node->b_blocknr)
85 86
			break;
		btrfs_set_root_blocknr(&extent_root->root_item,
C
Chris Mason 已提交
87
				       extent_root->node->b_blocknr);
88
		ret = btrfs_update_root(trans, tree_root,
89 90 91 92 93 94 95
					&extent_root->root_key,
					&extent_root->root_item);
		BUG_ON(ret);
	}
	return 0;
}

96 97
int btrfs_commit_transaction(struct btrfs_trans_handle *trans, struct
			     btrfs_root *root, struct btrfs_super_block *s)
98
{
99
	int ret = 0;
C
Chris Mason 已提交
100
	struct buffer_head *snap = root->commit_root;
101
	struct btrfs_key snap_key;
102

103 104 105 106 107 108
	if (root->commit_root == root->node)
		return 0;

	memcpy(&snap_key, &root->root_key, sizeof(snap_key));
	root->root_key.offset++;

C
Chris Mason 已提交
109
	btrfs_set_root_blocknr(&root->root_item, root->node->b_blocknr);
110 111 112 113 114
	ret = btrfs_insert_root(trans, root->fs_info->tree_root,
				&root->root_key, &root->root_item);
	BUG_ON(ret);

	ret = commit_tree_roots(trans, root->fs_info);
115 116
	BUG_ON(ret);

117
	ret = __commit_transaction(trans, root);
118 119
	BUG_ON(ret);

120
	write_ctree_super(trans, root, s);
121 122
	btrfs_finish_extent_commit(trans, root->fs_info->extent_root);
	btrfs_finish_extent_commit(trans, root->fs_info->tree_root);
123 124

	root->commit_root = root->node;
C
Chris Mason 已提交
125
	get_bh(root->node);
126
	ret = btrfs_drop_snapshot(trans, root, snap);
127 128
	BUG_ON(ret);

129
	ret = btrfs_del_root(trans, root->fs_info->tree_root, &snap_key);
130
	BUG_ON(ret);
131
	root->fs_info->generation = root->root_key.offset + 1;
132

133 134 135
	return ret;
}

C
Chris Mason 已提交
136
static int __setup_root(struct btrfs_super_block *super,
137 138
			struct btrfs_root *root,
			struct btrfs_fs_info *fs_info,
C
Chris Mason 已提交
139
			u64 objectid)
140
{
C
Chris Mason 已提交
141
	root->node = NULL;
142
	root->commit_root = NULL;
C
Chris Mason 已提交
143 144
	root->blocksize = btrfs_super_blocksize(super);
	root->ref_cows = 0;
145
	root->fs_info = fs_info;
146 147 148 149 150
	memset(&root->root_key, 0, sizeof(root->root_key));
	memset(&root->root_item, 0, sizeof(root->root_item));
	return 0;
}

C
Chris Mason 已提交
151
static int find_and_setup_root(struct btrfs_super_block *super,
152 153 154
			       struct btrfs_root *tree_root,
			       struct btrfs_fs_info *fs_info,
			       u64 objectid,
C
Chris Mason 已提交
155
			       struct btrfs_root *root)
156 157 158
{
	int ret;

C
Chris Mason 已提交
159
	__setup_root(super, root, fs_info, objectid);
160 161 162 163 164 165 166
	ret = btrfs_find_last_root(tree_root, objectid,
				   &root->root_item, &root->root_key);
	BUG_ON(ret);

	root->node = read_tree_block(root,
				     btrfs_root_blocknr(&root->root_item));
	BUG_ON(!root->node);
167 168 169
	return 0;
}

C
Chris Mason 已提交
170 171 172
struct btrfs_root *open_ctree(struct super_block *sb,
			      struct buffer_head *sb_buffer,
			      struct btrfs_super_block *disk_super)
173
{
C
Chris Mason 已提交
174 175 176 177 178 179 180 181 182 183
	struct btrfs_root *root = kmalloc(sizeof(struct btrfs_root),
					  GFP_NOFS);
	struct btrfs_root *extent_root = kmalloc(sizeof(struct btrfs_root),
						 GFP_NOFS);
	struct btrfs_root *tree_root = kmalloc(sizeof(struct btrfs_root),
					       GFP_NOFS);
	struct btrfs_root *inode_root = kmalloc(sizeof(struct btrfs_root),
						GFP_NOFS);
	struct btrfs_fs_info *fs_info = kmalloc(sizeof(*fs_info),
						GFP_NOFS);
184 185
	int ret;

C
Chris Mason 已提交
186 187 188
	/* FIXME: don't be stupid */
	if (!btrfs_super_root(disk_super))
		return NULL;
189 190 191 192 193 194 195 196
	INIT_RADIX_TREE(&fs_info->pinned_radix, GFP_KERNEL);
	fs_info->running_transaction = NULL;
	fs_info->fs_root = root;
	fs_info->tree_root = tree_root;
	fs_info->extent_root = extent_root;
	fs_info->inode_root = inode_root;
	fs_info->last_inode_alloc = 0;
	fs_info->last_inode_alloc_dirid = 0;
C
Chris Mason 已提交
197 198 199
	fs_info->disk_super = disk_super;
	fs_info->sb_buffer = sb_buffer;
	fs_info->sb = sb;
200 201
	memset(&fs_info->current_insert, 0, sizeof(fs_info->current_insert));
	memset(&fs_info->last_insert, 0, sizeof(fs_info->last_insert));
202

C
Chris Mason 已提交
203 204 205
	__setup_root(disk_super, tree_root, fs_info, BTRFS_ROOT_TREE_OBJECTID);
	tree_root->node = read_tree_block(tree_root,
					  btrfs_super_root(disk_super));
206 207
	BUG_ON(!tree_root->node);

C
Chris Mason 已提交
208 209
	ret = find_and_setup_root(disk_super, tree_root, fs_info,
				  BTRFS_EXTENT_TREE_OBJECTID, extent_root);
210 211
	BUG_ON(ret);

C
Chris Mason 已提交
212 213
	ret = find_and_setup_root(disk_super, tree_root, fs_info,
				  BTRFS_INODE_MAP_OBJECTID, inode_root);
214 215
	BUG_ON(ret);

C
Chris Mason 已提交
216 217
	ret = find_and_setup_root(disk_super, tree_root, fs_info,
				  BTRFS_FS_TREE_OBJECTID, root);
218 219
	BUG_ON(ret);

220
	root->commit_root = root->node;
C
Chris Mason 已提交
221
	get_bh(root->node);
222
	root->ref_cows = 1;
223
	root->fs_info->generation = root->root_key.offset + 1;
224 225 226
	return root;
}

227 228
int write_ctree_super(struct btrfs_trans_handle *trans, struct btrfs_root
		      *root, struct btrfs_super_block *s)
229
{
C
Chris Mason 已提交
230 231
	return 0;
#if 0
232
	int ret;
C
Chris Mason 已提交
233 234
	btrfs_set_super_root(s, root->fs_info->tree_root->node->b_blocknr);

235
	ret = pwrite(root->fs_info->fp, s, sizeof(*s),
C
Chris Mason 已提交
236
		     BTRFS_SUPER_INFO_OFFSET);
C
Chris Mason 已提交
237 238
	if (ret != sizeof(*s)) {
		fprintf(stderr, "failed to write new super block err %d\n", ret);
239
		return ret;
C
Chris Mason 已提交
240 241
	}
	return 0;
C
Chris Mason 已提交
242
#endif
C
Chris Mason 已提交
243 244
}

C
Chris Mason 已提交
245
static int drop_cache(struct btrfs_root *root)
246
{
C
Chris Mason 已提交
247 248
	return 0;
#if 0
249
	while(!list_empty(&root->fs_info->cache)) {
C
Chris Mason 已提交
250 251
		struct buffer_head *b = list_entry(root->fs_info->cache.next,
						    struct buffer_head,
252
						    cache);
253
		list_del_init(&b->cache);
C
Chris Mason 已提交
254
		btrfs_block_release(root, b);
255 256
	}
	return 0;
C
Chris Mason 已提交
257
#endif
258
}
C
Chris Mason 已提交
259 260

int close_ctree(struct btrfs_root *root)
C
Chris Mason 已提交
261
{
262
	int ret;
263 264
	struct btrfs_trans_handle *trans;

265
	trans = root->fs_info->running_transaction;
C
Chris Mason 已提交
266
	btrfs_commit_transaction(trans, root, root->fs_info->disk_super);
267 268 269
	ret = commit_tree_roots(trans, root->fs_info);
	BUG_ON(ret);
	ret = __commit_transaction(trans, root);
270
	BUG_ON(ret);
C
Chris Mason 已提交
271
	write_ctree_super(trans, root, root->fs_info->disk_super);
272 273
	drop_cache(root);

C
Chris Mason 已提交
274
	if (root->node)
C
Chris Mason 已提交
275
		btrfs_block_release(root, root->node);
276 277 278 279 280 281 282 283 284
	if (root->fs_info->extent_root->node)
		btrfs_block_release(root->fs_info->extent_root,
				    root->fs_info->extent_root->node);
	if (root->fs_info->inode_root->node)
		btrfs_block_release(root->fs_info->inode_root,
				    root->fs_info->inode_root->node);
	if (root->fs_info->tree_root->node)
		btrfs_block_release(root->fs_info->tree_root,
				    root->fs_info->tree_root->node);
C
Chris Mason 已提交
285
	btrfs_block_release(root, root->commit_root);
C
Chris Mason 已提交
286 287 288 289 290 291
	btrfs_block_release(root, root->fs_info->sb_buffer);
	kfree(root->fs_info->extent_root);
	kfree(root->fs_info->inode_root);
	kfree(root->fs_info->tree_root);
	kfree(root->fs_info);
	kfree(root);
292 293 294
	return 0;
}

C
Chris Mason 已提交
295
void btrfs_block_release(struct btrfs_root *root, struct buffer_head *buf)
296
{
C
Chris Mason 已提交
297
	brelse(buf);
298 299
}