Btrfs: early fsync support
[linux-2.6-block.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>
22b0ebda 6#include <linux/swap.h>
0f7d52f4 7#include <linux/radix-tree.h>
eb60ceac
CM
8#include "ctree.h"
9#include "disk-io.h"
e089f05c 10#include "transaction.h"
0f7d52f4 11#include "btrfs_inode.h"
eb60ceac 12
7eccb903
CM
13struct dev_lookup {
14 u64 block_start;
15 u64 num_blocks;
b4100d64 16 u64 device_id;
7eccb903
CM
17 struct block_device *bdev;
18};
19
8352d8a4
CM
20int btrfs_insert_dev_radix(struct btrfs_root *root,
21 struct block_device *bdev,
b4100d64 22 u64 device_id,
8352d8a4
CM
23 u64 block_start,
24 u64 num_blocks)
25{
26 struct dev_lookup *lookup;
8352d8a4
CM
27 int ret;
28
29 lookup = kmalloc(sizeof(*lookup), GFP_NOFS);
30 if (!lookup)
31 return -ENOMEM;
32 lookup->block_start = block_start;
33 lookup->num_blocks = num_blocks;
34 lookup->bdev = bdev;
b4100d64 35 lookup->device_id = device_id;
8352d8a4
CM
36
37 ret = radix_tree_insert(&root->fs_info->dev_radix, block_start +
38 num_blocks - 1, lookup);
39 return ret;
40}
41
7eccb903
CM
42u64 bh_blocknr(struct buffer_head *bh)
43{
44 int blkbits = bh->b_page->mapping->host->i_blkbits;
45 u64 blocknr = bh->b_page->index << (PAGE_CACHE_SHIFT - blkbits);
46 unsigned long offset;
47
48 if (PageHighMem(bh->b_page))
49 offset = (unsigned long)bh->b_data;
50 else
51 offset = bh->b_data - (char *)page_address(bh->b_page);
52 blocknr += offset >> (PAGE_CACHE_SHIFT - blkbits);
53 return blocknr;
54}
55
e20d96d6 56static int check_tree_block(struct btrfs_root *root, struct buffer_head *buf)
eb60ceac 57{
e20d96d6 58 struct btrfs_node *node = btrfs_buffer_node(buf);
7eccb903 59 if (bh_blocknr(buf) != btrfs_header_blocknr(&node->header)) {
8352d8a4
CM
60 printk(KERN_CRIT "bh_blocknr(buf) is %Lu, header is %Lu\n",
61 bh_blocknr(buf), btrfs_header_blocknr(&node->header));
9a8dd150 62 BUG();
d98237b3 63 }
9a8dd150 64 return 0;
eb60ceac
CM
65}
66
d98237b3
CM
67struct buffer_head *btrfs_find_tree_block(struct btrfs_root *root, u64 blocknr)
68{
69 struct address_space *mapping = root->fs_info->btree_inode->i_mapping;
70 int blockbits = root->fs_info->sb->s_blocksize_bits;
71 unsigned long index = blocknr >> (PAGE_CACHE_SHIFT - blockbits);
72 struct page *page;
73 struct buffer_head *bh;
74 struct buffer_head *head;
75 struct buffer_head *ret = NULL;
76
2c90e5d6 77
d98237b3
CM
78 page = find_lock_page(mapping, index);
79 if (!page)
80 return NULL;
81
82 if (!page_has_buffers(page))
83 goto out_unlock;
84
85 head = page_buffers(page);
86 bh = head;
87 do {
7eccb903 88 if (buffer_mapped(bh) && bh_blocknr(bh) == blocknr) {
d98237b3
CM
89 ret = bh;
90 get_bh(bh);
91 goto out_unlock;
92 }
93 bh = bh->b_this_page;
94 } while (bh != head);
95out_unlock:
96 unlock_page(page);
d6025579 97 if (ret) {
22b0ebda 98 touch_buffer(ret);
d6025579 99 }
d98237b3
CM
100 page_cache_release(page);
101 return ret;
102}
103
8352d8a4 104int btrfs_map_bh_to_logical(struct btrfs_root *root, struct buffer_head *bh,
7eccb903
CM
105 u64 logical)
106{
107 struct dev_lookup *lookup[2];
7eccb903
CM
108
109 int ret;
110
236454df
CM
111 if (logical == 0) {
112 bh->b_bdev = NULL;
113 bh->b_blocknr = 0;
114 set_buffer_mapped(bh);
115 return 0;
116 }
7eccb903
CM
117 root = root->fs_info->dev_root;
118 ret = radix_tree_gang_lookup(&root->fs_info->dev_radix,
119 (void **)lookup,
120 (unsigned long)logical,
121 ARRAY_SIZE(lookup));
122 if (ret == 0 || lookup[0]->block_start > logical ||
123 lookup[0]->block_start + lookup[0]->num_blocks <= logical) {
124 ret = -ENOENT;
125 goto out;
126 }
127 bh->b_bdev = lookup[0]->bdev;
128 bh->b_blocknr = logical - lookup[0]->block_start;
7eccb903
CM
129 set_buffer_mapped(bh);
130 ret = 0;
131out:
132 return ret;
133}
134
d98237b3
CM
135struct buffer_head *btrfs_find_create_tree_block(struct btrfs_root *root,
136 u64 blocknr)
137{
138 struct address_space *mapping = root->fs_info->btree_inode->i_mapping;
139 int blockbits = root->fs_info->sb->s_blocksize_bits;
140 unsigned long index = blocknr >> (PAGE_CACHE_SHIFT - blockbits);
141 struct page *page;
142 struct buffer_head *bh;
143 struct buffer_head *head;
144 struct buffer_head *ret = NULL;
7eccb903 145 int err;
d98237b3 146 u64 first_block = index << (PAGE_CACHE_SHIFT - blockbits);
22b0ebda 147
d98237b3
CM
148 page = grab_cache_page(mapping, index);
149 if (!page)
150 return NULL;
151
d98237b3
CM
152 if (!page_has_buffers(page))
153 create_empty_buffers(page, root->fs_info->sb->s_blocksize, 0);
154 head = page_buffers(page);
155 bh = head;
156 do {
157 if (!buffer_mapped(bh)) {
8352d8a4 158 err = btrfs_map_bh_to_logical(root, bh, first_block);
7eccb903 159 BUG_ON(err);
d98237b3 160 }
7eccb903 161 if (bh_blocknr(bh) == blocknr) {
d98237b3
CM
162 ret = bh;
163 get_bh(bh);
164 goto out_unlock;
165 }
166 bh = bh->b_this_page;
167 first_block++;
168 } while (bh != head);
169out_unlock:
170 unlock_page(page);
22b0ebda
CM
171 if (ret)
172 touch_buffer(ret);
d98237b3
CM
173 page_cache_release(page);
174 return ret;
175}
176
d98237b3
CM
177static int btree_get_block(struct inode *inode, sector_t iblock,
178 struct buffer_head *bh, int create)
179{
7eccb903
CM
180 int err;
181 struct btrfs_root *root = BTRFS_I(bh->b_page->mapping->host)->root;
8352d8a4 182 err = btrfs_map_bh_to_logical(root, bh, iblock);
7eccb903 183 return err;
d98237b3
CM
184}
185
f254e52c
CM
186int btrfs_csum_data(struct btrfs_root * root, char *data, size_t len,
187 char *result)
87cbda5c 188{
87cbda5c
CM
189 struct scatterlist sg;
190 struct crypto_hash *tfm = root->fs_info->hash_tfm;
191 struct hash_desc desc;
192 int ret;
87cbda5c
CM
193
194 desc.tfm = tfm;
195 desc.flags = 0;
f254e52c 196 sg_init_one(&sg, data, len);
87cbda5c 197 spin_lock(&root->fs_info->hash_lock);
22b0ebda 198 ret = crypto_hash_digest(&desc, &sg, 1, result);
87cbda5c
CM
199 spin_unlock(&root->fs_info->hash_lock);
200 if (ret) {
201 printk("sha256 digest failed\n");
202 }
f254e52c
CM
203 return ret;
204}
205static int csum_tree_block(struct btrfs_root *root, struct buffer_head *bh,
206 int verify)
207{
208 char result[BTRFS_CSUM_SIZE];
209 int ret;
210 struct btrfs_node *node;
211
212 ret = btrfs_csum_data(root, bh->b_data + BTRFS_CSUM_SIZE,
213 bh->b_size - BTRFS_CSUM_SIZE, result);
214 if (ret)
215 return ret;
87cbda5c 216 if (verify) {
f254e52c 217 if (memcmp(bh->b_data, result, BTRFS_CSUM_SIZE)) {
7eccb903
CM
218 printk("checksum verify failed on %Lu\n",
219 bh_blocknr(bh));
f254e52c
CM
220 return 1;
221 }
222 } else {
223 node = btrfs_buffer_node(bh);
22b0ebda 224 memcpy(node->header.csum, result, BTRFS_CSUM_SIZE);
f254e52c 225 }
87cbda5c
CM
226 return 0;
227}
228
d98237b3 229static int btree_writepage(struct page *page, struct writeback_control *wbc)
ed2ff2cb 230{
87cbda5c 231 struct buffer_head *bh;
0f7d52f4 232 struct btrfs_root *root = BTRFS_I(page->mapping->host)->root;
87cbda5c 233 struct buffer_head *head;
87cbda5c
CM
234 if (!page_has_buffers(page)) {
235 create_empty_buffers(page, root->fs_info->sb->s_blocksize,
236 (1 << BH_Dirty)|(1 << BH_Uptodate));
237 }
238 head = page_buffers(page);
239 bh = head;
240 do {
241 if (buffer_dirty(bh))
242 csum_tree_block(root, bh, 0);
243 bh = bh->b_this_page;
244 } while (bh != head);
d98237b3 245 return block_write_full_page(page, btree_get_block, wbc);
ed2ff2cb
CM
246}
247
d98237b3 248static int btree_readpage(struct file * file, struct page * page)
eb60ceac 249{
d98237b3 250 return block_read_full_page(page, btree_get_block);
eb60ceac
CM
251}
252
d98237b3
CM
253static struct address_space_operations btree_aops = {
254 .readpage = btree_readpage,
255 .writepage = btree_writepage,
256 .sync_page = block_sync_page,
257};
258
e20d96d6 259struct buffer_head *read_tree_block(struct btrfs_root *root, u64 blocknr)
eb60ceac 260{
d98237b3 261 struct buffer_head *bh = NULL;
eb60ceac 262
d98237b3
CM
263 bh = btrfs_find_create_tree_block(root, blocknr);
264 if (!bh)
265 return bh;
9d64272c
CM
266 if (buffer_uptodate(bh))
267 goto uptodate;
d98237b3
CM
268 lock_buffer(bh);
269 if (!buffer_uptodate(bh)) {
270 get_bh(bh);
271 bh->b_end_io = end_buffer_read_sync;
272 submit_bh(READ, bh);
273 wait_on_buffer(bh);
274 if (!buffer_uptodate(bh))
275 goto fail;
87cbda5c 276 csum_tree_block(root, bh, 1);
d98237b3
CM
277 } else {
278 unlock_buffer(bh);
279 }
9d64272c 280uptodate:
d98237b3 281 if (check_tree_block(root, bh))
cfaa7295 282 BUG();
d98237b3
CM
283 return bh;
284fail:
285 brelse(bh);
286 return NULL;
eb60ceac
CM
287}
288
e089f05c 289int dirty_tree_block(struct btrfs_trans_handle *trans, struct btrfs_root *root,
e20d96d6 290 struct buffer_head *buf)
ed2ff2cb 291{
d6025579 292 WARN_ON(atomic_read(&buf->b_count) == 0);
e20d96d6 293 mark_buffer_dirty(buf);
ed2ff2cb
CM
294 return 0;
295}
296
e089f05c 297int clean_tree_block(struct btrfs_trans_handle *trans, struct btrfs_root *root,
e20d96d6 298 struct buffer_head *buf)
ed2ff2cb 299{
d6025579 300 WARN_ON(atomic_read(&buf->b_count) == 0);
e20d96d6 301 clear_buffer_dirty(buf);
ed2ff2cb
CM
302 return 0;
303}
304
2c90e5d6 305static int __setup_root(int blocksize,
9f5fae2f
CM
306 struct btrfs_root *root,
307 struct btrfs_fs_info *fs_info,
e20d96d6 308 u64 objectid)
d97e63b6 309{
cfaa7295 310 root->node = NULL;
0f7d52f4 311 root->inode = NULL;
a28ec197 312 root->commit_root = NULL;
2c90e5d6 313 root->blocksize = blocksize;
123abc88 314 root->ref_cows = 0;
9f5fae2f 315 root->fs_info = fs_info;
0f7d52f4
CM
316 root->objectid = objectid;
317 root->last_trans = 0;
1b05da2e
CM
318 root->highest_inode = 0;
319 root->last_inode_alloc = 0;
3768f368
CM
320 memset(&root->root_key, 0, sizeof(root->root_key));
321 memset(&root->root_item, 0, sizeof(root->root_item));
322 return 0;
323}
324
2c90e5d6 325static int find_and_setup_root(int blocksize,
9f5fae2f
CM
326 struct btrfs_root *tree_root,
327 struct btrfs_fs_info *fs_info,
328 u64 objectid,
e20d96d6 329 struct btrfs_root *root)
3768f368
CM
330{
331 int ret;
332
2c90e5d6 333 __setup_root(blocksize, root, fs_info, objectid);
3768f368
CM
334 ret = btrfs_find_last_root(tree_root, objectid,
335 &root->root_item, &root->root_key);
336 BUG_ON(ret);
337
338 root->node = read_tree_block(root,
339 btrfs_root_blocknr(&root->root_item));
3768f368 340 BUG_ON(!root->node);
d97e63b6
CM
341 return 0;
342}
343
0f7d52f4
CM
344struct btrfs_root *btrfs_read_fs_root(struct btrfs_fs_info *fs_info,
345 struct btrfs_key *location)
346{
347 struct btrfs_root *root;
348 struct btrfs_root *tree_root = fs_info->tree_root;
349 struct btrfs_path *path;
350 struct btrfs_leaf *l;
1b05da2e 351 u64 highest_inode;
0f7d52f4
CM
352 int ret = 0;
353
354printk("read_fs_root looking for %Lu %Lu %u\n", location->objectid, location->offset, location->flags);
2619ba1f
CM
355 root = radix_tree_lookup(&fs_info->fs_roots_radix,
356 (unsigned long)location->objectid);
357 if (root) {
358printk("found %p in cache\n", root);
359 return root;
360 }
0f7d52f4
CM
361 root = kmalloc(sizeof(*root), GFP_NOFS);
362 if (!root) {
363printk("failed1\n");
364 return ERR_PTR(-ENOMEM);
365 }
366 if (location->offset == (u64)-1) {
367 ret = find_and_setup_root(fs_info->sb->s_blocksize,
368 fs_info->tree_root, fs_info,
369 location->objectid, root);
370 if (ret) {
371printk("failed2\n");
372 kfree(root);
373 return ERR_PTR(ret);
374 }
375 goto insert;
376 }
377
378 __setup_root(fs_info->sb->s_blocksize, root, fs_info,
379 location->objectid);
380
381 path = btrfs_alloc_path();
382 BUG_ON(!path);
383 ret = btrfs_search_slot(NULL, tree_root, location, path, 0, 0);
384 if (ret != 0) {
385printk("internal search_slot gives us %d\n", ret);
386 if (ret > 0)
387 ret = -ENOENT;
388 goto out;
389 }
390 l = btrfs_buffer_leaf(path->nodes[0]);
391 memcpy(&root->root_item,
392 btrfs_item_ptr(l, path->slots[0], struct btrfs_root_item),
393 sizeof(root->root_item));
394 memcpy(&root->root_key, location, sizeof(*location));
395 ret = 0;
396out:
397 btrfs_release_path(root, path);
398 btrfs_free_path(path);
399 if (ret) {
400 kfree(root);
401 return ERR_PTR(ret);
402 }
403 root->node = read_tree_block(root,
404 btrfs_root_blocknr(&root->root_item));
405 BUG_ON(!root->node);
406insert:
407printk("inserting %p\n", root);
408 root->ref_cows = 1;
2619ba1f
CM
409 ret = radix_tree_insert(&fs_info->fs_roots_radix,
410 (unsigned long)root->root_key.objectid,
0f7d52f4
CM
411 root);
412 if (ret) {
413printk("radix_tree_insert gives us %d\n", ret);
414 brelse(root->node);
415 kfree(root);
416 return ERR_PTR(ret);
417 }
1b05da2e
CM
418 ret = btrfs_find_highest_inode(root, &highest_inode);
419 if (ret == 0) {
420 root->highest_inode = highest_inode;
421 root->last_inode_alloc = highest_inode;
422printk("highest inode is %Lu\n", highest_inode);
423 }
0f7d52f4
CM
424printk("all worked\n");
425 return root;
426}
427
b4100d64
CM
428static int btrfs_open_disk(struct btrfs_root *root, u64 device_id,
429 u64 block_start, u64 num_blocks,
430 char *filename, int name_len)
8352d8a4
CM
431{
432 char *null_filename;
433 struct block_device *bdev;
434 int ret;
435
8352d8a4
CM
436 null_filename = kmalloc(name_len + 1, GFP_NOFS);
437 if (!null_filename)
438 return -ENOMEM;
439 memcpy(null_filename, filename, name_len);
440 null_filename[name_len] = '\0';
441
442 bdev = open_bdev_excl(null_filename, O_RDWR, root->fs_info->sb);
443 if (IS_ERR(bdev)) {
444 ret = PTR_ERR(bdev);
445 goto out;
446 }
447 set_blocksize(bdev, root->fs_info->sb->s_blocksize);
b4100d64
CM
448 ret = btrfs_insert_dev_radix(root, bdev, device_id,
449 block_start, num_blocks);
8352d8a4
CM
450 BUG_ON(ret);
451 ret = 0;
452out:
453 kfree(null_filename);
454 return ret;
455}
456
457static int read_device_info(struct btrfs_root *root)
458{
459 struct btrfs_path *path;
460 int ret;
461 struct btrfs_key key;
462 struct btrfs_leaf *leaf;
463 struct btrfs_device_item *dev_item;
464 int nritems;
465 int slot;
466
467 root = root->fs_info->dev_root;
468
469 path = btrfs_alloc_path();
470 if (!path)
471 return -ENOMEM;
472 key.objectid = 0;
473 key.offset = 0;
474 key.flags = 0;
475 btrfs_set_key_type(&key, BTRFS_DEV_ITEM_KEY);
476
477 mutex_lock(&root->fs_info->fs_mutex);
478 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
479 leaf = btrfs_buffer_leaf(path->nodes[0]);
480 nritems = btrfs_header_nritems(&leaf->header);
481 while(1) {
482 slot = path->slots[0];
483 if (slot >= nritems) {
484 ret = btrfs_next_leaf(root, path);
485 if (ret)
486 break;
487 leaf = btrfs_buffer_leaf(path->nodes[0]);
488 nritems = btrfs_header_nritems(&leaf->header);
489 slot = path->slots[0];
490 }
491 btrfs_disk_key_to_cpu(&key, &leaf->items[slot].key);
492 if (btrfs_key_type(&key) != BTRFS_DEV_ITEM_KEY) {
493 path->slots[0]++;
494 continue;
495 }
496 dev_item = btrfs_item_ptr(leaf, slot, struct btrfs_device_item);
497printk("found key %Lu %Lu\n", key.objectid, key.offset);
b4100d64
CM
498 if (btrfs_device_id(dev_item) !=
499 btrfs_super_device_id(root->fs_info->disk_super)) {
500 ret = btrfs_open_disk(root, btrfs_device_id(dev_item),
501 key.objectid, key.offset,
502 (char *)(dev_item + 1),
503 btrfs_device_pathlen(dev_item));
504 BUG_ON(ret);
505 }
8352d8a4
CM
506 path->slots[0]++;
507 }
508 btrfs_free_path(path);
509 mutex_unlock(&root->fs_info->fs_mutex);
510 return 0;
511}
512
2c90e5d6 513struct btrfs_root *open_ctree(struct super_block *sb)
2e635a27 514{
e20d96d6
CM
515 struct btrfs_root *extent_root = kmalloc(sizeof(struct btrfs_root),
516 GFP_NOFS);
0bd93ba0
CM
517 struct btrfs_root *dev_root = kmalloc(sizeof(struct btrfs_root),
518 GFP_NOFS);
e20d96d6
CM
519 struct btrfs_root *tree_root = kmalloc(sizeof(struct btrfs_root),
520 GFP_NOFS);
e20d96d6
CM
521 struct btrfs_fs_info *fs_info = kmalloc(sizeof(*fs_info),
522 GFP_NOFS);
eb60ceac 523 int ret;
2c90e5d6 524 struct btrfs_super_block *disk_super;
7eccb903 525 struct dev_lookup *dev_lookup;
eb60ceac 526
8ef97622
CM
527 init_bit_radix(&fs_info->pinned_radix);
528 init_bit_radix(&fs_info->pending_del_radix);
0f7d52f4 529 INIT_RADIX_TREE(&fs_info->fs_roots_radix, GFP_NOFS);
7eccb903 530 INIT_RADIX_TREE(&fs_info->dev_radix, GFP_NOFS);
8fd17795 531 INIT_LIST_HEAD(&fs_info->trans_list);
2c90e5d6 532 sb_set_blocksize(sb, 4096);
9f5fae2f 533 fs_info->running_transaction = NULL;
9f5fae2f
CM
534 fs_info->tree_root = tree_root;
535 fs_info->extent_root = extent_root;
0bd93ba0 536 fs_info->dev_root = dev_root;
e20d96d6 537 fs_info->sb = sb;
d98237b3
CM
538 fs_info->btree_inode = new_inode(sb);
539 fs_info->btree_inode->i_ino = 1;
2c90e5d6 540 fs_info->btree_inode->i_nlink = 1;
d98237b3
CM
541 fs_info->btree_inode->i_size = sb->s_bdev->bd_inode->i_size;
542 fs_info->btree_inode->i_mapping->a_ops = &btree_aops;
0f7d52f4
CM
543 BTRFS_I(fs_info->btree_inode)->root = tree_root;
544 memset(&BTRFS_I(fs_info->btree_inode)->location, 0,
545 sizeof(struct btrfs_key));
22b0ebda 546 insert_inode_hash(fs_info->btree_inode);
d98237b3 547 mapping_set_gfp_mask(fs_info->btree_inode->i_mapping, GFP_NOFS);
87cbda5c 548 fs_info->hash_tfm = crypto_alloc_hash("sha256", 0, CRYPTO_ALG_ASYNC);
30ae8467 549 spin_lock_init(&fs_info->hash_lock);
30ae8467 550 if (!fs_info->hash_tfm || IS_ERR(fs_info->hash_tfm)) {
87cbda5c
CM
551 printk("failed to allocate sha256 hash\n");
552 return NULL;
553 }
79154b1b 554 mutex_init(&fs_info->trans_mutex);
d561c025 555 mutex_init(&fs_info->fs_mutex);
9f5fae2f
CM
556 memset(&fs_info->current_insert, 0, sizeof(fs_info->current_insert));
557 memset(&fs_info->last_insert, 0, sizeof(fs_info->last_insert));
3768f368 558
0bd93ba0
CM
559 __setup_root(sb->s_blocksize, dev_root,
560 fs_info, BTRFS_DEV_TREE_OBJECTID);
561
2c90e5d6
CM
562 __setup_root(sb->s_blocksize, tree_root,
563 fs_info, BTRFS_ROOT_TREE_OBJECTID);
7eccb903
CM
564
565 dev_lookup = kmalloc(sizeof(*dev_lookup), GFP_NOFS);
566 dev_lookup->block_start = 0;
567 dev_lookup->num_blocks = (u32)-2;
568 dev_lookup->bdev = sb->s_bdev;
b4100d64 569 dev_lookup->device_id = 0;
7eccb903
CM
570 ret = radix_tree_insert(&fs_info->dev_radix, (u32)-2, dev_lookup);
571 BUG_ON(ret);
2c90e5d6
CM
572 fs_info->sb_buffer = read_tree_block(tree_root,
573 BTRFS_SUPER_INFO_OFFSET /
574 sb->s_blocksize);
d98237b3 575
0f7d52f4 576 if (!fs_info->sb_buffer)
d98237b3 577 return NULL;
d98237b3 578 disk_super = (struct btrfs_super_block *)fs_info->sb_buffer->b_data;
0f7d52f4 579 if (!btrfs_super_root(disk_super))
2c90e5d6 580 return NULL;
0f7d52f4 581
8352d8a4
CM
582 i_size_write(fs_info->btree_inode,
583 btrfs_super_total_blocks(disk_super) <<
584 fs_info->btree_inode->i_blkbits);
585
7eccb903
CM
586 radix_tree_delete(&fs_info->dev_radix, (u32)-2);
587 dev_lookup->block_start = btrfs_super_device_block_start(disk_super);
588 dev_lookup->num_blocks = btrfs_super_device_num_blocks(disk_super);
b4100d64
CM
589 dev_lookup->device_id = btrfs_super_device_id(disk_super);
590
7eccb903
CM
591 ret = radix_tree_insert(&fs_info->dev_radix,
592 dev_lookup->block_start +
8352d8a4 593 dev_lookup->num_blocks - 1, dev_lookup);
7eccb903
CM
594 BUG_ON(ret);
595
d98237b3 596 fs_info->disk_super = disk_super;
8352d8a4 597
0bd93ba0
CM
598 dev_root->node = read_tree_block(tree_root,
599 btrfs_super_device_root(disk_super));
8352d8a4
CM
600
601 ret = read_device_info(dev_root);
602 BUG_ON(ret);
603
e20d96d6
CM
604 tree_root->node = read_tree_block(tree_root,
605 btrfs_super_root(disk_super));
3768f368
CM
606 BUG_ON(!tree_root->node);
607
2c90e5d6
CM
608 mutex_lock(&fs_info->fs_mutex);
609 ret = find_and_setup_root(sb->s_blocksize, tree_root, fs_info,
e20d96d6 610 BTRFS_EXTENT_TREE_OBJECTID, extent_root);
3768f368
CM
611 BUG_ON(ret);
612
0f7d52f4 613 fs_info->generation = btrfs_super_generation(disk_super) + 1;
d6e4a428
CM
614 memset(&fs_info->kobj, 0, sizeof(fs_info->kobj));
615 kobj_set_kset_s(fs_info, btrfs_subsys);
616 kobject_set_name(&fs_info->kobj, "%s", sb->s_id);
617 kobject_register(&fs_info->kobj);
5be6f7f1 618 mutex_unlock(&fs_info->fs_mutex);
0f7d52f4 619 return tree_root;
eb60ceac
CM
620}
621
e089f05c 622int write_ctree_super(struct btrfs_trans_handle *trans, struct btrfs_root
79154b1b 623 *root)
eb60ceac 624{
d5719762 625 struct buffer_head *bh = root->fs_info->sb_buffer;
2c90e5d6 626
d5719762 627 btrfs_set_super_root(root->fs_info->disk_super,
7eccb903 628 bh_blocknr(root->fs_info->tree_root->node));
d5719762 629 lock_buffer(bh);
2c90e5d6 630 WARN_ON(atomic_read(&bh->b_count) < 1);
d5719762 631 clear_buffer_dirty(bh);
87cbda5c 632 csum_tree_block(root, bh, 0);
d5719762
CM
633 bh->b_end_io = end_buffer_write_sync;
634 get_bh(bh);
635 submit_bh(WRITE, bh);
636 wait_on_buffer(bh);
637 if (!buffer_uptodate(bh)) {
638 WARN_ON(1);
639 return -EIO;
cfaa7295
CM
640 }
641 return 0;
642}
643
2619ba1f
CM
644static int free_fs_root(struct btrfs_fs_info *fs_info, struct btrfs_root *root)
645{
646 radix_tree_delete(&fs_info->fs_roots_radix,
647 (unsigned long)root->root_key.objectid);
648 if (root->inode)
649 iput(root->inode);
650 if (root->node)
651 brelse(root->node);
652 if (root->commit_root)
653 brelse(root->commit_root);
654 kfree(root);
655 return 0;
656}
657
0f7d52f4
CM
658int del_fs_roots(struct btrfs_fs_info *fs_info)
659{
660 int ret;
661 struct btrfs_root *gang[8];
662 int i;
663
664 while(1) {
665 ret = radix_tree_gang_lookup(&fs_info->fs_roots_radix,
666 (void **)gang, 0,
667 ARRAY_SIZE(gang));
668 if (!ret)
669 break;
2619ba1f
CM
670 for (i = 0; i < ret; i++)
671 free_fs_root(fs_info, gang[i]);
0f7d52f4
CM
672 }
673 return 0;
674}
b4100d64 675
7eccb903
CM
676static int free_dev_radix(struct btrfs_fs_info *fs_info)
677{
678 struct dev_lookup *lookup[8];
679 struct block_device *super_bdev = fs_info->sb->s_bdev;
680 int ret;
681 int i;
682 while(1) {
683 ret = radix_tree_gang_lookup(&fs_info->dev_radix,
684 (void **)lookup, 0,
685 ARRAY_SIZE(lookup));
686 if (!ret)
687 break;
688 for (i = 0; i < ret; i++) {
689 if (lookup[i]->bdev != super_bdev)
690 close_bdev_excl(lookup[i]->bdev);
691 radix_tree_delete(&fs_info->dev_radix,
692 lookup[i]->block_start +
8352d8a4 693 lookup[i]->num_blocks - 1);
7eccb903
CM
694 kfree(lookup[i]);
695 }
696 }
697 return 0;
698}
0f7d52f4 699
e20d96d6 700int close_ctree(struct btrfs_root *root)
cfaa7295 701{
3768f368 702 int ret;
e089f05c 703 struct btrfs_trans_handle *trans;
0f7d52f4 704 struct btrfs_fs_info *fs_info = root->fs_info;
e089f05c 705
0f7d52f4 706 mutex_lock(&fs_info->fs_mutex);
79154b1b
CM
707 trans = btrfs_start_transaction(root, 1);
708 btrfs_commit_transaction(trans, root);
709 /* run commit again to drop the original snapshot */
710 trans = btrfs_start_transaction(root, 1);
711 btrfs_commit_transaction(trans, root);
712 ret = btrfs_write_and_wait_transaction(NULL, root);
3768f368 713 BUG_ON(ret);
79154b1b 714 write_ctree_super(NULL, root);
0f7d52f4
CM
715 mutex_unlock(&fs_info->fs_mutex);
716
717 if (fs_info->extent_root->node)
718 btrfs_block_release(fs_info->extent_root,
719 fs_info->extent_root->node);
0bd93ba0
CM
720 if (fs_info->dev_root->node)
721 btrfs_block_release(fs_info->dev_root,
722 fs_info->dev_root->node);
0f7d52f4
CM
723 if (fs_info->tree_root->node)
724 btrfs_block_release(fs_info->tree_root,
725 fs_info->tree_root->node);
726 btrfs_block_release(root, fs_info->sb_buffer);
727 crypto_free_hash(fs_info->hash_tfm);
728 truncate_inode_pages(fs_info->btree_inode->i_mapping, 0);
729 iput(fs_info->btree_inode);
7eccb903
CM
730
731 free_dev_radix(fs_info);
0f7d52f4
CM
732 del_fs_roots(fs_info);
733 kfree(fs_info->extent_root);
0f7d52f4
CM
734 kfree(fs_info->tree_root);
735 kobject_unregister(&fs_info->kobj);
eb60ceac
CM
736 return 0;
737}
738
e20d96d6 739void btrfs_block_release(struct btrfs_root *root, struct buffer_head *buf)
eb60ceac 740{
7cfcc17e 741 brelse(buf);
eb60ceac
CM
742}
743