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