btrfs_mkdir
[deliverable/linux.git] / fs / btrfs / disk-io.c
CommitLineData
e20d96d6
CM
1#include <linux/module.h>
2#include <linux/fs.h>
eb60ceac
CM
3#include "ctree.h"
4#include "disk-io.h"
e089f05c 5#include "transaction.h"
eb60ceac 6
e20d96d6 7static int check_tree_block(struct btrfs_root *root, struct buffer_head *buf)
eb60ceac 8{
e20d96d6
CM
9 struct btrfs_node *node = btrfs_buffer_node(buf);
10 if (buf->b_blocknr != btrfs_header_blocknr(&node->header))
9a8dd150 11 BUG();
e20d96d6 12 if (root->node && btrfs_header_parentid(&node->header) !=
df2ce34c 13 btrfs_header_parentid(btrfs_buffer_header(root->node))) {
7f5c1516 14 BUG();
df2ce34c 15 }
9a8dd150 16 return 0;
eb60ceac
CM
17}
18
e20d96d6 19struct buffer_head *alloc_tree_block(struct btrfs_root *root, u64 blocknr)
ed2ff2cb 20{
e20d96d6 21 return sb_getblk(root->fs_info->sb, blocknr);
ed2ff2cb
CM
22}
23
e20d96d6 24struct buffer_head *find_tree_block(struct btrfs_root *root, u64 blocknr)
eb60ceac 25{
e20d96d6 26 return sb_getblk(root->fs_info->sb, blocknr);
eb60ceac
CM
27}
28
e20d96d6 29struct buffer_head *read_tree_block(struct btrfs_root *root, u64 blocknr)
eb60ceac 30{
e20d96d6 31 struct buffer_head *buf = sb_bread(root->fs_info->sb, blocknr);
eb60ceac 32
e20d96d6
CM
33 if (!buf)
34 return buf;
9a8dd150 35 if (check_tree_block(root, buf))
cfaa7295 36 BUG();
eb60ceac
CM
37 return buf;
38}
39
e089f05c 40int dirty_tree_block(struct btrfs_trans_handle *trans, struct btrfs_root *root,
e20d96d6 41 struct buffer_head *buf)
ed2ff2cb 42{
e20d96d6 43 mark_buffer_dirty(buf);
ed2ff2cb
CM
44 return 0;
45}
46
e089f05c 47int clean_tree_block(struct btrfs_trans_handle *trans, struct btrfs_root *root,
e20d96d6 48 struct buffer_head *buf)
ed2ff2cb 49{
e20d96d6 50 clear_buffer_dirty(buf);
ed2ff2cb
CM
51 return 0;
52}
53
123abc88 54static int __setup_root(struct btrfs_super_block *super,
9f5fae2f
CM
55 struct btrfs_root *root,
56 struct btrfs_fs_info *fs_info,
e20d96d6 57 u64 objectid)
d97e63b6 58{
cfaa7295 59 root->node = NULL;
a28ec197 60 root->commit_root = NULL;
123abc88
CM
61 root->blocksize = btrfs_super_blocksize(super);
62 root->ref_cows = 0;
9f5fae2f 63 root->fs_info = fs_info;
3768f368
CM
64 memset(&root->root_key, 0, sizeof(root->root_key));
65 memset(&root->root_item, 0, sizeof(root->root_item));
66 return 0;
67}
68
123abc88 69static int find_and_setup_root(struct btrfs_super_block *super,
9f5fae2f
CM
70 struct btrfs_root *tree_root,
71 struct btrfs_fs_info *fs_info,
72 u64 objectid,
e20d96d6 73 struct btrfs_root *root)
3768f368
CM
74{
75 int ret;
76
e20d96d6 77 __setup_root(super, root, fs_info, objectid);
3768f368
CM
78 ret = btrfs_find_last_root(tree_root, objectid,
79 &root->root_item, &root->root_key);
80 BUG_ON(ret);
81
82 root->node = read_tree_block(root,
83 btrfs_root_blocknr(&root->root_item));
3768f368 84 BUG_ON(!root->node);
d97e63b6
CM
85 return 0;
86}
87
e20d96d6
CM
88struct btrfs_root *open_ctree(struct super_block *sb,
89 struct buffer_head *sb_buffer,
90 struct btrfs_super_block *disk_super)
2e635a27 91{
e20d96d6
CM
92 struct btrfs_root *root = kmalloc(sizeof(struct btrfs_root),
93 GFP_NOFS);
94 struct btrfs_root *extent_root = kmalloc(sizeof(struct btrfs_root),
95 GFP_NOFS);
96 struct btrfs_root *tree_root = kmalloc(sizeof(struct btrfs_root),
97 GFP_NOFS);
98 struct btrfs_root *inode_root = kmalloc(sizeof(struct btrfs_root),
99 GFP_NOFS);
100 struct btrfs_fs_info *fs_info = kmalloc(sizeof(*fs_info),
101 GFP_NOFS);
eb60ceac
CM
102 int ret;
103
e20d96d6
CM
104 /* FIXME: don't be stupid */
105 if (!btrfs_super_root(disk_super))
106 return NULL;
9f5fae2f 107 INIT_RADIX_TREE(&fs_info->pinned_radix, GFP_KERNEL);
9f5fae2f
CM
108 fs_info->running_transaction = NULL;
109 fs_info->fs_root = root;
110 fs_info->tree_root = tree_root;
111 fs_info->extent_root = extent_root;
112 fs_info->inode_root = inode_root;
113 fs_info->last_inode_alloc = 0;
114 fs_info->last_inode_alloc_dirid = 0;
e20d96d6
CM
115 fs_info->disk_super = disk_super;
116 fs_info->sb_buffer = sb_buffer;
117 fs_info->sb = sb;
79154b1b 118 mutex_init(&fs_info->trans_mutex);
d561c025 119 mutex_init(&fs_info->fs_mutex);
9f5fae2f
CM
120 memset(&fs_info->current_insert, 0, sizeof(fs_info->current_insert));
121 memset(&fs_info->last_insert, 0, sizeof(fs_info->last_insert));
3768f368 122
e20d96d6
CM
123 __setup_root(disk_super, tree_root, fs_info, BTRFS_ROOT_TREE_OBJECTID);
124 tree_root->node = read_tree_block(tree_root,
125 btrfs_super_root(disk_super));
3768f368
CM
126 BUG_ON(!tree_root->node);
127
e20d96d6
CM
128 ret = find_and_setup_root(disk_super, tree_root, fs_info,
129 BTRFS_EXTENT_TREE_OBJECTID, extent_root);
3768f368
CM
130 BUG_ON(ret);
131
e20d96d6
CM
132 ret = find_and_setup_root(disk_super, tree_root, fs_info,
133 BTRFS_INODE_MAP_OBJECTID, inode_root);
9f5fae2f
CM
134 BUG_ON(ret);
135
e20d96d6
CM
136 ret = find_and_setup_root(disk_super, tree_root, fs_info,
137 BTRFS_FS_TREE_OBJECTID, root);
3768f368
CM
138 BUG_ON(ret);
139
a28ec197 140 root->commit_root = root->node;
e20d96d6 141 get_bh(root->node);
3768f368 142 root->ref_cows = 1;
293ffd5f 143 root->fs_info->generation = root->root_key.offset + 1;
eb60ceac
CM
144 return root;
145}
146
e089f05c 147int write_ctree_super(struct btrfs_trans_handle *trans, struct btrfs_root
79154b1b 148 *root)
eb60ceac 149{
d5719762
CM
150 struct buffer_head *bh = root->fs_info->sb_buffer;
151 btrfs_set_super_root(root->fs_info->disk_super,
152 root->fs_info->tree_root->node->b_blocknr);
153 lock_buffer(bh);
154 clear_buffer_dirty(bh);
155 bh->b_end_io = end_buffer_write_sync;
156 get_bh(bh);
157 submit_bh(WRITE, bh);
158 wait_on_buffer(bh);
159 if (!buffer_uptodate(bh)) {
160 WARN_ON(1);
161 return -EIO;
cfaa7295
CM
162 }
163 return 0;
164}
165
e20d96d6 166int close_ctree(struct btrfs_root *root)
cfaa7295 167{
3768f368 168 int ret;
e089f05c
CM
169 struct btrfs_trans_handle *trans;
170
79154b1b
CM
171 trans = btrfs_start_transaction(root, 1);
172 btrfs_commit_transaction(trans, root);
173 /* run commit again to drop the original snapshot */
174 trans = btrfs_start_transaction(root, 1);
175 btrfs_commit_transaction(trans, root);
176 ret = btrfs_write_and_wait_transaction(NULL, root);
3768f368 177 BUG_ON(ret);
79154b1b 178 write_ctree_super(NULL, root);
ed2ff2cb 179
cfaa7295 180 if (root->node)
234b63a0 181 btrfs_block_release(root, root->node);
9f5fae2f
CM
182 if (root->fs_info->extent_root->node)
183 btrfs_block_release(root->fs_info->extent_root,
184 root->fs_info->extent_root->node);
185 if (root->fs_info->inode_root->node)
186 btrfs_block_release(root->fs_info->inode_root,
187 root->fs_info->inode_root->node);
188 if (root->fs_info->tree_root->node)
189 btrfs_block_release(root->fs_info->tree_root,
190 root->fs_info->tree_root->node);
234b63a0 191 btrfs_block_release(root, root->commit_root);
e20d96d6
CM
192 btrfs_block_release(root, root->fs_info->sb_buffer);
193 kfree(root->fs_info->extent_root);
194 kfree(root->fs_info->inode_root);
195 kfree(root->fs_info->tree_root);
196 kfree(root->fs_info);
197 kfree(root);
eb60ceac
CM
198 return 0;
199}
200
e20d96d6 201void btrfs_block_release(struct btrfs_root *root, struct buffer_head *buf)
eb60ceac 202{
e20d96d6 203 brelse(buf);
eb60ceac
CM
204}
205
This page took 0.042593 seconds and 5 git commands to generate.