]> bbs.cooldavid.org Git - net-next-2.6.git/blame - fs/btrfs/disk-io.c
Btrfs: Use an array of pages in the extent buffers to reduce the cost of find_get_page
[net-next-2.6.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>
5f39d397 26#include <linux/buffer_head.h> // for block_sync_page
eb60ceac
CM
27#include "ctree.h"
28#include "disk-io.h"
e089f05c 29#include "transaction.h"
0f7d52f4 30#include "btrfs_inode.h"
db94535d 31#include "print-tree.h"
eb60ceac 32
5f39d397
CM
33#if 0
34static int check_tree_block(struct btrfs_root *root, struct extent_buffer *buf)
7eccb903 35{
5f39d397
CM
36 if (extent_buffer_blocknr(buf) != btrfs_header_blocknr(buf)) {
37 printk(KERN_CRIT "buf blocknr(buf) is %llu, header is %llu\n",
38 (unsigned long long)extent_buffer_blocknr(buf),
39 (unsigned long long)btrfs_header_blocknr(buf));
39279cc3 40 return 1;
d98237b3 41 }
9a8dd150 42 return 0;
eb60ceac 43}
5f39d397 44#endif
eb60ceac 45
5f39d397 46struct extent_buffer *btrfs_find_tree_block(struct btrfs_root *root,
db94535d 47 u64 bytenr, u32 blocksize)
d98237b3 48{
5f39d397 49 struct inode *btree_inode = root->fs_info->btree_inode;
f510cfec
CM
50 struct extent_buffer *eb;
51 eb = find_extent_buffer(&BTRFS_I(btree_inode)->extent_tree,
db94535d 52 bytenr, blocksize, GFP_NOFS);
f510cfec
CM
53 if (eb)
54 eb->alloc_addr = (unsigned long)__builtin_return_address(0);
55 return eb;
5f39d397 56}
d98237b3 57
5f39d397 58struct extent_buffer *btrfs_find_create_tree_block(struct btrfs_root *root,
db94535d 59 u64 bytenr, u32 blocksize)
5f39d397
CM
60{
61 struct inode *btree_inode = root->fs_info->btree_inode;
f510cfec 62 struct extent_buffer *eb;
db94535d 63
f510cfec 64 eb = alloc_extent_buffer(&BTRFS_I(btree_inode)->extent_tree,
db94535d 65 bytenr, blocksize, GFP_NOFS);
f510cfec
CM
66 eb->alloc_addr = (unsigned long)__builtin_return_address(0);
67 return eb;
d98237b3
CM
68}
69
5f39d397
CM
70struct extent_map *btree_get_extent(struct inode *inode, struct page *page,
71 size_t page_offset, u64 start, u64 end,
72 int create)
7eccb903 73{
5f39d397
CM
74 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
75 struct extent_map *em;
76 int ret;
77
78again:
79 em = lookup_extent_mapping(em_tree, start, end);
80 if (em) {
81 goto out;
7eccb903 82 }
5f39d397
CM
83 em = alloc_extent_map(GFP_NOFS);
84 if (!em) {
85 em = ERR_PTR(-ENOMEM);
86 goto out;
87 }
88 em->start = 0;
89 em->end = (i_size_read(inode) & ~((u64)PAGE_CACHE_SIZE -1)) - 1;
90 em->block_start = 0;
91 em->block_end = em->end;
92 em->bdev = inode->i_sb->s_bdev;
93 ret = add_extent_mapping(em_tree, em);
94 if (ret == -EEXIST) {
95 free_extent_map(em);
96 em = NULL;
97 goto again;
98 } else if (ret) {
99 em = ERR_PTR(ret);
100 }
101out:
102 return em;
7eccb903
CM
103}
104
5f39d397 105static int btree_writepage(struct page *page, struct writeback_control *wbc)
d98237b3 106{
5f39d397
CM
107 struct extent_map_tree *tree;
108 tree = &BTRFS_I(page->mapping->host)->extent_tree;
109 return extent_write_full_page(tree, page, btree_get_extent, wbc);
110}
111int btree_readpage(struct file *file, struct page *page)
112{
113 struct extent_map_tree *tree;
114 tree = &BTRFS_I(page->mapping->host)->extent_tree;
115 return extent_read_full_page(tree, page, btree_get_extent);
116}
22b0ebda 117
5f39d397
CM
118static int btree_releasepage(struct page *page, gfp_t unused_gfp_flags)
119{
120 struct extent_map_tree *tree;
121 int ret;
d98237b3 122
5f39d397
CM
123 BUG_ON(page->private != 1);
124 tree = &BTRFS_I(page->mapping->host)->extent_tree;
125 ret = try_release_extent_mapping(tree, page);
126 if (ret == 1) {
127 ClearPagePrivate(page);
128 set_page_private(page, 0);
129 page_cache_release(page);
130 }
d98237b3
CM
131 return ret;
132}
133
5f39d397 134static void btree_invalidatepage(struct page *page, unsigned long offset)
d98237b3 135{
5f39d397
CM
136 struct extent_map_tree *tree;
137 tree = &BTRFS_I(page->mapping->host)->extent_tree;
138 extent_invalidatepage(tree, page, offset);
139 btree_releasepage(page, GFP_NOFS);
d98237b3
CM
140}
141
f254e52c
CM
142int btrfs_csum_data(struct btrfs_root * root, char *data, size_t len,
143 char *result)
87cbda5c 144{
5f39d397
CM
145 return 0;
146#if 0
11bd143f
CM
147 u32 crc;
148 crc = crc32c(0, data, len);
149 memcpy(result, &crc, BTRFS_CRC32_SIZE);
150 return 0;
5f39d397 151#endif
f254e52c 152}
11bd143f 153
5f39d397
CM
154#if 0
155static int csum_tree_block(struct btrfs_root *root, struct extent_buffer *buf,
f254e52c
CM
156 int verify)
157{
5f39d397 158 return 0;
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}
5f39d397 180#endif
87cbda5c 181
5f39d397 182#if 0
d98237b3 183static int btree_writepage(struct page *page, struct writeback_control *wbc)
ed2ff2cb 184{
87cbda5c 185 struct buffer_head *bh;
0f7d52f4 186 struct btrfs_root *root = BTRFS_I(page->mapping->host)->root;
87cbda5c 187 struct buffer_head *head;
87cbda5c
CM
188 if (!page_has_buffers(page)) {
189 create_empty_buffers(page, root->fs_info->sb->s_blocksize,
190 (1 << BH_Dirty)|(1 << BH_Uptodate));
191 }
192 head = page_buffers(page);
193 bh = head;
194 do {
195 if (buffer_dirty(bh))
196 csum_tree_block(root, bh, 0);
197 bh = bh->b_this_page;
198 } while (bh != head);
d98237b3 199 return block_write_full_page(page, btree_get_block, wbc);
ed2ff2cb 200}
5f39d397 201#endif
eb60ceac 202
d98237b3
CM
203static struct address_space_operations btree_aops = {
204 .readpage = btree_readpage,
205 .writepage = btree_writepage,
5f39d397
CM
206 .releasepage = btree_releasepage,
207 .invalidatepage = btree_invalidatepage,
d98237b3
CM
208 .sync_page = block_sync_page,
209};
210
db94535d 211int readahead_tree_block(struct btrfs_root *root, u64 bytenr, u32 blocksize)
090d1875 212{
5f39d397
CM
213 struct extent_buffer *buf = NULL;
214 struct inode *btree_inode = root->fs_info->btree_inode;
de428b63 215 int ret = 0;
090d1875 216
db94535d 217 buf = btrfs_find_create_tree_block(root, bytenr, blocksize);
5f39d397 218 if (!buf)
090d1875 219 return 0;
5f39d397
CM
220 read_extent_buffer_pages(&BTRFS_I(btree_inode)->extent_tree,
221 buf, 0);
222 free_extent_buffer(buf);
de428b63 223 return ret;
090d1875
CM
224}
225
db94535d
CM
226struct extent_buffer *read_tree_block(struct btrfs_root *root, u64 bytenr,
227 u32 blocksize)
eb60ceac 228{
5f39d397
CM
229 struct extent_buffer *buf = NULL;
230 struct inode *btree_inode = root->fs_info->btree_inode;
231
db94535d 232 buf = btrfs_find_create_tree_block(root, bytenr, blocksize);
5f39d397
CM
233 if (!buf)
234 return NULL;
235 read_extent_buffer_pages(&BTRFS_I(btree_inode)->extent_tree,
236 buf, 1);
f510cfec 237 buf->alloc_addr = (unsigned long)__builtin_return_address(0);
5f39d397 238 return buf;
eb60ceac
CM
239}
240
e089f05c 241int clean_tree_block(struct btrfs_trans_handle *trans, struct btrfs_root *root,
5f39d397 242 struct extent_buffer *buf)
ed2ff2cb 243{
5f39d397
CM
244 struct inode *btree_inode = root->fs_info->btree_inode;
245 clear_extent_buffer_dirty(&BTRFS_I(btree_inode)->extent_tree, buf);
246 return 0;
247}
248
249int wait_on_tree_block_writeback(struct btrfs_root *root,
250 struct extent_buffer *buf)
251{
252 struct inode *btree_inode = root->fs_info->btree_inode;
253 wait_on_extent_buffer_writeback(&BTRFS_I(btree_inode)->extent_tree,
254 buf);
255 return 0;
256}
257
258int set_tree_block_dirty(struct btrfs_root *root, struct extent_buffer *buf)
259{
260 struct inode *btree_inode = root->fs_info->btree_inode;
261 set_extent_buffer_dirty(&BTRFS_I(btree_inode)->extent_tree, buf);
ed2ff2cb
CM
262 return 0;
263}
264
db94535d 265static int __setup_root(u32 nodesize, u32 leafsize, u32 sectorsize,
9f5fae2f
CM
266 struct btrfs_root *root,
267 struct btrfs_fs_info *fs_info,
e20d96d6 268 u64 objectid)
d97e63b6 269{
cfaa7295 270 root->node = NULL;
0f7d52f4 271 root->inode = NULL;
a28ec197 272 root->commit_root = NULL;
db94535d
CM
273 root->sectorsize = sectorsize;
274 root->nodesize = nodesize;
275 root->leafsize = leafsize;
123abc88 276 root->ref_cows = 0;
9f5fae2f 277 root->fs_info = fs_info;
0f7d52f4
CM
278 root->objectid = objectid;
279 root->last_trans = 0;
1b05da2e
CM
280 root->highest_inode = 0;
281 root->last_inode_alloc = 0;
58176a96 282 root->name = NULL;
3768f368
CM
283 memset(&root->root_key, 0, sizeof(root->root_key));
284 memset(&root->root_item, 0, sizeof(root->root_item));
6702ed49 285 memset(&root->defrag_progress, 0, sizeof(root->defrag_progress));
58176a96
JB
286 memset(&root->root_kobj, 0, sizeof(root->root_kobj));
287 init_completion(&root->kobj_unregister);
011410bd 288 init_rwsem(&root->snap_sem);
6702ed49
CM
289 root->defrag_running = 0;
290 root->defrag_level = 0;
4d775673 291 root->root_key.objectid = objectid;
3768f368
CM
292 return 0;
293}
294
db94535d 295static int find_and_setup_root(struct btrfs_root *tree_root,
9f5fae2f
CM
296 struct btrfs_fs_info *fs_info,
297 u64 objectid,
e20d96d6 298 struct btrfs_root *root)
3768f368
CM
299{
300 int ret;
db94535d 301 u32 blocksize;
3768f368 302
db94535d
CM
303 __setup_root(tree_root->nodesize, tree_root->leafsize,
304 tree_root->sectorsize, 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
db94535d
CM
309 blocksize = btrfs_level_size(root, btrfs_root_level(&root->root_item));
310 root->node = read_tree_block(root, btrfs_root_bytenr(&root->root_item),
311 blocksize);
3768f368 312 BUG_ON(!root->node);
d97e63b6
CM
313 return 0;
314}
315
5eda7b5e
CM
316struct btrfs_root *btrfs_read_fs_root_no_radix(struct btrfs_fs_info *fs_info,
317 struct btrfs_key *location)
0f7d52f4
CM
318{
319 struct btrfs_root *root;
320 struct btrfs_root *tree_root = fs_info->tree_root;
321 struct btrfs_path *path;
5f39d397 322 struct extent_buffer *l;
1b05da2e 323 u64 highest_inode;
db94535d 324 u32 blocksize;
0f7d52f4
CM
325 int ret = 0;
326
5eda7b5e 327 root = kzalloc(sizeof(*root), GFP_NOFS);
0cf6c620 328 if (!root)
0f7d52f4 329 return ERR_PTR(-ENOMEM);
0f7d52f4 330 if (location->offset == (u64)-1) {
db94535d 331 ret = find_and_setup_root(tree_root, fs_info,
0f7d52f4
CM
332 location->objectid, root);
333 if (ret) {
0f7d52f4
CM
334 kfree(root);
335 return ERR_PTR(ret);
336 }
337 goto insert;
338 }
339
db94535d
CM
340 __setup_root(tree_root->nodesize, tree_root->leafsize,
341 tree_root->sectorsize, root, fs_info,
0f7d52f4
CM
342 location->objectid);
343
344 path = btrfs_alloc_path();
345 BUG_ON(!path);
346 ret = btrfs_search_slot(NULL, tree_root, location, path, 0, 0);
347 if (ret != 0) {
0f7d52f4
CM
348 if (ret > 0)
349 ret = -ENOENT;
350 goto out;
351 }
5f39d397
CM
352 l = path->nodes[0];
353 read_extent_buffer(l, &root->root_item,
354 btrfs_item_ptr_offset(l, path->slots[0]),
0f7d52f4 355 sizeof(root->root_item));
0f7d52f4
CM
356 ret = 0;
357out:
358 btrfs_release_path(root, path);
359 btrfs_free_path(path);
360 if (ret) {
361 kfree(root);
362 return ERR_PTR(ret);
363 }
db94535d
CM
364 blocksize = btrfs_level_size(root, btrfs_root_level(&root->root_item));
365 root->node = read_tree_block(root, btrfs_root_bytenr(&root->root_item),
366 blocksize);
0f7d52f4
CM
367 BUG_ON(!root->node);
368insert:
0f7d52f4 369 root->ref_cows = 1;
5eda7b5e
CM
370 ret = btrfs_find_highest_inode(root, &highest_inode);
371 if (ret == 0) {
372 root->highest_inode = highest_inode;
373 root->last_inode_alloc = highest_inode;
374 }
375 return root;
376}
377
378struct btrfs_root *btrfs_read_fs_root(struct btrfs_fs_info *fs_info,
58176a96
JB
379 struct btrfs_key *location,
380 const char *name, int namelen)
5eda7b5e
CM
381{
382 struct btrfs_root *root;
383 int ret;
384
385 root = radix_tree_lookup(&fs_info->fs_roots_radix,
386 (unsigned long)location->objectid);
387 if (root)
388 return root;
389
390 root = btrfs_read_fs_root_no_radix(fs_info, location);
391 if (IS_ERR(root))
392 return root;
2619ba1f
CM
393 ret = radix_tree_insert(&fs_info->fs_roots_radix,
394 (unsigned long)root->root_key.objectid,
0f7d52f4
CM
395 root);
396 if (ret) {
5f39d397 397 free_extent_buffer(root->node);
0f7d52f4
CM
398 kfree(root);
399 return ERR_PTR(ret);
400 }
58176a96
JB
401
402 ret = btrfs_set_root_name(root, name, namelen);
403 if (ret) {
5f39d397 404 free_extent_buffer(root->node);
58176a96
JB
405 kfree(root);
406 return ERR_PTR(ret);
407 }
408
409 ret = btrfs_sysfs_add_root(root);
410 if (ret) {
5f39d397 411 free_extent_buffer(root->node);
58176a96
JB
412 kfree(root->name);
413 kfree(root);
414 return ERR_PTR(ret);
415 }
416
5ce14bbc
CM
417 ret = btrfs_find_dead_roots(fs_info->tree_root,
418 root->root_key.objectid, root);
419 BUG_ON(ret);
420
0f7d52f4
CM
421 return root;
422}
423
2c90e5d6 424struct btrfs_root *open_ctree(struct super_block *sb)
2e635a27 425{
db94535d
CM
426 u32 sectorsize;
427 u32 nodesize;
428 u32 leafsize;
429 u32 blocksize;
e20d96d6
CM
430 struct btrfs_root *extent_root = kmalloc(sizeof(struct btrfs_root),
431 GFP_NOFS);
432 struct btrfs_root *tree_root = kmalloc(sizeof(struct btrfs_root),
433 GFP_NOFS);
e20d96d6
CM
434 struct btrfs_fs_info *fs_info = kmalloc(sizeof(*fs_info),
435 GFP_NOFS);
eb60ceac 436 int ret;
39279cc3 437 int err = -EIO;
2c90e5d6 438 struct btrfs_super_block *disk_super;
eb60ceac 439
39279cc3
CM
440 if (!extent_root || !tree_root || !fs_info) {
441 err = -ENOMEM;
442 goto fail;
443 }
0f7d52f4 444 INIT_RADIX_TREE(&fs_info->fs_roots_radix, GFP_NOFS);
8fd17795 445 INIT_LIST_HEAD(&fs_info->trans_list);
facda1e7 446 INIT_LIST_HEAD(&fs_info->dead_roots);
58176a96
JB
447 memset(&fs_info->super_kobj, 0, sizeof(fs_info->super_kobj));
448 init_completion(&fs_info->kobj_unregister);
2c90e5d6 449 sb_set_blocksize(sb, 4096);
9f5fae2f 450 fs_info->running_transaction = NULL;
15ee9bc7 451 fs_info->last_trans_committed = 0;
9f5fae2f
CM
452 fs_info->tree_root = tree_root;
453 fs_info->extent_root = extent_root;
e20d96d6 454 fs_info->sb = sb;
d98237b3
CM
455 fs_info->btree_inode = new_inode(sb);
456 fs_info->btree_inode->i_ino = 1;
2c90e5d6 457 fs_info->btree_inode->i_nlink = 1;
d98237b3
CM
458 fs_info->btree_inode->i_size = sb->s_bdev->bd_inode->i_size;
459 fs_info->btree_inode->i_mapping->a_ops = &btree_aops;
5f39d397
CM
460 extent_map_tree_init(&BTRFS_I(fs_info->btree_inode)->extent_tree,
461 fs_info->btree_inode->i_mapping,
462 GFP_NOFS);
f510cfec
CM
463 extent_map_tree_init(&fs_info->free_space_cache,
464 fs_info->btree_inode->i_mapping, GFP_NOFS);
96b5179d
CM
465 extent_map_tree_init(&fs_info->block_group_cache,
466 fs_info->btree_inode->i_mapping, GFP_NOFS);
1a5bc167
CM
467 extent_map_tree_init(&fs_info->pinned_extents,
468 fs_info->btree_inode->i_mapping, GFP_NOFS);
469 extent_map_tree_init(&fs_info->pending_del,
470 fs_info->btree_inode->i_mapping, GFP_NOFS);
471 extent_map_tree_init(&fs_info->extent_ins,
472 fs_info->btree_inode->i_mapping, GFP_NOFS);
e66f709b 473 fs_info->do_barriers = 1;
facda1e7
CM
474 fs_info->closing = 0;
475
08607c1b 476 INIT_DELAYED_WORK(&fs_info->trans_work, btrfs_transaction_cleaner);
0f7d52f4
CM
477 BTRFS_I(fs_info->btree_inode)->root = tree_root;
478 memset(&BTRFS_I(fs_info->btree_inode)->location, 0,
479 sizeof(struct btrfs_key));
22b0ebda 480 insert_inode_hash(fs_info->btree_inode);
d98237b3 481 mapping_set_gfp_mask(fs_info->btree_inode->i_mapping, GFP_NOFS);
39279cc3 482
79154b1b 483 mutex_init(&fs_info->trans_mutex);
d561c025 484 mutex_init(&fs_info->fs_mutex);
3768f368 485
db94535d 486 __setup_root(512, 512, 512, tree_root,
2c90e5d6 487 fs_info, BTRFS_ROOT_TREE_OBJECTID);
7eccb903 488
2c90e5d6 489 fs_info->sb_buffer = read_tree_block(tree_root,
db94535d
CM
490 BTRFS_SUPER_INFO_OFFSET,
491 512);
d98237b3 492
0f7d52f4 493 if (!fs_info->sb_buffer)
39279cc3 494 goto fail_iput;
39279cc3 495
5f39d397
CM
496 read_extent_buffer(fs_info->sb_buffer, &fs_info->super_copy, 0,
497 sizeof(fs_info->super_copy));
498
499 read_extent_buffer(fs_info->sb_buffer, fs_info->fsid,
500 (unsigned long)btrfs_super_fsid(fs_info->sb_buffer),
501 BTRFS_FSID_SIZE);
502 disk_super = &fs_info->super_copy;
0f7d52f4 503 if (!btrfs_super_root(disk_super))
39279cc3 504 goto fail_sb_buffer;
0f7d52f4 505
db94535d
CM
506 nodesize = btrfs_super_nodesize(disk_super);
507 leafsize = btrfs_super_leafsize(disk_super);
508 sectorsize = btrfs_super_sectorsize(disk_super);
509 tree_root->nodesize = nodesize;
510 tree_root->leafsize = leafsize;
511 tree_root->sectorsize = sectorsize;
512
8352d8a4 513 i_size_write(fs_info->btree_inode,
db94535d 514 btrfs_super_total_bytes(disk_super));
8352d8a4 515
39279cc3
CM
516
517 if (strncmp((char *)(&disk_super->magic), BTRFS_MAGIC,
518 sizeof(disk_super->magic))) {
519 printk("btrfs: valid FS not found on %s\n", sb->s_id);
520 goto fail_sb_buffer;
521 }
db94535d
CM
522 blocksize = btrfs_level_size(tree_root,
523 btrfs_super_root_level(disk_super));
e20d96d6 524 tree_root->node = read_tree_block(tree_root,
db94535d
CM
525 btrfs_super_root(disk_super),
526 blocksize);
39279cc3
CM
527 if (!tree_root->node)
528 goto fail_sb_buffer;
3768f368 529
db94535d
CM
530#if 0
531 btrfs_print_leaf(tree_root, tree_root->node);
532 err = -EIO;
533 goto fail_tree_root;
534#endif
2c90e5d6 535 mutex_lock(&fs_info->fs_mutex);
db94535d
CM
536
537 ret = find_and_setup_root(tree_root, fs_info,
e20d96d6 538 BTRFS_EXTENT_TREE_OBJECTID, extent_root);
39279cc3
CM
539 if (ret) {
540 mutex_unlock(&fs_info->fs_mutex);
541 goto fail_tree_root;
542 }
3768f368 543
9078a3e1
CM
544 btrfs_read_block_groups(extent_root);
545
0f7d52f4 546 fs_info->generation = btrfs_super_generation(disk_super) + 1;
5be6f7f1 547 mutex_unlock(&fs_info->fs_mutex);
0f7d52f4 548 return tree_root;
39279cc3
CM
549
550fail_tree_root:
5f39d397 551 free_extent_buffer(tree_root->node);
39279cc3 552fail_sb_buffer:
5f39d397 553 free_extent_buffer(fs_info->sb_buffer);
39279cc3
CM
554fail_iput:
555 iput(fs_info->btree_inode);
556fail:
557 kfree(extent_root);
558 kfree(tree_root);
559 kfree(fs_info);
560 return ERR_PTR(err);
eb60ceac
CM
561}
562
e089f05c 563int write_ctree_super(struct btrfs_trans_handle *trans, struct btrfs_root
79154b1b 564 *root)
eb60ceac 565{
e66f709b 566 int ret;
5f39d397
CM
567 struct extent_buffer *super = root->fs_info->sb_buffer;
568 struct inode *btree_inode = root->fs_info->btree_inode;
569
570 set_extent_buffer_dirty(&BTRFS_I(btree_inode)->extent_tree, super);
571 ret = sync_page_range_nolock(btree_inode, btree_inode->i_mapping,
572 super->start, super->len);
573 return ret;
cfaa7295
CM
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)
5f39d397 584 free_extent_buffer(root->node);
2619ba1f 585 if (root->commit_root)
5f39d397 586 free_extent_buffer(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)
5f39d397 632 free_extent_buffer(fs_info->extent_root->node);
f510cfec 633
0f7d52f4 634 if (fs_info->tree_root->node)
5f39d397 635 free_extent_buffer(fs_info->tree_root->node);
f510cfec 636
5f39d397 637 free_extent_buffer(fs_info->sb_buffer);
7eccb903 638
9078a3e1 639 btrfs_free_block_groups(root->fs_info);
0f7d52f4 640 del_fs_roots(fs_info);
db94535d
CM
641 truncate_inode_pages(fs_info->btree_inode->i_mapping, 0);
642 iput(fs_info->btree_inode);
0f7d52f4 643 kfree(fs_info->extent_root);
0f7d52f4 644 kfree(fs_info->tree_root);
eb60ceac
CM
645 return 0;
646}
647
5f39d397
CM
648int btrfs_buffer_uptodate(struct extent_buffer *buf)
649{
09e71a32 650 struct inode *btree_inode = buf->pages[0]->mapping->host;
5f39d397
CM
651 return extent_buffer_uptodate(&BTRFS_I(btree_inode)->extent_tree, buf);
652}
653
654int btrfs_set_buffer_uptodate(struct extent_buffer *buf)
ccd467d6 655{
09e71a32 656 struct inode *btree_inode = buf->pages[0]->mapping->host;
5f39d397
CM
657 return set_extent_buffer_uptodate(&BTRFS_I(btree_inode)->extent_tree,
658 buf);
659}
6702ed49 660
5f39d397
CM
661void btrfs_mark_buffer_dirty(struct extent_buffer *buf)
662{
09e71a32 663 struct btrfs_root *root = BTRFS_I(buf->pages[0]->mapping->host)->root;
5f39d397
CM
664 u64 transid = btrfs_header_generation(buf);
665 struct inode *btree_inode = root->fs_info->btree_inode;
6702ed49 666
ccd467d6
CM
667 if (transid != root->fs_info->generation) {
668 printk(KERN_CRIT "transid mismatch buffer %llu, found %Lu running %Lu\n",
db94535d 669 (unsigned long long)buf->start,
ccd467d6
CM
670 transid, root->fs_info->generation);
671 WARN_ON(1);
672 }
5f39d397 673 set_extent_buffer_dirty(&BTRFS_I(btree_inode)->extent_tree, buf);
eb60ceac
CM
674}
675
d3c2fdcf 676void btrfs_btree_balance_dirty(struct btrfs_root *root, unsigned long nr)
35b7e476 677{
d3c2fdcf
CM
678 balance_dirty_pages_ratelimited_nr(
679 root->fs_info->btree_inode->i_mapping, nr);
35b7e476 680}