btrfs_file_write -- first pass
[deliverable/linux.git] / fs / btrfs / disk-io.c
CommitLineData
e20d96d6
CM
1#include <linux/module.h>
2#include <linux/fs.h>
d98237b3 3#include <linux/blkdev.h>
87cbda5c
CM
4#include <linux/crypto.h>
5#include <linux/scatterlist.h>
eb60ceac
CM
6#include "ctree.h"
7#include "disk-io.h"
e089f05c 8#include "transaction.h"
eb60ceac 9
d98237b3 10
e20d96d6 11static int check_tree_block(struct btrfs_root *root, struct buffer_head *buf)
eb60ceac 12{
e20d96d6 13 struct btrfs_node *node = btrfs_buffer_node(buf);
d98237b3 14 if (buf->b_blocknr != btrfs_header_blocknr(&node->header)) {
9a8dd150 15 BUG();
d98237b3 16 }
e20d96d6 17 if (root->node && btrfs_header_parentid(&node->header) !=
df2ce34c 18 btrfs_header_parentid(btrfs_buffer_header(root->node))) {
7f5c1516 19 BUG();
df2ce34c 20 }
9a8dd150 21 return 0;
eb60ceac
CM
22}
23
d98237b3
CM
24struct buffer_head *btrfs_find_tree_block(struct btrfs_root *root, u64 blocknr)
25{
26 struct address_space *mapping = root->fs_info->btree_inode->i_mapping;
27 int blockbits = root->fs_info->sb->s_blocksize_bits;
28 unsigned long index = blocknr >> (PAGE_CACHE_SHIFT - blockbits);
29 struct page *page;
30 struct buffer_head *bh;
31 struct buffer_head *head;
32 struct buffer_head *ret = NULL;
33
34 page = find_lock_page(mapping, index);
35 if (!page)
36 return NULL;
37
38 if (!page_has_buffers(page))
39 goto out_unlock;
40
41 head = page_buffers(page);
42 bh = head;
43 do {
44 if (buffer_mapped(bh) && bh->b_blocknr == blocknr) {
45 ret = bh;
46 get_bh(bh);
47 goto out_unlock;
48 }
49 bh = bh->b_this_page;
50 } while (bh != head);
51out_unlock:
52 unlock_page(page);
53 page_cache_release(page);
54 return ret;
55}
56
57struct buffer_head *btrfs_find_create_tree_block(struct btrfs_root *root,
58 u64 blocknr)
59{
60 struct address_space *mapping = root->fs_info->btree_inode->i_mapping;
61 int blockbits = root->fs_info->sb->s_blocksize_bits;
62 unsigned long index = blocknr >> (PAGE_CACHE_SHIFT - blockbits);
63 struct page *page;
64 struct buffer_head *bh;
65 struct buffer_head *head;
66 struct buffer_head *ret = NULL;
67 u64 first_block = index << (PAGE_CACHE_SHIFT - blockbits);
68 page = grab_cache_page(mapping, index);
69 if (!page)
70 return NULL;
71
d98237b3
CM
72 if (!page_has_buffers(page))
73 create_empty_buffers(page, root->fs_info->sb->s_blocksize, 0);
74 head = page_buffers(page);
75 bh = head;
76 do {
77 if (!buffer_mapped(bh)) {
78 bh->b_bdev = root->fs_info->sb->s_bdev;
79 bh->b_blocknr = first_block;
80 set_buffer_mapped(bh);
81 }
82 if (bh->b_blocknr == blocknr) {
83 ret = bh;
84 get_bh(bh);
85 goto out_unlock;
86 }
87 bh = bh->b_this_page;
88 first_block++;
89 } while (bh != head);
90out_unlock:
91 unlock_page(page);
92 page_cache_release(page);
93 return ret;
94}
95
96static sector_t max_block(struct block_device *bdev)
97{
98 sector_t retval = ~((sector_t)0);
99 loff_t sz = i_size_read(bdev->bd_inode);
100
101 if (sz) {
102 unsigned int size = block_size(bdev);
103 unsigned int sizebits = blksize_bits(size);
104 retval = (sz >> sizebits);
105 }
106 return retval;
107}
108
109static int btree_get_block(struct inode *inode, sector_t iblock,
110 struct buffer_head *bh, int create)
111{
112 if (iblock >= max_block(inode->i_sb->s_bdev)) {
113 if (create)
114 return -EIO;
115
116 /*
117 * for reads, we're just trying to fill a partial page.
118 * return a hole, they will have to call get_block again
119 * before they can fill it, and they will get -EIO at that
120 * time
121 */
122 return 0;
123 }
124 bh->b_bdev = inode->i_sb->s_bdev;
125 bh->b_blocknr = iblock;
126 set_buffer_mapped(bh);
127 return 0;
128}
129
87cbda5c
CM
130static int csum_tree_block(struct btrfs_root * root, struct buffer_head *bh,
131 int verify)
132{
133 struct btrfs_node *node = btrfs_buffer_node(bh);
134 struct scatterlist sg;
135 struct crypto_hash *tfm = root->fs_info->hash_tfm;
136 struct hash_desc desc;
137 int ret;
138 char result[32];
139
140 desc.tfm = tfm;
141 desc.flags = 0;
142 sg_init_one(&sg, bh->b_data + 32, bh->b_size - 32);
143 spin_lock(&root->fs_info->hash_lock);
144 ret = crypto_hash_digest(&desc, &sg, bh->b_size - 32, result);
145 spin_unlock(&root->fs_info->hash_lock);
146 if (ret) {
147 printk("sha256 digest failed\n");
148 }
149 if (verify) {
150 if (memcmp(node->header.csum, result, sizeof(result)))
151 printk("csum verify failed on %Lu\n", bh->b_blocknr);
152 return -EINVAL;
153 } else
154 memcpy(node->header.csum, result, sizeof(node->header.csum));
155 return 0;
156}
157
d98237b3 158static int btree_writepage(struct page *page, struct writeback_control *wbc)
ed2ff2cb 159{
87cbda5c
CM
160 struct buffer_head *bh;
161 struct btrfs_root *root = btrfs_sb(page->mapping->host->i_sb);
162 struct buffer_head *head;
163
164 if (!page_has_buffers(page)) {
165 create_empty_buffers(page, root->fs_info->sb->s_blocksize,
166 (1 << BH_Dirty)|(1 << BH_Uptodate));
167 }
168 head = page_buffers(page);
169 bh = head;
170 do {
171 if (buffer_dirty(bh))
172 csum_tree_block(root, bh, 0);
173 bh = bh->b_this_page;
174 } while (bh != head);
d98237b3 175 return block_write_full_page(page, btree_get_block, wbc);
ed2ff2cb
CM
176}
177
d98237b3 178static int btree_readpage(struct file * file, struct page * page)
eb60ceac 179{
d98237b3 180 return block_read_full_page(page, btree_get_block);
eb60ceac
CM
181}
182
d98237b3
CM
183static struct address_space_operations btree_aops = {
184 .readpage = btree_readpage,
185 .writepage = btree_writepage,
186 .sync_page = block_sync_page,
187};
188
e20d96d6 189struct buffer_head *read_tree_block(struct btrfs_root *root, u64 blocknr)
eb60ceac 190{
d98237b3 191 struct buffer_head *bh = NULL;
eb60ceac 192
d98237b3
CM
193 bh = btrfs_find_create_tree_block(root, blocknr);
194 if (!bh)
195 return bh;
196 lock_buffer(bh);
197 if (!buffer_uptodate(bh)) {
198 get_bh(bh);
199 bh->b_end_io = end_buffer_read_sync;
200 submit_bh(READ, bh);
201 wait_on_buffer(bh);
202 if (!buffer_uptodate(bh))
203 goto fail;
87cbda5c 204 csum_tree_block(root, bh, 1);
d98237b3
CM
205 } else {
206 unlock_buffer(bh);
207 }
208 if (check_tree_block(root, bh))
cfaa7295 209 BUG();
d98237b3
CM
210 return bh;
211fail:
212 brelse(bh);
213 return NULL;
214
eb60ceac
CM
215}
216
e089f05c 217int dirty_tree_block(struct btrfs_trans_handle *trans, struct btrfs_root *root,
e20d96d6 218 struct buffer_head *buf)
ed2ff2cb 219{
e20d96d6 220 mark_buffer_dirty(buf);
ed2ff2cb
CM
221 return 0;
222}
223
e089f05c 224int clean_tree_block(struct btrfs_trans_handle *trans, struct btrfs_root *root,
e20d96d6 225 struct buffer_head *buf)
ed2ff2cb 226{
e20d96d6 227 clear_buffer_dirty(buf);
ed2ff2cb
CM
228 return 0;
229}
230
123abc88 231static int __setup_root(struct btrfs_super_block *super,
9f5fae2f
CM
232 struct btrfs_root *root,
233 struct btrfs_fs_info *fs_info,
e20d96d6 234 u64 objectid)
d97e63b6 235{
cfaa7295 236 root->node = NULL;
a28ec197 237 root->commit_root = NULL;
123abc88
CM
238 root->blocksize = btrfs_super_blocksize(super);
239 root->ref_cows = 0;
9f5fae2f 240 root->fs_info = fs_info;
3768f368
CM
241 memset(&root->root_key, 0, sizeof(root->root_key));
242 memset(&root->root_item, 0, sizeof(root->root_item));
243 return 0;
244}
245
123abc88 246static int find_and_setup_root(struct btrfs_super_block *super,
9f5fae2f
CM
247 struct btrfs_root *tree_root,
248 struct btrfs_fs_info *fs_info,
249 u64 objectid,
e20d96d6 250 struct btrfs_root *root)
3768f368
CM
251{
252 int ret;
253
e20d96d6 254 __setup_root(super, root, fs_info, objectid);
3768f368
CM
255 ret = btrfs_find_last_root(tree_root, objectid,
256 &root->root_item, &root->root_key);
257 BUG_ON(ret);
258
259 root->node = read_tree_block(root,
260 btrfs_root_blocknr(&root->root_item));
3768f368 261 BUG_ON(!root->node);
d97e63b6
CM
262 return 0;
263}
264
e20d96d6
CM
265struct btrfs_root *open_ctree(struct super_block *sb,
266 struct buffer_head *sb_buffer,
267 struct btrfs_super_block *disk_super)
2e635a27 268{
e20d96d6
CM
269 struct btrfs_root *root = kmalloc(sizeof(struct btrfs_root),
270 GFP_NOFS);
271 struct btrfs_root *extent_root = kmalloc(sizeof(struct btrfs_root),
272 GFP_NOFS);
273 struct btrfs_root *tree_root = kmalloc(sizeof(struct btrfs_root),
274 GFP_NOFS);
275 struct btrfs_root *inode_root = kmalloc(sizeof(struct btrfs_root),
276 GFP_NOFS);
277 struct btrfs_fs_info *fs_info = kmalloc(sizeof(*fs_info),
278 GFP_NOFS);
eb60ceac
CM
279 int ret;
280
87cbda5c 281 if (!btrfs_super_root(disk_super)) {
e20d96d6 282 return NULL;
87cbda5c 283 }
8ef97622
CM
284 init_bit_radix(&fs_info->pinned_radix);
285 init_bit_radix(&fs_info->pending_del_radix);
d98237b3 286 sb_set_blocksize(sb, sb_buffer->b_size);
9f5fae2f
CM
287 fs_info->running_transaction = NULL;
288 fs_info->fs_root = root;
289 fs_info->tree_root = tree_root;
290 fs_info->extent_root = extent_root;
291 fs_info->inode_root = inode_root;
292 fs_info->last_inode_alloc = 0;
293 fs_info->last_inode_alloc_dirid = 0;
e20d96d6 294 fs_info->disk_super = disk_super;
e20d96d6 295 fs_info->sb = sb;
d98237b3
CM
296 fs_info->btree_inode = new_inode(sb);
297 fs_info->btree_inode->i_ino = 1;
298 fs_info->btree_inode->i_size = sb->s_bdev->bd_inode->i_size;
299 fs_info->btree_inode->i_mapping->a_ops = &btree_aops;
300 mapping_set_gfp_mask(fs_info->btree_inode->i_mapping, GFP_NOFS);
87cbda5c 301 fs_info->hash_tfm = crypto_alloc_hash("sha256", 0, CRYPTO_ALG_ASYNC);
30ae8467
CM
302 spin_lock_init(&fs_info->hash_lock);
303
304 if (!fs_info->hash_tfm || IS_ERR(fs_info->hash_tfm)) {
87cbda5c
CM
305 printk("failed to allocate sha256 hash\n");
306 return NULL;
307 }
d98237b3 308
79154b1b 309 mutex_init(&fs_info->trans_mutex);
d561c025 310 mutex_init(&fs_info->fs_mutex);
9f5fae2f
CM
311 memset(&fs_info->current_insert, 0, sizeof(fs_info->current_insert));
312 memset(&fs_info->last_insert, 0, sizeof(fs_info->last_insert));
3768f368 313
e20d96d6 314 __setup_root(disk_super, tree_root, fs_info, BTRFS_ROOT_TREE_OBJECTID);
d98237b3
CM
315
316 fs_info->sb_buffer = read_tree_block(tree_root, sb_buffer->b_blocknr);
317
87cbda5c
CM
318 if (!fs_info->sb_buffer) {
319printk("failed2\n");
d98237b3 320 return NULL;
87cbda5c 321 }
d98237b3
CM
322 brelse(sb_buffer);
323 sb_buffer = NULL;
324 disk_super = (struct btrfs_super_block *)fs_info->sb_buffer->b_data;
325 fs_info->disk_super = disk_super;
326
e20d96d6
CM
327 tree_root->node = read_tree_block(tree_root,
328 btrfs_super_root(disk_super));
3768f368
CM
329 BUG_ON(!tree_root->node);
330
e20d96d6
CM
331 ret = find_and_setup_root(disk_super, tree_root, fs_info,
332 BTRFS_EXTENT_TREE_OBJECTID, extent_root);
3768f368
CM
333 BUG_ON(ret);
334
e20d96d6
CM
335 ret = find_and_setup_root(disk_super, tree_root, fs_info,
336 BTRFS_INODE_MAP_OBJECTID, inode_root);
9f5fae2f
CM
337 BUG_ON(ret);
338
e20d96d6
CM
339 ret = find_and_setup_root(disk_super, tree_root, fs_info,
340 BTRFS_FS_TREE_OBJECTID, root);
3768f368 341 BUG_ON(ret);
a28ec197 342 root->commit_root = root->node;
e20d96d6 343 get_bh(root->node);
3768f368 344 root->ref_cows = 1;
293ffd5f 345 root->fs_info->generation = root->root_key.offset + 1;
eb60ceac
CM
346 return root;
347}
348
e089f05c 349int write_ctree_super(struct btrfs_trans_handle *trans, struct btrfs_root
79154b1b 350 *root)
eb60ceac 351{
d5719762
CM
352 struct buffer_head *bh = root->fs_info->sb_buffer;
353 btrfs_set_super_root(root->fs_info->disk_super,
354 root->fs_info->tree_root->node->b_blocknr);
355 lock_buffer(bh);
356 clear_buffer_dirty(bh);
87cbda5c 357 csum_tree_block(root, bh, 0);
d5719762
CM
358 bh->b_end_io = end_buffer_write_sync;
359 get_bh(bh);
360 submit_bh(WRITE, bh);
361 wait_on_buffer(bh);
362 if (!buffer_uptodate(bh)) {
363 WARN_ON(1);
364 return -EIO;
cfaa7295
CM
365 }
366 return 0;
367}
368
e20d96d6 369int close_ctree(struct btrfs_root *root)
cfaa7295 370{
3768f368 371 int ret;
e089f05c
CM
372 struct btrfs_trans_handle *trans;
373
79154b1b
CM
374 trans = btrfs_start_transaction(root, 1);
375 btrfs_commit_transaction(trans, root);
376 /* run commit again to drop the original snapshot */
377 trans = btrfs_start_transaction(root, 1);
378 btrfs_commit_transaction(trans, root);
379 ret = btrfs_write_and_wait_transaction(NULL, root);
3768f368 380 BUG_ON(ret);
79154b1b 381 write_ctree_super(NULL, root);
ed2ff2cb 382
cfaa7295 383 if (root->node)
234b63a0 384 btrfs_block_release(root, root->node);
9f5fae2f
CM
385 if (root->fs_info->extent_root->node)
386 btrfs_block_release(root->fs_info->extent_root,
387 root->fs_info->extent_root->node);
388 if (root->fs_info->inode_root->node)
389 btrfs_block_release(root->fs_info->inode_root,
390 root->fs_info->inode_root->node);
391 if (root->fs_info->tree_root->node)
392 btrfs_block_release(root->fs_info->tree_root,
393 root->fs_info->tree_root->node);
234b63a0 394 btrfs_block_release(root, root->commit_root);
e20d96d6 395 btrfs_block_release(root, root->fs_info->sb_buffer);
87cbda5c 396 crypto_free_hash(root->fs_info->hash_tfm);
30ae8467 397 truncate_inode_pages(root->fs_info->btree_inode->i_mapping, 0);
d98237b3 398 iput(root->fs_info->btree_inode);
e20d96d6
CM
399 kfree(root->fs_info->extent_root);
400 kfree(root->fs_info->inode_root);
401 kfree(root->fs_info->tree_root);
402 kfree(root->fs_info);
403 kfree(root);
eb60ceac
CM
404 return 0;
405}
406
e20d96d6 407void btrfs_block_release(struct btrfs_root *root, struct buffer_head *buf)
eb60ceac 408{
e20d96d6 409 brelse(buf);
eb60ceac
CM
410}
411
This page took 0.047914 seconds and 5 git commands to generate.