Btrfs: Do metadata checksums for reads via a workqueue
[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
ce9adaa5 27#include <linux/workqueue.h>
eb60ceac
CM
28#include "ctree.h"
29#include "disk-io.h"
e089f05c 30#include "transaction.h"
0f7d52f4 31#include "btrfs_inode.h"
0b86a832 32#include "volumes.h"
db94535d 33#include "print-tree.h"
eb60ceac 34
5f39d397
CM
35#if 0
36static int check_tree_block(struct btrfs_root *root, struct extent_buffer *buf)
7eccb903 37{
5f39d397
CM
38 if (extent_buffer_blocknr(buf) != btrfs_header_blocknr(buf)) {
39 printk(KERN_CRIT "buf blocknr(buf) is %llu, header is %llu\n",
40 (unsigned long long)extent_buffer_blocknr(buf),
41 (unsigned long long)btrfs_header_blocknr(buf));
39279cc3 42 return 1;
d98237b3 43 }
9a8dd150 44 return 0;
eb60ceac 45}
5f39d397 46#endif
eb60ceac 47
d1310b2e 48static struct extent_io_ops btree_extent_io_ops;
ce9adaa5
CM
49static struct workqueue_struct *end_io_workqueue;
50
51struct end_io_wq {
52 struct bio *bio;
53 bio_end_io_t *end_io;
54 void *private;
55 struct btrfs_fs_info *info;
56 int error;
57 struct list_head list;
58};
0da5468f 59
5f39d397 60struct extent_map *btree_get_extent(struct inode *inode, struct page *page,
d1310b2e 61 size_t page_offset, u64 start, u64 len,
5f39d397 62 int create)
7eccb903 63{
5f39d397
CM
64 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
65 struct extent_map *em;
66 int ret;
67
68again:
d1310b2e
CM
69 spin_lock(&em_tree->lock);
70 em = lookup_extent_mapping(em_tree, start, len);
71 spin_unlock(&em_tree->lock);
5f39d397
CM
72 if (em) {
73 goto out;
7eccb903 74 }
5f39d397
CM
75 em = alloc_extent_map(GFP_NOFS);
76 if (!em) {
77 em = ERR_PTR(-ENOMEM);
78 goto out;
79 }
80 em->start = 0;
d1310b2e 81 em->len = i_size_read(inode);
5f39d397 82 em->block_start = 0;
5f39d397 83 em->bdev = inode->i_sb->s_bdev;
d1310b2e
CM
84
85 spin_lock(&em_tree->lock);
5f39d397 86 ret = add_extent_mapping(em_tree, em);
d1310b2e
CM
87 spin_unlock(&em_tree->lock);
88
5f39d397
CM
89 if (ret == -EEXIST) {
90 free_extent_map(em);
91 em = NULL;
92 goto again;
93 } else if (ret) {
94 em = ERR_PTR(ret);
95 }
96out:
97 return em;
7eccb903
CM
98}
99
19c00ddc
CM
100u32 btrfs_csum_data(struct btrfs_root *root, char *data, u32 seed, size_t len)
101{
102 return crc32c(seed, data, len);
103}
104
105void btrfs_csum_final(u32 crc, char *result)
106{
107 *(__le32 *)result = ~cpu_to_le32(crc);
108}
109
110static int csum_tree_block(struct btrfs_root *root, struct extent_buffer *buf,
111 int verify)
112{
113 char result[BTRFS_CRC32_SIZE];
114 unsigned long len;
115 unsigned long cur_len;
116 unsigned long offset = BTRFS_CSUM_SIZE;
117 char *map_token = NULL;
118 char *kaddr;
119 unsigned long map_start;
120 unsigned long map_len;
121 int err;
122 u32 crc = ~(u32)0;
123
124 len = buf->len - offset;
125 while(len > 0) {
126 err = map_private_extent_buffer(buf, offset, 32,
127 &map_token, &kaddr,
128 &map_start, &map_len, KM_USER0);
129 if (err) {
130 printk("failed to map extent buffer! %lu\n",
131 offset);
132 return 1;
133 }
134 cur_len = min(len, map_len - (offset - map_start));
135 crc = btrfs_csum_data(root, kaddr + offset - map_start,
136 crc, cur_len);
137 len -= cur_len;
138 offset += cur_len;
139 unmap_extent_buffer(buf, map_token, KM_USER0);
140 }
141 btrfs_csum_final(crc, result);
142
143 if (verify) {
e4204ded
CM
144 int from_this_trans = 0;
145
146 if (root->fs_info->running_transaction &&
147 btrfs_header_generation(buf) ==
148 root->fs_info->running_transaction->transid)
149 from_this_trans = 1;
150
151 /* FIXME, this is not good */
63b10fc4 152 if (memcmp_extent_buffer(buf, result, 0, BTRFS_CRC32_SIZE)) {
e4204ded
CM
153 u32 val;
154 u32 found = 0;
155 memcpy(&found, result, BTRFS_CRC32_SIZE);
156
157 read_extent_buffer(buf, &val, 0, BTRFS_CRC32_SIZE);
63b10fc4 158 WARN_ON(1);
e4204ded 159 printk("btrfs: %s checksum verify failed on %llu "
63b10fc4
CM
160 "wanted %X found %X from_this_trans %d "
161 "level %d\n",
19c00ddc 162 root->fs_info->sb->s_id,
63b10fc4
CM
163 buf->start, val, found, from_this_trans,
164 btrfs_header_level(buf));
19c00ddc
CM
165 return 1;
166 }
167 } else {
168 write_extent_buffer(buf, result, 0, BTRFS_CRC32_SIZE);
169 }
170 return 0;
171}
172
173
174int csum_dirty_buffer(struct btrfs_root *root, struct page *page)
175{
d1310b2e 176 struct extent_io_tree *tree;
35ebb934 177 u64 start = (u64)page->index << PAGE_CACHE_SHIFT;
19c00ddc
CM
178 u64 found_start;
179 int found_level;
180 unsigned long len;
181 struct extent_buffer *eb;
d1310b2e 182 tree = &BTRFS_I(page->mapping->host)->io_tree;
19c00ddc
CM
183
184 if (page->private == EXTENT_PAGE_PRIVATE)
185 goto out;
186 if (!page->private)
187 goto out;
188 len = page->private >> 2;
189 if (len == 0) {
190 WARN_ON(1);
191 }
192 eb = alloc_extent_buffer(tree, start, len, page, GFP_NOFS);
a86c12c7
CM
193 read_extent_buffer_pages(tree, eb, start + PAGE_CACHE_SIZE, 1,
194 btree_get_extent);
e18e4809 195 btrfs_clear_buffer_defrag(eb);
19c00ddc
CM
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);
55c69072
CM
200 WARN_ON(1);
201 goto err;
202 }
203 if (eb->first_page != page) {
204 printk("bad first page %lu %lu\n", eb->first_page->index,
205 page->index);
206 WARN_ON(1);
207 goto err;
208 }
209 if (!PageUptodate(page)) {
210 printk("csum not up to date page %lu\n", page->index);
211 WARN_ON(1);
212 goto err;
19c00ddc
CM
213 }
214 found_level = btrfs_header_level(eb);
63b10fc4
CM
215 spin_lock(&root->fs_info->hash_lock);
216 btrfs_set_header_flag(eb, BTRFS_HEADER_FLAG_WRITTEN);
217 spin_unlock(&root->fs_info->hash_lock);
19c00ddc 218 csum_tree_block(root, eb, 0);
55c69072 219err:
19c00ddc
CM
220 free_extent_buffer(eb);
221out:
222 return 0;
223}
224
0da5468f 225static int btree_writepage_io_hook(struct page *page, u64 start, u64 end)
d98237b3 226{
19c00ddc 227 struct btrfs_root *root = BTRFS_I(page->mapping->host)->root;
19c00ddc
CM
228
229 csum_dirty_buffer(root, page);
0da5468f
CM
230 return 0;
231}
232
ce9adaa5
CM
233int btree_readpage_end_io_hook(struct page *page, u64 start, u64 end,
234 struct extent_state *state)
235{
236 struct extent_io_tree *tree;
237 u64 found_start;
238 int found_level;
239 unsigned long len;
240 struct extent_buffer *eb;
241 struct btrfs_root *root = BTRFS_I(page->mapping->host)->root;
242 int ret;
243
244 tree = &BTRFS_I(page->mapping->host)->io_tree;
245 if (page->private == EXTENT_PAGE_PRIVATE)
246 goto out;
247 if (!page->private)
248 goto out;
249 len = page->private >> 2;
250 if (len == 0) {
251 WARN_ON(1);
252 }
253 eb = alloc_extent_buffer(tree, start, len, page, GFP_NOFS);
254 read_extent_buffer_pages(tree, eb, start + PAGE_CACHE_SIZE, 1,
255 btree_get_extent);
256 btrfs_clear_buffer_defrag(eb);
257 found_start = btrfs_header_bytenr(eb);
258 if (found_start != start) {
259 printk("warning: eb start incorrect %Lu buffer %Lu len %lu\n",
260 start, found_start, len);
261 WARN_ON(1);
262 goto err;
263 }
264 if (eb->first_page != page) {
265 printk("bad first page %lu %lu\n", eb->first_page->index,
266 page->index);
267 WARN_ON(1);
268 goto err;
269 }
270 found_level = btrfs_header_level(eb);
271
272 ret = csum_tree_block(root, eb, 1);
273
274 end = min_t(u64, eb->len, PAGE_CACHE_SIZE);
275 end = eb->start + end - 1;
276 release_extent_buffer_tail_pages(eb);
277err:
278 free_extent_buffer(eb);
279out:
280 return 0;
281}
282
283#if LINUX_VERSION_CODE > KERNEL_VERSION(2,6,23)
284static void end_workqueue_bio(struct bio *bio, int err)
285#else
286static int end_workqueue_bio(struct bio *bio,
287 unsigned int bytes_done, int err)
288#endif
289{
290 struct end_io_wq *end_io_wq = bio->bi_private;
291 struct btrfs_fs_info *fs_info;
292 unsigned long flags;
293
294#if LINUX_VERSION_CODE <= KERNEL_VERSION(2,6,23)
295 if (bio->bi_size)
296 return 1;
297#endif
298
299 fs_info = end_io_wq->info;
300 spin_lock_irqsave(&fs_info->end_io_work_lock, flags);
301 end_io_wq->error = err;
302 list_add_tail(&end_io_wq->list, &fs_info->end_io_work_list);
303 spin_unlock_irqrestore(&fs_info->end_io_work_lock, flags);
304 queue_work(end_io_workqueue, &fs_info->end_io_work);
305
306#if LINUX_VERSION_CODE <= KERNEL_VERSION(2,6,23)
307 return 0;
308#endif
309}
310
0b86a832
CM
311static int btree_submit_bio_hook(struct inode *inode, int rw, struct bio *bio)
312{
313 struct btrfs_root *root = BTRFS_I(inode)->root;
ce9adaa5 314 struct end_io_wq *end_io_wq;
0b86a832
CM
315 u64 offset;
316 offset = bio->bi_sector << 9;
ce9adaa5
CM
317
318 if (rw & (1 << BIO_RW)) {
319 return btrfs_map_bio(BTRFS_I(inode)->root, rw, bio);
320 }
321
322 end_io_wq = kmalloc(sizeof(*end_io_wq), GFP_NOFS);
323 if (!end_io_wq)
324 return -ENOMEM;
325
326 end_io_wq->private = bio->bi_private;
327 end_io_wq->end_io = bio->bi_end_io;
328 end_io_wq->info = root->fs_info;
329 end_io_wq->error = 0;
330 end_io_wq->bio = bio;
331
332 bio->bi_private = end_io_wq;
333 bio->bi_end_io = end_workqueue_bio;
334
0b86a832
CM
335 if (offset == BTRFS_SUPER_INFO_OFFSET) {
336 bio->bi_bdev = root->fs_info->sb->s_bdev;
337 submit_bio(rw, bio);
338 return 0;
339 }
340 return btrfs_map_bio(BTRFS_I(inode)->root, rw, bio);
341}
342
0da5468f
CM
343static int btree_writepage(struct page *page, struct writeback_control *wbc)
344{
d1310b2e
CM
345 struct extent_io_tree *tree;
346 tree = &BTRFS_I(page->mapping->host)->io_tree;
5f39d397
CM
347 return extent_write_full_page(tree, page, btree_get_extent, wbc);
348}
0da5468f
CM
349
350static int btree_writepages(struct address_space *mapping,
351 struct writeback_control *wbc)
352{
d1310b2e
CM
353 struct extent_io_tree *tree;
354 tree = &BTRFS_I(mapping->host)->io_tree;
d8d5f3e1 355 if (wbc->sync_mode == WB_SYNC_NONE) {
793955bc
CM
356 u64 num_dirty;
357 u64 start = 0;
358 unsigned long thresh = 96 * 1024 * 1024;
448d640b
CM
359
360 if (wbc->for_kupdate)
361 return 0;
362
ca664626
CM
363 if (current_is_pdflush()) {
364 thresh = 96 * 1024 * 1024;
365 } else {
366 thresh = 8 * 1024 * 1024;
367 }
1832a6d5
CM
368 num_dirty = count_range_bits(tree, &start, (u64)-1,
369 thresh, EXTENT_DIRTY);
793955bc
CM
370 if (num_dirty < thresh) {
371 return 0;
372 }
373 }
0da5468f
CM
374 return extent_writepages(tree, mapping, btree_get_extent, wbc);
375}
376
5f39d397
CM
377int btree_readpage(struct file *file, struct page *page)
378{
d1310b2e
CM
379 struct extent_io_tree *tree;
380 tree = &BTRFS_I(page->mapping->host)->io_tree;
5f39d397
CM
381 return extent_read_full_page(tree, page, btree_get_extent);
382}
22b0ebda 383
70dec807 384static int btree_releasepage(struct page *page, gfp_t gfp_flags)
5f39d397 385{
d1310b2e
CM
386 struct extent_io_tree *tree;
387 struct extent_map_tree *map;
5f39d397 388 int ret;
d98237b3 389
d1310b2e
CM
390 tree = &BTRFS_I(page->mapping->host)->io_tree;
391 map = &BTRFS_I(page->mapping->host)->extent_tree;
70dec807 392 ret = try_release_extent_mapping(map, tree, page, gfp_flags);
5f39d397 393 if (ret == 1) {
728131d8 394 invalidate_extent_lru(tree, page_offset(page), PAGE_CACHE_SIZE);
5f39d397
CM
395 ClearPagePrivate(page);
396 set_page_private(page, 0);
397 page_cache_release(page);
398 }
d98237b3
CM
399 return ret;
400}
401
5f39d397 402static void btree_invalidatepage(struct page *page, unsigned long offset)
d98237b3 403{
d1310b2e
CM
404 struct extent_io_tree *tree;
405 tree = &BTRFS_I(page->mapping->host)->io_tree;
5f39d397
CM
406 extent_invalidatepage(tree, page, offset);
407 btree_releasepage(page, GFP_NOFS);
d98237b3
CM
408}
409
5f39d397 410#if 0
d98237b3 411static int btree_writepage(struct page *page, struct writeback_control *wbc)
ed2ff2cb 412{
87cbda5c 413 struct buffer_head *bh;
0f7d52f4 414 struct btrfs_root *root = BTRFS_I(page->mapping->host)->root;
87cbda5c 415 struct buffer_head *head;
87cbda5c
CM
416 if (!page_has_buffers(page)) {
417 create_empty_buffers(page, root->fs_info->sb->s_blocksize,
418 (1 << BH_Dirty)|(1 << BH_Uptodate));
419 }
420 head = page_buffers(page);
421 bh = head;
422 do {
423 if (buffer_dirty(bh))
424 csum_tree_block(root, bh, 0);
425 bh = bh->b_this_page;
426 } while (bh != head);
d98237b3 427 return block_write_full_page(page, btree_get_block, wbc);
ed2ff2cb 428}
5f39d397 429#endif
eb60ceac 430
d98237b3
CM
431static struct address_space_operations btree_aops = {
432 .readpage = btree_readpage,
433 .writepage = btree_writepage,
0da5468f 434 .writepages = btree_writepages,
5f39d397
CM
435 .releasepage = btree_releasepage,
436 .invalidatepage = btree_invalidatepage,
d98237b3
CM
437 .sync_page = block_sync_page,
438};
439
db94535d 440int readahead_tree_block(struct btrfs_root *root, u64 bytenr, u32 blocksize)
090d1875 441{
5f39d397
CM
442 struct extent_buffer *buf = NULL;
443 struct inode *btree_inode = root->fs_info->btree_inode;
de428b63 444 int ret = 0;
090d1875 445
db94535d 446 buf = btrfs_find_create_tree_block(root, bytenr, blocksize);
5f39d397 447 if (!buf)
090d1875 448 return 0;
d1310b2e 449 read_extent_buffer_pages(&BTRFS_I(btree_inode)->io_tree,
a86c12c7 450 buf, 0, 0, btree_get_extent);
5f39d397 451 free_extent_buffer(buf);
de428b63 452 return ret;
090d1875
CM
453}
454
0b86a832
CM
455static int close_all_devices(struct btrfs_fs_info *fs_info)
456{
457 struct list_head *list;
458 struct list_head *next;
459 struct btrfs_device *device;
460
8a4b83cc
CM
461 list = &fs_info->fs_devices->devices;
462 list_for_each(next, list) {
0b86a832 463 device = list_entry(next, struct btrfs_device, dev_list);
8a4b83cc
CM
464 if (device->bdev && device->bdev != fs_info->sb->s_bdev)
465 close_bdev_excl(device->bdev);
466 device->bdev = NULL;
0b86a832
CM
467 }
468 return 0;
469}
470
0999df54
CM
471int btrfs_verify_block_csum(struct btrfs_root *root,
472 struct extent_buffer *buf)
eb60ceac 473{
ce9adaa5 474 return btrfs_buffer_uptodate(buf);
0999df54
CM
475}
476
477struct extent_buffer *btrfs_find_tree_block(struct btrfs_root *root,
478 u64 bytenr, u32 blocksize)
479{
480 struct inode *btree_inode = root->fs_info->btree_inode;
481 struct extent_buffer *eb;
482 eb = find_extent_buffer(&BTRFS_I(btree_inode)->io_tree,
483 bytenr, blocksize, GFP_NOFS);
484 return eb;
485}
486
487struct extent_buffer *btrfs_find_create_tree_block(struct btrfs_root *root,
488 u64 bytenr, u32 blocksize)
489{
490 struct inode *btree_inode = root->fs_info->btree_inode;
491 struct extent_buffer *eb;
492
493 eb = alloc_extent_buffer(&BTRFS_I(btree_inode)->io_tree,
494 bytenr, blocksize, NULL, GFP_NOFS);
495 return eb;
496}
497
498
499struct extent_buffer *read_tree_block(struct btrfs_root *root, u64 bytenr,
500 u32 blocksize)
501{
502 struct extent_buffer *buf = NULL;
503 struct inode *btree_inode = root->fs_info->btree_inode;
504 struct extent_io_tree *io_tree;
505 int ret;
506
507 io_tree = &BTRFS_I(btree_inode)->io_tree;
508
509 buf = btrfs_find_create_tree_block(root, bytenr, blocksize);
510 if (!buf)
511 return NULL;
0999df54 512
ce9adaa5
CM
513 ret = read_extent_buffer_pages(&BTRFS_I(btree_inode)->io_tree, buf, 0,
514 1, btree_get_extent);
515
516 if (ret == 0) {
517 buf->flags |= EXTENT_UPTODATE;
518 }
5f39d397 519 return buf;
ce9adaa5 520
eb60ceac
CM
521}
522
e089f05c 523int clean_tree_block(struct btrfs_trans_handle *trans, struct btrfs_root *root,
5f39d397 524 struct extent_buffer *buf)
ed2ff2cb 525{
5f39d397 526 struct inode *btree_inode = root->fs_info->btree_inode;
55c69072
CM
527 if (btrfs_header_generation(buf) ==
528 root->fs_info->running_transaction->transid)
d1310b2e 529 clear_extent_buffer_dirty(&BTRFS_I(btree_inode)->io_tree,
55c69072 530 buf);
5f39d397
CM
531 return 0;
532}
533
534int wait_on_tree_block_writeback(struct btrfs_root *root,
535 struct extent_buffer *buf)
536{
537 struct inode *btree_inode = root->fs_info->btree_inode;
d1310b2e 538 wait_on_extent_buffer_writeback(&BTRFS_I(btree_inode)->io_tree,
5f39d397
CM
539 buf);
540 return 0;
541}
542
db94535d 543static int __setup_root(u32 nodesize, u32 leafsize, u32 sectorsize,
87ee04eb 544 u32 stripesize, struct btrfs_root *root,
9f5fae2f 545 struct btrfs_fs_info *fs_info,
e20d96d6 546 u64 objectid)
d97e63b6 547{
cfaa7295 548 root->node = NULL;
0f7d52f4 549 root->inode = NULL;
a28ec197 550 root->commit_root = NULL;
db94535d
CM
551 root->sectorsize = sectorsize;
552 root->nodesize = nodesize;
553 root->leafsize = leafsize;
87ee04eb 554 root->stripesize = stripesize;
123abc88 555 root->ref_cows = 0;
0b86a832
CM
556 root->track_dirty = 0;
557
9f5fae2f 558 root->fs_info = fs_info;
0f7d52f4
CM
559 root->objectid = objectid;
560 root->last_trans = 0;
1b05da2e
CM
561 root->highest_inode = 0;
562 root->last_inode_alloc = 0;
58176a96 563 root->name = NULL;
4313b399 564 root->in_sysfs = 0;
0b86a832
CM
565
566 INIT_LIST_HEAD(&root->dirty_list);
3768f368
CM
567 memset(&root->root_key, 0, sizeof(root->root_key));
568 memset(&root->root_item, 0, sizeof(root->root_item));
6702ed49 569 memset(&root->defrag_progress, 0, sizeof(root->defrag_progress));
58176a96
JB
570 memset(&root->root_kobj, 0, sizeof(root->root_kobj));
571 init_completion(&root->kobj_unregister);
6702ed49
CM
572 root->defrag_running = 0;
573 root->defrag_level = 0;
4d775673 574 root->root_key.objectid = objectid;
3768f368
CM
575 return 0;
576}
577
db94535d 578static int find_and_setup_root(struct btrfs_root *tree_root,
9f5fae2f
CM
579 struct btrfs_fs_info *fs_info,
580 u64 objectid,
e20d96d6 581 struct btrfs_root *root)
3768f368
CM
582{
583 int ret;
db94535d 584 u32 blocksize;
3768f368 585
db94535d 586 __setup_root(tree_root->nodesize, tree_root->leafsize,
87ee04eb
CM
587 tree_root->sectorsize, tree_root->stripesize,
588 root, fs_info, objectid);
3768f368
CM
589 ret = btrfs_find_last_root(tree_root, objectid,
590 &root->root_item, &root->root_key);
591 BUG_ON(ret);
592
db94535d
CM
593 blocksize = btrfs_level_size(root, btrfs_root_level(&root->root_item));
594 root->node = read_tree_block(root, btrfs_root_bytenr(&root->root_item),
595 blocksize);
3768f368 596 BUG_ON(!root->node);
d97e63b6
CM
597 return 0;
598}
599
5eda7b5e
CM
600struct btrfs_root *btrfs_read_fs_root_no_radix(struct btrfs_fs_info *fs_info,
601 struct btrfs_key *location)
0f7d52f4
CM
602{
603 struct btrfs_root *root;
604 struct btrfs_root *tree_root = fs_info->tree_root;
605 struct btrfs_path *path;
5f39d397 606 struct extent_buffer *l;
1b05da2e 607 u64 highest_inode;
db94535d 608 u32 blocksize;
0f7d52f4
CM
609 int ret = 0;
610
5eda7b5e 611 root = kzalloc(sizeof(*root), GFP_NOFS);
0cf6c620 612 if (!root)
0f7d52f4 613 return ERR_PTR(-ENOMEM);
0f7d52f4 614 if (location->offset == (u64)-1) {
db94535d 615 ret = find_and_setup_root(tree_root, fs_info,
0f7d52f4
CM
616 location->objectid, root);
617 if (ret) {
0f7d52f4
CM
618 kfree(root);
619 return ERR_PTR(ret);
620 }
621 goto insert;
622 }
623
db94535d 624 __setup_root(tree_root->nodesize, tree_root->leafsize,
87ee04eb
CM
625 tree_root->sectorsize, tree_root->stripesize,
626 root, fs_info, location->objectid);
0f7d52f4
CM
627
628 path = btrfs_alloc_path();
629 BUG_ON(!path);
630 ret = btrfs_search_slot(NULL, tree_root, location, path, 0, 0);
631 if (ret != 0) {
0f7d52f4
CM
632 if (ret > 0)
633 ret = -ENOENT;
634 goto out;
635 }
5f39d397
CM
636 l = path->nodes[0];
637 read_extent_buffer(l, &root->root_item,
638 btrfs_item_ptr_offset(l, path->slots[0]),
0f7d52f4 639 sizeof(root->root_item));
44b36eb2 640 memcpy(&root->root_key, location, sizeof(*location));
0f7d52f4
CM
641 ret = 0;
642out:
643 btrfs_release_path(root, path);
644 btrfs_free_path(path);
645 if (ret) {
646 kfree(root);
647 return ERR_PTR(ret);
648 }
db94535d
CM
649 blocksize = btrfs_level_size(root, btrfs_root_level(&root->root_item));
650 root->node = read_tree_block(root, btrfs_root_bytenr(&root->root_item),
651 blocksize);
0f7d52f4
CM
652 BUG_ON(!root->node);
653insert:
0f7d52f4 654 root->ref_cows = 1;
5eda7b5e
CM
655 ret = btrfs_find_highest_inode(root, &highest_inode);
656 if (ret == 0) {
657 root->highest_inode = highest_inode;
658 root->last_inode_alloc = highest_inode;
659 }
660 return root;
661}
662
dc17ff8f
CM
663struct btrfs_root *btrfs_lookup_fs_root(struct btrfs_fs_info *fs_info,
664 u64 root_objectid)
665{
666 struct btrfs_root *root;
667
668 if (root_objectid == BTRFS_ROOT_TREE_OBJECTID)
669 return fs_info->tree_root;
670 if (root_objectid == BTRFS_EXTENT_TREE_OBJECTID)
671 return fs_info->extent_root;
672
673 root = radix_tree_lookup(&fs_info->fs_roots_radix,
674 (unsigned long)root_objectid);
675 return root;
676}
677
edbd8d4e
CM
678struct btrfs_root *btrfs_read_fs_root_no_name(struct btrfs_fs_info *fs_info,
679 struct btrfs_key *location)
5eda7b5e
CM
680{
681 struct btrfs_root *root;
682 int ret;
683
edbd8d4e
CM
684 if (location->objectid == BTRFS_ROOT_TREE_OBJECTID)
685 return fs_info->tree_root;
686 if (location->objectid == BTRFS_EXTENT_TREE_OBJECTID)
687 return fs_info->extent_root;
688
5eda7b5e
CM
689 root = radix_tree_lookup(&fs_info->fs_roots_radix,
690 (unsigned long)location->objectid);
691 if (root)
692 return root;
693
694 root = btrfs_read_fs_root_no_radix(fs_info, location);
695 if (IS_ERR(root))
696 return root;
2619ba1f
CM
697 ret = radix_tree_insert(&fs_info->fs_roots_radix,
698 (unsigned long)root->root_key.objectid,
0f7d52f4
CM
699 root);
700 if (ret) {
5f39d397 701 free_extent_buffer(root->node);
0f7d52f4
CM
702 kfree(root);
703 return ERR_PTR(ret);
704 }
edbd8d4e
CM
705 ret = btrfs_find_dead_roots(fs_info->tree_root,
706 root->root_key.objectid, root);
707 BUG_ON(ret);
708
709 return root;
710}
711
712struct btrfs_root *btrfs_read_fs_root(struct btrfs_fs_info *fs_info,
713 struct btrfs_key *location,
714 const char *name, int namelen)
715{
716 struct btrfs_root *root;
717 int ret;
718
719 root = btrfs_read_fs_root_no_name(fs_info, location);
720 if (!root)
721 return NULL;
58176a96 722
4313b399
CM
723 if (root->in_sysfs)
724 return root;
725
58176a96
JB
726 ret = btrfs_set_root_name(root, name, namelen);
727 if (ret) {
5f39d397 728 free_extent_buffer(root->node);
58176a96
JB
729 kfree(root);
730 return ERR_PTR(ret);
731 }
732
733 ret = btrfs_sysfs_add_root(root);
734 if (ret) {
5f39d397 735 free_extent_buffer(root->node);
58176a96
JB
736 kfree(root->name);
737 kfree(root);
738 return ERR_PTR(ret);
739 }
4313b399 740 root->in_sysfs = 1;
0f7d52f4
CM
741 return root;
742}
19c00ddc
CM
743#if 0
744static int add_hasher(struct btrfs_fs_info *info, char *type) {
745 struct btrfs_hasher *hasher;
746
747 hasher = kmalloc(sizeof(*hasher), GFP_NOFS);
748 if (!hasher)
749 return -ENOMEM;
750 hasher->hash_tfm = crypto_alloc_hash(type, 0, CRYPTO_ALG_ASYNC);
751 if (!hasher->hash_tfm) {
752 kfree(hasher);
753 return -EINVAL;
754 }
755 spin_lock(&info->hash_lock);
756 list_add(&hasher->list, &info->hashers);
757 spin_unlock(&info->hash_lock);
758 return 0;
759}
760#endif
04160088
CM
761
762static int btrfs_congested_fn(void *congested_data, int bdi_bits)
763{
764 struct btrfs_fs_info *info = (struct btrfs_fs_info *)congested_data;
765 int ret = 0;
766 struct list_head *cur;
767 struct btrfs_device *device;
768 struct backing_dev_info *bdi;
769
770 list_for_each(cur, &info->fs_devices->devices) {
771 device = list_entry(cur, struct btrfs_device, dev_list);
772 bdi = blk_get_backing_dev_info(device->bdev);
773 if (bdi && bdi_congested(bdi, bdi_bits)) {
774 ret = 1;
775 break;
776 }
777 }
778 return ret;
779}
780
781void btrfs_unplug_io_fn(struct backing_dev_info *bdi, struct page *page)
782{
783 struct list_head *cur;
784 struct btrfs_device *device;
785 struct btrfs_fs_info *info;
786
787 info = (struct btrfs_fs_info *)bdi->unplug_io_data;
788 list_for_each(cur, &info->fs_devices->devices) {
789 device = list_entry(cur, struct btrfs_device, dev_list);
790 bdi = blk_get_backing_dev_info(device->bdev);
791 if (bdi->unplug_io_fn) {
792 bdi->unplug_io_fn(bdi, page);
793 }
794 }
795}
796
797static int setup_bdi(struct btrfs_fs_info *info, struct backing_dev_info *bdi)
798{
799 bdi_init(bdi);
83041add 800 bdi->ra_pages = default_backing_dev_info.ra_pages * 4;
04160088
CM
801 bdi->state = 0;
802 bdi->capabilities = default_backing_dev_info.capabilities;
803 bdi->unplug_io_fn = btrfs_unplug_io_fn;
804 bdi->unplug_io_data = info;
805 bdi->congested_fn = btrfs_congested_fn;
806 bdi->congested_data = info;
807 return 0;
808}
809
ce9adaa5
CM
810static int bio_ready_for_csum(struct bio *bio)
811{
812 u64 length = 0;
813 u64 buf_len = 0;
814 u64 start = 0;
815 struct page *page;
816 struct extent_io_tree *io_tree = NULL;
817 struct btrfs_fs_info *info = NULL;
818 struct bio_vec *bvec;
819 int i;
820 int ret;
821
822 bio_for_each_segment(bvec, bio, i) {
823 page = bvec->bv_page;
824 if (page->private == EXTENT_PAGE_PRIVATE) {
825 length += bvec->bv_len;
826 continue;
827 }
828 if (!page->private) {
829 length += bvec->bv_len;
830 continue;
831 }
832 length = bvec->bv_len;
833 buf_len = page->private >> 2;
834 start = page_offset(page) + bvec->bv_offset;
835 io_tree = &BTRFS_I(page->mapping->host)->io_tree;
836 info = BTRFS_I(page->mapping->host)->root->fs_info;
837 }
838 /* are we fully contained in this bio? */
839 if (buf_len <= length)
840 return 1;
841
842 ret = extent_range_uptodate(io_tree, start + length,
843 start + buf_len - 1);
844 if (ret == 1)
845 return ret;
846 return ret;
847}
848
849#if LINUX_VERSION_CODE <= KERNEL_VERSION(2,6,18)
850void btrfs_end_io_csum(void *p)
851#else
852void btrfs_end_io_csum(struct work_struct *work)
853#endif
854{
855#if LINUX_VERSION_CODE <= KERNEL_VERSION(2,6,18)
856 struct btrfs_fs_info *fs_info = p;
857#else
858 struct btrfs_fs_info *fs_info = container_of(work,
859 struct btrfs_fs_info,
860 end_io_work);
861#endif
862 unsigned long flags;
863 struct end_io_wq *end_io_wq;
864 struct bio *bio;
865 struct list_head *next;
866 int error;
867 int was_empty;
868
869 while(1) {
870 spin_lock_irqsave(&fs_info->end_io_work_lock, flags);
871 if (list_empty(&fs_info->end_io_work_list)) {
872 spin_unlock_irqrestore(&fs_info->end_io_work_lock,
873 flags);
874 return;
875 }
876 next = fs_info->end_io_work_list.next;
877 list_del(next);
878 spin_unlock_irqrestore(&fs_info->end_io_work_lock, flags);
879
880 end_io_wq = list_entry(next, struct end_io_wq, list);
881
882 bio = end_io_wq->bio;
883 if (!bio_ready_for_csum(bio)) {
884 spin_lock_irqsave(&fs_info->end_io_work_lock, flags);
885 was_empty = list_empty(&fs_info->end_io_work_list);
886 list_add_tail(&end_io_wq->list,
887 &fs_info->end_io_work_list);
888 spin_unlock_irqrestore(&fs_info->end_io_work_lock,
889 flags);
890 if (was_empty)
891 return;
892 continue;
893 }
894 error = end_io_wq->error;
895 bio->bi_private = end_io_wq->private;
896 bio->bi_end_io = end_io_wq->end_io;
897 kfree(end_io_wq);
898 bio_endio(bio, error);
899 }
900}
901
902
8a4b83cc
CM
903struct btrfs_root *open_ctree(struct super_block *sb,
904 struct btrfs_fs_devices *fs_devices)
2e635a27 905{
db94535d
CM
906 u32 sectorsize;
907 u32 nodesize;
908 u32 leafsize;
909 u32 blocksize;
87ee04eb 910 u32 stripesize;
e20d96d6
CM
911 struct btrfs_root *extent_root = kmalloc(sizeof(struct btrfs_root),
912 GFP_NOFS);
913 struct btrfs_root *tree_root = kmalloc(sizeof(struct btrfs_root),
914 GFP_NOFS);
8790d502 915 struct btrfs_fs_info *fs_info = kzalloc(sizeof(*fs_info),
e20d96d6 916 GFP_NOFS);
0b86a832
CM
917 struct btrfs_root *chunk_root = kmalloc(sizeof(struct btrfs_root),
918 GFP_NOFS);
919 struct btrfs_root *dev_root = kmalloc(sizeof(struct btrfs_root),
920 GFP_NOFS);
eb60ceac 921 int ret;
e58ca020 922 int err = -EINVAL;
2c90e5d6 923 struct btrfs_super_block *disk_super;
8790d502 924
39279cc3
CM
925 if (!extent_root || !tree_root || !fs_info) {
926 err = -ENOMEM;
927 goto fail;
928 }
ce9adaa5
CM
929 end_io_workqueue = create_workqueue("btrfs-end-io");
930 BUG_ON(!end_io_workqueue);
931
0f7d52f4 932 INIT_RADIX_TREE(&fs_info->fs_roots_radix, GFP_NOFS);
8fd17795 933 INIT_LIST_HEAD(&fs_info->trans_list);
facda1e7 934 INIT_LIST_HEAD(&fs_info->dead_roots);
19c00ddc 935 INIT_LIST_HEAD(&fs_info->hashers);
ce9adaa5 936 INIT_LIST_HEAD(&fs_info->end_io_work_list);
19c00ddc 937 spin_lock_init(&fs_info->hash_lock);
ce9adaa5 938 spin_lock_init(&fs_info->end_io_work_lock);
1832a6d5 939 spin_lock_init(&fs_info->delalloc_lock);
cee36a03 940 spin_lock_init(&fs_info->new_trans_lock);
19c00ddc 941
58176a96 942 init_completion(&fs_info->kobj_unregister);
2c90e5d6 943 sb_set_blocksize(sb, 4096);
9f5fae2f
CM
944 fs_info->tree_root = tree_root;
945 fs_info->extent_root = extent_root;
0b86a832
CM
946 fs_info->chunk_root = chunk_root;
947 fs_info->dev_root = dev_root;
8a4b83cc 948 fs_info->fs_devices = fs_devices;
0b86a832 949 INIT_LIST_HEAD(&fs_info->dirty_cowonly_roots);
6324fbf3 950 INIT_LIST_HEAD(&fs_info->space_info);
0b86a832 951 btrfs_mapping_init(&fs_info->mapping_tree);
e20d96d6 952 fs_info->sb = sb;
c59f8951 953 fs_info->max_extent = (u64)-1;
6f568d35 954 fs_info->max_inline = 8192 * 1024;
04160088 955 setup_bdi(fs_info, &fs_info->bdi);
d98237b3
CM
956 fs_info->btree_inode = new_inode(sb);
957 fs_info->btree_inode->i_ino = 1;
2c90e5d6 958 fs_info->btree_inode->i_nlink = 1;
d98237b3
CM
959 fs_info->btree_inode->i_size = sb->s_bdev->bd_inode->i_size;
960 fs_info->btree_inode->i_mapping->a_ops = &btree_aops;
04160088
CM
961 fs_info->btree_inode->i_mapping->backing_dev_info = &fs_info->bdi;
962
d1310b2e 963 extent_io_tree_init(&BTRFS_I(fs_info->btree_inode)->io_tree,
5f39d397
CM
964 fs_info->btree_inode->i_mapping,
965 GFP_NOFS);
d1310b2e
CM
966 extent_map_tree_init(&BTRFS_I(fs_info->btree_inode)->extent_tree,
967 GFP_NOFS);
968
969 BTRFS_I(fs_info->btree_inode)->io_tree.ops = &btree_extent_io_ops;
0da5468f 970
d1310b2e 971 extent_io_tree_init(&fs_info->free_space_cache,
f510cfec 972 fs_info->btree_inode->i_mapping, GFP_NOFS);
d1310b2e 973 extent_io_tree_init(&fs_info->block_group_cache,
96b5179d 974 fs_info->btree_inode->i_mapping, GFP_NOFS);
d1310b2e 975 extent_io_tree_init(&fs_info->pinned_extents,
1a5bc167 976 fs_info->btree_inode->i_mapping, GFP_NOFS);
d1310b2e 977 extent_io_tree_init(&fs_info->pending_del,
1a5bc167 978 fs_info->btree_inode->i_mapping, GFP_NOFS);
d1310b2e 979 extent_io_tree_init(&fs_info->extent_ins,
1a5bc167 980 fs_info->btree_inode->i_mapping, GFP_NOFS);
e66f709b 981 fs_info->do_barriers = 1;
e18e4809 982
ce9adaa5 983 INIT_WORK(&fs_info->end_io_work, btrfs_end_io_csum);
6da6abae
CM
984#if LINUX_VERSION_CODE <= KERNEL_VERSION(2,6,18)
985 INIT_WORK(&fs_info->trans_work, btrfs_transaction_cleaner, fs_info);
986#else
08607c1b 987 INIT_DELAYED_WORK(&fs_info->trans_work, btrfs_transaction_cleaner);
6da6abae 988#endif
0f7d52f4
CM
989 BTRFS_I(fs_info->btree_inode)->root = tree_root;
990 memset(&BTRFS_I(fs_info->btree_inode)->location, 0,
991 sizeof(struct btrfs_key));
22b0ebda 992 insert_inode_hash(fs_info->btree_inode);
d98237b3 993 mapping_set_gfp_mask(fs_info->btree_inode->i_mapping, GFP_NOFS);
39279cc3 994
79154b1b 995 mutex_init(&fs_info->trans_mutex);
d561c025 996 mutex_init(&fs_info->fs_mutex);
3768f368 997
19c00ddc
CM
998#if 0
999 ret = add_hasher(fs_info, "crc32c");
1000 if (ret) {
1001 printk("btrfs: failed hash setup, modprobe cryptomgr?\n");
1002 err = -ENOMEM;
1003 goto fail_iput;
1004 }
1005#endif
0b86a832 1006 __setup_root(4096, 4096, 4096, 4096, tree_root,
2c90e5d6 1007 fs_info, BTRFS_ROOT_TREE_OBJECTID);
7eccb903 1008
2c90e5d6 1009 fs_info->sb_buffer = read_tree_block(tree_root,
db94535d 1010 BTRFS_SUPER_INFO_OFFSET,
0b86a832 1011 4096);
d98237b3 1012
0f7d52f4 1013 if (!fs_info->sb_buffer)
39279cc3 1014 goto fail_iput;
39279cc3 1015
5f39d397
CM
1016 read_extent_buffer(fs_info->sb_buffer, &fs_info->super_copy, 0,
1017 sizeof(fs_info->super_copy));
1018
1019 read_extent_buffer(fs_info->sb_buffer, fs_info->fsid,
1020 (unsigned long)btrfs_super_fsid(fs_info->sb_buffer),
1021 BTRFS_FSID_SIZE);
0b86a832 1022
5f39d397 1023 disk_super = &fs_info->super_copy;
0f7d52f4 1024 if (!btrfs_super_root(disk_super))
39279cc3 1025 goto fail_sb_buffer;
0f7d52f4 1026
8a4b83cc
CM
1027 if (btrfs_super_num_devices(disk_super) != fs_devices->num_devices) {
1028 printk("Btrfs: wanted %llu devices, but found %llu\n",
1029 (unsigned long long)btrfs_super_num_devices(disk_super),
1030 (unsigned long long)fs_devices->num_devices);
1031 goto fail_sb_buffer;
1032 }
db94535d
CM
1033 nodesize = btrfs_super_nodesize(disk_super);
1034 leafsize = btrfs_super_leafsize(disk_super);
1035 sectorsize = btrfs_super_sectorsize(disk_super);
87ee04eb 1036 stripesize = btrfs_super_stripesize(disk_super);
db94535d
CM
1037 tree_root->nodesize = nodesize;
1038 tree_root->leafsize = leafsize;
1039 tree_root->sectorsize = sectorsize;
87ee04eb 1040 tree_root->stripesize = stripesize;
ff79f819 1041 sb_set_blocksize(sb, sectorsize);
db94535d 1042
8352d8a4 1043 i_size_write(fs_info->btree_inode,
db94535d 1044 btrfs_super_total_bytes(disk_super));
8352d8a4 1045
39279cc3
CM
1046 if (strncmp((char *)(&disk_super->magic), BTRFS_MAGIC,
1047 sizeof(disk_super->magic))) {
1048 printk("btrfs: valid FS not found on %s\n", sb->s_id);
1049 goto fail_sb_buffer;
1050 }
19c00ddc 1051
0b86a832 1052 mutex_lock(&fs_info->fs_mutex);
0d81ba5d 1053
0b86a832
CM
1054 ret = btrfs_read_sys_array(tree_root);
1055 BUG_ON(ret);
1056
1057 blocksize = btrfs_level_size(tree_root,
1058 btrfs_super_chunk_root_level(disk_super));
1059
1060 __setup_root(nodesize, leafsize, sectorsize, stripesize,
1061 chunk_root, fs_info, BTRFS_CHUNK_TREE_OBJECTID);
1062
1063 chunk_root->node = read_tree_block(chunk_root,
1064 btrfs_super_chunk_root(disk_super),
1065 blocksize);
1066 BUG_ON(!chunk_root->node);
1067
1068 ret = btrfs_read_chunk_tree(chunk_root);
1069 BUG_ON(ret);
1070
db94535d
CM
1071 blocksize = btrfs_level_size(tree_root,
1072 btrfs_super_root_level(disk_super));
19c00ddc 1073
0b86a832 1074
e20d96d6 1075 tree_root->node = read_tree_block(tree_root,
db94535d
CM
1076 btrfs_super_root(disk_super),
1077 blocksize);
39279cc3
CM
1078 if (!tree_root->node)
1079 goto fail_sb_buffer;
3768f368 1080
db94535d
CM
1081
1082 ret = find_and_setup_root(tree_root, fs_info,
e20d96d6 1083 BTRFS_EXTENT_TREE_OBJECTID, extent_root);
0b86a832 1084 if (ret)
39279cc3 1085 goto fail_tree_root;
0b86a832
CM
1086 extent_root->track_dirty = 1;
1087
1088 ret = find_and_setup_root(tree_root, fs_info,
1089 BTRFS_DEV_TREE_OBJECTID, dev_root);
1090 dev_root->track_dirty = 1;
1091
1092 if (ret)
1093 goto fail_extent_root;
3768f368 1094
9078a3e1
CM
1095 btrfs_read_block_groups(extent_root);
1096
0f7d52f4 1097 fs_info->generation = btrfs_super_generation(disk_super) + 1;
d18a2c44
CM
1098 fs_info->data_alloc_profile = (u64)-1;
1099 fs_info->metadata_alloc_profile = (u64)-1;
1100 fs_info->system_alloc_profile = fs_info->metadata_alloc_profile;
1101
5be6f7f1 1102 mutex_unlock(&fs_info->fs_mutex);
0f7d52f4 1103 return tree_root;
39279cc3 1104
0b86a832
CM
1105fail_extent_root:
1106 free_extent_buffer(extent_root->node);
39279cc3 1107fail_tree_root:
0b86a832 1108 mutex_unlock(&fs_info->fs_mutex);
5f39d397 1109 free_extent_buffer(tree_root->node);
39279cc3 1110fail_sb_buffer:
5f39d397 1111 free_extent_buffer(fs_info->sb_buffer);
2d2ae547 1112 extent_io_tree_empty_lru(&BTRFS_I(fs_info->btree_inode)->io_tree);
39279cc3
CM
1113fail_iput:
1114 iput(fs_info->btree_inode);
1115fail:
8a4b83cc 1116 close_all_devices(fs_info);
39279cc3
CM
1117 kfree(extent_root);
1118 kfree(tree_root);
2d2ae547 1119 bdi_destroy(&fs_info->bdi);
39279cc3
CM
1120 kfree(fs_info);
1121 return ERR_PTR(err);
eb60ceac
CM
1122}
1123
e089f05c 1124int write_ctree_super(struct btrfs_trans_handle *trans, struct btrfs_root
79154b1b 1125 *root)
eb60ceac 1126{
e66f709b 1127 int ret;
5f39d397
CM
1128 struct extent_buffer *super = root->fs_info->sb_buffer;
1129 struct inode *btree_inode = root->fs_info->btree_inode;
21ad10cf 1130 struct super_block *sb = root->fs_info->sb;
5f39d397 1131
21ad10cf
CM
1132 if (!btrfs_test_opt(root, NOBARRIER))
1133 blkdev_issue_flush(sb->s_bdev, NULL);
d1310b2e 1134 set_extent_buffer_dirty(&BTRFS_I(btree_inode)->io_tree, super);
5f39d397
CM
1135 ret = sync_page_range_nolock(btree_inode, btree_inode->i_mapping,
1136 super->start, super->len);
21ad10cf
CM
1137 if (!btrfs_test_opt(root, NOBARRIER))
1138 blkdev_issue_flush(sb->s_bdev, NULL);
5f39d397 1139 return ret;
cfaa7295
CM
1140}
1141
5eda7b5e 1142int btrfs_free_fs_root(struct btrfs_fs_info *fs_info, struct btrfs_root *root)
2619ba1f
CM
1143{
1144 radix_tree_delete(&fs_info->fs_roots_radix,
1145 (unsigned long)root->root_key.objectid);
b99aa6cb
CM
1146 if (root->in_sysfs)
1147 btrfs_sysfs_del_root(root);
2619ba1f
CM
1148 if (root->inode)
1149 iput(root->inode);
1150 if (root->node)
5f39d397 1151 free_extent_buffer(root->node);
2619ba1f 1152 if (root->commit_root)
5f39d397 1153 free_extent_buffer(root->commit_root);
58176a96
JB
1154 if (root->name)
1155 kfree(root->name);
2619ba1f
CM
1156 kfree(root);
1157 return 0;
1158}
1159
35b7e476 1160static int del_fs_roots(struct btrfs_fs_info *fs_info)
0f7d52f4
CM
1161{
1162 int ret;
1163 struct btrfs_root *gang[8];
1164 int i;
1165
1166 while(1) {
1167 ret = radix_tree_gang_lookup(&fs_info->fs_roots_radix,
1168 (void **)gang, 0,
1169 ARRAY_SIZE(gang));
1170 if (!ret)
1171 break;
2619ba1f 1172 for (i = 0; i < ret; i++)
5eda7b5e 1173 btrfs_free_fs_root(fs_info, gang[i]);
0f7d52f4
CM
1174 }
1175 return 0;
1176}
b4100d64 1177
e20d96d6 1178int close_ctree(struct btrfs_root *root)
cfaa7295 1179{
3768f368 1180 int ret;
e089f05c 1181 struct btrfs_trans_handle *trans;
0f7d52f4 1182 struct btrfs_fs_info *fs_info = root->fs_info;
e089f05c 1183
facda1e7 1184 fs_info->closing = 1;
08607c1b 1185 btrfs_transaction_flush_work(root);
0f7d52f4 1186 mutex_lock(&fs_info->fs_mutex);
6702ed49 1187 btrfs_defrag_dirty_roots(root->fs_info);
79154b1b 1188 trans = btrfs_start_transaction(root, 1);
54aa1f4d 1189 ret = btrfs_commit_transaction(trans, root);
79154b1b
CM
1190 /* run commit again to drop the original snapshot */
1191 trans = btrfs_start_transaction(root, 1);
1192 btrfs_commit_transaction(trans, root);
1193 ret = btrfs_write_and_wait_transaction(NULL, root);
3768f368 1194 BUG_ON(ret);
79154b1b 1195 write_ctree_super(NULL, root);
0f7d52f4
CM
1196 mutex_unlock(&fs_info->fs_mutex);
1197
b0c68f8b
CM
1198 if (fs_info->delalloc_bytes) {
1199 printk("btrfs: at unmount delalloc count %Lu\n",
1200 fs_info->delalloc_bytes);
1201 }
0f7d52f4 1202 if (fs_info->extent_root->node)
5f39d397 1203 free_extent_buffer(fs_info->extent_root->node);
f510cfec 1204
0f7d52f4 1205 if (fs_info->tree_root->node)
5f39d397 1206 free_extent_buffer(fs_info->tree_root->node);
f510cfec 1207
0b86a832
CM
1208 if (root->fs_info->chunk_root->node);
1209 free_extent_buffer(root->fs_info->chunk_root->node);
1210
1211 if (root->fs_info->dev_root->node);
1212 free_extent_buffer(root->fs_info->dev_root->node);
1213
5f39d397 1214 free_extent_buffer(fs_info->sb_buffer);
7eccb903 1215
9078a3e1 1216 btrfs_free_block_groups(root->fs_info);
0f7d52f4 1217 del_fs_roots(fs_info);
d10c5f31
CM
1218
1219 filemap_write_and_wait(fs_info->btree_inode->i_mapping);
1220
d1310b2e
CM
1221 extent_io_tree_empty_lru(&fs_info->free_space_cache);
1222 extent_io_tree_empty_lru(&fs_info->block_group_cache);
1223 extent_io_tree_empty_lru(&fs_info->pinned_extents);
1224 extent_io_tree_empty_lru(&fs_info->pending_del);
1225 extent_io_tree_empty_lru(&fs_info->extent_ins);
1226 extent_io_tree_empty_lru(&BTRFS_I(fs_info->btree_inode)->io_tree);
d10c5f31 1227
db94535d 1228 truncate_inode_pages(fs_info->btree_inode->i_mapping, 0);
ce9adaa5
CM
1229 flush_workqueue(end_io_workqueue);
1230 destroy_workqueue(end_io_workqueue);
d10c5f31 1231
db94535d 1232 iput(fs_info->btree_inode);
19c00ddc
CM
1233#if 0
1234 while(!list_empty(&fs_info->hashers)) {
1235 struct btrfs_hasher *hasher;
1236 hasher = list_entry(fs_info->hashers.next, struct btrfs_hasher,
1237 hashers);
1238 list_del(&hasher->hashers);
1239 crypto_free_hash(&fs_info->hash_tfm);
1240 kfree(hasher);
1241 }
1242#endif
0b86a832
CM
1243 close_all_devices(fs_info);
1244 btrfs_mapping_tree_free(&fs_info->mapping_tree);
04160088 1245 bdi_destroy(&fs_info->bdi);
0b86a832 1246
0f7d52f4 1247 kfree(fs_info->extent_root);
0f7d52f4 1248 kfree(fs_info->tree_root);
0b86a832
CM
1249 kfree(fs_info->chunk_root);
1250 kfree(fs_info->dev_root);
eb60ceac
CM
1251 return 0;
1252}
1253
5f39d397
CM
1254int btrfs_buffer_uptodate(struct extent_buffer *buf)
1255{
810191ff 1256 struct inode *btree_inode = buf->first_page->mapping->host;
d1310b2e 1257 return extent_buffer_uptodate(&BTRFS_I(btree_inode)->io_tree, buf);
5f39d397
CM
1258}
1259
1260int btrfs_set_buffer_uptodate(struct extent_buffer *buf)
ccd467d6 1261{
810191ff 1262 struct inode *btree_inode = buf->first_page->mapping->host;
d1310b2e 1263 return set_extent_buffer_uptodate(&BTRFS_I(btree_inode)->io_tree,
5f39d397
CM
1264 buf);
1265}
6702ed49 1266
5f39d397
CM
1267void btrfs_mark_buffer_dirty(struct extent_buffer *buf)
1268{
810191ff 1269 struct btrfs_root *root = BTRFS_I(buf->first_page->mapping->host)->root;
5f39d397
CM
1270 u64 transid = btrfs_header_generation(buf);
1271 struct inode *btree_inode = root->fs_info->btree_inode;
6702ed49 1272
ccd467d6
CM
1273 if (transid != root->fs_info->generation) {
1274 printk(KERN_CRIT "transid mismatch buffer %llu, found %Lu running %Lu\n",
db94535d 1275 (unsigned long long)buf->start,
ccd467d6
CM
1276 transid, root->fs_info->generation);
1277 WARN_ON(1);
1278 }
d1310b2e 1279 set_extent_buffer_dirty(&BTRFS_I(btree_inode)->io_tree, buf);
eb60ceac
CM
1280}
1281
e2008b61
CM
1282void btrfs_throttle(struct btrfs_root *root)
1283{
55c69072
CM
1284 struct backing_dev_info *bdi;
1285
1286 bdi = root->fs_info->sb->s_bdev->bd_inode->i_mapping->backing_dev_info;
04005cc7
CM
1287 if (root->fs_info->throttles && bdi_write_congested(bdi)) {
1288#if LINUX_VERSION_CODE > KERNEL_VERSION(2,6,18)
55c69072 1289 congestion_wait(WRITE, HZ/20);
04005cc7
CM
1290#else
1291 blk_congestion_wait(WRITE, HZ/20);
1292#endif
1293 }
e2008b61
CM
1294}
1295
d3c2fdcf 1296void btrfs_btree_balance_dirty(struct btrfs_root *root, unsigned long nr)
35b7e476 1297{
d3c2fdcf 1298 balance_dirty_pages_ratelimited_nr(
d7fc640e 1299 root->fs_info->btree_inode->i_mapping, 1);
35b7e476 1300}
6b80053d
CM
1301
1302void btrfs_set_buffer_defrag(struct extent_buffer *buf)
1303{
810191ff 1304 struct btrfs_root *root = BTRFS_I(buf->first_page->mapping->host)->root;
6b80053d 1305 struct inode *btree_inode = root->fs_info->btree_inode;
d1310b2e 1306 set_extent_bits(&BTRFS_I(btree_inode)->io_tree, buf->start,
6b80053d
CM
1307 buf->start + buf->len - 1, EXTENT_DEFRAG, GFP_NOFS);
1308}
1309
1310void btrfs_set_buffer_defrag_done(struct extent_buffer *buf)
1311{
810191ff 1312 struct btrfs_root *root = BTRFS_I(buf->first_page->mapping->host)->root;
6b80053d 1313 struct inode *btree_inode = root->fs_info->btree_inode;
d1310b2e 1314 set_extent_bits(&BTRFS_I(btree_inode)->io_tree, buf->start,
6b80053d
CM
1315 buf->start + buf->len - 1, EXTENT_DEFRAG_DONE,
1316 GFP_NOFS);
1317}
1318
1319int btrfs_buffer_defrag(struct extent_buffer *buf)
1320{
810191ff 1321 struct btrfs_root *root = BTRFS_I(buf->first_page->mapping->host)->root;
6b80053d 1322 struct inode *btree_inode = root->fs_info->btree_inode;
d1310b2e 1323 return test_range_bit(&BTRFS_I(btree_inode)->io_tree,
6b80053d
CM
1324 buf->start, buf->start + buf->len - 1, EXTENT_DEFRAG, 0);
1325}
1326
1327int btrfs_buffer_defrag_done(struct extent_buffer *buf)
1328{
810191ff 1329 struct btrfs_root *root = BTRFS_I(buf->first_page->mapping->host)->root;
6b80053d 1330 struct inode *btree_inode = root->fs_info->btree_inode;
d1310b2e 1331 return test_range_bit(&BTRFS_I(btree_inode)->io_tree,
6b80053d
CM
1332 buf->start, buf->start + buf->len - 1,
1333 EXTENT_DEFRAG_DONE, 0);
1334}
1335
1336int btrfs_clear_buffer_defrag_done(struct extent_buffer *buf)
1337{
810191ff 1338 struct btrfs_root *root = BTRFS_I(buf->first_page->mapping->host)->root;
6b80053d 1339 struct inode *btree_inode = root->fs_info->btree_inode;
d1310b2e 1340 return clear_extent_bits(&BTRFS_I(btree_inode)->io_tree,
6b80053d
CM
1341 buf->start, buf->start + buf->len - 1,
1342 EXTENT_DEFRAG_DONE, GFP_NOFS);
1343}
1344
1345int btrfs_clear_buffer_defrag(struct extent_buffer *buf)
1346{
810191ff 1347 struct btrfs_root *root = BTRFS_I(buf->first_page->mapping->host)->root;
6b80053d 1348 struct inode *btree_inode = root->fs_info->btree_inode;
d1310b2e 1349 return clear_extent_bits(&BTRFS_I(btree_inode)->io_tree,
6b80053d
CM
1350 buf->start, buf->start + buf->len - 1,
1351 EXTENT_DEFRAG, GFP_NOFS);
1352}
1353
1354int btrfs_read_buffer(struct extent_buffer *buf)
1355{
810191ff 1356 struct btrfs_root *root = BTRFS_I(buf->first_page->mapping->host)->root;
6b80053d 1357 struct inode *btree_inode = root->fs_info->btree_inode;
ce9adaa5
CM
1358 int ret;
1359 ret = read_extent_buffer_pages(&BTRFS_I(btree_inode)->io_tree,
a86c12c7 1360 buf, 0, 1, btree_get_extent);
ce9adaa5
CM
1361 if (ret == 0) {
1362 buf->flags |= EXTENT_UPTODATE;
1363 }
1364 return ret;
6b80053d 1365}
0da5468f 1366
d1310b2e 1367static struct extent_io_ops btree_extent_io_ops = {
0da5468f 1368 .writepage_io_hook = btree_writepage_io_hook,
ce9adaa5 1369 .readpage_end_io_hook = btree_readpage_end_io_hook,
0b86a832 1370 .submit_bio_hook = btree_submit_bio_hook,
239b14b3
CM
1371 /* note we're sharing with inode.c for the merge bio hook */
1372 .merge_bio_hook = btrfs_merge_bio_hook,
0da5468f 1373};