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