2 * Copyright (C) 2007 Oracle. All rights reserved.
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.
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.
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.
20 #include <linux/blkdev.h>
21 #include <linux/scatterlist.h>
22 #include <linux/swap.h>
23 #include <linux/radix-tree.h>
24 #include <linux/writeback.h>
25 #include <linux/buffer_head.h> // for block_sync_page
26 #include <linux/workqueue.h>
30 #include "transaction.h"
31 #include "btrfs_inode.h"
33 #include "print-tree.h"
34 #include "async-thread.h"
37 static int check_tree_block(struct btrfs_root *root, struct extent_buffer *buf)
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));
49 static struct extent_io_ops btree_extent_io_ops;
50 static void end_workqueue_fn(struct btrfs_work *work);
56 struct btrfs_fs_info *info;
59 struct list_head list;
60 struct btrfs_work work;
63 struct async_submit_bio {
66 struct list_head list;
67 extent_submit_bio_hook_t *submit_bio_hook;
70 struct btrfs_work work;
73 struct extent_map *btree_get_extent(struct inode *inode, struct page *page,
74 size_t page_offset, u64 start, u64 len,
77 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
78 struct extent_map *em;
81 spin_lock(&em_tree->lock);
82 em = lookup_extent_mapping(em_tree, start, len);
85 BTRFS_I(inode)->root->fs_info->fs_devices->latest_bdev;
86 spin_unlock(&em_tree->lock);
89 spin_unlock(&em_tree->lock);
91 em = alloc_extent_map(GFP_NOFS);
93 em = ERR_PTR(-ENOMEM);
99 em->bdev = BTRFS_I(inode)->root->fs_info->fs_devices->latest_bdev;
101 spin_lock(&em_tree->lock);
102 ret = add_extent_mapping(em_tree, em);
103 if (ret == -EEXIST) {
104 u64 failed_start = em->start;
105 u64 failed_len = em->len;
107 printk("failed to insert %Lu %Lu -> %Lu into tree\n",
108 em->start, em->len, em->block_start);
110 em = lookup_extent_mapping(em_tree, start, len);
112 printk("after failing, found %Lu %Lu %Lu\n",
113 em->start, em->len, em->block_start);
116 em = lookup_extent_mapping(em_tree, failed_start,
119 printk("double failure lookup gives us "
120 "%Lu %Lu -> %Lu\n", em->start,
121 em->len, em->block_start);
130 spin_unlock(&em_tree->lock);
138 u32 btrfs_csum_data(struct btrfs_root *root, char *data, u32 seed, size_t len)
140 return btrfs_crc32c(seed, data, len);
143 void btrfs_csum_final(u32 crc, char *result)
145 *(__le32 *)result = ~cpu_to_le32(crc);
148 static int csum_tree_block(struct btrfs_root *root, struct extent_buffer *buf,
151 char result[BTRFS_CRC32_SIZE];
153 unsigned long cur_len;
154 unsigned long offset = BTRFS_CSUM_SIZE;
155 char *map_token = NULL;
157 unsigned long map_start;
158 unsigned long map_len;
162 len = buf->len - offset;
164 err = map_private_extent_buffer(buf, offset, 32,
166 &map_start, &map_len, KM_USER0);
168 printk("failed to map extent buffer! %lu\n",
172 cur_len = min(len, map_len - (offset - map_start));
173 crc = btrfs_csum_data(root, kaddr + offset - map_start,
177 unmap_extent_buffer(buf, map_token, KM_USER0);
179 btrfs_csum_final(crc, result);
182 int from_this_trans = 0;
184 if (root->fs_info->running_transaction &&
185 btrfs_header_generation(buf) ==
186 root->fs_info->running_transaction->transid)
189 /* FIXME, this is not good */
190 if (memcmp_extent_buffer(buf, result, 0, BTRFS_CRC32_SIZE)) {
193 memcpy(&found, result, BTRFS_CRC32_SIZE);
195 read_extent_buffer(buf, &val, 0, BTRFS_CRC32_SIZE);
196 printk("btrfs: %s checksum verify failed on %llu "
197 "wanted %X found %X from_this_trans %d "
199 root->fs_info->sb->s_id,
200 buf->start, val, found, from_this_trans,
201 btrfs_header_level(buf));
205 write_extent_buffer(buf, result, 0, BTRFS_CRC32_SIZE);
210 static int verify_parent_transid(struct extent_io_tree *io_tree,
211 struct extent_buffer *eb, u64 parent_transid)
215 if (!parent_transid || btrfs_header_generation(eb) == parent_transid)
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) {
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));
230 clear_extent_buffer_uptodate(io_tree, eb);
231 unlock_extent(io_tree, eb->start, eb->start + eb->len - 1,
237 static int btree_read_extent_buffer_pages(struct btrfs_root *root,
238 struct extent_buffer *eb,
239 u64 start, u64 parent_transid)
241 struct extent_io_tree *io_tree;
246 io_tree = &BTRFS_I(root->fs_info->btree_inode)->io_tree;
248 ret = read_extent_buffer_pages(io_tree, eb, start, 1,
249 btree_get_extent, mirror_num);
251 !verify_parent_transid(io_tree, eb, parent_transid))
254 num_copies = btrfs_num_copies(&root->fs_info->mapping_tree,
260 if (mirror_num > num_copies)
266 int csum_dirty_buffer(struct btrfs_root *root, struct page *page)
268 struct extent_io_tree *tree;
269 u64 start = (u64)page->index << PAGE_CACHE_SHIFT;
273 struct extent_buffer *eb;
276 tree = &BTRFS_I(page->mapping->host)->io_tree;
278 if (page->private == EXTENT_PAGE_PRIVATE)
282 len = page->private >> 2;
286 eb = alloc_extent_buffer(tree, start, len, page, GFP_NOFS);
287 ret = btree_read_extent_buffer_pages(root, eb, start + PAGE_CACHE_SIZE,
288 btrfs_header_generation(eb));
290 btrfs_clear_buffer_defrag(eb);
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);
298 if (eb->first_page != page) {
299 printk("bad first page %lu %lu\n", eb->first_page->index,
304 if (!PageUptodate(page)) {
305 printk("csum not up to date page %lu\n", page->index);
309 found_level = btrfs_header_level(eb);
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);
313 csum_tree_block(root, eb, 0);
315 free_extent_buffer(eb);
320 static int btree_writepage_io_hook(struct page *page, u64 start, u64 end)
322 struct btrfs_root *root = BTRFS_I(page->mapping->host)->root;
324 csum_dirty_buffer(root, page);
328 int btree_readpage_end_io_hook(struct page *page, u64 start, u64 end,
329 struct extent_state *state)
331 struct extent_io_tree *tree;
335 struct extent_buffer *eb;
336 struct btrfs_root *root = BTRFS_I(page->mapping->host)->root;
339 tree = &BTRFS_I(page->mapping->host)->io_tree;
340 if (page->private == EXTENT_PAGE_PRIVATE)
344 len = page->private >> 2;
348 eb = alloc_extent_buffer(tree, start, len, page, GFP_NOFS);
350 btrfs_clear_buffer_defrag(eb);
351 found_start = btrfs_header_bytenr(eb);
352 if (found_start != start) {
356 if (eb->first_page != page) {
357 printk("bad first page %lu %lu\n", eb->first_page->index,
363 if (memcmp_extent_buffer(eb, root->fs_info->fsid,
364 (unsigned long)btrfs_header_fsid(eb),
366 printk("bad fsid on block %Lu\n", eb->start);
370 found_level = btrfs_header_level(eb);
372 ret = csum_tree_block(root, eb, 1);
376 end = min_t(u64, eb->len, PAGE_CACHE_SIZE);
377 end = eb->start + end - 1;
378 release_extent_buffer_tail_pages(eb);
380 free_extent_buffer(eb);
385 #if LINUX_VERSION_CODE > KERNEL_VERSION(2,6,23)
386 static void end_workqueue_bio(struct bio *bio, int err)
388 static int end_workqueue_bio(struct bio *bio,
389 unsigned int bytes_done, int err)
392 struct end_io_wq *end_io_wq = bio->bi_private;
393 struct btrfs_fs_info *fs_info;
395 #if LINUX_VERSION_CODE <= KERNEL_VERSION(2,6,23)
400 fs_info = end_io_wq->info;
401 end_io_wq->error = err;
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);
406 #if LINUX_VERSION_CODE <= KERNEL_VERSION(2,6,23)
411 int btrfs_bio_wq_end_io(struct btrfs_fs_info *info, struct bio *bio,
414 struct end_io_wq *end_io_wq;
415 end_io_wq = kmalloc(sizeof(*end_io_wq), GFP_NOFS);
419 end_io_wq->private = bio->bi_private;
420 end_io_wq->end_io = bio->bi_end_io;
421 end_io_wq->info = info;
422 end_io_wq->error = 0;
423 end_io_wq->bio = bio;
424 end_io_wq->metadata = metadata;
426 bio->bi_private = end_io_wq;
427 bio->bi_end_io = end_workqueue_bio;
431 static void run_one_async_submit(struct btrfs_work *work)
433 struct btrfs_fs_info *fs_info;
434 struct async_submit_bio *async;
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,
444 int 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)
448 struct async_submit_bio *async;
450 async = kmalloc(sizeof(*async), GFP_NOFS);
454 async->inode = inode;
457 async->mirror_num = mirror_num;
458 async->submit_bio_hook = submit_bio_hook;
459 async->work.func = run_one_async_submit;
460 async->work.flags = 0;
461 atomic_inc(&fs_info->nr_async_submits);
462 btrfs_queue_worker(&fs_info->workers, &async->work);
466 static int __btree_submit_bio_hook(struct inode *inode, int rw, struct bio *bio,
469 struct btrfs_root *root = BTRFS_I(inode)->root;
473 offset = bio->bi_sector << 9;
476 * when we're called for a write, we're already in the async
477 * submission context. Just jump ingo btrfs_map_bio
479 if (rw & (1 << BIO_RW)) {
480 return btrfs_map_bio(BTRFS_I(inode)->root, rw, bio,
485 * called for a read, do the setup so that checksum validation
486 * can happen in the async kernel threads
488 ret = btrfs_bio_wq_end_io(root->fs_info, bio, 1);
491 return btrfs_map_bio(BTRFS_I(inode)->root, rw, bio, mirror_num, 1);
494 static int btree_submit_bio_hook(struct inode *inode, int rw, struct bio *bio,
498 * kthread helpers are used to submit writes so that checksumming
499 * can happen in parallel across all CPUs
501 if (!(rw & (1 << BIO_RW))) {
502 return __btree_submit_bio_hook(inode, rw, bio, mirror_num);
504 return btrfs_wq_submit_bio(BTRFS_I(inode)->root->fs_info,
505 inode, rw, bio, mirror_num,
506 __btree_submit_bio_hook);
509 static int btree_writepage(struct page *page, struct writeback_control *wbc)
511 struct extent_io_tree *tree;
512 tree = &BTRFS_I(page->mapping->host)->io_tree;
513 return extent_write_full_page(tree, page, btree_get_extent, wbc);
516 static int btree_writepages(struct address_space *mapping,
517 struct writeback_control *wbc)
519 struct extent_io_tree *tree;
520 tree = &BTRFS_I(mapping->host)->io_tree;
521 if (wbc->sync_mode == WB_SYNC_NONE) {
524 unsigned long thresh = 96 * 1024 * 1024;
526 if (wbc->for_kupdate)
529 if (current_is_pdflush()) {
530 thresh = 96 * 1024 * 1024;
532 thresh = 8 * 1024 * 1024;
534 num_dirty = count_range_bits(tree, &start, (u64)-1,
535 thresh, EXTENT_DIRTY);
536 if (num_dirty < thresh) {
540 return extent_writepages(tree, mapping, btree_get_extent, wbc);
543 int btree_readpage(struct file *file, struct page *page)
545 struct extent_io_tree *tree;
546 tree = &BTRFS_I(page->mapping->host)->io_tree;
547 return extent_read_full_page(tree, page, btree_get_extent);
550 static int btree_releasepage(struct page *page, gfp_t gfp_flags)
552 struct extent_io_tree *tree;
553 struct extent_map_tree *map;
556 if (page_count(page) > 3) {
557 /* once for page->private, once for the caller, once
558 * once for the page cache
562 tree = &BTRFS_I(page->mapping->host)->io_tree;
563 map = &BTRFS_I(page->mapping->host)->extent_tree;
564 ret = try_release_extent_state(map, tree, page, gfp_flags);
566 invalidate_extent_lru(tree, page_offset(page), PAGE_CACHE_SIZE);
567 ClearPagePrivate(page);
568 set_page_private(page, 0);
569 page_cache_release(page);
574 static void btree_invalidatepage(struct page *page, unsigned long offset)
576 struct extent_io_tree *tree;
577 tree = &BTRFS_I(page->mapping->host)->io_tree;
578 extent_invalidatepage(tree, page, offset);
579 btree_releasepage(page, GFP_NOFS);
580 if (PagePrivate(page)) {
581 invalidate_extent_lru(tree, page_offset(page), PAGE_CACHE_SIZE);
582 ClearPagePrivate(page);
583 set_page_private(page, 0);
584 page_cache_release(page);
589 static int btree_writepage(struct page *page, struct writeback_control *wbc)
591 struct buffer_head *bh;
592 struct btrfs_root *root = BTRFS_I(page->mapping->host)->root;
593 struct buffer_head *head;
594 if (!page_has_buffers(page)) {
595 create_empty_buffers(page, root->fs_info->sb->s_blocksize,
596 (1 << BH_Dirty)|(1 << BH_Uptodate));
598 head = page_buffers(page);
601 if (buffer_dirty(bh))
602 csum_tree_block(root, bh, 0);
603 bh = bh->b_this_page;
604 } while (bh != head);
605 return block_write_full_page(page, btree_get_block, wbc);
609 static struct address_space_operations btree_aops = {
610 .readpage = btree_readpage,
611 .writepage = btree_writepage,
612 .writepages = btree_writepages,
613 .releasepage = btree_releasepage,
614 .invalidatepage = btree_invalidatepage,
615 .sync_page = block_sync_page,
618 int readahead_tree_block(struct btrfs_root *root, u64 bytenr, u32 blocksize,
621 struct extent_buffer *buf = NULL;
622 struct inode *btree_inode = root->fs_info->btree_inode;
625 buf = btrfs_find_create_tree_block(root, bytenr, blocksize);
628 read_extent_buffer_pages(&BTRFS_I(btree_inode)->io_tree,
629 buf, 0, 0, btree_get_extent, 0);
630 free_extent_buffer(buf);
634 struct extent_buffer *btrfs_find_tree_block(struct btrfs_root *root,
635 u64 bytenr, u32 blocksize)
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);
644 struct extent_buffer *btrfs_find_create_tree_block(struct btrfs_root *root,
645 u64 bytenr, u32 blocksize)
647 struct inode *btree_inode = root->fs_info->btree_inode;
648 struct extent_buffer *eb;
650 eb = alloc_extent_buffer(&BTRFS_I(btree_inode)->io_tree,
651 bytenr, blocksize, NULL, GFP_NOFS);
656 struct extent_buffer *read_tree_block(struct btrfs_root *root, u64 bytenr,
657 u32 blocksize, u64 parent_transid)
659 struct extent_buffer *buf = NULL;
660 struct inode *btree_inode = root->fs_info->btree_inode;
661 struct extent_io_tree *io_tree;
664 io_tree = &BTRFS_I(btree_inode)->io_tree;
666 buf = btrfs_find_create_tree_block(root, bytenr, blocksize);
670 ret = btree_read_extent_buffer_pages(root, buf, 0, parent_transid);
673 buf->flags |= EXTENT_UPTODATE;
679 int clean_tree_block(struct btrfs_trans_handle *trans, struct btrfs_root *root,
680 struct extent_buffer *buf)
682 struct inode *btree_inode = root->fs_info->btree_inode;
683 if (btrfs_header_generation(buf) ==
684 root->fs_info->running_transaction->transid)
685 clear_extent_buffer_dirty(&BTRFS_I(btree_inode)->io_tree,
690 int wait_on_tree_block_writeback(struct btrfs_root *root,
691 struct extent_buffer *buf)
693 struct inode *btree_inode = root->fs_info->btree_inode;
694 wait_on_extent_buffer_writeback(&BTRFS_I(btree_inode)->io_tree,
699 static int __setup_root(u32 nodesize, u32 leafsize, u32 sectorsize,
700 u32 stripesize, struct btrfs_root *root,
701 struct btrfs_fs_info *fs_info,
706 root->commit_root = NULL;
707 root->sectorsize = sectorsize;
708 root->nodesize = nodesize;
709 root->leafsize = leafsize;
710 root->stripesize = stripesize;
712 root->track_dirty = 0;
714 root->fs_info = fs_info;
715 root->objectid = objectid;
716 root->last_trans = 0;
717 root->highest_inode = 0;
718 root->last_inode_alloc = 0;
722 INIT_LIST_HEAD(&root->dirty_list);
723 memset(&root->root_key, 0, sizeof(root->root_key));
724 memset(&root->root_item, 0, sizeof(root->root_item));
725 memset(&root->defrag_progress, 0, sizeof(root->defrag_progress));
726 memset(&root->root_kobj, 0, sizeof(root->root_kobj));
727 init_completion(&root->kobj_unregister);
728 root->defrag_running = 0;
729 root->defrag_level = 0;
730 root->root_key.objectid = objectid;
734 static int find_and_setup_root(struct btrfs_root *tree_root,
735 struct btrfs_fs_info *fs_info,
737 struct btrfs_root *root)
742 __setup_root(tree_root->nodesize, tree_root->leafsize,
743 tree_root->sectorsize, tree_root->stripesize,
744 root, fs_info, objectid);
745 ret = btrfs_find_last_root(tree_root, objectid,
746 &root->root_item, &root->root_key);
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),
756 struct btrfs_root *btrfs_read_fs_root_no_radix(struct btrfs_fs_info *fs_info,
757 struct btrfs_key *location)
759 struct btrfs_root *root;
760 struct btrfs_root *tree_root = fs_info->tree_root;
761 struct btrfs_path *path;
762 struct extent_buffer *l;
767 root = kzalloc(sizeof(*root), GFP_NOFS);
769 return ERR_PTR(-ENOMEM);
770 if (location->offset == (u64)-1) {
771 ret = find_and_setup_root(tree_root, fs_info,
772 location->objectid, root);
780 __setup_root(tree_root->nodesize, tree_root->leafsize,
781 tree_root->sectorsize, tree_root->stripesize,
782 root, fs_info, location->objectid);
784 path = btrfs_alloc_path();
786 ret = btrfs_search_slot(NULL, tree_root, location, path, 0, 0);
793 read_extent_buffer(l, &root->root_item,
794 btrfs_item_ptr_offset(l, path->slots[0]),
795 sizeof(root->root_item));
796 memcpy(&root->root_key, location, sizeof(*location));
799 btrfs_release_path(root, path);
800 btrfs_free_path(path);
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),
811 ret = btrfs_find_highest_inode(root, &highest_inode);
813 root->highest_inode = highest_inode;
814 root->last_inode_alloc = highest_inode;
819 struct btrfs_root *btrfs_lookup_fs_root(struct btrfs_fs_info *fs_info,
822 struct btrfs_root *root;
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;
829 root = radix_tree_lookup(&fs_info->fs_roots_radix,
830 (unsigned long)root_objectid);
834 struct btrfs_root *btrfs_read_fs_root_no_name(struct btrfs_fs_info *fs_info,
835 struct btrfs_key *location)
837 struct btrfs_root *root;
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;
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;
849 root = radix_tree_lookup(&fs_info->fs_roots_radix,
850 (unsigned long)location->objectid);
854 root = btrfs_read_fs_root_no_radix(fs_info, location);
857 ret = radix_tree_insert(&fs_info->fs_roots_radix,
858 (unsigned long)root->root_key.objectid,
861 free_extent_buffer(root->node);
865 ret = btrfs_find_dead_roots(fs_info->tree_root,
866 root->root_key.objectid, root);
872 struct btrfs_root *btrfs_read_fs_root(struct btrfs_fs_info *fs_info,
873 struct btrfs_key *location,
874 const char *name, int namelen)
876 struct btrfs_root *root;
879 root = btrfs_read_fs_root_no_name(fs_info, location);
886 ret = btrfs_set_root_name(root, name, namelen);
888 free_extent_buffer(root->node);
893 ret = btrfs_sysfs_add_root(root);
895 free_extent_buffer(root->node);
904 static int add_hasher(struct btrfs_fs_info *info, char *type) {
905 struct btrfs_hasher *hasher;
907 hasher = kmalloc(sizeof(*hasher), GFP_NOFS);
910 hasher->hash_tfm = crypto_alloc_hash(type, 0, CRYPTO_ALG_ASYNC);
911 if (!hasher->hash_tfm) {
915 spin_lock(&info->hash_lock);
916 list_add(&hasher->list, &info->hashers);
917 spin_unlock(&info->hash_lock);
922 static int btrfs_congested_fn(void *congested_data, int bdi_bits)
924 struct btrfs_fs_info *info = (struct btrfs_fs_info *)congested_data;
926 int limit = 256 * info->fs_devices->open_devices;
927 struct list_head *cur;
928 struct btrfs_device *device;
929 struct backing_dev_info *bdi;
931 if ((bdi_bits & (1 << BDI_write_congested)) &&
932 atomic_read(&info->nr_async_submits) > limit) {
936 list_for_each(cur, &info->fs_devices->devices) {
937 device = list_entry(cur, struct btrfs_device, dev_list);
940 bdi = blk_get_backing_dev_info(device->bdev);
941 if (bdi && bdi_congested(bdi, bdi_bits)) {
950 * this unplugs every device on the box, and it is only used when page
953 static void __unplug_io_fn(struct backing_dev_info *bdi, struct page *page)
955 struct list_head *cur;
956 struct btrfs_device *device;
957 struct btrfs_fs_info *info;
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);
969 void btrfs_unplug_io_fn(struct backing_dev_info *bdi, struct page *page)
972 struct extent_map_tree *em_tree;
973 struct extent_map *em;
974 struct address_space *mapping;
977 /* the generic O_DIRECT read code does this */
979 __unplug_io_fn(bdi, page);
984 * page->mapping may change at any time. Get a consistent copy
985 * and use that for everything below
988 mapping = page->mapping;
992 inode = mapping->host;
993 offset = page_offset(page);
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);
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);
1008 static int setup_bdi(struct btrfs_fs_info *info, struct backing_dev_info *bdi)
1010 #if LINUX_VERSION_CODE > KERNEL_VERSION(2,6,23)
1013 bdi->ra_pages = default_backing_dev_info.ra_pages;
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;
1023 static int bio_ready_for_csum(struct bio *bio)
1029 struct extent_io_tree *io_tree = NULL;
1030 struct btrfs_fs_info *info = NULL;
1031 struct bio_vec *bvec;
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;
1041 if (!page->private) {
1042 length += bvec->bv_len;
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;
1051 /* are we fully contained in this bio? */
1052 if (buf_len <= length)
1055 ret = extent_range_uptodate(io_tree, start + length,
1056 start + buf_len - 1);
1063 * called by the kthread helper functions to finally call the bio end_io
1064 * functions. This is where read checksum verification actually happens
1066 static void end_workqueue_fn(struct btrfs_work *work)
1069 struct end_io_wq *end_io_wq;
1070 struct btrfs_fs_info *fs_info;
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;
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
1082 if (end_io_wq->metadata && !bio_ready_for_csum(bio)) {
1083 btrfs_queue_worker(&fs_info->endio_workers,
1087 error = end_io_wq->error;
1088 bio->bi_private = end_io_wq->private;
1089 bio->bi_end_io = end_io_wq->end_io;
1091 #if LINUX_VERSION_CODE <= KERNEL_VERSION(2,6,23)
1092 bio_endio(bio, bio->bi_size, error);
1094 bio_endio(bio, error);
1098 struct btrfs_root *open_ctree(struct super_block *sb,
1099 struct btrfs_fs_devices *fs_devices,
1107 struct buffer_head *bh;
1108 struct btrfs_root *extent_root = kmalloc(sizeof(struct btrfs_root),
1110 struct btrfs_root *tree_root = kmalloc(sizeof(struct btrfs_root),
1112 struct btrfs_fs_info *fs_info = kzalloc(sizeof(*fs_info),
1114 struct btrfs_root *chunk_root = kmalloc(sizeof(struct btrfs_root),
1116 struct btrfs_root *dev_root = kmalloc(sizeof(struct btrfs_root),
1121 struct btrfs_super_block *disk_super;
1123 if (!extent_root || !tree_root || !fs_info) {
1127 INIT_RADIX_TREE(&fs_info->fs_roots_radix, GFP_NOFS);
1128 INIT_LIST_HEAD(&fs_info->trans_list);
1129 INIT_LIST_HEAD(&fs_info->dead_roots);
1130 INIT_LIST_HEAD(&fs_info->hashers);
1131 spin_lock_init(&fs_info->hash_lock);
1132 spin_lock_init(&fs_info->delalloc_lock);
1133 spin_lock_init(&fs_info->new_trans_lock);
1135 init_completion(&fs_info->kobj_unregister);
1136 fs_info->tree_root = tree_root;
1137 fs_info->extent_root = extent_root;
1138 fs_info->chunk_root = chunk_root;
1139 fs_info->dev_root = dev_root;
1140 fs_info->fs_devices = fs_devices;
1141 INIT_LIST_HEAD(&fs_info->dirty_cowonly_roots);
1142 INIT_LIST_HEAD(&fs_info->space_info);
1143 btrfs_mapping_init(&fs_info->mapping_tree);
1144 atomic_set(&fs_info->nr_async_submits, 0);
1146 fs_info->max_extent = (u64)-1;
1147 fs_info->max_inline = 8192 * 1024;
1148 setup_bdi(fs_info, &fs_info->bdi);
1149 fs_info->btree_inode = new_inode(sb);
1150 fs_info->btree_inode->i_ino = 1;
1151 fs_info->btree_inode->i_nlink = 1;
1152 fs_info->thread_pool_size = min(num_online_cpus() + 2, 8);
1154 sb->s_blocksize = 4096;
1155 sb->s_blocksize_bits = blksize_bits(4096);
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
1162 fs_info->btree_inode->i_size = OFFSET_MAX;
1163 fs_info->btree_inode->i_mapping->a_ops = &btree_aops;
1164 fs_info->btree_inode->i_mapping->backing_dev_info = &fs_info->bdi;
1166 extent_io_tree_init(&BTRFS_I(fs_info->btree_inode)->io_tree,
1167 fs_info->btree_inode->i_mapping,
1169 extent_map_tree_init(&BTRFS_I(fs_info->btree_inode)->extent_tree,
1172 BTRFS_I(fs_info->btree_inode)->io_tree.ops = &btree_extent_io_ops;
1174 extent_io_tree_init(&fs_info->free_space_cache,
1175 fs_info->btree_inode->i_mapping, GFP_NOFS);
1176 extent_io_tree_init(&fs_info->block_group_cache,
1177 fs_info->btree_inode->i_mapping, GFP_NOFS);
1178 extent_io_tree_init(&fs_info->pinned_extents,
1179 fs_info->btree_inode->i_mapping, GFP_NOFS);
1180 extent_io_tree_init(&fs_info->pending_del,
1181 fs_info->btree_inode->i_mapping, GFP_NOFS);
1182 extent_io_tree_init(&fs_info->extent_ins,
1183 fs_info->btree_inode->i_mapping, GFP_NOFS);
1184 fs_info->do_barriers = 1;
1186 #if LINUX_VERSION_CODE <= KERNEL_VERSION(2,6,18)
1187 INIT_WORK(&fs_info->trans_work, btrfs_transaction_cleaner, fs_info);
1189 INIT_DELAYED_WORK(&fs_info->trans_work, btrfs_transaction_cleaner);
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));
1194 insert_inode_hash(fs_info->btree_inode);
1195 mapping_set_gfp_mask(fs_info->btree_inode->i_mapping, GFP_NOFS);
1197 mutex_init(&fs_info->trans_mutex);
1198 mutex_init(&fs_info->fs_mutex);
1201 ret = add_hasher(fs_info, "crc32c");
1203 printk("btrfs: failed hash setup, modprobe cryptomgr?\n");
1208 __setup_root(4096, 4096, 4096, 4096, tree_root,
1209 fs_info, BTRFS_ROOT_TREE_OBJECTID);
1212 bh = __bread(fs_devices->latest_bdev,
1213 BTRFS_SUPER_INFO_OFFSET / 4096, 4096);
1217 memcpy(&fs_info->super_copy, bh->b_data, sizeof(fs_info->super_copy));
1220 memcpy(fs_info->fsid, fs_info->super_copy.fsid, BTRFS_FSID_SIZE);
1222 disk_super = &fs_info->super_copy;
1223 if (!btrfs_super_root(disk_super))
1224 goto fail_sb_buffer;
1226 err = btrfs_parse_options(tree_root, options);
1228 goto fail_sb_buffer;
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.
1235 btrfs_init_workers(&fs_info->workers, fs_info->thread_pool_size);
1236 btrfs_init_workers(&fs_info->endio_workers, fs_info->thread_pool_size);
1237 btrfs_start_workers(&fs_info->workers, 1);
1238 btrfs_start_workers(&fs_info->endio_workers, fs_info->thread_pool_size);
1242 if (btrfs_super_num_devices(disk_super) > fs_devices->open_devices) {
1243 printk("Btrfs: wanted %llu devices, but found %llu\n",
1244 (unsigned long long)btrfs_super_num_devices(disk_super),
1245 (unsigned long long)fs_devices->open_devices);
1246 if (btrfs_test_opt(tree_root, DEGRADED))
1247 printk("continuing in degraded mode\n");
1249 goto fail_sb_buffer;
1253 fs_info->bdi.ra_pages *= btrfs_super_num_devices(disk_super);
1255 nodesize = btrfs_super_nodesize(disk_super);
1256 leafsize = btrfs_super_leafsize(disk_super);
1257 sectorsize = btrfs_super_sectorsize(disk_super);
1258 stripesize = btrfs_super_stripesize(disk_super);
1259 tree_root->nodesize = nodesize;
1260 tree_root->leafsize = leafsize;
1261 tree_root->sectorsize = sectorsize;
1262 tree_root->stripesize = stripesize;
1264 sb->s_blocksize = sectorsize;
1265 sb->s_blocksize_bits = blksize_bits(sectorsize);
1267 if (strncmp((char *)(&disk_super->magic), BTRFS_MAGIC,
1268 sizeof(disk_super->magic))) {
1269 printk("btrfs: valid FS not found on %s\n", sb->s_id);
1270 goto fail_sb_buffer;
1273 mutex_lock(&fs_info->fs_mutex);
1275 ret = btrfs_read_sys_array(tree_root);
1277 printk("btrfs: failed to read the system array on %s\n",
1279 goto fail_sys_array;
1282 blocksize = btrfs_level_size(tree_root,
1283 btrfs_super_chunk_root_level(disk_super));
1285 __setup_root(nodesize, leafsize, sectorsize, stripesize,
1286 chunk_root, fs_info, BTRFS_CHUNK_TREE_OBJECTID);
1288 chunk_root->node = read_tree_block(chunk_root,
1289 btrfs_super_chunk_root(disk_super),
1291 BUG_ON(!chunk_root->node);
1293 read_extent_buffer(chunk_root->node, fs_info->chunk_tree_uuid,
1294 (unsigned long)btrfs_header_chunk_tree_uuid(chunk_root->node),
1297 ret = btrfs_read_chunk_tree(chunk_root);
1300 btrfs_close_extra_devices(fs_devices);
1302 blocksize = btrfs_level_size(tree_root,
1303 btrfs_super_root_level(disk_super));
1306 tree_root->node = read_tree_block(tree_root,
1307 btrfs_super_root(disk_super),
1309 if (!tree_root->node)
1310 goto fail_sb_buffer;
1313 ret = find_and_setup_root(tree_root, fs_info,
1314 BTRFS_EXTENT_TREE_OBJECTID, extent_root);
1316 goto fail_tree_root;
1317 extent_root->track_dirty = 1;
1319 ret = find_and_setup_root(tree_root, fs_info,
1320 BTRFS_DEV_TREE_OBJECTID, dev_root);
1321 dev_root->track_dirty = 1;
1324 goto fail_extent_root;
1326 btrfs_read_block_groups(extent_root);
1328 fs_info->generation = btrfs_super_generation(disk_super) + 1;
1329 fs_info->data_alloc_profile = (u64)-1;
1330 fs_info->metadata_alloc_profile = (u64)-1;
1331 fs_info->system_alloc_profile = fs_info->metadata_alloc_profile;
1333 mutex_unlock(&fs_info->fs_mutex);
1337 free_extent_buffer(extent_root->node);
1339 free_extent_buffer(tree_root->node);
1341 mutex_unlock(&fs_info->fs_mutex);
1343 extent_io_tree_empty_lru(&BTRFS_I(fs_info->btree_inode)->io_tree);
1344 btrfs_stop_workers(&fs_info->workers);
1345 btrfs_stop_workers(&fs_info->endio_workers);
1347 iput(fs_info->btree_inode);
1349 btrfs_close_devices(fs_info->fs_devices);
1350 btrfs_mapping_tree_free(&fs_info->mapping_tree);
1354 #if LINUX_VERSION_CODE > KERNEL_VERSION(2,6,23)
1355 bdi_destroy(&fs_info->bdi);
1358 return ERR_PTR(err);
1361 static void btrfs_end_buffer_write_sync(struct buffer_head *bh, int uptodate)
1363 char b[BDEVNAME_SIZE];
1366 set_buffer_uptodate(bh);
1368 if (!buffer_eopnotsupp(bh) && printk_ratelimit()) {
1369 printk(KERN_WARNING "lost page write due to "
1370 "I/O error on %s\n",
1371 bdevname(bh->b_bdev, b));
1373 /* note, we dont' set_buffer_write_io_error because we have
1374 * our own ways of dealing with the IO errors
1376 clear_buffer_uptodate(bh);
1382 int write_all_supers(struct btrfs_root *root)
1384 struct list_head *cur;
1385 struct list_head *head = &root->fs_info->fs_devices->devices;
1386 struct btrfs_device *dev;
1387 struct btrfs_super_block *sb;
1388 struct btrfs_dev_item *dev_item;
1389 struct buffer_head *bh;
1393 int total_errors = 0;
1397 max_errors = btrfs_super_num_devices(&root->fs_info->super_copy) - 1;
1398 do_barriers = !btrfs_test_opt(root, NOBARRIER);
1400 sb = &root->fs_info->super_for_commit;
1401 dev_item = &sb->dev_item;
1402 list_for_each(cur, head) {
1403 dev = list_entry(cur, struct btrfs_device, dev_list);
1408 if (!dev->in_fs_metadata)
1411 btrfs_set_stack_device_type(dev_item, dev->type);
1412 btrfs_set_stack_device_id(dev_item, dev->devid);
1413 btrfs_set_stack_device_total_bytes(dev_item, dev->total_bytes);
1414 btrfs_set_stack_device_bytes_used(dev_item, dev->bytes_used);
1415 btrfs_set_stack_device_io_align(dev_item, dev->io_align);
1416 btrfs_set_stack_device_io_width(dev_item, dev->io_width);
1417 btrfs_set_stack_device_sector_size(dev_item, dev->sector_size);
1418 memcpy(dev_item->uuid, dev->uuid, BTRFS_UUID_SIZE);
1419 flags = btrfs_super_flags(sb);
1420 btrfs_set_super_flags(sb, flags | BTRFS_HEADER_FLAG_WRITTEN);
1424 crc = btrfs_csum_data(root, (char *)sb + BTRFS_CSUM_SIZE, crc,
1425 BTRFS_SUPER_INFO_SIZE - BTRFS_CSUM_SIZE);
1426 btrfs_csum_final(crc, sb->csum);
1428 bh = __getblk(dev->bdev, BTRFS_SUPER_INFO_OFFSET / 4096,
1429 BTRFS_SUPER_INFO_SIZE);
1431 memcpy(bh->b_data, sb, BTRFS_SUPER_INFO_SIZE);
1432 dev->pending_io = bh;
1435 set_buffer_uptodate(bh);
1437 bh->b_end_io = btrfs_end_buffer_write_sync;
1439 if (do_barriers && dev->barriers) {
1440 ret = submit_bh(WRITE_BARRIER, bh);
1441 if (ret == -EOPNOTSUPP) {
1442 printk("btrfs: disabling barriers on dev %s\n",
1444 set_buffer_uptodate(bh);
1448 ret = submit_bh(WRITE, bh);
1451 ret = submit_bh(WRITE, bh);
1456 if (total_errors > max_errors) {
1457 printk("btrfs: %d errors while writing supers\n", total_errors);
1462 list_for_each(cur, head) {
1463 dev = list_entry(cur, struct btrfs_device, dev_list);
1466 if (!dev->in_fs_metadata)
1469 BUG_ON(!dev->pending_io);
1470 bh = dev->pending_io;
1472 if (!buffer_uptodate(dev->pending_io)) {
1473 if (do_barriers && dev->barriers) {
1474 printk("btrfs: disabling barriers on dev %s\n",
1476 set_buffer_uptodate(bh);
1480 ret = submit_bh(WRITE, bh);
1483 if (!buffer_uptodate(bh))
1490 dev->pending_io = NULL;
1493 if (total_errors > max_errors) {
1494 printk("btrfs: %d errors while writing supers\n", total_errors);
1500 int write_ctree_super(struct btrfs_trans_handle *trans, struct btrfs_root
1505 ret = write_all_supers(root);
1509 int btrfs_free_fs_root(struct btrfs_fs_info *fs_info, struct btrfs_root *root)
1511 radix_tree_delete(&fs_info->fs_roots_radix,
1512 (unsigned long)root->root_key.objectid);
1514 btrfs_sysfs_del_root(root);
1518 free_extent_buffer(root->node);
1519 if (root->commit_root)
1520 free_extent_buffer(root->commit_root);
1527 static int del_fs_roots(struct btrfs_fs_info *fs_info)
1530 struct btrfs_root *gang[8];
1534 ret = radix_tree_gang_lookup(&fs_info->fs_roots_radix,
1539 for (i = 0; i < ret; i++)
1540 btrfs_free_fs_root(fs_info, gang[i]);
1545 int close_ctree(struct btrfs_root *root)
1548 struct btrfs_trans_handle *trans;
1549 struct btrfs_fs_info *fs_info = root->fs_info;
1551 fs_info->closing = 1;
1552 btrfs_transaction_flush_work(root);
1553 mutex_lock(&fs_info->fs_mutex);
1554 btrfs_defrag_dirty_roots(root->fs_info);
1555 trans = btrfs_start_transaction(root, 1);
1556 ret = btrfs_commit_transaction(trans, root);
1557 /* run commit again to drop the original snapshot */
1558 trans = btrfs_start_transaction(root, 1);
1559 btrfs_commit_transaction(trans, root);
1560 ret = btrfs_write_and_wait_transaction(NULL, root);
1563 write_ctree_super(NULL, root);
1564 mutex_unlock(&fs_info->fs_mutex);
1566 btrfs_transaction_flush_work(root);
1568 if (fs_info->delalloc_bytes) {
1569 printk("btrfs: at unmount delalloc count %Lu\n",
1570 fs_info->delalloc_bytes);
1572 if (fs_info->extent_root->node)
1573 free_extent_buffer(fs_info->extent_root->node);
1575 if (fs_info->tree_root->node)
1576 free_extent_buffer(fs_info->tree_root->node);
1578 if (root->fs_info->chunk_root->node);
1579 free_extent_buffer(root->fs_info->chunk_root->node);
1581 if (root->fs_info->dev_root->node);
1582 free_extent_buffer(root->fs_info->dev_root->node);
1584 btrfs_free_block_groups(root->fs_info);
1585 del_fs_roots(fs_info);
1587 filemap_write_and_wait(fs_info->btree_inode->i_mapping);
1589 extent_io_tree_empty_lru(&fs_info->free_space_cache);
1590 extent_io_tree_empty_lru(&fs_info->block_group_cache);
1591 extent_io_tree_empty_lru(&fs_info->pinned_extents);
1592 extent_io_tree_empty_lru(&fs_info->pending_del);
1593 extent_io_tree_empty_lru(&fs_info->extent_ins);
1594 extent_io_tree_empty_lru(&BTRFS_I(fs_info->btree_inode)->io_tree);
1596 truncate_inode_pages(fs_info->btree_inode->i_mapping, 0);
1598 btrfs_stop_workers(&fs_info->workers);
1599 btrfs_stop_workers(&fs_info->endio_workers);
1601 iput(fs_info->btree_inode);
1603 while(!list_empty(&fs_info->hashers)) {
1604 struct btrfs_hasher *hasher;
1605 hasher = list_entry(fs_info->hashers.next, struct btrfs_hasher,
1607 list_del(&hasher->hashers);
1608 crypto_free_hash(&fs_info->hash_tfm);
1612 btrfs_close_devices(fs_info->fs_devices);
1613 btrfs_mapping_tree_free(&fs_info->mapping_tree);
1615 #if LINUX_VERSION_CODE > KERNEL_VERSION(2,6,23)
1616 bdi_destroy(&fs_info->bdi);
1619 kfree(fs_info->extent_root);
1620 kfree(fs_info->tree_root);
1621 kfree(fs_info->chunk_root);
1622 kfree(fs_info->dev_root);
1626 int btrfs_buffer_uptodate(struct extent_buffer *buf, u64 parent_transid)
1629 struct inode *btree_inode = buf->first_page->mapping->host;
1631 ret = extent_buffer_uptodate(&BTRFS_I(btree_inode)->io_tree, buf);
1635 ret = verify_parent_transid(&BTRFS_I(btree_inode)->io_tree, buf,
1640 int btrfs_set_buffer_uptodate(struct extent_buffer *buf)
1642 struct inode *btree_inode = buf->first_page->mapping->host;
1643 return set_extent_buffer_uptodate(&BTRFS_I(btree_inode)->io_tree,
1647 void btrfs_mark_buffer_dirty(struct extent_buffer *buf)
1649 struct btrfs_root *root = BTRFS_I(buf->first_page->mapping->host)->root;
1650 u64 transid = btrfs_header_generation(buf);
1651 struct inode *btree_inode = root->fs_info->btree_inode;
1653 if (transid != root->fs_info->generation) {
1654 printk(KERN_CRIT "transid mismatch buffer %llu, found %Lu running %Lu\n",
1655 (unsigned long long)buf->start,
1656 transid, root->fs_info->generation);
1659 set_extent_buffer_dirty(&BTRFS_I(btree_inode)->io_tree, buf);
1662 void btrfs_throttle(struct btrfs_root *root)
1664 struct backing_dev_info *bdi;
1666 bdi = &root->fs_info->bdi;
1667 if (root->fs_info->throttles && bdi_write_congested(bdi)) {
1668 #if LINUX_VERSION_CODE > KERNEL_VERSION(2,6,18)
1669 congestion_wait(WRITE, HZ/20);
1671 blk_congestion_wait(WRITE, HZ/20);
1676 void btrfs_btree_balance_dirty(struct btrfs_root *root, unsigned long nr)
1679 * looks as though older kernels can get into trouble with
1680 * this code, they end up stuck in balance_dirty_pages forever
1682 struct extent_io_tree *tree;
1685 unsigned long thresh = 16 * 1024 * 1024;
1686 tree = &BTRFS_I(root->fs_info->btree_inode)->io_tree;
1688 if (current_is_pdflush())
1691 num_dirty = count_range_bits(tree, &start, (u64)-1,
1692 thresh, EXTENT_DIRTY);
1693 if (num_dirty > thresh) {
1694 balance_dirty_pages_ratelimited_nr(
1695 root->fs_info->btree_inode->i_mapping, 1);
1700 void btrfs_set_buffer_defrag(struct extent_buffer *buf)
1702 struct btrfs_root *root = BTRFS_I(buf->first_page->mapping->host)->root;
1703 struct inode *btree_inode = root->fs_info->btree_inode;
1704 set_extent_bits(&BTRFS_I(btree_inode)->io_tree, buf->start,
1705 buf->start + buf->len - 1, EXTENT_DEFRAG, GFP_NOFS);
1708 void btrfs_set_buffer_defrag_done(struct extent_buffer *buf)
1710 struct btrfs_root *root = BTRFS_I(buf->first_page->mapping->host)->root;
1711 struct inode *btree_inode = root->fs_info->btree_inode;
1712 set_extent_bits(&BTRFS_I(btree_inode)->io_tree, buf->start,
1713 buf->start + buf->len - 1, EXTENT_DEFRAG_DONE,
1717 int btrfs_buffer_defrag(struct extent_buffer *buf)
1719 struct btrfs_root *root = BTRFS_I(buf->first_page->mapping->host)->root;
1720 struct inode *btree_inode = root->fs_info->btree_inode;
1721 return test_range_bit(&BTRFS_I(btree_inode)->io_tree,
1722 buf->start, buf->start + buf->len - 1, EXTENT_DEFRAG, 0);
1725 int btrfs_buffer_defrag_done(struct extent_buffer *buf)
1727 struct btrfs_root *root = BTRFS_I(buf->first_page->mapping->host)->root;
1728 struct inode *btree_inode = root->fs_info->btree_inode;
1729 return test_range_bit(&BTRFS_I(btree_inode)->io_tree,
1730 buf->start, buf->start + buf->len - 1,
1731 EXTENT_DEFRAG_DONE, 0);
1734 int btrfs_clear_buffer_defrag_done(struct extent_buffer *buf)
1736 struct btrfs_root *root = BTRFS_I(buf->first_page->mapping->host)->root;
1737 struct inode *btree_inode = root->fs_info->btree_inode;
1738 return clear_extent_bits(&BTRFS_I(btree_inode)->io_tree,
1739 buf->start, buf->start + buf->len - 1,
1740 EXTENT_DEFRAG_DONE, GFP_NOFS);
1743 int btrfs_clear_buffer_defrag(struct extent_buffer *buf)
1745 struct btrfs_root *root = BTRFS_I(buf->first_page->mapping->host)->root;
1746 struct inode *btree_inode = root->fs_info->btree_inode;
1747 return clear_extent_bits(&BTRFS_I(btree_inode)->io_tree,
1748 buf->start, buf->start + buf->len - 1,
1749 EXTENT_DEFRAG, GFP_NOFS);
1752 int btrfs_read_buffer(struct extent_buffer *buf, u64 parent_transid)
1754 struct btrfs_root *root = BTRFS_I(buf->first_page->mapping->host)->root;
1756 ret = btree_read_extent_buffer_pages(root, buf, 0, parent_transid);
1758 buf->flags |= EXTENT_UPTODATE;
1763 static struct extent_io_ops btree_extent_io_ops = {
1764 .writepage_io_hook = btree_writepage_io_hook,
1765 .readpage_end_io_hook = btree_readpage_end_io_hook,
1766 .submit_bio_hook = btree_submit_bio_hook,
1767 /* note we're sharing with inode.c for the merge bio hook */
1768 .merge_bio_hook = btrfs_merge_bio_hook,