btrfs_drop_extents: make sure the item is getting smaller before truncate
[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>
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
0da5468f
CM
46static struct extent_map_ops btree_extent_map_ops;
47
5f39d397 48struct extent_buffer *btrfs_find_tree_block(struct btrfs_root *root,
db94535d 49 u64 bytenr, u32 blocksize)
d98237b3 50{
5f39d397 51 struct inode *btree_inode = root->fs_info->btree_inode;
f510cfec
CM
52 struct extent_buffer *eb;
53 eb = find_extent_buffer(&BTRFS_I(btree_inode)->extent_tree,
db94535d 54 bytenr, blocksize, GFP_NOFS);
f510cfec 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,
19c00ddc 65 bytenr, blocksize, NULL, GFP_NOFS);
f510cfec 66 return eb;
d98237b3
CM
67}
68
5f39d397
CM
69struct extent_map *btree_get_extent(struct inode *inode, struct page *page,
70 size_t page_offset, u64 start, u64 end,
71 int create)
7eccb903 72{
5f39d397
CM
73 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
74 struct extent_map *em;
75 int ret;
76
77again:
78 em = lookup_extent_mapping(em_tree, start, end);
79 if (em) {
80 goto out;
7eccb903 81 }
5f39d397
CM
82 em = alloc_extent_map(GFP_NOFS);
83 if (!em) {
84 em = ERR_PTR(-ENOMEM);
85 goto out;
86 }
87 em->start = 0;
88 em->end = (i_size_read(inode) & ~((u64)PAGE_CACHE_SIZE -1)) - 1;
89 em->block_start = 0;
90 em->block_end = em->end;
91 em->bdev = inode->i_sb->s_bdev;
92 ret = add_extent_mapping(em_tree, em);
93 if (ret == -EEXIST) {
94 free_extent_map(em);
95 em = NULL;
96 goto again;
97 } else if (ret) {
98 em = ERR_PTR(ret);
99 }
100out:
101 return em;
7eccb903
CM
102}
103
19c00ddc
CM
104u32 btrfs_csum_data(struct btrfs_root *root, char *data, u32 seed, size_t len)
105{
106 return crc32c(seed, data, len);
107}
108
109void btrfs_csum_final(u32 crc, char *result)
110{
111 *(__le32 *)result = ~cpu_to_le32(crc);
112}
113
114static int csum_tree_block(struct btrfs_root *root, struct extent_buffer *buf,
115 int verify)
116{
117 char result[BTRFS_CRC32_SIZE];
118 unsigned long len;
119 unsigned long cur_len;
120 unsigned long offset = BTRFS_CSUM_SIZE;
121 char *map_token = NULL;
122 char *kaddr;
123 unsigned long map_start;
124 unsigned long map_len;
125 int err;
126 u32 crc = ~(u32)0;
127
128 len = buf->len - offset;
129 while(len > 0) {
130 err = map_private_extent_buffer(buf, offset, 32,
131 &map_token, &kaddr,
132 &map_start, &map_len, KM_USER0);
133 if (err) {
134 printk("failed to map extent buffer! %lu\n",
135 offset);
136 return 1;
137 }
138 cur_len = min(len, map_len - (offset - map_start));
139 crc = btrfs_csum_data(root, kaddr + offset - map_start,
140 crc, cur_len);
141 len -= cur_len;
142 offset += cur_len;
143 unmap_extent_buffer(buf, map_token, KM_USER0);
144 }
145 btrfs_csum_final(crc, result);
146
147 if (verify) {
148 if (memcmp_extent_buffer(buf, result, 0, BTRFS_CRC32_SIZE)) {
149 printk("btrfs: %s checksum verify failed on %llu\n",
150 root->fs_info->sb->s_id,
151 buf->start);
152 return 1;
153 }
154 } else {
155 write_extent_buffer(buf, result, 0, BTRFS_CRC32_SIZE);
156 }
157 return 0;
158}
159
160
161int csum_dirty_buffer(struct btrfs_root *root, struct page *page)
162{
163 struct extent_map_tree *tree;
35ebb934 164 u64 start = (u64)page->index << PAGE_CACHE_SHIFT;
19c00ddc
CM
165 u64 found_start;
166 int found_level;
167 unsigned long len;
168 struct extent_buffer *eb;
169 tree = &BTRFS_I(page->mapping->host)->extent_tree;
170
171 if (page->private == EXTENT_PAGE_PRIVATE)
172 goto out;
173 if (!page->private)
174 goto out;
175 len = page->private >> 2;
176 if (len == 0) {
177 WARN_ON(1);
178 }
179 eb = alloc_extent_buffer(tree, start, len, page, GFP_NOFS);
180 read_extent_buffer_pages(tree, eb, start + PAGE_CACHE_SIZE, 1);
181 found_start = btrfs_header_bytenr(eb);
182 if (found_start != start) {
183 printk("warning: eb start incorrect %Lu buffer %Lu len %lu\n",
184 start, found_start, len);
185 }
186 found_level = btrfs_header_level(eb);
187 csum_tree_block(root, eb, 0);
188 free_extent_buffer(eb);
189out:
190 return 0;
191}
192
0da5468f 193static int btree_writepage_io_hook(struct page *page, u64 start, u64 end)
d98237b3 194{
19c00ddc 195 struct btrfs_root *root = BTRFS_I(page->mapping->host)->root;
19c00ddc
CM
196
197 csum_dirty_buffer(root, page);
0da5468f
CM
198 return 0;
199}
200
201static int btree_writepage(struct page *page, struct writeback_control *wbc)
202{
203 struct extent_map_tree *tree;
204 tree = &BTRFS_I(page->mapping->host)->extent_tree;
5f39d397
CM
205 return extent_write_full_page(tree, page, btree_get_extent, wbc);
206}
0da5468f
CM
207
208static int btree_writepages(struct address_space *mapping,
209 struct writeback_control *wbc)
210{
211 struct extent_map_tree *tree;
212 tree = &BTRFS_I(mapping->host)->extent_tree;
ca664626 213 if (wbc->sync_mode == WB_SYNC_NONE) {
793955bc
CM
214 u64 num_dirty;
215 u64 start = 0;
216 unsigned long thresh = 96 * 1024 * 1024;
448d640b
CM
217
218 if (wbc->for_kupdate)
219 return 0;
220
ca664626
CM
221 if (current_is_pdflush()) {
222 thresh = 96 * 1024 * 1024;
223 } else {
224 thresh = 8 * 1024 * 1024;
225 }
793955bc
CM
226 num_dirty = count_range_bits(tree, &start, thresh, EXTENT_DIRTY);
227 if (num_dirty < thresh) {
228 return 0;
229 }
230 }
0da5468f
CM
231 return extent_writepages(tree, mapping, btree_get_extent, wbc);
232}
233
5f39d397
CM
234int btree_readpage(struct file *file, struct page *page)
235{
236 struct extent_map_tree *tree;
237 tree = &BTRFS_I(page->mapping->host)->extent_tree;
238 return extent_read_full_page(tree, page, btree_get_extent);
239}
22b0ebda 240
5f39d397
CM
241static int btree_releasepage(struct page *page, gfp_t unused_gfp_flags)
242{
243 struct extent_map_tree *tree;
244 int ret;
d98237b3 245
5f39d397
CM
246 tree = &BTRFS_I(page->mapping->host)->extent_tree;
247 ret = try_release_extent_mapping(tree, page);
248 if (ret == 1) {
249 ClearPagePrivate(page);
250 set_page_private(page, 0);
251 page_cache_release(page);
252 }
d98237b3
CM
253 return ret;
254}
255
5f39d397 256static void btree_invalidatepage(struct page *page, unsigned long offset)
d98237b3 257{
5f39d397
CM
258 struct extent_map_tree *tree;
259 tree = &BTRFS_I(page->mapping->host)->extent_tree;
260 extent_invalidatepage(tree, page, offset);
261 btree_releasepage(page, GFP_NOFS);
d98237b3
CM
262}
263
5f39d397 264#if 0
d98237b3 265static int btree_writepage(struct page *page, struct writeback_control *wbc)
ed2ff2cb 266{
87cbda5c 267 struct buffer_head *bh;
0f7d52f4 268 struct btrfs_root *root = BTRFS_I(page->mapping->host)->root;
87cbda5c 269 struct buffer_head *head;
87cbda5c
CM
270 if (!page_has_buffers(page)) {
271 create_empty_buffers(page, root->fs_info->sb->s_blocksize,
272 (1 << BH_Dirty)|(1 << BH_Uptodate));
273 }
274 head = page_buffers(page);
275 bh = head;
276 do {
277 if (buffer_dirty(bh))
278 csum_tree_block(root, bh, 0);
279 bh = bh->b_this_page;
280 } while (bh != head);
d98237b3 281 return block_write_full_page(page, btree_get_block, wbc);
ed2ff2cb 282}
5f39d397 283#endif
eb60ceac 284
d98237b3
CM
285static struct address_space_operations btree_aops = {
286 .readpage = btree_readpage,
287 .writepage = btree_writepage,
0da5468f 288 .writepages = btree_writepages,
5f39d397
CM
289 .releasepage = btree_releasepage,
290 .invalidatepage = btree_invalidatepage,
d98237b3
CM
291 .sync_page = block_sync_page,
292};
293
db94535d 294int readahead_tree_block(struct btrfs_root *root, u64 bytenr, u32 blocksize)
090d1875 295{
5f39d397
CM
296 struct extent_buffer *buf = NULL;
297 struct inode *btree_inode = root->fs_info->btree_inode;
de428b63 298 int ret = 0;
090d1875 299
db94535d 300 buf = btrfs_find_create_tree_block(root, bytenr, blocksize);
5f39d397 301 if (!buf)
090d1875 302 return 0;
5f39d397 303 read_extent_buffer_pages(&BTRFS_I(btree_inode)->extent_tree,
19c00ddc 304 buf, 0, 0);
5f39d397 305 free_extent_buffer(buf);
de428b63 306 return ret;
090d1875
CM
307}
308
db94535d
CM
309struct extent_buffer *read_tree_block(struct btrfs_root *root, u64 bytenr,
310 u32 blocksize)
eb60ceac 311{
5f39d397
CM
312 struct extent_buffer *buf = NULL;
313 struct inode *btree_inode = root->fs_info->btree_inode;
19c00ddc
CM
314 struct extent_map_tree *extent_tree;
315 int ret;
316
317 extent_tree = &BTRFS_I(btree_inode)->extent_tree;
5f39d397 318
db94535d 319 buf = btrfs_find_create_tree_block(root, bytenr, blocksize);
5f39d397
CM
320 if (!buf)
321 return NULL;
322 read_extent_buffer_pages(&BTRFS_I(btree_inode)->extent_tree,
19c00ddc
CM
323 buf, 0, 1);
324 if (buf->flags & EXTENT_CSUM) {
325 return buf;
326 }
327 if (test_range_bit(extent_tree, buf->start, buf->start + buf->len - 1,
328 EXTENT_CSUM, 1)) {
329 buf->flags |= EXTENT_CSUM;
330 return buf;
331 }
332 ret = csum_tree_block(root, buf, 1);
333 set_extent_bits(extent_tree, buf->start,
334 buf->start + buf->len - 1,
335 EXTENT_CSUM, GFP_NOFS);
336 buf->flags |= EXTENT_CSUM;
5f39d397 337 return buf;
eb60ceac
CM
338}
339
e089f05c 340int clean_tree_block(struct btrfs_trans_handle *trans, struct btrfs_root *root,
5f39d397 341 struct extent_buffer *buf)
ed2ff2cb 342{
5f39d397
CM
343 struct inode *btree_inode = root->fs_info->btree_inode;
344 clear_extent_buffer_dirty(&BTRFS_I(btree_inode)->extent_tree, buf);
345 return 0;
346}
347
348int wait_on_tree_block_writeback(struct btrfs_root *root,
349 struct extent_buffer *buf)
350{
351 struct inode *btree_inode = root->fs_info->btree_inode;
352 wait_on_extent_buffer_writeback(&BTRFS_I(btree_inode)->extent_tree,
353 buf);
354 return 0;
355}
356
db94535d 357static int __setup_root(u32 nodesize, u32 leafsize, u32 sectorsize,
9f5fae2f
CM
358 struct btrfs_root *root,
359 struct btrfs_fs_info *fs_info,
e20d96d6 360 u64 objectid)
d97e63b6 361{
cfaa7295 362 root->node = NULL;
0f7d52f4 363 root->inode = NULL;
a28ec197 364 root->commit_root = NULL;
db94535d
CM
365 root->sectorsize = sectorsize;
366 root->nodesize = nodesize;
367 root->leafsize = leafsize;
123abc88 368 root->ref_cows = 0;
9f5fae2f 369 root->fs_info = fs_info;
0f7d52f4
CM
370 root->objectid = objectid;
371 root->last_trans = 0;
1b05da2e
CM
372 root->highest_inode = 0;
373 root->last_inode_alloc = 0;
58176a96 374 root->name = NULL;
3768f368
CM
375 memset(&root->root_key, 0, sizeof(root->root_key));
376 memset(&root->root_item, 0, sizeof(root->root_item));
6702ed49 377 memset(&root->defrag_progress, 0, sizeof(root->defrag_progress));
58176a96
JB
378 memset(&root->root_kobj, 0, sizeof(root->root_kobj));
379 init_completion(&root->kobj_unregister);
011410bd 380 init_rwsem(&root->snap_sem);
6702ed49
CM
381 root->defrag_running = 0;
382 root->defrag_level = 0;
4d775673 383 root->root_key.objectid = objectid;
3768f368
CM
384 return 0;
385}
386
db94535d 387static int find_and_setup_root(struct btrfs_root *tree_root,
9f5fae2f
CM
388 struct btrfs_fs_info *fs_info,
389 u64 objectid,
e20d96d6 390 struct btrfs_root *root)
3768f368
CM
391{
392 int ret;
db94535d 393 u32 blocksize;
3768f368 394
db94535d
CM
395 __setup_root(tree_root->nodesize, tree_root->leafsize,
396 tree_root->sectorsize, root, fs_info, objectid);
3768f368
CM
397 ret = btrfs_find_last_root(tree_root, objectid,
398 &root->root_item, &root->root_key);
399 BUG_ON(ret);
400
db94535d
CM
401 blocksize = btrfs_level_size(root, btrfs_root_level(&root->root_item));
402 root->node = read_tree_block(root, btrfs_root_bytenr(&root->root_item),
403 blocksize);
3768f368 404 BUG_ON(!root->node);
d97e63b6
CM
405 return 0;
406}
407
5eda7b5e
CM
408struct btrfs_root *btrfs_read_fs_root_no_radix(struct btrfs_fs_info *fs_info,
409 struct btrfs_key *location)
0f7d52f4
CM
410{
411 struct btrfs_root *root;
412 struct btrfs_root *tree_root = fs_info->tree_root;
413 struct btrfs_path *path;
5f39d397 414 struct extent_buffer *l;
1b05da2e 415 u64 highest_inode;
db94535d 416 u32 blocksize;
0f7d52f4
CM
417 int ret = 0;
418
5eda7b5e 419 root = kzalloc(sizeof(*root), GFP_NOFS);
0cf6c620 420 if (!root)
0f7d52f4 421 return ERR_PTR(-ENOMEM);
0f7d52f4 422 if (location->offset == (u64)-1) {
db94535d 423 ret = find_and_setup_root(tree_root, fs_info,
0f7d52f4
CM
424 location->objectid, root);
425 if (ret) {
0f7d52f4
CM
426 kfree(root);
427 return ERR_PTR(ret);
428 }
429 goto insert;
430 }
431
db94535d
CM
432 __setup_root(tree_root->nodesize, tree_root->leafsize,
433 tree_root->sectorsize, root, fs_info,
0f7d52f4
CM
434 location->objectid);
435
436 path = btrfs_alloc_path();
437 BUG_ON(!path);
438 ret = btrfs_search_slot(NULL, tree_root, location, path, 0, 0);
439 if (ret != 0) {
0f7d52f4
CM
440 if (ret > 0)
441 ret = -ENOENT;
442 goto out;
443 }
5f39d397
CM
444 l = path->nodes[0];
445 read_extent_buffer(l, &root->root_item,
446 btrfs_item_ptr_offset(l, path->slots[0]),
0f7d52f4 447 sizeof(root->root_item));
44b36eb2 448 memcpy(&root->root_key, location, sizeof(*location));
0f7d52f4
CM
449 ret = 0;
450out:
451 btrfs_release_path(root, path);
452 btrfs_free_path(path);
453 if (ret) {
454 kfree(root);
455 return ERR_PTR(ret);
456 }
db94535d
CM
457 blocksize = btrfs_level_size(root, btrfs_root_level(&root->root_item));
458 root->node = read_tree_block(root, btrfs_root_bytenr(&root->root_item),
459 blocksize);
0f7d52f4
CM
460 BUG_ON(!root->node);
461insert:
0f7d52f4 462 root->ref_cows = 1;
5eda7b5e
CM
463 ret = btrfs_find_highest_inode(root, &highest_inode);
464 if (ret == 0) {
465 root->highest_inode = highest_inode;
466 root->last_inode_alloc = highest_inode;
467 }
468 return root;
469}
470
471struct btrfs_root *btrfs_read_fs_root(struct btrfs_fs_info *fs_info,
58176a96
JB
472 struct btrfs_key *location,
473 const char *name, int namelen)
5eda7b5e
CM
474{
475 struct btrfs_root *root;
476 int ret;
477
478 root = radix_tree_lookup(&fs_info->fs_roots_radix,
479 (unsigned long)location->objectid);
480 if (root)
481 return root;
482
483 root = btrfs_read_fs_root_no_radix(fs_info, location);
484 if (IS_ERR(root))
485 return root;
2619ba1f
CM
486 ret = radix_tree_insert(&fs_info->fs_roots_radix,
487 (unsigned long)root->root_key.objectid,
0f7d52f4
CM
488 root);
489 if (ret) {
5f39d397 490 free_extent_buffer(root->node);
0f7d52f4
CM
491 kfree(root);
492 return ERR_PTR(ret);
493 }
58176a96
JB
494
495 ret = btrfs_set_root_name(root, name, namelen);
496 if (ret) {
5f39d397 497 free_extent_buffer(root->node);
58176a96
JB
498 kfree(root);
499 return ERR_PTR(ret);
500 }
501
502 ret = btrfs_sysfs_add_root(root);
503 if (ret) {
5f39d397 504 free_extent_buffer(root->node);
58176a96
JB
505 kfree(root->name);
506 kfree(root);
507 return ERR_PTR(ret);
508 }
509
5ce14bbc
CM
510 ret = btrfs_find_dead_roots(fs_info->tree_root,
511 root->root_key.objectid, root);
512 BUG_ON(ret);
513
0f7d52f4
CM
514 return root;
515}
19c00ddc
CM
516#if 0
517static int add_hasher(struct btrfs_fs_info *info, char *type) {
518 struct btrfs_hasher *hasher;
519
520 hasher = kmalloc(sizeof(*hasher), GFP_NOFS);
521 if (!hasher)
522 return -ENOMEM;
523 hasher->hash_tfm = crypto_alloc_hash(type, 0, CRYPTO_ALG_ASYNC);
524 if (!hasher->hash_tfm) {
525 kfree(hasher);
526 return -EINVAL;
527 }
528 spin_lock(&info->hash_lock);
529 list_add(&hasher->list, &info->hashers);
530 spin_unlock(&info->hash_lock);
531 return 0;
532}
533#endif
2c90e5d6 534struct btrfs_root *open_ctree(struct super_block *sb)
2e635a27 535{
db94535d
CM
536 u32 sectorsize;
537 u32 nodesize;
538 u32 leafsize;
539 u32 blocksize;
e20d96d6
CM
540 struct btrfs_root *extent_root = kmalloc(sizeof(struct btrfs_root),
541 GFP_NOFS);
542 struct btrfs_root *tree_root = kmalloc(sizeof(struct btrfs_root),
543 GFP_NOFS);
e20d96d6
CM
544 struct btrfs_fs_info *fs_info = kmalloc(sizeof(*fs_info),
545 GFP_NOFS);
eb60ceac 546 int ret;
39279cc3 547 int err = -EIO;
2c90e5d6 548 struct btrfs_super_block *disk_super;
eb60ceac 549
39279cc3
CM
550 if (!extent_root || !tree_root || !fs_info) {
551 err = -ENOMEM;
552 goto fail;
553 }
0f7d52f4 554 INIT_RADIX_TREE(&fs_info->fs_roots_radix, GFP_NOFS);
8fd17795 555 INIT_LIST_HEAD(&fs_info->trans_list);
facda1e7 556 INIT_LIST_HEAD(&fs_info->dead_roots);
19c00ddc
CM
557 INIT_LIST_HEAD(&fs_info->hashers);
558 spin_lock_init(&fs_info->hash_lock);
559
58176a96
JB
560 memset(&fs_info->super_kobj, 0, sizeof(fs_info->super_kobj));
561 init_completion(&fs_info->kobj_unregister);
2c90e5d6 562 sb_set_blocksize(sb, 4096);
9f5fae2f 563 fs_info->running_transaction = NULL;
15ee9bc7 564 fs_info->last_trans_committed = 0;
9f5fae2f
CM
565 fs_info->tree_root = tree_root;
566 fs_info->extent_root = extent_root;
e20d96d6 567 fs_info->sb = sb;
d98237b3
CM
568 fs_info->btree_inode = new_inode(sb);
569 fs_info->btree_inode->i_ino = 1;
2c90e5d6 570 fs_info->btree_inode->i_nlink = 1;
d98237b3
CM
571 fs_info->btree_inode->i_size = sb->s_bdev->bd_inode->i_size;
572 fs_info->btree_inode->i_mapping->a_ops = &btree_aops;
5f39d397
CM
573 extent_map_tree_init(&BTRFS_I(fs_info->btree_inode)->extent_tree,
574 fs_info->btree_inode->i_mapping,
575 GFP_NOFS);
0da5468f
CM
576 BTRFS_I(fs_info->btree_inode)->extent_tree.ops = &btree_extent_map_ops;
577
f510cfec
CM
578 extent_map_tree_init(&fs_info->free_space_cache,
579 fs_info->btree_inode->i_mapping, GFP_NOFS);
96b5179d
CM
580 extent_map_tree_init(&fs_info->block_group_cache,
581 fs_info->btree_inode->i_mapping, GFP_NOFS);
1a5bc167
CM
582 extent_map_tree_init(&fs_info->pinned_extents,
583 fs_info->btree_inode->i_mapping, GFP_NOFS);
584 extent_map_tree_init(&fs_info->pending_del,
585 fs_info->btree_inode->i_mapping, GFP_NOFS);
586 extent_map_tree_init(&fs_info->extent_ins,
587 fs_info->btree_inode->i_mapping, GFP_NOFS);
e66f709b 588 fs_info->do_barriers = 1;
facda1e7 589 fs_info->closing = 0;
324ae4df 590 fs_info->total_pinned = 0;
facda1e7 591
08607c1b 592 INIT_DELAYED_WORK(&fs_info->trans_work, btrfs_transaction_cleaner);
0f7d52f4
CM
593 BTRFS_I(fs_info->btree_inode)->root = tree_root;
594 memset(&BTRFS_I(fs_info->btree_inode)->location, 0,
595 sizeof(struct btrfs_key));
22b0ebda 596 insert_inode_hash(fs_info->btree_inode);
d98237b3 597 mapping_set_gfp_mask(fs_info->btree_inode->i_mapping, GFP_NOFS);
39279cc3 598
79154b1b 599 mutex_init(&fs_info->trans_mutex);
d561c025 600 mutex_init(&fs_info->fs_mutex);
3768f368 601
19c00ddc
CM
602#if 0
603 ret = add_hasher(fs_info, "crc32c");
604 if (ret) {
605 printk("btrfs: failed hash setup, modprobe cryptomgr?\n");
606 err = -ENOMEM;
607 goto fail_iput;
608 }
609#endif
db94535d 610 __setup_root(512, 512, 512, tree_root,
2c90e5d6 611 fs_info, BTRFS_ROOT_TREE_OBJECTID);
7eccb903 612
2c90e5d6 613 fs_info->sb_buffer = read_tree_block(tree_root,
db94535d
CM
614 BTRFS_SUPER_INFO_OFFSET,
615 512);
d98237b3 616
0f7d52f4 617 if (!fs_info->sb_buffer)
39279cc3 618 goto fail_iput;
39279cc3 619
5f39d397
CM
620 read_extent_buffer(fs_info->sb_buffer, &fs_info->super_copy, 0,
621 sizeof(fs_info->super_copy));
622
623 read_extent_buffer(fs_info->sb_buffer, fs_info->fsid,
624 (unsigned long)btrfs_super_fsid(fs_info->sb_buffer),
625 BTRFS_FSID_SIZE);
626 disk_super = &fs_info->super_copy;
0f7d52f4 627 if (!btrfs_super_root(disk_super))
39279cc3 628 goto fail_sb_buffer;
0f7d52f4 629
db94535d
CM
630 nodesize = btrfs_super_nodesize(disk_super);
631 leafsize = btrfs_super_leafsize(disk_super);
632 sectorsize = btrfs_super_sectorsize(disk_super);
633 tree_root->nodesize = nodesize;
634 tree_root->leafsize = leafsize;
635 tree_root->sectorsize = sectorsize;
ff79f819 636 sb_set_blocksize(sb, sectorsize);
db94535d 637
8352d8a4 638 i_size_write(fs_info->btree_inode,
db94535d 639 btrfs_super_total_bytes(disk_super));
8352d8a4 640
39279cc3
CM
641 if (strncmp((char *)(&disk_super->magic), BTRFS_MAGIC,
642 sizeof(disk_super->magic))) {
643 printk("btrfs: valid FS not found on %s\n", sb->s_id);
644 goto fail_sb_buffer;
645 }
19c00ddc 646
db94535d
CM
647 blocksize = btrfs_level_size(tree_root,
648 btrfs_super_root_level(disk_super));
19c00ddc 649
e20d96d6 650 tree_root->node = read_tree_block(tree_root,
db94535d
CM
651 btrfs_super_root(disk_super),
652 blocksize);
39279cc3
CM
653 if (!tree_root->node)
654 goto fail_sb_buffer;
3768f368 655
2c90e5d6 656 mutex_lock(&fs_info->fs_mutex);
db94535d
CM
657
658 ret = find_and_setup_root(tree_root, fs_info,
e20d96d6 659 BTRFS_EXTENT_TREE_OBJECTID, extent_root);
39279cc3
CM
660 if (ret) {
661 mutex_unlock(&fs_info->fs_mutex);
662 goto fail_tree_root;
663 }
3768f368 664
9078a3e1
CM
665 btrfs_read_block_groups(extent_root);
666
0f7d52f4 667 fs_info->generation = btrfs_super_generation(disk_super) + 1;
5be6f7f1 668 mutex_unlock(&fs_info->fs_mutex);
0f7d52f4 669 return tree_root;
39279cc3
CM
670
671fail_tree_root:
5f39d397 672 free_extent_buffer(tree_root->node);
39279cc3 673fail_sb_buffer:
5f39d397 674 free_extent_buffer(fs_info->sb_buffer);
39279cc3
CM
675fail_iput:
676 iput(fs_info->btree_inode);
677fail:
678 kfree(extent_root);
679 kfree(tree_root);
680 kfree(fs_info);
681 return ERR_PTR(err);
eb60ceac
CM
682}
683
e089f05c 684int write_ctree_super(struct btrfs_trans_handle *trans, struct btrfs_root
79154b1b 685 *root)
eb60ceac 686{
e66f709b 687 int ret;
5f39d397
CM
688 struct extent_buffer *super = root->fs_info->sb_buffer;
689 struct inode *btree_inode = root->fs_info->btree_inode;
690
691 set_extent_buffer_dirty(&BTRFS_I(btree_inode)->extent_tree, super);
692 ret = sync_page_range_nolock(btree_inode, btree_inode->i_mapping,
693 super->start, super->len);
694 return ret;
cfaa7295
CM
695}
696
5eda7b5e 697int btrfs_free_fs_root(struct btrfs_fs_info *fs_info, struct btrfs_root *root)
2619ba1f
CM
698{
699 radix_tree_delete(&fs_info->fs_roots_radix,
700 (unsigned long)root->root_key.objectid);
58176a96 701 btrfs_sysfs_del_root(root);
2619ba1f
CM
702 if (root->inode)
703 iput(root->inode);
704 if (root->node)
5f39d397 705 free_extent_buffer(root->node);
2619ba1f 706 if (root->commit_root)
5f39d397 707 free_extent_buffer(root->commit_root);
58176a96
JB
708 if (root->name)
709 kfree(root->name);
2619ba1f
CM
710 kfree(root);
711 return 0;
712}
713
35b7e476 714static int del_fs_roots(struct btrfs_fs_info *fs_info)
0f7d52f4
CM
715{
716 int ret;
717 struct btrfs_root *gang[8];
718 int i;
719
720 while(1) {
721 ret = radix_tree_gang_lookup(&fs_info->fs_roots_radix,
722 (void **)gang, 0,
723 ARRAY_SIZE(gang));
724 if (!ret)
725 break;
2619ba1f 726 for (i = 0; i < ret; i++)
5eda7b5e 727 btrfs_free_fs_root(fs_info, gang[i]);
0f7d52f4
CM
728 }
729 return 0;
730}
b4100d64 731
e20d96d6 732int close_ctree(struct btrfs_root *root)
cfaa7295 733{
3768f368 734 int ret;
e089f05c 735 struct btrfs_trans_handle *trans;
0f7d52f4 736 struct btrfs_fs_info *fs_info = root->fs_info;
e089f05c 737
facda1e7 738 fs_info->closing = 1;
08607c1b 739 btrfs_transaction_flush_work(root);
0f7d52f4 740 mutex_lock(&fs_info->fs_mutex);
6702ed49 741 btrfs_defrag_dirty_roots(root->fs_info);
79154b1b 742 trans = btrfs_start_transaction(root, 1);
54aa1f4d 743 ret = btrfs_commit_transaction(trans, root);
79154b1b
CM
744 /* run commit again to drop the original snapshot */
745 trans = btrfs_start_transaction(root, 1);
746 btrfs_commit_transaction(trans, root);
747 ret = btrfs_write_and_wait_transaction(NULL, root);
3768f368 748 BUG_ON(ret);
79154b1b 749 write_ctree_super(NULL, root);
0f7d52f4
CM
750 mutex_unlock(&fs_info->fs_mutex);
751
752 if (fs_info->extent_root->node)
5f39d397 753 free_extent_buffer(fs_info->extent_root->node);
f510cfec 754
0f7d52f4 755 if (fs_info->tree_root->node)
5f39d397 756 free_extent_buffer(fs_info->tree_root->node);
f510cfec 757
5f39d397 758 free_extent_buffer(fs_info->sb_buffer);
7eccb903 759
9078a3e1 760 btrfs_free_block_groups(root->fs_info);
0f7d52f4 761 del_fs_roots(fs_info);
19c00ddc 762 extent_map_tree_empty_lru(&BTRFS_I(fs_info->btree_inode)->extent_tree);
db94535d
CM
763 truncate_inode_pages(fs_info->btree_inode->i_mapping, 0);
764 iput(fs_info->btree_inode);
19c00ddc
CM
765#if 0
766 while(!list_empty(&fs_info->hashers)) {
767 struct btrfs_hasher *hasher;
768 hasher = list_entry(fs_info->hashers.next, struct btrfs_hasher,
769 hashers);
770 list_del(&hasher->hashers);
771 crypto_free_hash(&fs_info->hash_tfm);
772 kfree(hasher);
773 }
774#endif
0f7d52f4 775 kfree(fs_info->extent_root);
0f7d52f4 776 kfree(fs_info->tree_root);
eb60ceac
CM
777 return 0;
778}
779
5f39d397
CM
780int btrfs_buffer_uptodate(struct extent_buffer *buf)
781{
810191ff 782 struct inode *btree_inode = buf->first_page->mapping->host;
5f39d397
CM
783 return extent_buffer_uptodate(&BTRFS_I(btree_inode)->extent_tree, buf);
784}
785
786int btrfs_set_buffer_uptodate(struct extent_buffer *buf)
ccd467d6 787{
810191ff 788 struct inode *btree_inode = buf->first_page->mapping->host;
5f39d397
CM
789 return set_extent_buffer_uptodate(&BTRFS_I(btree_inode)->extent_tree,
790 buf);
791}
6702ed49 792
5f39d397
CM
793void btrfs_mark_buffer_dirty(struct extent_buffer *buf)
794{
810191ff 795 struct btrfs_root *root = BTRFS_I(buf->first_page->mapping->host)->root;
5f39d397
CM
796 u64 transid = btrfs_header_generation(buf);
797 struct inode *btree_inode = root->fs_info->btree_inode;
6702ed49 798
ccd467d6
CM
799 if (transid != root->fs_info->generation) {
800 printk(KERN_CRIT "transid mismatch buffer %llu, found %Lu running %Lu\n",
db94535d 801 (unsigned long long)buf->start,
ccd467d6
CM
802 transid, root->fs_info->generation);
803 WARN_ON(1);
804 }
5f39d397 805 set_extent_buffer_dirty(&BTRFS_I(btree_inode)->extent_tree, buf);
eb60ceac
CM
806}
807
d3c2fdcf 808void btrfs_btree_balance_dirty(struct btrfs_root *root, unsigned long nr)
35b7e476 809{
d3c2fdcf 810 balance_dirty_pages_ratelimited_nr(
304fced6 811 root->fs_info->btree_inode->i_mapping, 1);
35b7e476 812}
6b80053d
CM
813
814void btrfs_set_buffer_defrag(struct extent_buffer *buf)
815{
810191ff 816 struct btrfs_root *root = BTRFS_I(buf->first_page->mapping->host)->root;
6b80053d
CM
817 struct inode *btree_inode = root->fs_info->btree_inode;
818 set_extent_bits(&BTRFS_I(btree_inode)->extent_tree, buf->start,
819 buf->start + buf->len - 1, EXTENT_DEFRAG, GFP_NOFS);
820}
821
822void btrfs_set_buffer_defrag_done(struct extent_buffer *buf)
823{
810191ff 824 struct btrfs_root *root = BTRFS_I(buf->first_page->mapping->host)->root;
6b80053d
CM
825 struct inode *btree_inode = root->fs_info->btree_inode;
826 set_extent_bits(&BTRFS_I(btree_inode)->extent_tree, buf->start,
827 buf->start + buf->len - 1, EXTENT_DEFRAG_DONE,
828 GFP_NOFS);
829}
830
831int btrfs_buffer_defrag(struct extent_buffer *buf)
832{
810191ff 833 struct btrfs_root *root = BTRFS_I(buf->first_page->mapping->host)->root;
6b80053d
CM
834 struct inode *btree_inode = root->fs_info->btree_inode;
835 return test_range_bit(&BTRFS_I(btree_inode)->extent_tree,
836 buf->start, buf->start + buf->len - 1, EXTENT_DEFRAG, 0);
837}
838
839int btrfs_buffer_defrag_done(struct extent_buffer *buf)
840{
810191ff 841 struct btrfs_root *root = BTRFS_I(buf->first_page->mapping->host)->root;
6b80053d
CM
842 struct inode *btree_inode = root->fs_info->btree_inode;
843 return test_range_bit(&BTRFS_I(btree_inode)->extent_tree,
844 buf->start, buf->start + buf->len - 1,
845 EXTENT_DEFRAG_DONE, 0);
846}
847
848int btrfs_clear_buffer_defrag_done(struct extent_buffer *buf)
849{
810191ff 850 struct btrfs_root *root = BTRFS_I(buf->first_page->mapping->host)->root;
6b80053d
CM
851 struct inode *btree_inode = root->fs_info->btree_inode;
852 return clear_extent_bits(&BTRFS_I(btree_inode)->extent_tree,
853 buf->start, buf->start + buf->len - 1,
854 EXTENT_DEFRAG_DONE, GFP_NOFS);
855}
856
857int btrfs_clear_buffer_defrag(struct extent_buffer *buf)
858{
810191ff 859 struct btrfs_root *root = BTRFS_I(buf->first_page->mapping->host)->root;
6b80053d
CM
860 struct inode *btree_inode = root->fs_info->btree_inode;
861 return clear_extent_bits(&BTRFS_I(btree_inode)->extent_tree,
862 buf->start, buf->start + buf->len - 1,
863 EXTENT_DEFRAG, GFP_NOFS);
864}
865
866int btrfs_read_buffer(struct extent_buffer *buf)
867{
810191ff 868 struct btrfs_root *root = BTRFS_I(buf->first_page->mapping->host)->root;
6b80053d
CM
869 struct inode *btree_inode = root->fs_info->btree_inode;
870 return read_extent_buffer_pages(&BTRFS_I(btree_inode)->extent_tree,
19c00ddc 871 buf, 0, 1);
6b80053d 872}
0da5468f
CM
873
874static struct extent_map_ops btree_extent_map_ops = {
875 .writepage_io_hook = btree_writepage_io_hook,
876};