Btrfs: crash recovery fixes
[linux-2.6-block.git] / fs / btrfs / disk-io.c
CommitLineData
6cbd5570
CM
1/*
2 * Copyright (C) 2007 Oracle. All rights reserved.
3 *
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public
6 * License v2 as published by the Free Software Foundation.
7 *
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 * General Public License for more details.
12 *
13 * You should have received a copy of the GNU General Public
14 * License along with this program; if not, write to the
15 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
16 * Boston, MA 021110-1307, USA.
17 */
18
e20d96d6
CM
19#include <linux/module.h>
20#include <linux/fs.h>
d98237b3 21#include <linux/blkdev.h>
11bd143f 22#include <linux/crc32c.h>
87cbda5c 23#include <linux/scatterlist.h>
22b0ebda 24#include <linux/swap.h>
0f7d52f4 25#include <linux/radix-tree.h>
35b7e476 26#include <linux/writeback.h>
eb60ceac
CM
27#include "ctree.h"
28#include "disk-io.h"
e089f05c 29#include "transaction.h"
0f7d52f4 30#include "btrfs_inode.h"
eb60ceac 31
7eccb903
CM
32u64 bh_blocknr(struct buffer_head *bh)
33{
0cf6c620 34 return bh->b_blocknr;
7eccb903
CM
35}
36
e20d96d6 37static int check_tree_block(struct btrfs_root *root, struct buffer_head *buf)
eb60ceac 38{
e20d96d6 39 struct btrfs_node *node = btrfs_buffer_node(buf);
7eccb903 40 if (bh_blocknr(buf) != btrfs_header_blocknr(&node->header)) {
5af3981c
CM
41 printk(KERN_CRIT "bh_blocknr(buf) is %llu, header is %llu\n",
42 (unsigned long long)bh_blocknr(buf),
43 (unsigned long long)btrfs_header_blocknr(&node->header));
39279cc3 44 return 1;
d98237b3 45 }
9a8dd150 46 return 0;
eb60ceac
CM
47}
48
d98237b3
CM
49struct buffer_head *btrfs_find_tree_block(struct btrfs_root *root, u64 blocknr)
50{
51 struct address_space *mapping = root->fs_info->btree_inode->i_mapping;
52 int blockbits = root->fs_info->sb->s_blocksize_bits;
53 unsigned long index = blocknr >> (PAGE_CACHE_SHIFT - blockbits);
54 struct page *page;
55 struct buffer_head *bh;
56 struct buffer_head *head;
57 struct buffer_head *ret = NULL;
58
2c90e5d6 59
d98237b3
CM
60 page = find_lock_page(mapping, index);
61 if (!page)
62 return NULL;
63
64 if (!page_has_buffers(page))
65 goto out_unlock;
66
67 head = page_buffers(page);
68 bh = head;
69 do {
7eccb903 70 if (buffer_mapped(bh) && bh_blocknr(bh) == blocknr) {
d98237b3
CM
71 ret = bh;
72 get_bh(bh);
73 goto out_unlock;
74 }
75 bh = bh->b_this_page;
76 } while (bh != head);
77out_unlock:
78 unlock_page(page);
79 page_cache_release(page);
80 return ret;
81}
82
8352d8a4 83int btrfs_map_bh_to_logical(struct btrfs_root *root, struct buffer_head *bh,
7eccb903
CM
84 u64 logical)
85{
236454df
CM
86 if (logical == 0) {
87 bh->b_bdev = NULL;
88 bh->b_blocknr = 0;
89 set_buffer_mapped(bh);
0cf6c620
CM
90 } else {
91 map_bh(bh, root->fs_info->sb, logical);
7eccb903 92 }
0cf6c620 93 return 0;
7eccb903
CM
94}
95
d98237b3
CM
96struct buffer_head *btrfs_find_create_tree_block(struct btrfs_root *root,
97 u64 blocknr)
98{
99 struct address_space *mapping = root->fs_info->btree_inode->i_mapping;
100 int blockbits = root->fs_info->sb->s_blocksize_bits;
101 unsigned long index = blocknr >> (PAGE_CACHE_SHIFT - blockbits);
102 struct page *page;
103 struct buffer_head *bh;
104 struct buffer_head *head;
105 struct buffer_head *ret = NULL;
7eccb903 106 int err;
d98237b3 107 u64 first_block = index << (PAGE_CACHE_SHIFT - blockbits);
22b0ebda 108
34088780 109 page = find_or_create_page(mapping, index, GFP_NOFS);
d98237b3
CM
110 if (!page)
111 return NULL;
112
d98237b3
CM
113 if (!page_has_buffers(page))
114 create_empty_buffers(page, root->fs_info->sb->s_blocksize, 0);
115 head = page_buffers(page);
116 bh = head;
117 do {
118 if (!buffer_mapped(bh)) {
8352d8a4 119 err = btrfs_map_bh_to_logical(root, bh, first_block);
7eccb903 120 BUG_ON(err);
d98237b3 121 }
7eccb903 122 if (bh_blocknr(bh) == blocknr) {
d98237b3
CM
123 ret = bh;
124 get_bh(bh);
125 goto out_unlock;
126 }
127 bh = bh->b_this_page;
128 first_block++;
129 } while (bh != head);
130out_unlock:
131 unlock_page(page);
22b0ebda
CM
132 if (ret)
133 touch_buffer(ret);
d98237b3
CM
134 page_cache_release(page);
135 return ret;
136}
137
d98237b3
CM
138static int btree_get_block(struct inode *inode, sector_t iblock,
139 struct buffer_head *bh, int create)
140{
7eccb903
CM
141 int err;
142 struct btrfs_root *root = BTRFS_I(bh->b_page->mapping->host)->root;
8352d8a4 143 err = btrfs_map_bh_to_logical(root, bh, iblock);
7eccb903 144 return err;
d98237b3
CM
145}
146
f254e52c
CM
147int btrfs_csum_data(struct btrfs_root * root, char *data, size_t len,
148 char *result)
87cbda5c 149{
11bd143f
CM
150 u32 crc;
151 crc = crc32c(0, data, len);
152 memcpy(result, &crc, BTRFS_CRC32_SIZE);
153 return 0;
f254e52c 154}
11bd143f 155
f254e52c
CM
156static int csum_tree_block(struct btrfs_root *root, struct buffer_head *bh,
157 int verify)
158{
509659cd 159 char result[BTRFS_CRC32_SIZE];
f254e52c
CM
160 int ret;
161 struct btrfs_node *node;
162
163 ret = btrfs_csum_data(root, bh->b_data + BTRFS_CSUM_SIZE,
164 bh->b_size - BTRFS_CSUM_SIZE, result);
165 if (ret)
166 return ret;
87cbda5c 167 if (verify) {
509659cd 168 if (memcmp(bh->b_data, result, BTRFS_CRC32_SIZE)) {
5af3981c
CM
169 printk("btrfs: %s checksum verify failed on %llu\n",
170 root->fs_info->sb->s_id,
171 (unsigned long long)bh_blocknr(bh));
f254e52c
CM
172 return 1;
173 }
174 } else {
175 node = btrfs_buffer_node(bh);
509659cd 176 memcpy(node->header.csum, result, BTRFS_CRC32_SIZE);
f254e52c 177 }
87cbda5c
CM
178 return 0;
179}
180
d98237b3 181static int btree_writepage(struct page *page, struct writeback_control *wbc)
ed2ff2cb 182{
87cbda5c 183 struct buffer_head *bh;
0f7d52f4 184 struct btrfs_root *root = BTRFS_I(page->mapping->host)->root;
87cbda5c 185 struct buffer_head *head;
87cbda5c
CM
186 if (!page_has_buffers(page)) {
187 create_empty_buffers(page, root->fs_info->sb->s_blocksize,
188 (1 << BH_Dirty)|(1 << BH_Uptodate));
189 }
190 head = page_buffers(page);
191 bh = head;
192 do {
193 if (buffer_dirty(bh))
194 csum_tree_block(root, bh, 0);
195 bh = bh->b_this_page;
196 } while (bh != head);
d98237b3 197 return block_write_full_page(page, btree_get_block, wbc);
ed2ff2cb
CM
198}
199
d98237b3 200static int btree_readpage(struct file * file, struct page * page)
eb60ceac 201{
d98237b3 202 return block_read_full_page(page, btree_get_block);
eb60ceac
CM
203}
204
d98237b3
CM
205static struct address_space_operations btree_aops = {
206 .readpage = btree_readpage,
207 .writepage = btree_writepage,
208 .sync_page = block_sync_page,
209};
210
090d1875
CM
211int readahead_tree_block(struct btrfs_root *root, u64 blocknr)
212{
213 struct buffer_head *bh = NULL;
de428b63 214 int ret = 0;
090d1875
CM
215
216 bh = btrfs_find_create_tree_block(root, blocknr);
217 if (!bh)
218 return 0;
de428b63
CM
219 if (buffer_uptodate(bh)) {
220 ret = 1;
090d1875 221 goto done;
de428b63
CM
222 }
223 if (test_set_buffer_locked(bh)) {
224 ret = 1;
090d1875 225 goto done;
de428b63 226 }
090d1875
CM
227 if (!buffer_uptodate(bh)) {
228 get_bh(bh);
229 bh->b_end_io = end_buffer_read_sync;
230 submit_bh(READ, bh);
231 } else {
232 unlock_buffer(bh);
de428b63 233 ret = 1;
090d1875
CM
234 }
235done:
236 brelse(bh);
de428b63 237 return ret;
090d1875
CM
238}
239
e20d96d6 240struct buffer_head *read_tree_block(struct btrfs_root *root, u64 blocknr)
eb60ceac 241{
d98237b3 242 struct buffer_head *bh = NULL;
eb60ceac 243
d98237b3
CM
244 bh = btrfs_find_create_tree_block(root, blocknr);
245 if (!bh)
246 return bh;
9d64272c
CM
247 if (buffer_uptodate(bh))
248 goto uptodate;
d98237b3
CM
249 lock_buffer(bh);
250 if (!buffer_uptodate(bh)) {
251 get_bh(bh);
252 bh->b_end_io = end_buffer_read_sync;
253 submit_bh(READ, bh);
254 wait_on_buffer(bh);
255 if (!buffer_uptodate(bh))
256 goto fail;
257 } else {
258 unlock_buffer(bh);
259 }
9d64272c 260uptodate:
090d1875
CM
261 if (!buffer_checked(bh)) {
262 csum_tree_block(root, bh, 1);
263 set_buffer_checked(bh);
264 }
d98237b3 265 if (check_tree_block(root, bh))
39279cc3 266 goto fail;
d98237b3
CM
267 return bh;
268fail:
269 brelse(bh);
270 return NULL;
eb60ceac
CM
271}
272
e089f05c 273int clean_tree_block(struct btrfs_trans_handle *trans, struct btrfs_root *root,
e20d96d6 274 struct buffer_head *buf)
ed2ff2cb 275{
d6025579 276 WARN_ON(atomic_read(&buf->b_count) == 0);
e20d96d6 277 clear_buffer_dirty(buf);
ed2ff2cb
CM
278 return 0;
279}
280
2c90e5d6 281static int __setup_root(int blocksize,
9f5fae2f
CM
282 struct btrfs_root *root,
283 struct btrfs_fs_info *fs_info,
e20d96d6 284 u64 objectid)
d97e63b6 285{
cfaa7295 286 root->node = NULL;
0f7d52f4 287 root->inode = NULL;
a28ec197 288 root->commit_root = NULL;
2c90e5d6 289 root->blocksize = blocksize;
123abc88 290 root->ref_cows = 0;
9f5fae2f 291 root->fs_info = fs_info;
0f7d52f4
CM
292 root->objectid = objectid;
293 root->last_trans = 0;
1b05da2e
CM
294 root->highest_inode = 0;
295 root->last_inode_alloc = 0;
3768f368
CM
296 memset(&root->root_key, 0, sizeof(root->root_key));
297 memset(&root->root_item, 0, sizeof(root->root_item));
4d775673 298 root->root_key.objectid = objectid;
3768f368
CM
299 return 0;
300}
301
2c90e5d6 302static int find_and_setup_root(int blocksize,
9f5fae2f
CM
303 struct btrfs_root *tree_root,
304 struct btrfs_fs_info *fs_info,
305 u64 objectid,
e20d96d6 306 struct btrfs_root *root)
3768f368
CM
307{
308 int ret;
309
2c90e5d6 310 __setup_root(blocksize, root, fs_info, objectid);
3768f368
CM
311 ret = btrfs_find_last_root(tree_root, objectid,
312 &root->root_item, &root->root_key);
313 BUG_ON(ret);
314
315 root->node = read_tree_block(root,
316 btrfs_root_blocknr(&root->root_item));
3768f368 317 BUG_ON(!root->node);
d97e63b6
CM
318 return 0;
319}
320
5eda7b5e
CM
321struct btrfs_root *btrfs_read_fs_root_no_radix(struct btrfs_fs_info *fs_info,
322 struct btrfs_key *location)
0f7d52f4
CM
323{
324 struct btrfs_root *root;
325 struct btrfs_root *tree_root = fs_info->tree_root;
326 struct btrfs_path *path;
327 struct btrfs_leaf *l;
1b05da2e 328 u64 highest_inode;
0f7d52f4
CM
329 int ret = 0;
330
5eda7b5e 331 root = kzalloc(sizeof(*root), GFP_NOFS);
0cf6c620 332 if (!root)
0f7d52f4 333 return ERR_PTR(-ENOMEM);
0f7d52f4
CM
334 if (location->offset == (u64)-1) {
335 ret = find_and_setup_root(fs_info->sb->s_blocksize,
336 fs_info->tree_root, fs_info,
337 location->objectid, root);
338 if (ret) {
0f7d52f4
CM
339 kfree(root);
340 return ERR_PTR(ret);
341 }
342 goto insert;
343 }
344
345 __setup_root(fs_info->sb->s_blocksize, root, fs_info,
346 location->objectid);
347
348 path = btrfs_alloc_path();
349 BUG_ON(!path);
350 ret = btrfs_search_slot(NULL, tree_root, location, path, 0, 0);
351 if (ret != 0) {
0f7d52f4
CM
352 if (ret > 0)
353 ret = -ENOENT;
354 goto out;
355 }
356 l = btrfs_buffer_leaf(path->nodes[0]);
357 memcpy(&root->root_item,
358 btrfs_item_ptr(l, path->slots[0], struct btrfs_root_item),
359 sizeof(root->root_item));
360 memcpy(&root->root_key, location, sizeof(*location));
361 ret = 0;
362out:
363 btrfs_release_path(root, path);
364 btrfs_free_path(path);
365 if (ret) {
366 kfree(root);
367 return ERR_PTR(ret);
368 }
369 root->node = read_tree_block(root,
370 btrfs_root_blocknr(&root->root_item));
371 BUG_ON(!root->node);
372insert:
0f7d52f4 373 root->ref_cows = 1;
5eda7b5e
CM
374 ret = btrfs_find_highest_inode(root, &highest_inode);
375 if (ret == 0) {
376 root->highest_inode = highest_inode;
377 root->last_inode_alloc = highest_inode;
378 }
379 return root;
380}
381
382struct btrfs_root *btrfs_read_fs_root(struct btrfs_fs_info *fs_info,
383 struct btrfs_key *location)
384{
385 struct btrfs_root *root;
386 int ret;
387
388 root = radix_tree_lookup(&fs_info->fs_roots_radix,
389 (unsigned long)location->objectid);
390 if (root)
391 return root;
392
393 root = btrfs_read_fs_root_no_radix(fs_info, location);
394 if (IS_ERR(root))
395 return root;
2619ba1f
CM
396 ret = radix_tree_insert(&fs_info->fs_roots_radix,
397 (unsigned long)root->root_key.objectid,
0f7d52f4
CM
398 root);
399 if (ret) {
0f7d52f4
CM
400 brelse(root->node);
401 kfree(root);
402 return ERR_PTR(ret);
403 }
0f7d52f4
CM
404 return root;
405}
406
2c90e5d6 407struct btrfs_root *open_ctree(struct super_block *sb)
2e635a27 408{
e20d96d6
CM
409 struct btrfs_root *extent_root = kmalloc(sizeof(struct btrfs_root),
410 GFP_NOFS);
411 struct btrfs_root *tree_root = kmalloc(sizeof(struct btrfs_root),
412 GFP_NOFS);
e20d96d6
CM
413 struct btrfs_fs_info *fs_info = kmalloc(sizeof(*fs_info),
414 GFP_NOFS);
eb60ceac 415 int ret;
39279cc3 416 int err = -EIO;
2c90e5d6 417 struct btrfs_super_block *disk_super;
eb60ceac 418
39279cc3
CM
419 if (!extent_root || !tree_root || !fs_info) {
420 err = -ENOMEM;
421 goto fail;
422 }
8ef97622
CM
423 init_bit_radix(&fs_info->pinned_radix);
424 init_bit_radix(&fs_info->pending_del_radix);
e37c9e69 425 init_bit_radix(&fs_info->extent_map_radix);
0f7d52f4 426 INIT_RADIX_TREE(&fs_info->fs_roots_radix, GFP_NOFS);
9078a3e1 427 INIT_RADIX_TREE(&fs_info->block_group_radix, GFP_KERNEL);
be744175 428 INIT_RADIX_TREE(&fs_info->block_group_data_radix, GFP_KERNEL);
8fd17795 429 INIT_LIST_HEAD(&fs_info->trans_list);
facda1e7 430 INIT_LIST_HEAD(&fs_info->dead_roots);
2c90e5d6 431 sb_set_blocksize(sb, 4096);
9f5fae2f 432 fs_info->running_transaction = NULL;
9f5fae2f
CM
433 fs_info->tree_root = tree_root;
434 fs_info->extent_root = extent_root;
e20d96d6 435 fs_info->sb = sb;
d98237b3
CM
436 fs_info->btree_inode = new_inode(sb);
437 fs_info->btree_inode->i_ino = 1;
2c90e5d6 438 fs_info->btree_inode->i_nlink = 1;
d98237b3
CM
439 fs_info->btree_inode->i_size = sb->s_bdev->bd_inode->i_size;
440 fs_info->btree_inode->i_mapping->a_ops = &btree_aops;
e66f709b 441 fs_info->do_barriers = 1;
f2458e1d
CM
442 fs_info->extent_tree_insert_nr = 0;
443 fs_info->extent_tree_prealloc_nr = 0;
facda1e7
CM
444 fs_info->closing = 0;
445
08607c1b 446 INIT_DELAYED_WORK(&fs_info->trans_work, btrfs_transaction_cleaner);
0f7d52f4
CM
447 BTRFS_I(fs_info->btree_inode)->root = tree_root;
448 memset(&BTRFS_I(fs_info->btree_inode)->location, 0,
449 sizeof(struct btrfs_key));
22b0ebda 450 insert_inode_hash(fs_info->btree_inode);
d98237b3 451 mapping_set_gfp_mask(fs_info->btree_inode->i_mapping, GFP_NOFS);
39279cc3 452
79154b1b 453 mutex_init(&fs_info->trans_mutex);
d561c025 454 mutex_init(&fs_info->fs_mutex);
3768f368 455
2c90e5d6
CM
456 __setup_root(sb->s_blocksize, tree_root,
457 fs_info, BTRFS_ROOT_TREE_OBJECTID);
7eccb903 458
2c90e5d6
CM
459 fs_info->sb_buffer = read_tree_block(tree_root,
460 BTRFS_SUPER_INFO_OFFSET /
461 sb->s_blocksize);
d98237b3 462
0f7d52f4 463 if (!fs_info->sb_buffer)
39279cc3 464 goto fail_iput;
d98237b3 465 disk_super = (struct btrfs_super_block *)fs_info->sb_buffer->b_data;
4b52dff6
CM
466 fs_info->disk_super = disk_super;
467 memcpy(&fs_info->super_copy, disk_super, sizeof(fs_info->super_copy));
39279cc3 468
0f7d52f4 469 if (!btrfs_super_root(disk_super))
39279cc3 470 goto fail_sb_buffer;
0f7d52f4 471
8352d8a4
CM
472 i_size_write(fs_info->btree_inode,
473 btrfs_super_total_blocks(disk_super) <<
474 fs_info->btree_inode->i_blkbits);
475
39279cc3
CM
476
477 if (strncmp((char *)(&disk_super->magic), BTRFS_MAGIC,
478 sizeof(disk_super->magic))) {
479 printk("btrfs: valid FS not found on %s\n", sb->s_id);
480 goto fail_sb_buffer;
481 }
e20d96d6
CM
482 tree_root->node = read_tree_block(tree_root,
483 btrfs_super_root(disk_super));
39279cc3
CM
484 if (!tree_root->node)
485 goto fail_sb_buffer;
3768f368 486
2c90e5d6
CM
487 mutex_lock(&fs_info->fs_mutex);
488 ret = find_and_setup_root(sb->s_blocksize, tree_root, fs_info,
e20d96d6 489 BTRFS_EXTENT_TREE_OBJECTID, extent_root);
39279cc3
CM
490 if (ret) {
491 mutex_unlock(&fs_info->fs_mutex);
492 goto fail_tree_root;
493 }
3768f368 494
9078a3e1
CM
495 btrfs_read_block_groups(extent_root);
496
0f7d52f4 497 fs_info->generation = btrfs_super_generation(disk_super) + 1;
5eda7b5e
CM
498 ret = btrfs_find_dead_roots(tree_root);
499 if (ret)
500 goto fail_tree_root;
5be6f7f1 501 mutex_unlock(&fs_info->fs_mutex);
0f7d52f4 502 return tree_root;
39279cc3
CM
503
504fail_tree_root:
505 btrfs_block_release(tree_root, tree_root->node);
506fail_sb_buffer:
507 btrfs_block_release(tree_root, fs_info->sb_buffer);
508fail_iput:
509 iput(fs_info->btree_inode);
510fail:
511 kfree(extent_root);
512 kfree(tree_root);
513 kfree(fs_info);
514 return ERR_PTR(err);
eb60ceac
CM
515}
516
e089f05c 517int write_ctree_super(struct btrfs_trans_handle *trans, struct btrfs_root
79154b1b 518 *root)
eb60ceac 519{
e66f709b 520 int ret;
d5719762 521 struct buffer_head *bh = root->fs_info->sb_buffer;
2c90e5d6 522
d5719762 523 lock_buffer(bh);
2c90e5d6 524 WARN_ON(atomic_read(&bh->b_count) < 1);
d5719762 525 clear_buffer_dirty(bh);
87cbda5c 526 csum_tree_block(root, bh, 0);
d5719762
CM
527 bh->b_end_io = end_buffer_write_sync;
528 get_bh(bh);
e66f709b
CM
529 if (root->fs_info->do_barriers)
530 ret = submit_bh(WRITE_BARRIER, bh);
531 else
532 ret = submit_bh(WRITE, bh);
533 if (ret == -EOPNOTSUPP) {
8c2383c3
CM
534 get_bh(bh);
535 lock_buffer(bh);
e66f709b
CM
536 set_buffer_uptodate(bh);
537 root->fs_info->do_barriers = 0;
538 ret = submit_bh(WRITE, bh);
539 }
d5719762
CM
540 wait_on_buffer(bh);
541 if (!buffer_uptodate(bh)) {
542 WARN_ON(1);
543 return -EIO;
cfaa7295
CM
544 }
545 return 0;
546}
547
5eda7b5e 548int btrfs_free_fs_root(struct btrfs_fs_info *fs_info, struct btrfs_root *root)
2619ba1f
CM
549{
550 radix_tree_delete(&fs_info->fs_roots_radix,
551 (unsigned long)root->root_key.objectid);
552 if (root->inode)
553 iput(root->inode);
554 if (root->node)
555 brelse(root->node);
556 if (root->commit_root)
557 brelse(root->commit_root);
558 kfree(root);
559 return 0;
560}
561
35b7e476 562static int del_fs_roots(struct btrfs_fs_info *fs_info)
0f7d52f4
CM
563{
564 int ret;
565 struct btrfs_root *gang[8];
566 int i;
567
568 while(1) {
569 ret = radix_tree_gang_lookup(&fs_info->fs_roots_radix,
570 (void **)gang, 0,
571 ARRAY_SIZE(gang));
572 if (!ret)
573 break;
2619ba1f 574 for (i = 0; i < ret; i++)
5eda7b5e 575 btrfs_free_fs_root(fs_info, gang[i]);
0f7d52f4
CM
576 }
577 return 0;
578}
b4100d64 579
e20d96d6 580int close_ctree(struct btrfs_root *root)
cfaa7295 581{
3768f368 582 int ret;
e089f05c 583 struct btrfs_trans_handle *trans;
0f7d52f4 584 struct btrfs_fs_info *fs_info = root->fs_info;
e089f05c 585
facda1e7 586 fs_info->closing = 1;
08607c1b 587 btrfs_transaction_flush_work(root);
0f7d52f4 588 mutex_lock(&fs_info->fs_mutex);
79154b1b 589 trans = btrfs_start_transaction(root, 1);
54aa1f4d 590 ret = btrfs_commit_transaction(trans, root);
79154b1b
CM
591 /* run commit again to drop the original snapshot */
592 trans = btrfs_start_transaction(root, 1);
593 btrfs_commit_transaction(trans, root);
594 ret = btrfs_write_and_wait_transaction(NULL, root);
3768f368 595 BUG_ON(ret);
79154b1b 596 write_ctree_super(NULL, root);
0f7d52f4
CM
597 mutex_unlock(&fs_info->fs_mutex);
598
599 if (fs_info->extent_root->node)
600 btrfs_block_release(fs_info->extent_root,
601 fs_info->extent_root->node);
0f7d52f4
CM
602 if (fs_info->tree_root->node)
603 btrfs_block_release(fs_info->tree_root,
604 fs_info->tree_root->node);
605 btrfs_block_release(root, fs_info->sb_buffer);
0f7d52f4
CM
606 truncate_inode_pages(fs_info->btree_inode->i_mapping, 0);
607 iput(fs_info->btree_inode);
7eccb903 608
9078a3e1 609 btrfs_free_block_groups(root->fs_info);
0f7d52f4
CM
610 del_fs_roots(fs_info);
611 kfree(fs_info->extent_root);
0f7d52f4 612 kfree(fs_info->tree_root);
eb60ceac
CM
613 return 0;
614}
615
ccd467d6
CM
616void btrfs_mark_buffer_dirty(struct buffer_head *bh)
617{
618 struct btrfs_root *root = BTRFS_I(bh->b_page->mapping->host)->root;
619 u64 transid = btrfs_header_generation(btrfs_buffer_header(bh));
620 WARN_ON(!atomic_read(&bh->b_count));
621 if (transid != root->fs_info->generation) {
622 printk(KERN_CRIT "transid mismatch buffer %llu, found %Lu running %Lu\n",
623 (unsigned long long)bh->b_blocknr,
624 transid, root->fs_info->generation);
625 WARN_ON(1);
626 }
627 mark_buffer_dirty(bh);
628}
629
e20d96d6 630void btrfs_block_release(struct btrfs_root *root, struct buffer_head *buf)
eb60ceac 631{
7cfcc17e 632 brelse(buf);
eb60ceac
CM
633}
634
35b7e476
CM
635void btrfs_btree_balance_dirty(struct btrfs_root *root)
636{
637 balance_dirty_pages_ratelimited(root->fs_info->btree_inode->i_mapping);
638}