Btrfs: Add a thread pool just for submit_bio
[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>
87cbda5c 21#include <linux/scatterlist.h>
22b0ebda 22#include <linux/swap.h>
0f7d52f4 23#include <linux/radix-tree.h>
35b7e476 24#include <linux/writeback.h>
5f39d397 25#include <linux/buffer_head.h> // for block_sync_page
ce9adaa5 26#include <linux/workqueue.h>
a5eb62e3 27#include "crc32c.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"
8b712842 34#include "async-thread.h"
eb60ceac 35
5f39d397
CM
36#if 0
37static int check_tree_block(struct btrfs_root *root, struct extent_buffer *buf)
7eccb903 38{
5f39d397
CM
39 if (extent_buffer_blocknr(buf) != btrfs_header_blocknr(buf)) {
40 printk(KERN_CRIT "buf blocknr(buf) is %llu, header is %llu\n",
41 (unsigned long long)extent_buffer_blocknr(buf),
42 (unsigned long long)btrfs_header_blocknr(buf));
39279cc3 43 return 1;
d98237b3 44 }
9a8dd150 45 return 0;
eb60ceac 46}
5f39d397 47#endif
eb60ceac 48
d1310b2e 49static struct extent_io_ops btree_extent_io_ops;
8b712842 50static void end_workqueue_fn(struct btrfs_work *work);
ce9adaa5
CM
51
52struct end_io_wq {
53 struct bio *bio;
54 bio_end_io_t *end_io;
55 void *private;
56 struct btrfs_fs_info *info;
57 int error;
22c59948 58 int metadata;
ce9adaa5 59 struct list_head list;
8b712842 60 struct btrfs_work work;
ce9adaa5 61};
0da5468f 62
44b8bd7e
CM
63struct async_submit_bio {
64 struct inode *inode;
65 struct bio *bio;
66 struct list_head list;
67 extent_submit_bio_hook_t *submit_bio_hook;
68 int rw;
69 int mirror_num;
8b712842 70 struct btrfs_work work;
44b8bd7e
CM
71};
72
5f39d397 73struct extent_map *btree_get_extent(struct inode *inode, struct page *page,
d1310b2e 74 size_t page_offset, u64 start, u64 len,
5f39d397 75 int create)
7eccb903 76{
5f39d397
CM
77 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
78 struct extent_map *em;
79 int ret;
80
d1310b2e
CM
81 spin_lock(&em_tree->lock);
82 em = lookup_extent_mapping(em_tree, start, len);
a061fc8d
CM
83 if (em) {
84 em->bdev =
85 BTRFS_I(inode)->root->fs_info->fs_devices->latest_bdev;
86 spin_unlock(&em_tree->lock);
5f39d397 87 goto out;
a061fc8d
CM
88 }
89 spin_unlock(&em_tree->lock);
7b13b7b1 90
5f39d397
CM
91 em = alloc_extent_map(GFP_NOFS);
92 if (!em) {
93 em = ERR_PTR(-ENOMEM);
94 goto out;
95 }
96 em->start = 0;
0afbaf8c 97 em->len = (u64)-1;
5f39d397 98 em->block_start = 0;
a061fc8d 99 em->bdev = BTRFS_I(inode)->root->fs_info->fs_devices->latest_bdev;
d1310b2e
CM
100
101 spin_lock(&em_tree->lock);
5f39d397
CM
102 ret = add_extent_mapping(em_tree, em);
103 if (ret == -EEXIST) {
0afbaf8c
CM
104 u64 failed_start = em->start;
105 u64 failed_len = em->len;
106
107 printk("failed to insert %Lu %Lu -> %Lu into tree\n",
108 em->start, em->len, em->block_start);
5f39d397 109 free_extent_map(em);
7b13b7b1 110 em = lookup_extent_mapping(em_tree, start, len);
0afbaf8c
CM
111 if (em) {
112 printk("after failing, found %Lu %Lu %Lu\n",
113 em->start, em->len, em->block_start);
7b13b7b1 114 ret = 0;
0afbaf8c
CM
115 } else {
116 em = lookup_extent_mapping(em_tree, failed_start,
117 failed_len);
118 if (em) {
119 printk("double failure lookup gives us "
120 "%Lu %Lu -> %Lu\n", em->start,
121 em->len, em->block_start);
122 free_extent_map(em);
123 }
7b13b7b1 124 ret = -EIO;
0afbaf8c 125 }
5f39d397 126 } else if (ret) {
7b13b7b1
CM
127 free_extent_map(em);
128 em = NULL;
5f39d397 129 }
7b13b7b1
CM
130 spin_unlock(&em_tree->lock);
131
132 if (ret)
133 em = ERR_PTR(ret);
5f39d397
CM
134out:
135 return em;
7eccb903
CM
136}
137
19c00ddc
CM
138u32 btrfs_csum_data(struct btrfs_root *root, char *data, u32 seed, size_t len)
139{
a5eb62e3 140 return btrfs_crc32c(seed, data, len);
19c00ddc
CM
141}
142
143void btrfs_csum_final(u32 crc, char *result)
144{
145 *(__le32 *)result = ~cpu_to_le32(crc);
146}
147
148static int csum_tree_block(struct btrfs_root *root, struct extent_buffer *buf,
149 int verify)
150{
151 char result[BTRFS_CRC32_SIZE];
152 unsigned long len;
153 unsigned long cur_len;
154 unsigned long offset = BTRFS_CSUM_SIZE;
155 char *map_token = NULL;
156 char *kaddr;
157 unsigned long map_start;
158 unsigned long map_len;
159 int err;
160 u32 crc = ~(u32)0;
161
162 len = buf->len - offset;
163 while(len > 0) {
164 err = map_private_extent_buffer(buf, offset, 32,
165 &map_token, &kaddr,
166 &map_start, &map_len, KM_USER0);
167 if (err) {
168 printk("failed to map extent buffer! %lu\n",
169 offset);
170 return 1;
171 }
172 cur_len = min(len, map_len - (offset - map_start));
173 crc = btrfs_csum_data(root, kaddr + offset - map_start,
174 crc, cur_len);
175 len -= cur_len;
176 offset += cur_len;
177 unmap_extent_buffer(buf, map_token, KM_USER0);
178 }
179 btrfs_csum_final(crc, result);
180
181 if (verify) {
e4204ded
CM
182 int from_this_trans = 0;
183
184 if (root->fs_info->running_transaction &&
185 btrfs_header_generation(buf) ==
186 root->fs_info->running_transaction->transid)
187 from_this_trans = 1;
188
189 /* FIXME, this is not good */
63b10fc4 190 if (memcmp_extent_buffer(buf, result, 0, BTRFS_CRC32_SIZE)) {
e4204ded
CM
191 u32 val;
192 u32 found = 0;
193 memcpy(&found, result, BTRFS_CRC32_SIZE);
194
195 read_extent_buffer(buf, &val, 0, BTRFS_CRC32_SIZE);
196 printk("btrfs: %s checksum verify failed on %llu "
63b10fc4
CM
197 "wanted %X found %X from_this_trans %d "
198 "level %d\n",
19c00ddc 199 root->fs_info->sb->s_id,
63b10fc4
CM
200 buf->start, val, found, from_this_trans,
201 btrfs_header_level(buf));
19c00ddc
CM
202 return 1;
203 }
204 } else {
205 write_extent_buffer(buf, result, 0, BTRFS_CRC32_SIZE);
206 }
207 return 0;
208}
209
1259ab75
CM
210static int verify_parent_transid(struct extent_io_tree *io_tree,
211 struct extent_buffer *eb, u64 parent_transid)
212{
213 int ret;
214
215 if (!parent_transid || btrfs_header_generation(eb) == parent_transid)
216 return 0;
217
218 lock_extent(io_tree, eb->start, eb->start + eb->len - 1, GFP_NOFS);
219 if (extent_buffer_uptodate(io_tree, eb) &&
220 btrfs_header_generation(eb) == parent_transid) {
221 ret = 0;
222 goto out;
223 }
224 printk("parent transid verify failed on %llu wanted %llu found %llu\n",
225 (unsigned long long)eb->start,
226 (unsigned long long)parent_transid,
227 (unsigned long long)btrfs_header_generation(eb));
228 ret = 1;
229out:
230 clear_extent_buffer_uptodate(io_tree, eb);
231 unlock_extent(io_tree, eb->start, eb->start + eb->len - 1,
232 GFP_NOFS);
233 return ret;
234
235}
236
f188591e
CM
237static int btree_read_extent_buffer_pages(struct btrfs_root *root,
238 struct extent_buffer *eb,
ca7a79ad 239 u64 start, u64 parent_transid)
f188591e
CM
240{
241 struct extent_io_tree *io_tree;
242 int ret;
243 int num_copies = 0;
244 int mirror_num = 0;
245
246 io_tree = &BTRFS_I(root->fs_info->btree_inode)->io_tree;
247 while (1) {
248 ret = read_extent_buffer_pages(io_tree, eb, start, 1,
249 btree_get_extent, mirror_num);
1259ab75
CM
250 if (!ret &&
251 !verify_parent_transid(io_tree, eb, parent_transid))
f188591e 252 return ret;
4235298e 253
f188591e
CM
254 num_copies = btrfs_num_copies(&root->fs_info->mapping_tree,
255 eb->start, eb->len);
4235298e 256 if (num_copies == 1)
f188591e 257 return ret;
4235298e 258
f188591e 259 mirror_num++;
4235298e 260 if (mirror_num > num_copies)
f188591e 261 return ret;
f188591e 262 }
f188591e
CM
263 return -EIO;
264}
19c00ddc
CM
265
266int csum_dirty_buffer(struct btrfs_root *root, struct page *page)
267{
d1310b2e 268 struct extent_io_tree *tree;
35ebb934 269 u64 start = (u64)page->index << PAGE_CACHE_SHIFT;
19c00ddc
CM
270 u64 found_start;
271 int found_level;
272 unsigned long len;
273 struct extent_buffer *eb;
f188591e
CM
274 int ret;
275
d1310b2e 276 tree = &BTRFS_I(page->mapping->host)->io_tree;
19c00ddc
CM
277
278 if (page->private == EXTENT_PAGE_PRIVATE)
279 goto out;
280 if (!page->private)
281 goto out;
282 len = page->private >> 2;
283 if (len == 0) {
284 WARN_ON(1);
285 }
286 eb = alloc_extent_buffer(tree, start, len, page, GFP_NOFS);
ca7a79ad
CM
287 ret = btree_read_extent_buffer_pages(root, eb, start + PAGE_CACHE_SIZE,
288 btrfs_header_generation(eb));
f188591e 289 BUG_ON(ret);
e18e4809 290 btrfs_clear_buffer_defrag(eb);
19c00ddc
CM
291 found_start = btrfs_header_bytenr(eb);
292 if (found_start != start) {
293 printk("warning: eb start incorrect %Lu buffer %Lu len %lu\n",
294 start, found_start, len);
55c69072
CM
295 WARN_ON(1);
296 goto err;
297 }
298 if (eb->first_page != page) {
299 printk("bad first page %lu %lu\n", eb->first_page->index,
300 page->index);
301 WARN_ON(1);
302 goto err;
303 }
304 if (!PageUptodate(page)) {
305 printk("csum not up to date page %lu\n", page->index);
306 WARN_ON(1);
307 goto err;
19c00ddc
CM
308 }
309 found_level = btrfs_header_level(eb);
63b10fc4
CM
310 spin_lock(&root->fs_info->hash_lock);
311 btrfs_set_header_flag(eb, BTRFS_HEADER_FLAG_WRITTEN);
312 spin_unlock(&root->fs_info->hash_lock);
19c00ddc 313 csum_tree_block(root, eb, 0);
55c69072 314err:
19c00ddc
CM
315 free_extent_buffer(eb);
316out:
317 return 0;
318}
319
0da5468f 320static int btree_writepage_io_hook(struct page *page, u64 start, u64 end)
d98237b3 321{
19c00ddc 322 struct btrfs_root *root = BTRFS_I(page->mapping->host)->root;
19c00ddc
CM
323
324 csum_dirty_buffer(root, page);
0da5468f
CM
325 return 0;
326}
327
ce9adaa5
CM
328int btree_readpage_end_io_hook(struct page *page, u64 start, u64 end,
329 struct extent_state *state)
330{
331 struct extent_io_tree *tree;
332 u64 found_start;
333 int found_level;
334 unsigned long len;
335 struct extent_buffer *eb;
336 struct btrfs_root *root = BTRFS_I(page->mapping->host)->root;
f188591e 337 int ret = 0;
ce9adaa5
CM
338
339 tree = &BTRFS_I(page->mapping->host)->io_tree;
340 if (page->private == EXTENT_PAGE_PRIVATE)
341 goto out;
342 if (!page->private)
343 goto out;
344 len = page->private >> 2;
345 if (len == 0) {
346 WARN_ON(1);
347 }
348 eb = alloc_extent_buffer(tree, start, len, page, GFP_NOFS);
f188591e 349
ce9adaa5
CM
350 btrfs_clear_buffer_defrag(eb);
351 found_start = btrfs_header_bytenr(eb);
352 if (found_start != start) {
f188591e 353 ret = -EIO;
ce9adaa5
CM
354 goto err;
355 }
356 if (eb->first_page != page) {
357 printk("bad first page %lu %lu\n", eb->first_page->index,
358 page->index);
359 WARN_ON(1);
f188591e 360 ret = -EIO;
ce9adaa5
CM
361 goto err;
362 }
1259ab75
CM
363 if (memcmp_extent_buffer(eb, root->fs_info->fsid,
364 (unsigned long)btrfs_header_fsid(eb),
365 BTRFS_FSID_SIZE)) {
366 printk("bad fsid on block %Lu\n", eb->start);
367 ret = -EIO;
368 goto err;
369 }
ce9adaa5
CM
370 found_level = btrfs_header_level(eb);
371
372 ret = csum_tree_block(root, eb, 1);
f188591e
CM
373 if (ret)
374 ret = -EIO;
ce9adaa5
CM
375
376 end = min_t(u64, eb->len, PAGE_CACHE_SIZE);
377 end = eb->start + end - 1;
378 release_extent_buffer_tail_pages(eb);
379err:
380 free_extent_buffer(eb);
381out:
f188591e 382 return ret;
ce9adaa5
CM
383}
384
385#if LINUX_VERSION_CODE > KERNEL_VERSION(2,6,23)
386static void end_workqueue_bio(struct bio *bio, int err)
387#else
388static int end_workqueue_bio(struct bio *bio,
389 unsigned int bytes_done, int err)
390#endif
391{
392 struct end_io_wq *end_io_wq = bio->bi_private;
393 struct btrfs_fs_info *fs_info;
ce9adaa5
CM
394
395#if LINUX_VERSION_CODE <= KERNEL_VERSION(2,6,23)
396 if (bio->bi_size)
397 return 1;
398#endif
399
400 fs_info = end_io_wq->info;
ce9adaa5 401 end_io_wq->error = err;
8b712842
CM
402 end_io_wq->work.func = end_workqueue_fn;
403 end_io_wq->work.flags = 0;
404 btrfs_queue_worker(&fs_info->endio_workers, &end_io_wq->work);
ce9adaa5
CM
405
406#if LINUX_VERSION_CODE <= KERNEL_VERSION(2,6,23)
407 return 0;
408#endif
409}
410
22c59948
CM
411int btrfs_bio_wq_end_io(struct btrfs_fs_info *info, struct bio *bio,
412 int metadata)
0b86a832 413{
ce9adaa5 414 struct end_io_wq *end_io_wq;
ce9adaa5
CM
415 end_io_wq = kmalloc(sizeof(*end_io_wq), GFP_NOFS);
416 if (!end_io_wq)
417 return -ENOMEM;
418
419 end_io_wq->private = bio->bi_private;
420 end_io_wq->end_io = bio->bi_end_io;
22c59948 421 end_io_wq->info = info;
ce9adaa5
CM
422 end_io_wq->error = 0;
423 end_io_wq->bio = bio;
22c59948 424 end_io_wq->metadata = metadata;
ce9adaa5
CM
425
426 bio->bi_private = end_io_wq;
427 bio->bi_end_io = end_workqueue_bio;
22c59948
CM
428 return 0;
429}
430
8b712842
CM
431static void run_one_async_submit(struct btrfs_work *work)
432{
433 struct btrfs_fs_info *fs_info;
434 struct async_submit_bio *async;
435
436 async = container_of(work, struct async_submit_bio, work);
437 fs_info = BTRFS_I(async->inode)->root->fs_info;
438 atomic_dec(&fs_info->nr_async_submits);
439 async->submit_bio_hook(async->inode, async->rw, async->bio,
440 async->mirror_num);
441 kfree(async);
442}
443
44b8bd7e
CM
444int btrfs_wq_submit_bio(struct btrfs_fs_info *fs_info, struct inode *inode,
445 int rw, struct bio *bio, int mirror_num,
446 extent_submit_bio_hook_t *submit_bio_hook)
447{
448 struct async_submit_bio *async;
449
450 async = kmalloc(sizeof(*async), GFP_NOFS);
451 if (!async)
452 return -ENOMEM;
453
454 async->inode = inode;
455 async->rw = rw;
456 async->bio = bio;
457 async->mirror_num = mirror_num;
458 async->submit_bio_hook = submit_bio_hook;
8b712842
CM
459 async->work.func = run_one_async_submit;
460 async->work.flags = 0;
cb03c743 461 atomic_inc(&fs_info->nr_async_submits);
8b712842 462 btrfs_queue_worker(&fs_info->workers, &async->work);
44b8bd7e
CM
463 return 0;
464}
465
466static int __btree_submit_bio_hook(struct inode *inode, int rw, struct bio *bio,
f188591e 467 int mirror_num)
22c59948
CM
468{
469 struct btrfs_root *root = BTRFS_I(inode)->root;
470 u64 offset;
471 int ret;
472
473 offset = bio->bi_sector << 9;
474
8b712842
CM
475 /*
476 * when we're called for a write, we're already in the async
477 * submission context. Just jump ingo btrfs_map_bio
478 */
22c59948 479 if (rw & (1 << BIO_RW)) {
8b712842
CM
480 return btrfs_map_bio(BTRFS_I(inode)->root, rw, bio,
481 mirror_num, 0);
22c59948
CM
482 }
483
8b712842
CM
484 /*
485 * called for a read, do the setup so that checksum validation
486 * can happen in the async kernel threads
487 */
22c59948
CM
488 ret = btrfs_bio_wq_end_io(root->fs_info, bio, 1);
489 BUG_ON(ret);
ce9adaa5 490
8b712842 491 return btrfs_map_bio(BTRFS_I(inode)->root, rw, bio, mirror_num, 1);
0b86a832
CM
492}
493
44b8bd7e
CM
494static int btree_submit_bio_hook(struct inode *inode, int rw, struct bio *bio,
495 int mirror_num)
496{
8b712842
CM
497 /*
498 * kthread helpers are used to submit writes so that checksumming
499 * can happen in parallel across all CPUs
500 */
44b8bd7e
CM
501 if (!(rw & (1 << BIO_RW))) {
502 return __btree_submit_bio_hook(inode, rw, bio, mirror_num);
503 }
504 return btrfs_wq_submit_bio(BTRFS_I(inode)->root->fs_info,
505 inode, rw, bio, mirror_num,
506 __btree_submit_bio_hook);
507}
508
0da5468f
CM
509static int btree_writepage(struct page *page, struct writeback_control *wbc)
510{
d1310b2e
CM
511 struct extent_io_tree *tree;
512 tree = &BTRFS_I(page->mapping->host)->io_tree;
5f39d397
CM
513 return extent_write_full_page(tree, page, btree_get_extent, wbc);
514}
0da5468f
CM
515
516static int btree_writepages(struct address_space *mapping,
517 struct writeback_control *wbc)
518{
d1310b2e
CM
519 struct extent_io_tree *tree;
520 tree = &BTRFS_I(mapping->host)->io_tree;
d8d5f3e1 521 if (wbc->sync_mode == WB_SYNC_NONE) {
793955bc
CM
522 u64 num_dirty;
523 u64 start = 0;
524 unsigned long thresh = 96 * 1024 * 1024;
448d640b
CM
525
526 if (wbc->for_kupdate)
527 return 0;
528
ca664626
CM
529 if (current_is_pdflush()) {
530 thresh = 96 * 1024 * 1024;
531 } else {
532 thresh = 8 * 1024 * 1024;
533 }
1832a6d5
CM
534 num_dirty = count_range_bits(tree, &start, (u64)-1,
535 thresh, EXTENT_DIRTY);
793955bc
CM
536 if (num_dirty < thresh) {
537 return 0;
538 }
539 }
0da5468f
CM
540 return extent_writepages(tree, mapping, btree_get_extent, wbc);
541}
542
5f39d397
CM
543int btree_readpage(struct file *file, struct page *page)
544{
d1310b2e
CM
545 struct extent_io_tree *tree;
546 tree = &BTRFS_I(page->mapping->host)->io_tree;
5f39d397
CM
547 return extent_read_full_page(tree, page, btree_get_extent);
548}
22b0ebda 549
70dec807 550static int btree_releasepage(struct page *page, gfp_t gfp_flags)
5f39d397 551{
d1310b2e
CM
552 struct extent_io_tree *tree;
553 struct extent_map_tree *map;
5f39d397 554 int ret;
d98237b3 555
3dd39914
CM
556 if (page_count(page) > 3) {
557 /* once for page->private, once for the caller, once
558 * once for the page cache
559 */
560 return 0;
561 }
d1310b2e
CM
562 tree = &BTRFS_I(page->mapping->host)->io_tree;
563 map = &BTRFS_I(page->mapping->host)->extent_tree;
7b13b7b1 564 ret = try_release_extent_state(map, tree, page, gfp_flags);
5f39d397 565 if (ret == 1) {
728131d8 566 invalidate_extent_lru(tree, page_offset(page), PAGE_CACHE_SIZE);
5f39d397
CM
567 ClearPagePrivate(page);
568 set_page_private(page, 0);
569 page_cache_release(page);
570 }
d98237b3
CM
571 return ret;
572}
573
5f39d397 574static void btree_invalidatepage(struct page *page, unsigned long offset)
d98237b3 575{
d1310b2e
CM
576 struct extent_io_tree *tree;
577 tree = &BTRFS_I(page->mapping->host)->io_tree;
5f39d397
CM
578 extent_invalidatepage(tree, page, offset);
579 btree_releasepage(page, GFP_NOFS);
9ad6b7bc 580 if (PagePrivate(page)) {
4ef64eae 581 invalidate_extent_lru(tree, page_offset(page), PAGE_CACHE_SIZE);
9ad6b7bc
CM
582 ClearPagePrivate(page);
583 set_page_private(page, 0);
584 page_cache_release(page);
585 }
d98237b3
CM
586}
587
5f39d397 588#if 0
d98237b3 589static int btree_writepage(struct page *page, struct writeback_control *wbc)
ed2ff2cb 590{
87cbda5c 591 struct buffer_head *bh;
0f7d52f4 592 struct btrfs_root *root = BTRFS_I(page->mapping->host)->root;
87cbda5c 593 struct buffer_head *head;
87cbda5c
CM
594 if (!page_has_buffers(page)) {
595 create_empty_buffers(page, root->fs_info->sb->s_blocksize,
596 (1 << BH_Dirty)|(1 << BH_Uptodate));
597 }
598 head = page_buffers(page);
599 bh = head;
600 do {
601 if (buffer_dirty(bh))
602 csum_tree_block(root, bh, 0);
603 bh = bh->b_this_page;
604 } while (bh != head);
d98237b3 605 return block_write_full_page(page, btree_get_block, wbc);
ed2ff2cb 606}
5f39d397 607#endif
eb60ceac 608
d98237b3
CM
609static struct address_space_operations btree_aops = {
610 .readpage = btree_readpage,
611 .writepage = btree_writepage,
0da5468f 612 .writepages = btree_writepages,
5f39d397
CM
613 .releasepage = btree_releasepage,
614 .invalidatepage = btree_invalidatepage,
d98237b3
CM
615 .sync_page = block_sync_page,
616};
617
ca7a79ad
CM
618int readahead_tree_block(struct btrfs_root *root, u64 bytenr, u32 blocksize,
619 u64 parent_transid)
090d1875 620{
5f39d397
CM
621 struct extent_buffer *buf = NULL;
622 struct inode *btree_inode = root->fs_info->btree_inode;
de428b63 623 int ret = 0;
090d1875 624
db94535d 625 buf = btrfs_find_create_tree_block(root, bytenr, blocksize);
5f39d397 626 if (!buf)
090d1875 627 return 0;
d1310b2e 628 read_extent_buffer_pages(&BTRFS_I(btree_inode)->io_tree,
f188591e 629 buf, 0, 0, btree_get_extent, 0);
5f39d397 630 free_extent_buffer(buf);
de428b63 631 return ret;
090d1875
CM
632}
633
0999df54
CM
634struct extent_buffer *btrfs_find_tree_block(struct btrfs_root *root,
635 u64 bytenr, u32 blocksize)
636{
637 struct inode *btree_inode = root->fs_info->btree_inode;
638 struct extent_buffer *eb;
639 eb = find_extent_buffer(&BTRFS_I(btree_inode)->io_tree,
640 bytenr, blocksize, GFP_NOFS);
641 return eb;
642}
643
644struct extent_buffer *btrfs_find_create_tree_block(struct btrfs_root *root,
645 u64 bytenr, u32 blocksize)
646{
647 struct inode *btree_inode = root->fs_info->btree_inode;
648 struct extent_buffer *eb;
649
650 eb = alloc_extent_buffer(&BTRFS_I(btree_inode)->io_tree,
651 bytenr, blocksize, NULL, GFP_NOFS);
652 return eb;
653}
654
655
656struct extent_buffer *read_tree_block(struct btrfs_root *root, u64 bytenr,
ca7a79ad 657 u32 blocksize, u64 parent_transid)
0999df54
CM
658{
659 struct extent_buffer *buf = NULL;
660 struct inode *btree_inode = root->fs_info->btree_inode;
661 struct extent_io_tree *io_tree;
662 int ret;
663
664 io_tree = &BTRFS_I(btree_inode)->io_tree;
665
666 buf = btrfs_find_create_tree_block(root, bytenr, blocksize);
667 if (!buf)
668 return NULL;
0999df54 669
ca7a79ad 670 ret = btree_read_extent_buffer_pages(root, buf, 0, parent_transid);
ce9adaa5
CM
671
672 if (ret == 0) {
673 buf->flags |= EXTENT_UPTODATE;
674 }
5f39d397 675 return buf;
ce9adaa5 676
eb60ceac
CM
677}
678
e089f05c 679int clean_tree_block(struct btrfs_trans_handle *trans, struct btrfs_root *root,
5f39d397 680 struct extent_buffer *buf)
ed2ff2cb 681{
5f39d397 682 struct inode *btree_inode = root->fs_info->btree_inode;
55c69072
CM
683 if (btrfs_header_generation(buf) ==
684 root->fs_info->running_transaction->transid)
d1310b2e 685 clear_extent_buffer_dirty(&BTRFS_I(btree_inode)->io_tree,
55c69072 686 buf);
5f39d397
CM
687 return 0;
688}
689
690int wait_on_tree_block_writeback(struct btrfs_root *root,
691 struct extent_buffer *buf)
692{
693 struct inode *btree_inode = root->fs_info->btree_inode;
d1310b2e 694 wait_on_extent_buffer_writeback(&BTRFS_I(btree_inode)->io_tree,
5f39d397
CM
695 buf);
696 return 0;
697}
698
db94535d 699static int __setup_root(u32 nodesize, u32 leafsize, u32 sectorsize,
87ee04eb 700 u32 stripesize, struct btrfs_root *root,
9f5fae2f 701 struct btrfs_fs_info *fs_info,
e20d96d6 702 u64 objectid)
d97e63b6 703{
cfaa7295 704 root->node = NULL;
0f7d52f4 705 root->inode = NULL;
a28ec197 706 root->commit_root = NULL;
db94535d
CM
707 root->sectorsize = sectorsize;
708 root->nodesize = nodesize;
709 root->leafsize = leafsize;
87ee04eb 710 root->stripesize = stripesize;
123abc88 711 root->ref_cows = 0;
0b86a832
CM
712 root->track_dirty = 0;
713
9f5fae2f 714 root->fs_info = fs_info;
0f7d52f4
CM
715 root->objectid = objectid;
716 root->last_trans = 0;
1b05da2e
CM
717 root->highest_inode = 0;
718 root->last_inode_alloc = 0;
58176a96 719 root->name = NULL;
4313b399 720 root->in_sysfs = 0;
0b86a832
CM
721
722 INIT_LIST_HEAD(&root->dirty_list);
3768f368
CM
723 memset(&root->root_key, 0, sizeof(root->root_key));
724 memset(&root->root_item, 0, sizeof(root->root_item));
6702ed49 725 memset(&root->defrag_progress, 0, sizeof(root->defrag_progress));
58176a96
JB
726 memset(&root->root_kobj, 0, sizeof(root->root_kobj));
727 init_completion(&root->kobj_unregister);
6702ed49
CM
728 root->defrag_running = 0;
729 root->defrag_level = 0;
4d775673 730 root->root_key.objectid = objectid;
3768f368
CM
731 return 0;
732}
733
db94535d 734static int find_and_setup_root(struct btrfs_root *tree_root,
9f5fae2f
CM
735 struct btrfs_fs_info *fs_info,
736 u64 objectid,
e20d96d6 737 struct btrfs_root *root)
3768f368
CM
738{
739 int ret;
db94535d 740 u32 blocksize;
3768f368 741
db94535d 742 __setup_root(tree_root->nodesize, tree_root->leafsize,
87ee04eb
CM
743 tree_root->sectorsize, tree_root->stripesize,
744 root, fs_info, objectid);
3768f368
CM
745 ret = btrfs_find_last_root(tree_root, objectid,
746 &root->root_item, &root->root_key);
747 BUG_ON(ret);
748
db94535d
CM
749 blocksize = btrfs_level_size(root, btrfs_root_level(&root->root_item));
750 root->node = read_tree_block(root, btrfs_root_bytenr(&root->root_item),
ca7a79ad 751 blocksize, 0);
3768f368 752 BUG_ON(!root->node);
d97e63b6
CM
753 return 0;
754}
755
5eda7b5e
CM
756struct btrfs_root *btrfs_read_fs_root_no_radix(struct btrfs_fs_info *fs_info,
757 struct btrfs_key *location)
0f7d52f4
CM
758{
759 struct btrfs_root *root;
760 struct btrfs_root *tree_root = fs_info->tree_root;
761 struct btrfs_path *path;
5f39d397 762 struct extent_buffer *l;
1b05da2e 763 u64 highest_inode;
db94535d 764 u32 blocksize;
0f7d52f4
CM
765 int ret = 0;
766
5eda7b5e 767 root = kzalloc(sizeof(*root), GFP_NOFS);
0cf6c620 768 if (!root)
0f7d52f4 769 return ERR_PTR(-ENOMEM);
0f7d52f4 770 if (location->offset == (u64)-1) {
db94535d 771 ret = find_and_setup_root(tree_root, fs_info,
0f7d52f4
CM
772 location->objectid, root);
773 if (ret) {
0f7d52f4
CM
774 kfree(root);
775 return ERR_PTR(ret);
776 }
777 goto insert;
778 }
779
db94535d 780 __setup_root(tree_root->nodesize, tree_root->leafsize,
87ee04eb
CM
781 tree_root->sectorsize, tree_root->stripesize,
782 root, fs_info, location->objectid);
0f7d52f4
CM
783
784 path = btrfs_alloc_path();
785 BUG_ON(!path);
786 ret = btrfs_search_slot(NULL, tree_root, location, path, 0, 0);
787 if (ret != 0) {
0f7d52f4
CM
788 if (ret > 0)
789 ret = -ENOENT;
790 goto out;
791 }
5f39d397
CM
792 l = path->nodes[0];
793 read_extent_buffer(l, &root->root_item,
794 btrfs_item_ptr_offset(l, path->slots[0]),
0f7d52f4 795 sizeof(root->root_item));
44b36eb2 796 memcpy(&root->root_key, location, sizeof(*location));
0f7d52f4
CM
797 ret = 0;
798out:
799 btrfs_release_path(root, path);
800 btrfs_free_path(path);
801 if (ret) {
802 kfree(root);
803 return ERR_PTR(ret);
804 }
db94535d
CM
805 blocksize = btrfs_level_size(root, btrfs_root_level(&root->root_item));
806 root->node = read_tree_block(root, btrfs_root_bytenr(&root->root_item),
ca7a79ad 807 blocksize, 0);
0f7d52f4
CM
808 BUG_ON(!root->node);
809insert:
0f7d52f4 810 root->ref_cows = 1;
5eda7b5e
CM
811 ret = btrfs_find_highest_inode(root, &highest_inode);
812 if (ret == 0) {
813 root->highest_inode = highest_inode;
814 root->last_inode_alloc = highest_inode;
815 }
816 return root;
817}
818
dc17ff8f
CM
819struct btrfs_root *btrfs_lookup_fs_root(struct btrfs_fs_info *fs_info,
820 u64 root_objectid)
821{
822 struct btrfs_root *root;
823
824 if (root_objectid == BTRFS_ROOT_TREE_OBJECTID)
825 return fs_info->tree_root;
826 if (root_objectid == BTRFS_EXTENT_TREE_OBJECTID)
827 return fs_info->extent_root;
828
829 root = radix_tree_lookup(&fs_info->fs_roots_radix,
830 (unsigned long)root_objectid);
831 return root;
832}
833
edbd8d4e
CM
834struct btrfs_root *btrfs_read_fs_root_no_name(struct btrfs_fs_info *fs_info,
835 struct btrfs_key *location)
5eda7b5e
CM
836{
837 struct btrfs_root *root;
838 int ret;
839
edbd8d4e
CM
840 if (location->objectid == BTRFS_ROOT_TREE_OBJECTID)
841 return fs_info->tree_root;
842 if (location->objectid == BTRFS_EXTENT_TREE_OBJECTID)
843 return fs_info->extent_root;
8f18cf13
CM
844 if (location->objectid == BTRFS_CHUNK_TREE_OBJECTID)
845 return fs_info->chunk_root;
846 if (location->objectid == BTRFS_DEV_TREE_OBJECTID)
847 return fs_info->dev_root;
edbd8d4e 848
5eda7b5e
CM
849 root = radix_tree_lookup(&fs_info->fs_roots_radix,
850 (unsigned long)location->objectid);
851 if (root)
852 return root;
853
854 root = btrfs_read_fs_root_no_radix(fs_info, location);
855 if (IS_ERR(root))
856 return root;
2619ba1f
CM
857 ret = radix_tree_insert(&fs_info->fs_roots_radix,
858 (unsigned long)root->root_key.objectid,
0f7d52f4
CM
859 root);
860 if (ret) {
5f39d397 861 free_extent_buffer(root->node);
0f7d52f4
CM
862 kfree(root);
863 return ERR_PTR(ret);
864 }
edbd8d4e
CM
865 ret = btrfs_find_dead_roots(fs_info->tree_root,
866 root->root_key.objectid, root);
867 BUG_ON(ret);
868
869 return root;
870}
871
872struct btrfs_root *btrfs_read_fs_root(struct btrfs_fs_info *fs_info,
873 struct btrfs_key *location,
874 const char *name, int namelen)
875{
876 struct btrfs_root *root;
877 int ret;
878
879 root = btrfs_read_fs_root_no_name(fs_info, location);
880 if (!root)
881 return NULL;
58176a96 882
4313b399
CM
883 if (root->in_sysfs)
884 return root;
885
58176a96
JB
886 ret = btrfs_set_root_name(root, name, namelen);
887 if (ret) {
5f39d397 888 free_extent_buffer(root->node);
58176a96
JB
889 kfree(root);
890 return ERR_PTR(ret);
891 }
892
893 ret = btrfs_sysfs_add_root(root);
894 if (ret) {
5f39d397 895 free_extent_buffer(root->node);
58176a96
JB
896 kfree(root->name);
897 kfree(root);
898 return ERR_PTR(ret);
899 }
4313b399 900 root->in_sysfs = 1;
0f7d52f4
CM
901 return root;
902}
19c00ddc
CM
903#if 0
904static int add_hasher(struct btrfs_fs_info *info, char *type) {
905 struct btrfs_hasher *hasher;
906
907 hasher = kmalloc(sizeof(*hasher), GFP_NOFS);
908 if (!hasher)
909 return -ENOMEM;
910 hasher->hash_tfm = crypto_alloc_hash(type, 0, CRYPTO_ALG_ASYNC);
911 if (!hasher->hash_tfm) {
912 kfree(hasher);
913 return -EINVAL;
914 }
915 spin_lock(&info->hash_lock);
916 list_add(&hasher->list, &info->hashers);
917 spin_unlock(&info->hash_lock);
918 return 0;
919}
920#endif
04160088
CM
921
922static int btrfs_congested_fn(void *congested_data, int bdi_bits)
923{
924 struct btrfs_fs_info *info = (struct btrfs_fs_info *)congested_data;
925 int ret = 0;
cb03c743 926 int limit = 256 * info->fs_devices->open_devices;
04160088
CM
927 struct list_head *cur;
928 struct btrfs_device *device;
929 struct backing_dev_info *bdi;
930
cb03c743
CM
931 if ((bdi_bits & (1 << BDI_write_congested)) &&
932 atomic_read(&info->nr_async_submits) > limit) {
933 return 1;
934 }
935
04160088
CM
936 list_for_each(cur, &info->fs_devices->devices) {
937 device = list_entry(cur, struct btrfs_device, dev_list);
dfe25020
CM
938 if (!device->bdev)
939 continue;
04160088
CM
940 bdi = blk_get_backing_dev_info(device->bdev);
941 if (bdi && bdi_congested(bdi, bdi_bits)) {
942 ret = 1;
943 break;
944 }
945 }
946 return ret;
947}
948
38b66988
CM
949/*
950 * this unplugs every device on the box, and it is only used when page
951 * is null
952 */
953static void __unplug_io_fn(struct backing_dev_info *bdi, struct page *page)
954{
955 struct list_head *cur;
956 struct btrfs_device *device;
957 struct btrfs_fs_info *info;
958
959 info = (struct btrfs_fs_info *)bdi->unplug_io_data;
960 list_for_each(cur, &info->fs_devices->devices) {
961 device = list_entry(cur, struct btrfs_device, dev_list);
962 bdi = blk_get_backing_dev_info(device->bdev);
963 if (bdi->unplug_io_fn) {
964 bdi->unplug_io_fn(bdi, page);
965 }
966 }
967}
968
04160088
CM
969void btrfs_unplug_io_fn(struct backing_dev_info *bdi, struct page *page)
970{
38b66988 971 struct inode *inode;
f2d8d74d
CM
972 struct extent_map_tree *em_tree;
973 struct extent_map *em;
bcbfce8a 974 struct address_space *mapping;
38b66988
CM
975 u64 offset;
976
bcbfce8a 977 /* the generic O_DIRECT read code does this */
38b66988
CM
978 if (!page) {
979 __unplug_io_fn(bdi, page);
980 return;
981 }
982
bcbfce8a
CM
983 /*
984 * page->mapping may change at any time. Get a consistent copy
985 * and use that for everything below
986 */
987 smp_mb();
988 mapping = page->mapping;
989 if (!mapping)
990 return;
991
992 inode = mapping->host;
38b66988 993 offset = page_offset(page);
04160088 994
f2d8d74d
CM
995 em_tree = &BTRFS_I(inode)->extent_tree;
996 spin_lock(&em_tree->lock);
997 em = lookup_extent_mapping(em_tree, offset, PAGE_CACHE_SIZE);
998 spin_unlock(&em_tree->lock);
999 if (!em)
1000 return;
1001
1002 offset = offset - em->start;
1003 btrfs_unplug_page(&BTRFS_I(inode)->root->fs_info->mapping_tree,
1004 em->block_start + offset, page);
1005 free_extent_map(em);
04160088
CM
1006}
1007
1008static int setup_bdi(struct btrfs_fs_info *info, struct backing_dev_info *bdi)
1009{
51ebc0d3 1010#if LINUX_VERSION_CODE > KERNEL_VERSION(2,6,23)
04160088 1011 bdi_init(bdi);
b248a415 1012#endif
4575c9cc 1013 bdi->ra_pages = default_backing_dev_info.ra_pages;
04160088
CM
1014 bdi->state = 0;
1015 bdi->capabilities = default_backing_dev_info.capabilities;
1016 bdi->unplug_io_fn = btrfs_unplug_io_fn;
1017 bdi->unplug_io_data = info;
1018 bdi->congested_fn = btrfs_congested_fn;
1019 bdi->congested_data = info;
1020 return 0;
1021}
1022
ce9adaa5
CM
1023static int bio_ready_for_csum(struct bio *bio)
1024{
1025 u64 length = 0;
1026 u64 buf_len = 0;
1027 u64 start = 0;
1028 struct page *page;
1029 struct extent_io_tree *io_tree = NULL;
1030 struct btrfs_fs_info *info = NULL;
1031 struct bio_vec *bvec;
1032 int i;
1033 int ret;
1034
1035 bio_for_each_segment(bvec, bio, i) {
1036 page = bvec->bv_page;
1037 if (page->private == EXTENT_PAGE_PRIVATE) {
1038 length += bvec->bv_len;
1039 continue;
1040 }
1041 if (!page->private) {
1042 length += bvec->bv_len;
1043 continue;
1044 }
1045 length = bvec->bv_len;
1046 buf_len = page->private >> 2;
1047 start = page_offset(page) + bvec->bv_offset;
1048 io_tree = &BTRFS_I(page->mapping->host)->io_tree;
1049 info = BTRFS_I(page->mapping->host)->root->fs_info;
1050 }
1051 /* are we fully contained in this bio? */
1052 if (buf_len <= length)
1053 return 1;
1054
1055 ret = extent_range_uptodate(io_tree, start + length,
1056 start + buf_len - 1);
1057 if (ret == 1)
1058 return ret;
1059 return ret;
1060}
1061
8b712842
CM
1062/*
1063 * called by the kthread helper functions to finally call the bio end_io
1064 * functions. This is where read checksum verification actually happens
1065 */
1066static void end_workqueue_fn(struct btrfs_work *work)
ce9adaa5 1067{
ce9adaa5 1068 struct bio *bio;
8b712842
CM
1069 struct end_io_wq *end_io_wq;
1070 struct btrfs_fs_info *fs_info;
ce9adaa5 1071 int error;
ce9adaa5 1072
8b712842
CM
1073 end_io_wq = container_of(work, struct end_io_wq, work);
1074 bio = end_io_wq->bio;
1075 fs_info = end_io_wq->info;
ce9adaa5 1076
8b712842
CM
1077 /* metadata bios are special because the whole tree block must
1078 * be checksummed at once. This makes sure the entire block is in
1079 * ram and up to date before trying to verify things. For
1080 * blocksize <= pagesize, it is basically a noop
1081 */
1082 if (end_io_wq->metadata && !bio_ready_for_csum(bio)) {
1083 btrfs_queue_worker(&fs_info->endio_workers,
1084 &end_io_wq->work);
1085 return;
1086 }
1087 error = end_io_wq->error;
1088 bio->bi_private = end_io_wq->private;
1089 bio->bi_end_io = end_io_wq->end_io;
1090 kfree(end_io_wq);
1091#if LINUX_VERSION_CODE <= KERNEL_VERSION(2,6,23)
1092 bio_endio(bio, bio->bi_size, error);
44b8bd7e 1093#else
8b712842 1094 bio_endio(bio, error);
44b8bd7e 1095#endif
44b8bd7e
CM
1096}
1097
8a4b83cc 1098struct btrfs_root *open_ctree(struct super_block *sb,
dfe25020
CM
1099 struct btrfs_fs_devices *fs_devices,
1100 char *options)
2e635a27 1101{
db94535d
CM
1102 u32 sectorsize;
1103 u32 nodesize;
1104 u32 leafsize;
1105 u32 blocksize;
87ee04eb 1106 u32 stripesize;
a061fc8d 1107 struct buffer_head *bh;
e20d96d6
CM
1108 struct btrfs_root *extent_root = kmalloc(sizeof(struct btrfs_root),
1109 GFP_NOFS);
1110 struct btrfs_root *tree_root = kmalloc(sizeof(struct btrfs_root),
1111 GFP_NOFS);
8790d502 1112 struct btrfs_fs_info *fs_info = kzalloc(sizeof(*fs_info),
e20d96d6 1113 GFP_NOFS);
0b86a832
CM
1114 struct btrfs_root *chunk_root = kmalloc(sizeof(struct btrfs_root),
1115 GFP_NOFS);
1116 struct btrfs_root *dev_root = kmalloc(sizeof(struct btrfs_root),
1117 GFP_NOFS);
eb60ceac 1118 int ret;
e58ca020 1119 int err = -EINVAL;
4543df7e 1120
2c90e5d6 1121 struct btrfs_super_block *disk_super;
8790d502 1122
39279cc3
CM
1123 if (!extent_root || !tree_root || !fs_info) {
1124 err = -ENOMEM;
1125 goto fail;
1126 }
0f7d52f4 1127 INIT_RADIX_TREE(&fs_info->fs_roots_radix, GFP_NOFS);
8fd17795 1128 INIT_LIST_HEAD(&fs_info->trans_list);
facda1e7 1129 INIT_LIST_HEAD(&fs_info->dead_roots);
19c00ddc
CM
1130 INIT_LIST_HEAD(&fs_info->hashers);
1131 spin_lock_init(&fs_info->hash_lock);
1832a6d5 1132 spin_lock_init(&fs_info->delalloc_lock);
cee36a03 1133 spin_lock_init(&fs_info->new_trans_lock);
19c00ddc 1134
58176a96 1135 init_completion(&fs_info->kobj_unregister);
9f5fae2f
CM
1136 fs_info->tree_root = tree_root;
1137 fs_info->extent_root = extent_root;
0b86a832
CM
1138 fs_info->chunk_root = chunk_root;
1139 fs_info->dev_root = dev_root;
8a4b83cc 1140 fs_info->fs_devices = fs_devices;
0b86a832 1141 INIT_LIST_HEAD(&fs_info->dirty_cowonly_roots);
6324fbf3 1142 INIT_LIST_HEAD(&fs_info->space_info);
0b86a832 1143 btrfs_mapping_init(&fs_info->mapping_tree);
cb03c743 1144 atomic_set(&fs_info->nr_async_submits, 0);
e20d96d6 1145 fs_info->sb = sb;
c59f8951 1146 fs_info->max_extent = (u64)-1;
6f568d35 1147 fs_info->max_inline = 8192 * 1024;
04160088 1148 setup_bdi(fs_info, &fs_info->bdi);
d98237b3
CM
1149 fs_info->btree_inode = new_inode(sb);
1150 fs_info->btree_inode->i_ino = 1;
2c90e5d6 1151 fs_info->btree_inode->i_nlink = 1;
4543df7e 1152 fs_info->thread_pool_size = min(num_online_cpus() + 2, 8);
0afbaf8c 1153
a061fc8d
CM
1154 sb->s_blocksize = 4096;
1155 sb->s_blocksize_bits = blksize_bits(4096);
1156
0afbaf8c
CM
1157 /*
1158 * we set the i_size on the btree inode to the max possible int.
1159 * the real end of the address space is determined by all of
1160 * the devices in the system
1161 */
1162 fs_info->btree_inode->i_size = OFFSET_MAX;
d98237b3 1163 fs_info->btree_inode->i_mapping->a_ops = &btree_aops;
04160088
CM
1164 fs_info->btree_inode->i_mapping->backing_dev_info = &fs_info->bdi;
1165
d1310b2e 1166 extent_io_tree_init(&BTRFS_I(fs_info->btree_inode)->io_tree,
5f39d397
CM
1167 fs_info->btree_inode->i_mapping,
1168 GFP_NOFS);
d1310b2e
CM
1169 extent_map_tree_init(&BTRFS_I(fs_info->btree_inode)->extent_tree,
1170 GFP_NOFS);
1171
1172 BTRFS_I(fs_info->btree_inode)->io_tree.ops = &btree_extent_io_ops;
0da5468f 1173
d1310b2e 1174 extent_io_tree_init(&fs_info->free_space_cache,
f510cfec 1175 fs_info->btree_inode->i_mapping, GFP_NOFS);
d1310b2e 1176 extent_io_tree_init(&fs_info->block_group_cache,
96b5179d 1177 fs_info->btree_inode->i_mapping, GFP_NOFS);
d1310b2e 1178 extent_io_tree_init(&fs_info->pinned_extents,
1a5bc167 1179 fs_info->btree_inode->i_mapping, GFP_NOFS);
d1310b2e 1180 extent_io_tree_init(&fs_info->pending_del,
1a5bc167 1181 fs_info->btree_inode->i_mapping, GFP_NOFS);
d1310b2e 1182 extent_io_tree_init(&fs_info->extent_ins,
1a5bc167 1183 fs_info->btree_inode->i_mapping, GFP_NOFS);
e66f709b 1184 fs_info->do_barriers = 1;
e18e4809 1185
6da6abae
CM
1186#if LINUX_VERSION_CODE <= KERNEL_VERSION(2,6,18)
1187 INIT_WORK(&fs_info->trans_work, btrfs_transaction_cleaner, fs_info);
1188#else
08607c1b 1189 INIT_DELAYED_WORK(&fs_info->trans_work, btrfs_transaction_cleaner);
6da6abae 1190#endif
0f7d52f4
CM
1191 BTRFS_I(fs_info->btree_inode)->root = tree_root;
1192 memset(&BTRFS_I(fs_info->btree_inode)->location, 0,
1193 sizeof(struct btrfs_key));
22b0ebda 1194 insert_inode_hash(fs_info->btree_inode);
d98237b3 1195 mapping_set_gfp_mask(fs_info->btree_inode->i_mapping, GFP_NOFS);
39279cc3 1196
79154b1b 1197 mutex_init(&fs_info->trans_mutex);
d561c025 1198 mutex_init(&fs_info->fs_mutex);
3768f368 1199
19c00ddc
CM
1200#if 0
1201 ret = add_hasher(fs_info, "crc32c");
1202 if (ret) {
1203 printk("btrfs: failed hash setup, modprobe cryptomgr?\n");
1204 err = -ENOMEM;
1205 goto fail_iput;
1206 }
1207#endif
0b86a832 1208 __setup_root(4096, 4096, 4096, 4096, tree_root,
2c90e5d6 1209 fs_info, BTRFS_ROOT_TREE_OBJECTID);
7eccb903 1210
d98237b3 1211
a061fc8d
CM
1212 bh = __bread(fs_devices->latest_bdev,
1213 BTRFS_SUPER_INFO_OFFSET / 4096, 4096);
1214 if (!bh)
39279cc3 1215 goto fail_iput;
39279cc3 1216
a061fc8d
CM
1217 memcpy(&fs_info->super_copy, bh->b_data, sizeof(fs_info->super_copy));
1218 brelse(bh);
5f39d397 1219
a061fc8d 1220 memcpy(fs_info->fsid, fs_info->super_copy.fsid, BTRFS_FSID_SIZE);
0b86a832 1221
5f39d397 1222 disk_super = &fs_info->super_copy;
0f7d52f4 1223 if (!btrfs_super_root(disk_super))
39279cc3 1224 goto fail_sb_buffer;
0f7d52f4 1225
edf24abe
CH
1226 err = btrfs_parse_options(tree_root, options);
1227 if (err)
1228 goto fail_sb_buffer;
dfe25020 1229
4543df7e
CM
1230 /*
1231 * we need to start all the end_io workers up front because the
1232 * queue work function gets called at interrupt time, and so it
1233 * cannot dynamically grow.
1234 */
1235 btrfs_init_workers(&fs_info->workers, fs_info->thread_pool_size);
1cc127b5 1236 btrfs_init_workers(&fs_info->submit_workers, fs_info->thread_pool_size);
4543df7e
CM
1237 btrfs_init_workers(&fs_info->endio_workers, fs_info->thread_pool_size);
1238 btrfs_start_workers(&fs_info->workers, 1);
1cc127b5 1239 btrfs_start_workers(&fs_info->submit_workers, 1);
4543df7e
CM
1240 btrfs_start_workers(&fs_info->endio_workers, fs_info->thread_pool_size);
1241
1242
edf24abe 1243 err = -EINVAL;
a0af469b 1244 if (btrfs_super_num_devices(disk_super) > fs_devices->open_devices) {
8a4b83cc
CM
1245 printk("Btrfs: wanted %llu devices, but found %llu\n",
1246 (unsigned long long)btrfs_super_num_devices(disk_super),
a0af469b 1247 (unsigned long long)fs_devices->open_devices);
dfe25020
CM
1248 if (btrfs_test_opt(tree_root, DEGRADED))
1249 printk("continuing in degraded mode\n");
1250 else {
1251 goto fail_sb_buffer;
1252 }
8a4b83cc 1253 }
dfe25020 1254
4575c9cc
CM
1255 fs_info->bdi.ra_pages *= btrfs_super_num_devices(disk_super);
1256
db94535d
CM
1257 nodesize = btrfs_super_nodesize(disk_super);
1258 leafsize = btrfs_super_leafsize(disk_super);
1259 sectorsize = btrfs_super_sectorsize(disk_super);
87ee04eb 1260 stripesize = btrfs_super_stripesize(disk_super);
db94535d
CM
1261 tree_root->nodesize = nodesize;
1262 tree_root->leafsize = leafsize;
1263 tree_root->sectorsize = sectorsize;
87ee04eb 1264 tree_root->stripesize = stripesize;
a061fc8d
CM
1265
1266 sb->s_blocksize = sectorsize;
1267 sb->s_blocksize_bits = blksize_bits(sectorsize);
db94535d 1268
39279cc3
CM
1269 if (strncmp((char *)(&disk_super->magic), BTRFS_MAGIC,
1270 sizeof(disk_super->magic))) {
1271 printk("btrfs: valid FS not found on %s\n", sb->s_id);
1272 goto fail_sb_buffer;
1273 }
19c00ddc 1274
0b86a832 1275 mutex_lock(&fs_info->fs_mutex);
0d81ba5d 1276
0b86a832 1277 ret = btrfs_read_sys_array(tree_root);
84eed90f
CM
1278 if (ret) {
1279 printk("btrfs: failed to read the system array on %s\n",
1280 sb->s_id);
1281 goto fail_sys_array;
1282 }
0b86a832
CM
1283
1284 blocksize = btrfs_level_size(tree_root,
1285 btrfs_super_chunk_root_level(disk_super));
1286
1287 __setup_root(nodesize, leafsize, sectorsize, stripesize,
1288 chunk_root, fs_info, BTRFS_CHUNK_TREE_OBJECTID);
1289
1290 chunk_root->node = read_tree_block(chunk_root,
1291 btrfs_super_chunk_root(disk_super),
ca7a79ad 1292 blocksize, 0);
0b86a832
CM
1293 BUG_ON(!chunk_root->node);
1294
e17cade2
CM
1295 read_extent_buffer(chunk_root->node, fs_info->chunk_tree_uuid,
1296 (unsigned long)btrfs_header_chunk_tree_uuid(chunk_root->node),
1297 BTRFS_UUID_SIZE);
1298
0b86a832
CM
1299 ret = btrfs_read_chunk_tree(chunk_root);
1300 BUG_ON(ret);
1301
dfe25020
CM
1302 btrfs_close_extra_devices(fs_devices);
1303
db94535d
CM
1304 blocksize = btrfs_level_size(tree_root,
1305 btrfs_super_root_level(disk_super));
19c00ddc 1306
0b86a832 1307
e20d96d6 1308 tree_root->node = read_tree_block(tree_root,
db94535d 1309 btrfs_super_root(disk_super),
ca7a79ad 1310 blocksize, 0);
39279cc3
CM
1311 if (!tree_root->node)
1312 goto fail_sb_buffer;
3768f368 1313
db94535d
CM
1314
1315 ret = find_and_setup_root(tree_root, fs_info,
e20d96d6 1316 BTRFS_EXTENT_TREE_OBJECTID, extent_root);
0b86a832 1317 if (ret)
39279cc3 1318 goto fail_tree_root;
0b86a832
CM
1319 extent_root->track_dirty = 1;
1320
1321 ret = find_and_setup_root(tree_root, fs_info,
1322 BTRFS_DEV_TREE_OBJECTID, dev_root);
1323 dev_root->track_dirty = 1;
1324
1325 if (ret)
1326 goto fail_extent_root;
3768f368 1327
9078a3e1
CM
1328 btrfs_read_block_groups(extent_root);
1329
0f7d52f4 1330 fs_info->generation = btrfs_super_generation(disk_super) + 1;
d18a2c44
CM
1331 fs_info->data_alloc_profile = (u64)-1;
1332 fs_info->metadata_alloc_profile = (u64)-1;
1333 fs_info->system_alloc_profile = fs_info->metadata_alloc_profile;
1334
5be6f7f1 1335 mutex_unlock(&fs_info->fs_mutex);
0f7d52f4 1336 return tree_root;
39279cc3 1337
0b86a832
CM
1338fail_extent_root:
1339 free_extent_buffer(extent_root->node);
39279cc3 1340fail_tree_root:
5f39d397 1341 free_extent_buffer(tree_root->node);
84eed90f
CM
1342fail_sys_array:
1343 mutex_unlock(&fs_info->fs_mutex);
39279cc3 1344fail_sb_buffer:
2d2ae547 1345 extent_io_tree_empty_lru(&BTRFS_I(fs_info->btree_inode)->io_tree);
8b712842
CM
1346 btrfs_stop_workers(&fs_info->workers);
1347 btrfs_stop_workers(&fs_info->endio_workers);
1cc127b5 1348 btrfs_stop_workers(&fs_info->submit_workers);
4543df7e
CM
1349fail_iput:
1350 iput(fs_info->btree_inode);
39279cc3 1351fail:
dfe25020 1352 btrfs_close_devices(fs_info->fs_devices);
84eed90f
CM
1353 btrfs_mapping_tree_free(&fs_info->mapping_tree);
1354
39279cc3
CM
1355 kfree(extent_root);
1356 kfree(tree_root);
51ebc0d3 1357#if LINUX_VERSION_CODE > KERNEL_VERSION(2,6,23)
2d2ae547 1358 bdi_destroy(&fs_info->bdi);
b248a415 1359#endif
39279cc3
CM
1360 kfree(fs_info);
1361 return ERR_PTR(err);
eb60ceac
CM
1362}
1363
f2984462
CM
1364static void btrfs_end_buffer_write_sync(struct buffer_head *bh, int uptodate)
1365{
1366 char b[BDEVNAME_SIZE];
1367
1368 if (uptodate) {
1369 set_buffer_uptodate(bh);
1370 } else {
1371 if (!buffer_eopnotsupp(bh) && printk_ratelimit()) {
1372 printk(KERN_WARNING "lost page write due to "
1373 "I/O error on %s\n",
1374 bdevname(bh->b_bdev, b));
1375 }
1259ab75
CM
1376 /* note, we dont' set_buffer_write_io_error because we have
1377 * our own ways of dealing with the IO errors
1378 */
f2984462
CM
1379 clear_buffer_uptodate(bh);
1380 }
1381 unlock_buffer(bh);
1382 put_bh(bh);
1383}
1384
1385int write_all_supers(struct btrfs_root *root)
1386{
1387 struct list_head *cur;
1388 struct list_head *head = &root->fs_info->fs_devices->devices;
1389 struct btrfs_device *dev;
a061fc8d 1390 struct btrfs_super_block *sb;
f2984462
CM
1391 struct btrfs_dev_item *dev_item;
1392 struct buffer_head *bh;
1393 int ret;
1394 int do_barriers;
a236aed1
CM
1395 int max_errors;
1396 int total_errors = 0;
a061fc8d
CM
1397 u32 crc;
1398 u64 flags;
f2984462 1399
a236aed1 1400 max_errors = btrfs_super_num_devices(&root->fs_info->super_copy) - 1;
f2984462
CM
1401 do_barriers = !btrfs_test_opt(root, NOBARRIER);
1402
a061fc8d
CM
1403 sb = &root->fs_info->super_for_commit;
1404 dev_item = &sb->dev_item;
f2984462
CM
1405 list_for_each(cur, head) {
1406 dev = list_entry(cur, struct btrfs_device, dev_list);
dfe25020
CM
1407 if (!dev->bdev) {
1408 total_errors++;
1409 continue;
1410 }
1411 if (!dev->in_fs_metadata)
1412 continue;
1413
a061fc8d
CM
1414 btrfs_set_stack_device_type(dev_item, dev->type);
1415 btrfs_set_stack_device_id(dev_item, dev->devid);
1416 btrfs_set_stack_device_total_bytes(dev_item, dev->total_bytes);
1417 btrfs_set_stack_device_bytes_used(dev_item, dev->bytes_used);
1418 btrfs_set_stack_device_io_align(dev_item, dev->io_align);
1419 btrfs_set_stack_device_io_width(dev_item, dev->io_width);
1420 btrfs_set_stack_device_sector_size(dev_item, dev->sector_size);
1421 memcpy(dev_item->uuid, dev->uuid, BTRFS_UUID_SIZE);
1422 flags = btrfs_super_flags(sb);
1423 btrfs_set_super_flags(sb, flags | BTRFS_HEADER_FLAG_WRITTEN);
1424
1425
1426 crc = ~(u32)0;
1427 crc = btrfs_csum_data(root, (char *)sb + BTRFS_CSUM_SIZE, crc,
1428 BTRFS_SUPER_INFO_SIZE - BTRFS_CSUM_SIZE);
1429 btrfs_csum_final(crc, sb->csum);
1430
1431 bh = __getblk(dev->bdev, BTRFS_SUPER_INFO_OFFSET / 4096,
f2984462
CM
1432 BTRFS_SUPER_INFO_SIZE);
1433
a061fc8d 1434 memcpy(bh->b_data, sb, BTRFS_SUPER_INFO_SIZE);
f2984462
CM
1435 dev->pending_io = bh;
1436
1437 get_bh(bh);
1438 set_buffer_uptodate(bh);
1439 lock_buffer(bh);
1440 bh->b_end_io = btrfs_end_buffer_write_sync;
1441
1442 if (do_barriers && dev->barriers) {
1443 ret = submit_bh(WRITE_BARRIER, bh);
1444 if (ret == -EOPNOTSUPP) {
1445 printk("btrfs: disabling barriers on dev %s\n",
1446 dev->name);
1447 set_buffer_uptodate(bh);
1448 dev->barriers = 0;
1449 get_bh(bh);
1450 lock_buffer(bh);
1451 ret = submit_bh(WRITE, bh);
1452 }
1453 } else {
1454 ret = submit_bh(WRITE, bh);
1455 }
a236aed1
CM
1456 if (ret)
1457 total_errors++;
f2984462 1458 }
a236aed1
CM
1459 if (total_errors > max_errors) {
1460 printk("btrfs: %d errors while writing supers\n", total_errors);
1461 BUG();
1462 }
1463 total_errors = 0;
f2984462
CM
1464
1465 list_for_each(cur, head) {
1466 dev = list_entry(cur, struct btrfs_device, dev_list);
dfe25020
CM
1467 if (!dev->bdev)
1468 continue;
1469 if (!dev->in_fs_metadata)
1470 continue;
1471
f2984462
CM
1472 BUG_ON(!dev->pending_io);
1473 bh = dev->pending_io;
1474 wait_on_buffer(bh);
1475 if (!buffer_uptodate(dev->pending_io)) {
1476 if (do_barriers && dev->barriers) {
1477 printk("btrfs: disabling barriers on dev %s\n",
1478 dev->name);
1479 set_buffer_uptodate(bh);
1480 get_bh(bh);
1481 lock_buffer(bh);
1482 dev->barriers = 0;
1483 ret = submit_bh(WRITE, bh);
1484 BUG_ON(ret);
1485 wait_on_buffer(bh);
1259ab75
CM
1486 if (!buffer_uptodate(bh))
1487 total_errors++;
f2984462 1488 } else {
a236aed1 1489 total_errors++;
f2984462
CM
1490 }
1491
1492 }
1493 dev->pending_io = NULL;
1494 brelse(bh);
1495 }
a236aed1
CM
1496 if (total_errors > max_errors) {
1497 printk("btrfs: %d errors while writing supers\n", total_errors);
1498 BUG();
1499 }
f2984462
CM
1500 return 0;
1501}
1502
e089f05c 1503int write_ctree_super(struct btrfs_trans_handle *trans, struct btrfs_root
79154b1b 1504 *root)
eb60ceac 1505{
e66f709b 1506 int ret;
5f39d397 1507
f2984462 1508 ret = write_all_supers(root);
5f39d397 1509 return ret;
cfaa7295
CM
1510}
1511
5eda7b5e 1512int btrfs_free_fs_root(struct btrfs_fs_info *fs_info, struct btrfs_root *root)
2619ba1f
CM
1513{
1514 radix_tree_delete(&fs_info->fs_roots_radix,
1515 (unsigned long)root->root_key.objectid);
b99aa6cb
CM
1516 if (root->in_sysfs)
1517 btrfs_sysfs_del_root(root);
2619ba1f
CM
1518 if (root->inode)
1519 iput(root->inode);
1520 if (root->node)
5f39d397 1521 free_extent_buffer(root->node);
2619ba1f 1522 if (root->commit_root)
5f39d397 1523 free_extent_buffer(root->commit_root);
58176a96
JB
1524 if (root->name)
1525 kfree(root->name);
2619ba1f
CM
1526 kfree(root);
1527 return 0;
1528}
1529
35b7e476 1530static int del_fs_roots(struct btrfs_fs_info *fs_info)
0f7d52f4
CM
1531{
1532 int ret;
1533 struct btrfs_root *gang[8];
1534 int i;
1535
1536 while(1) {
1537 ret = radix_tree_gang_lookup(&fs_info->fs_roots_radix,
1538 (void **)gang, 0,
1539 ARRAY_SIZE(gang));
1540 if (!ret)
1541 break;
2619ba1f 1542 for (i = 0; i < ret; i++)
5eda7b5e 1543 btrfs_free_fs_root(fs_info, gang[i]);
0f7d52f4
CM
1544 }
1545 return 0;
1546}
b4100d64 1547
e20d96d6 1548int close_ctree(struct btrfs_root *root)
cfaa7295 1549{
3768f368 1550 int ret;
e089f05c 1551 struct btrfs_trans_handle *trans;
0f7d52f4 1552 struct btrfs_fs_info *fs_info = root->fs_info;
e089f05c 1553
facda1e7 1554 fs_info->closing = 1;
08607c1b 1555 btrfs_transaction_flush_work(root);
0f7d52f4 1556 mutex_lock(&fs_info->fs_mutex);
6702ed49 1557 btrfs_defrag_dirty_roots(root->fs_info);
79154b1b 1558 trans = btrfs_start_transaction(root, 1);
54aa1f4d 1559 ret = btrfs_commit_transaction(trans, root);
79154b1b
CM
1560 /* run commit again to drop the original snapshot */
1561 trans = btrfs_start_transaction(root, 1);
1562 btrfs_commit_transaction(trans, root);
1563 ret = btrfs_write_and_wait_transaction(NULL, root);
3768f368 1564 BUG_ON(ret);
d6bfde87 1565
79154b1b 1566 write_ctree_super(NULL, root);
0f7d52f4
CM
1567 mutex_unlock(&fs_info->fs_mutex);
1568
9ad6b7bc
CM
1569 btrfs_transaction_flush_work(root);
1570
b0c68f8b
CM
1571 if (fs_info->delalloc_bytes) {
1572 printk("btrfs: at unmount delalloc count %Lu\n",
1573 fs_info->delalloc_bytes);
1574 }
0f7d52f4 1575 if (fs_info->extent_root->node)
5f39d397 1576 free_extent_buffer(fs_info->extent_root->node);
f510cfec 1577
0f7d52f4 1578 if (fs_info->tree_root->node)
5f39d397 1579 free_extent_buffer(fs_info->tree_root->node);
f510cfec 1580
0b86a832
CM
1581 if (root->fs_info->chunk_root->node);
1582 free_extent_buffer(root->fs_info->chunk_root->node);
1583
1584 if (root->fs_info->dev_root->node);
1585 free_extent_buffer(root->fs_info->dev_root->node);
1586
9078a3e1 1587 btrfs_free_block_groups(root->fs_info);
0f7d52f4 1588 del_fs_roots(fs_info);
d10c5f31
CM
1589
1590 filemap_write_and_wait(fs_info->btree_inode->i_mapping);
1591
d1310b2e
CM
1592 extent_io_tree_empty_lru(&fs_info->free_space_cache);
1593 extent_io_tree_empty_lru(&fs_info->block_group_cache);
1594 extent_io_tree_empty_lru(&fs_info->pinned_extents);
1595 extent_io_tree_empty_lru(&fs_info->pending_del);
1596 extent_io_tree_empty_lru(&fs_info->extent_ins);
1597 extent_io_tree_empty_lru(&BTRFS_I(fs_info->btree_inode)->io_tree);
d10c5f31 1598
db94535d 1599 truncate_inode_pages(fs_info->btree_inode->i_mapping, 0);
9ad6b7bc 1600
8b712842
CM
1601 btrfs_stop_workers(&fs_info->workers);
1602 btrfs_stop_workers(&fs_info->endio_workers);
1cc127b5 1603 btrfs_stop_workers(&fs_info->submit_workers);
d6bfde87 1604
db94535d 1605 iput(fs_info->btree_inode);
19c00ddc
CM
1606#if 0
1607 while(!list_empty(&fs_info->hashers)) {
1608 struct btrfs_hasher *hasher;
1609 hasher = list_entry(fs_info->hashers.next, struct btrfs_hasher,
1610 hashers);
1611 list_del(&hasher->hashers);
1612 crypto_free_hash(&fs_info->hash_tfm);
1613 kfree(hasher);
1614 }
1615#endif
dfe25020 1616 btrfs_close_devices(fs_info->fs_devices);
0b86a832 1617 btrfs_mapping_tree_free(&fs_info->mapping_tree);
b248a415 1618
51ebc0d3 1619#if LINUX_VERSION_CODE > KERNEL_VERSION(2,6,23)
04160088 1620 bdi_destroy(&fs_info->bdi);
b248a415 1621#endif
0b86a832 1622
0f7d52f4 1623 kfree(fs_info->extent_root);
0f7d52f4 1624 kfree(fs_info->tree_root);
0b86a832
CM
1625 kfree(fs_info->chunk_root);
1626 kfree(fs_info->dev_root);
eb60ceac
CM
1627 return 0;
1628}
1629
1259ab75 1630int btrfs_buffer_uptodate(struct extent_buffer *buf, u64 parent_transid)
5f39d397 1631{
1259ab75 1632 int ret;
810191ff 1633 struct inode *btree_inode = buf->first_page->mapping->host;
1259ab75
CM
1634
1635 ret = extent_buffer_uptodate(&BTRFS_I(btree_inode)->io_tree, buf);
1636 if (!ret)
1637 return ret;
1638
1639 ret = verify_parent_transid(&BTRFS_I(btree_inode)->io_tree, buf,
1640 parent_transid);
1641 return !ret;
5f39d397
CM
1642}
1643
1644int btrfs_set_buffer_uptodate(struct extent_buffer *buf)
ccd467d6 1645{
810191ff 1646 struct inode *btree_inode = buf->first_page->mapping->host;
d1310b2e 1647 return set_extent_buffer_uptodate(&BTRFS_I(btree_inode)->io_tree,
5f39d397
CM
1648 buf);
1649}
6702ed49 1650
5f39d397
CM
1651void btrfs_mark_buffer_dirty(struct extent_buffer *buf)
1652{
810191ff 1653 struct btrfs_root *root = BTRFS_I(buf->first_page->mapping->host)->root;
5f39d397
CM
1654 u64 transid = btrfs_header_generation(buf);
1655 struct inode *btree_inode = root->fs_info->btree_inode;
6702ed49 1656
ccd467d6
CM
1657 if (transid != root->fs_info->generation) {
1658 printk(KERN_CRIT "transid mismatch buffer %llu, found %Lu running %Lu\n",
db94535d 1659 (unsigned long long)buf->start,
ccd467d6
CM
1660 transid, root->fs_info->generation);
1661 WARN_ON(1);
1662 }
d1310b2e 1663 set_extent_buffer_dirty(&BTRFS_I(btree_inode)->io_tree, buf);
eb60ceac
CM
1664}
1665
e2008b61
CM
1666void btrfs_throttle(struct btrfs_root *root)
1667{
55c69072
CM
1668 struct backing_dev_info *bdi;
1669
a061fc8d 1670 bdi = &root->fs_info->bdi;
04005cc7
CM
1671 if (root->fs_info->throttles && bdi_write_congested(bdi)) {
1672#if LINUX_VERSION_CODE > KERNEL_VERSION(2,6,18)
55c69072 1673 congestion_wait(WRITE, HZ/20);
04005cc7
CM
1674#else
1675 blk_congestion_wait(WRITE, HZ/20);
1676#endif
1677 }
e2008b61
CM
1678}
1679
d3c2fdcf 1680void btrfs_btree_balance_dirty(struct btrfs_root *root, unsigned long nr)
35b7e476 1681{
188de649
CM
1682 /*
1683 * looks as though older kernels can get into trouble with
1684 * this code, they end up stuck in balance_dirty_pages forever
1685 */
d6bfde87
CM
1686 struct extent_io_tree *tree;
1687 u64 num_dirty;
1688 u64 start = 0;
1689 unsigned long thresh = 16 * 1024 * 1024;
1690 tree = &BTRFS_I(root->fs_info->btree_inode)->io_tree;
1691
1692 if (current_is_pdflush())
1693 return;
1694
1695 num_dirty = count_range_bits(tree, &start, (u64)-1,
1696 thresh, EXTENT_DIRTY);
1697 if (num_dirty > thresh) {
1698 balance_dirty_pages_ratelimited_nr(
d7fc640e 1699 root->fs_info->btree_inode->i_mapping, 1);
d6bfde87 1700 }
188de649 1701 return;
35b7e476 1702}
6b80053d
CM
1703
1704void btrfs_set_buffer_defrag(struct extent_buffer *buf)
1705{
810191ff 1706 struct btrfs_root *root = BTRFS_I(buf->first_page->mapping->host)->root;
6b80053d 1707 struct inode *btree_inode = root->fs_info->btree_inode;
d1310b2e 1708 set_extent_bits(&BTRFS_I(btree_inode)->io_tree, buf->start,
6b80053d
CM
1709 buf->start + buf->len - 1, EXTENT_DEFRAG, GFP_NOFS);
1710}
1711
1712void btrfs_set_buffer_defrag_done(struct extent_buffer *buf)
1713{
810191ff 1714 struct btrfs_root *root = BTRFS_I(buf->first_page->mapping->host)->root;
6b80053d 1715 struct inode *btree_inode = root->fs_info->btree_inode;
d1310b2e 1716 set_extent_bits(&BTRFS_I(btree_inode)->io_tree, buf->start,
6b80053d
CM
1717 buf->start + buf->len - 1, EXTENT_DEFRAG_DONE,
1718 GFP_NOFS);
1719}
1720
1721int btrfs_buffer_defrag(struct extent_buffer *buf)
1722{
810191ff 1723 struct btrfs_root *root = BTRFS_I(buf->first_page->mapping->host)->root;
6b80053d 1724 struct inode *btree_inode = root->fs_info->btree_inode;
d1310b2e 1725 return test_range_bit(&BTRFS_I(btree_inode)->io_tree,
6b80053d
CM
1726 buf->start, buf->start + buf->len - 1, EXTENT_DEFRAG, 0);
1727}
1728
1729int btrfs_buffer_defrag_done(struct extent_buffer *buf)
1730{
810191ff 1731 struct btrfs_root *root = BTRFS_I(buf->first_page->mapping->host)->root;
6b80053d 1732 struct inode *btree_inode = root->fs_info->btree_inode;
d1310b2e 1733 return test_range_bit(&BTRFS_I(btree_inode)->io_tree,
6b80053d
CM
1734 buf->start, buf->start + buf->len - 1,
1735 EXTENT_DEFRAG_DONE, 0);
1736}
1737
1738int btrfs_clear_buffer_defrag_done(struct extent_buffer *buf)
1739{
810191ff 1740 struct btrfs_root *root = BTRFS_I(buf->first_page->mapping->host)->root;
6b80053d 1741 struct inode *btree_inode = root->fs_info->btree_inode;
d1310b2e 1742 return clear_extent_bits(&BTRFS_I(btree_inode)->io_tree,
6b80053d
CM
1743 buf->start, buf->start + buf->len - 1,
1744 EXTENT_DEFRAG_DONE, GFP_NOFS);
1745}
1746
1747int btrfs_clear_buffer_defrag(struct extent_buffer *buf)
1748{
810191ff 1749 struct btrfs_root *root = BTRFS_I(buf->first_page->mapping->host)->root;
6b80053d 1750 struct inode *btree_inode = root->fs_info->btree_inode;
d1310b2e 1751 return clear_extent_bits(&BTRFS_I(btree_inode)->io_tree,
6b80053d
CM
1752 buf->start, buf->start + buf->len - 1,
1753 EXTENT_DEFRAG, GFP_NOFS);
1754}
1755
ca7a79ad 1756int btrfs_read_buffer(struct extent_buffer *buf, u64 parent_transid)
6b80053d 1757{
810191ff 1758 struct btrfs_root *root = BTRFS_I(buf->first_page->mapping->host)->root;
ce9adaa5 1759 int ret;
ca7a79ad 1760 ret = btree_read_extent_buffer_pages(root, buf, 0, parent_transid);
ce9adaa5
CM
1761 if (ret == 0) {
1762 buf->flags |= EXTENT_UPTODATE;
1763 }
1764 return ret;
6b80053d 1765}
0da5468f 1766
d1310b2e 1767static struct extent_io_ops btree_extent_io_ops = {
0da5468f 1768 .writepage_io_hook = btree_writepage_io_hook,
ce9adaa5 1769 .readpage_end_io_hook = btree_readpage_end_io_hook,
0b86a832 1770 .submit_bio_hook = btree_submit_bio_hook,
239b14b3
CM
1771 /* note we're sharing with inode.c for the merge bio hook */
1772 .merge_bio_hook = btrfs_merge_bio_hook,
0da5468f 1773};