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