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/crc32c.h>
22 #include <linux/scatterlist.h>
23 #include <linux/swap.h>
24 #include <linux/radix-tree.h>
25 #include <linux/writeback.h>
26 #include <linux/buffer_head.h> // for block_sync_page
29 #include "transaction.h"
30 #include "btrfs_inode.h"
31 #include "print-tree.h"
34 static int check_tree_block(struct btrfs_root *root, struct extent_buffer *buf)
36 if (extent_buffer_blocknr(buf) != btrfs_header_blocknr(buf)) {
37 printk(KERN_CRIT "buf blocknr(buf) is %llu, header is %llu\n",
38 (unsigned long long)extent_buffer_blocknr(buf),
39 (unsigned long long)btrfs_header_blocknr(buf));
46 static struct extent_map_ops btree_extent_map_ops;
48 struct extent_buffer *btrfs_find_tree_block(struct btrfs_root *root,
49 u64 bytenr, u32 blocksize)
51 struct inode *btree_inode = root->fs_info->btree_inode;
52 struct extent_buffer *eb;
53 eb = find_extent_buffer(&BTRFS_I(btree_inode)->extent_tree,
54 bytenr, blocksize, GFP_NOFS);
58 struct extent_buffer *btrfs_find_create_tree_block(struct btrfs_root *root,
59 u64 bytenr, u32 blocksize)
61 struct inode *btree_inode = root->fs_info->btree_inode;
62 struct extent_buffer *eb;
64 eb = alloc_extent_buffer(&BTRFS_I(btree_inode)->extent_tree,
65 bytenr, blocksize, NULL, GFP_NOFS);
69 struct extent_map *btree_get_extent(struct inode *inode, struct page *page,
70 size_t page_offset, u64 start, u64 end,
73 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
74 struct extent_map *em;
78 em = lookup_extent_mapping(em_tree, start, end);
82 em = alloc_extent_map(GFP_NOFS);
84 em = ERR_PTR(-ENOMEM);
88 em->end = (i_size_read(inode) & ~((u64)PAGE_CACHE_SIZE -1)) - 1;
90 em->block_end = em->end;
91 em->bdev = inode->i_sb->s_bdev;
92 ret = add_extent_mapping(em_tree, em);
104 u32 btrfs_csum_data(struct btrfs_root *root, char *data, u32 seed, size_t len)
106 return crc32c(seed, data, len);
109 void btrfs_csum_final(u32 crc, char *result)
111 *(__le32 *)result = ~cpu_to_le32(crc);
114 static int csum_tree_block(struct btrfs_root *root, struct extent_buffer *buf,
117 char result[BTRFS_CRC32_SIZE];
119 unsigned long cur_len;
120 unsigned long offset = BTRFS_CSUM_SIZE;
121 char *map_token = NULL;
123 unsigned long map_start;
124 unsigned long map_len;
128 len = buf->len - offset;
130 err = map_private_extent_buffer(buf, offset, 32,
132 &map_start, &map_len, KM_USER0);
134 printk("failed to map extent buffer! %lu\n",
138 cur_len = min(len, map_len - (offset - map_start));
139 crc = btrfs_csum_data(root, kaddr + offset - map_start,
143 unmap_extent_buffer(buf, map_token, KM_USER0);
145 btrfs_csum_final(crc, result);
148 if (memcmp_extent_buffer(buf, result, 0, BTRFS_CRC32_SIZE)) {
149 printk("btrfs: %s checksum verify failed on %llu\n",
150 root->fs_info->sb->s_id,
155 write_extent_buffer(buf, result, 0, BTRFS_CRC32_SIZE);
161 int csum_dirty_buffer(struct btrfs_root *root, struct page *page)
163 struct extent_map_tree *tree;
164 u64 start = (u64)page->index << PAGE_CACHE_SHIFT;
168 struct extent_buffer *eb;
169 tree = &BTRFS_I(page->mapping->host)->extent_tree;
171 if (page->private == EXTENT_PAGE_PRIVATE)
175 len = page->private >> 2;
179 eb = alloc_extent_buffer(tree, start, len, page, GFP_NOFS);
180 read_extent_buffer_pages(tree, eb, start + PAGE_CACHE_SIZE, 1);
181 found_start = btrfs_header_bytenr(eb);
182 if (found_start != start) {
183 printk("warning: eb start incorrect %Lu buffer %Lu len %lu\n",
184 start, found_start, len);
186 found_level = btrfs_header_level(eb);
187 csum_tree_block(root, eb, 0);
188 free_extent_buffer(eb);
193 static int btree_writepage_io_hook(struct page *page, u64 start, u64 end)
195 struct btrfs_root *root = BTRFS_I(page->mapping->host)->root;
197 csum_dirty_buffer(root, page);
201 static int btree_writepage(struct page *page, struct writeback_control *wbc)
203 struct extent_map_tree *tree;
204 tree = &BTRFS_I(page->mapping->host)->extent_tree;
205 return extent_write_full_page(tree, page, btree_get_extent, wbc);
208 static int btree_writepages(struct address_space *mapping,
209 struct writeback_control *wbc)
211 struct extent_map_tree *tree;
212 tree = &BTRFS_I(mapping->host)->extent_tree;
213 if (wbc->sync_mode == WB_SYNC_NONE && current_is_pdflush()) {
216 unsigned long thresh = 96 * 1024 * 1024;
218 if (wbc->for_kupdate)
221 num_dirty = count_range_bits(tree, &start, thresh, EXTENT_DIRTY);
222 if (num_dirty < thresh) {
226 return extent_writepages(tree, mapping, btree_get_extent, wbc);
229 int btree_readpage(struct file *file, struct page *page)
231 struct extent_map_tree *tree;
232 tree = &BTRFS_I(page->mapping->host)->extent_tree;
233 return extent_read_full_page(tree, page, btree_get_extent);
236 static int btree_releasepage(struct page *page, gfp_t unused_gfp_flags)
238 struct extent_map_tree *tree;
241 tree = &BTRFS_I(page->mapping->host)->extent_tree;
242 ret = try_release_extent_mapping(tree, page);
244 ClearPagePrivate(page);
245 set_page_private(page, 0);
246 page_cache_release(page);
251 static void btree_invalidatepage(struct page *page, unsigned long offset)
253 struct extent_map_tree *tree;
254 tree = &BTRFS_I(page->mapping->host)->extent_tree;
255 extent_invalidatepage(tree, page, offset);
256 btree_releasepage(page, GFP_NOFS);
260 static int btree_writepage(struct page *page, struct writeback_control *wbc)
262 struct buffer_head *bh;
263 struct btrfs_root *root = BTRFS_I(page->mapping->host)->root;
264 struct buffer_head *head;
265 if (!page_has_buffers(page)) {
266 create_empty_buffers(page, root->fs_info->sb->s_blocksize,
267 (1 << BH_Dirty)|(1 << BH_Uptodate));
269 head = page_buffers(page);
272 if (buffer_dirty(bh))
273 csum_tree_block(root, bh, 0);
274 bh = bh->b_this_page;
275 } while (bh != head);
276 return block_write_full_page(page, btree_get_block, wbc);
280 static struct address_space_operations btree_aops = {
281 .readpage = btree_readpage,
282 .writepage = btree_writepage,
283 .writepages = btree_writepages,
284 .releasepage = btree_releasepage,
285 .invalidatepage = btree_invalidatepage,
286 .sync_page = block_sync_page,
289 int readahead_tree_block(struct btrfs_root *root, u64 bytenr, u32 blocksize)
291 struct extent_buffer *buf = NULL;
292 struct inode *btree_inode = root->fs_info->btree_inode;
295 buf = btrfs_find_create_tree_block(root, bytenr, blocksize);
298 read_extent_buffer_pages(&BTRFS_I(btree_inode)->extent_tree,
300 free_extent_buffer(buf);
304 struct extent_buffer *read_tree_block(struct btrfs_root *root, u64 bytenr,
307 struct extent_buffer *buf = NULL;
308 struct inode *btree_inode = root->fs_info->btree_inode;
309 struct extent_map_tree *extent_tree;
312 extent_tree = &BTRFS_I(btree_inode)->extent_tree;
314 buf = btrfs_find_create_tree_block(root, bytenr, blocksize);
317 read_extent_buffer_pages(&BTRFS_I(btree_inode)->extent_tree,
319 if (buf->flags & EXTENT_CSUM) {
322 if (test_range_bit(extent_tree, buf->start, buf->start + buf->len - 1,
324 buf->flags |= EXTENT_CSUM;
327 ret = csum_tree_block(root, buf, 1);
328 set_extent_bits(extent_tree, buf->start,
329 buf->start + buf->len - 1,
330 EXTENT_CSUM, GFP_NOFS);
331 buf->flags |= EXTENT_CSUM;
335 int clean_tree_block(struct btrfs_trans_handle *trans, struct btrfs_root *root,
336 struct extent_buffer *buf)
338 struct inode *btree_inode = root->fs_info->btree_inode;
339 clear_extent_buffer_dirty(&BTRFS_I(btree_inode)->extent_tree, buf);
343 int wait_on_tree_block_writeback(struct btrfs_root *root,
344 struct extent_buffer *buf)
346 struct inode *btree_inode = root->fs_info->btree_inode;
347 wait_on_extent_buffer_writeback(&BTRFS_I(btree_inode)->extent_tree,
352 static int __setup_root(u32 nodesize, u32 leafsize, u32 sectorsize,
353 struct btrfs_root *root,
354 struct btrfs_fs_info *fs_info,
359 root->commit_root = NULL;
360 root->sectorsize = sectorsize;
361 root->nodesize = nodesize;
362 root->leafsize = leafsize;
364 root->fs_info = fs_info;
365 root->objectid = objectid;
366 root->last_trans = 0;
367 root->highest_inode = 0;
368 root->last_inode_alloc = 0;
370 memset(&root->root_key, 0, sizeof(root->root_key));
371 memset(&root->root_item, 0, sizeof(root->root_item));
372 memset(&root->defrag_progress, 0, sizeof(root->defrag_progress));
373 memset(&root->root_kobj, 0, sizeof(root->root_kobj));
374 init_completion(&root->kobj_unregister);
375 init_rwsem(&root->snap_sem);
376 root->defrag_running = 0;
377 root->defrag_level = 0;
378 root->root_key.objectid = objectid;
382 static int find_and_setup_root(struct btrfs_root *tree_root,
383 struct btrfs_fs_info *fs_info,
385 struct btrfs_root *root)
390 __setup_root(tree_root->nodesize, tree_root->leafsize,
391 tree_root->sectorsize, root, fs_info, objectid);
392 ret = btrfs_find_last_root(tree_root, objectid,
393 &root->root_item, &root->root_key);
396 blocksize = btrfs_level_size(root, btrfs_root_level(&root->root_item));
397 root->node = read_tree_block(root, btrfs_root_bytenr(&root->root_item),
403 struct btrfs_root *btrfs_read_fs_root_no_radix(struct btrfs_fs_info *fs_info,
404 struct btrfs_key *location)
406 struct btrfs_root *root;
407 struct btrfs_root *tree_root = fs_info->tree_root;
408 struct btrfs_path *path;
409 struct extent_buffer *l;
414 root = kzalloc(sizeof(*root), GFP_NOFS);
416 return ERR_PTR(-ENOMEM);
417 if (location->offset == (u64)-1) {
418 ret = find_and_setup_root(tree_root, fs_info,
419 location->objectid, root);
427 __setup_root(tree_root->nodesize, tree_root->leafsize,
428 tree_root->sectorsize, root, fs_info,
431 path = btrfs_alloc_path();
433 ret = btrfs_search_slot(NULL, tree_root, location, path, 0, 0);
440 read_extent_buffer(l, &root->root_item,
441 btrfs_item_ptr_offset(l, path->slots[0]),
442 sizeof(root->root_item));
443 memcpy(&root->root_key, location, sizeof(*location));
446 btrfs_release_path(root, path);
447 btrfs_free_path(path);
452 blocksize = btrfs_level_size(root, btrfs_root_level(&root->root_item));
453 root->node = read_tree_block(root, btrfs_root_bytenr(&root->root_item),
458 ret = btrfs_find_highest_inode(root, &highest_inode);
460 root->highest_inode = highest_inode;
461 root->last_inode_alloc = highest_inode;
466 struct btrfs_root *btrfs_read_fs_root(struct btrfs_fs_info *fs_info,
467 struct btrfs_key *location,
468 const char *name, int namelen)
470 struct btrfs_root *root;
473 root = radix_tree_lookup(&fs_info->fs_roots_radix,
474 (unsigned long)location->objectid);
478 root = btrfs_read_fs_root_no_radix(fs_info, location);
481 ret = radix_tree_insert(&fs_info->fs_roots_radix,
482 (unsigned long)root->root_key.objectid,
485 free_extent_buffer(root->node);
490 ret = btrfs_set_root_name(root, name, namelen);
492 free_extent_buffer(root->node);
497 ret = btrfs_sysfs_add_root(root);
499 free_extent_buffer(root->node);
505 ret = btrfs_find_dead_roots(fs_info->tree_root,
506 root->root_key.objectid, root);
512 static int add_hasher(struct btrfs_fs_info *info, char *type) {
513 struct btrfs_hasher *hasher;
515 hasher = kmalloc(sizeof(*hasher), GFP_NOFS);
518 hasher->hash_tfm = crypto_alloc_hash(type, 0, CRYPTO_ALG_ASYNC);
519 if (!hasher->hash_tfm) {
523 spin_lock(&info->hash_lock);
524 list_add(&hasher->list, &info->hashers);
525 spin_unlock(&info->hash_lock);
529 struct btrfs_root *open_ctree(struct super_block *sb)
535 struct btrfs_root *extent_root = kmalloc(sizeof(struct btrfs_root),
537 struct btrfs_root *tree_root = kmalloc(sizeof(struct btrfs_root),
539 struct btrfs_fs_info *fs_info = kmalloc(sizeof(*fs_info),
543 struct btrfs_super_block *disk_super;
545 if (!extent_root || !tree_root || !fs_info) {
549 INIT_RADIX_TREE(&fs_info->fs_roots_radix, GFP_NOFS);
550 INIT_LIST_HEAD(&fs_info->trans_list);
551 INIT_LIST_HEAD(&fs_info->dead_roots);
552 INIT_LIST_HEAD(&fs_info->hashers);
553 spin_lock_init(&fs_info->hash_lock);
555 memset(&fs_info->super_kobj, 0, sizeof(fs_info->super_kobj));
556 init_completion(&fs_info->kobj_unregister);
557 sb_set_blocksize(sb, 4096);
558 fs_info->running_transaction = NULL;
559 fs_info->last_trans_committed = 0;
560 fs_info->tree_root = tree_root;
561 fs_info->extent_root = extent_root;
563 fs_info->btree_inode = new_inode(sb);
564 fs_info->btree_inode->i_ino = 1;
565 fs_info->btree_inode->i_nlink = 1;
566 fs_info->btree_inode->i_size = sb->s_bdev->bd_inode->i_size;
567 fs_info->btree_inode->i_mapping->a_ops = &btree_aops;
568 extent_map_tree_init(&BTRFS_I(fs_info->btree_inode)->extent_tree,
569 fs_info->btree_inode->i_mapping,
571 BTRFS_I(fs_info->btree_inode)->extent_tree.ops = &btree_extent_map_ops;
573 extent_map_tree_init(&fs_info->free_space_cache,
574 fs_info->btree_inode->i_mapping, GFP_NOFS);
575 extent_map_tree_init(&fs_info->block_group_cache,
576 fs_info->btree_inode->i_mapping, GFP_NOFS);
577 extent_map_tree_init(&fs_info->pinned_extents,
578 fs_info->btree_inode->i_mapping, GFP_NOFS);
579 extent_map_tree_init(&fs_info->pending_del,
580 fs_info->btree_inode->i_mapping, GFP_NOFS);
581 extent_map_tree_init(&fs_info->extent_ins,
582 fs_info->btree_inode->i_mapping, GFP_NOFS);
583 fs_info->do_barriers = 1;
584 fs_info->closing = 0;
585 fs_info->total_pinned = 0;
587 INIT_DELAYED_WORK(&fs_info->trans_work, btrfs_transaction_cleaner);
588 BTRFS_I(fs_info->btree_inode)->root = tree_root;
589 memset(&BTRFS_I(fs_info->btree_inode)->location, 0,
590 sizeof(struct btrfs_key));
591 insert_inode_hash(fs_info->btree_inode);
592 mapping_set_gfp_mask(fs_info->btree_inode->i_mapping, GFP_NOFS);
594 mutex_init(&fs_info->trans_mutex);
595 mutex_init(&fs_info->fs_mutex);
598 ret = add_hasher(fs_info, "crc32c");
600 printk("btrfs: failed hash setup, modprobe cryptomgr?\n");
605 __setup_root(512, 512, 512, tree_root,
606 fs_info, BTRFS_ROOT_TREE_OBJECTID);
608 fs_info->sb_buffer = read_tree_block(tree_root,
609 BTRFS_SUPER_INFO_OFFSET,
612 if (!fs_info->sb_buffer)
615 read_extent_buffer(fs_info->sb_buffer, &fs_info->super_copy, 0,
616 sizeof(fs_info->super_copy));
618 read_extent_buffer(fs_info->sb_buffer, fs_info->fsid,
619 (unsigned long)btrfs_super_fsid(fs_info->sb_buffer),
621 disk_super = &fs_info->super_copy;
622 if (!btrfs_super_root(disk_super))
625 nodesize = btrfs_super_nodesize(disk_super);
626 leafsize = btrfs_super_leafsize(disk_super);
627 sectorsize = btrfs_super_sectorsize(disk_super);
628 tree_root->nodesize = nodesize;
629 tree_root->leafsize = leafsize;
630 tree_root->sectorsize = sectorsize;
631 sb_set_blocksize(sb, sectorsize);
633 i_size_write(fs_info->btree_inode,
634 btrfs_super_total_bytes(disk_super));
636 if (strncmp((char *)(&disk_super->magic), BTRFS_MAGIC,
637 sizeof(disk_super->magic))) {
638 printk("btrfs: valid FS not found on %s\n", sb->s_id);
642 blocksize = btrfs_level_size(tree_root,
643 btrfs_super_root_level(disk_super));
645 tree_root->node = read_tree_block(tree_root,
646 btrfs_super_root(disk_super),
648 if (!tree_root->node)
651 mutex_lock(&fs_info->fs_mutex);
653 ret = find_and_setup_root(tree_root, fs_info,
654 BTRFS_EXTENT_TREE_OBJECTID, extent_root);
656 mutex_unlock(&fs_info->fs_mutex);
660 btrfs_read_block_groups(extent_root);
662 fs_info->generation = btrfs_super_generation(disk_super) + 1;
663 mutex_unlock(&fs_info->fs_mutex);
667 free_extent_buffer(tree_root->node);
669 free_extent_buffer(fs_info->sb_buffer);
671 iput(fs_info->btree_inode);
679 int write_ctree_super(struct btrfs_trans_handle *trans, struct btrfs_root
683 struct extent_buffer *super = root->fs_info->sb_buffer;
684 struct inode *btree_inode = root->fs_info->btree_inode;
686 set_extent_buffer_dirty(&BTRFS_I(btree_inode)->extent_tree, super);
687 ret = sync_page_range_nolock(btree_inode, btree_inode->i_mapping,
688 super->start, super->len);
692 int btrfs_free_fs_root(struct btrfs_fs_info *fs_info, struct btrfs_root *root)
694 radix_tree_delete(&fs_info->fs_roots_radix,
695 (unsigned long)root->root_key.objectid);
696 btrfs_sysfs_del_root(root);
700 free_extent_buffer(root->node);
701 if (root->commit_root)
702 free_extent_buffer(root->commit_root);
709 static int del_fs_roots(struct btrfs_fs_info *fs_info)
712 struct btrfs_root *gang[8];
716 ret = radix_tree_gang_lookup(&fs_info->fs_roots_radix,
721 for (i = 0; i < ret; i++)
722 btrfs_free_fs_root(fs_info, gang[i]);
727 int close_ctree(struct btrfs_root *root)
730 struct btrfs_trans_handle *trans;
731 struct btrfs_fs_info *fs_info = root->fs_info;
733 fs_info->closing = 1;
734 btrfs_transaction_flush_work(root);
735 mutex_lock(&fs_info->fs_mutex);
736 btrfs_defrag_dirty_roots(root->fs_info);
737 trans = btrfs_start_transaction(root, 1);
738 ret = btrfs_commit_transaction(trans, root);
739 /* run commit again to drop the original snapshot */
740 trans = btrfs_start_transaction(root, 1);
741 btrfs_commit_transaction(trans, root);
742 ret = btrfs_write_and_wait_transaction(NULL, root);
744 write_ctree_super(NULL, root);
745 mutex_unlock(&fs_info->fs_mutex);
747 if (fs_info->extent_root->node)
748 free_extent_buffer(fs_info->extent_root->node);
750 if (fs_info->tree_root->node)
751 free_extent_buffer(fs_info->tree_root->node);
753 free_extent_buffer(fs_info->sb_buffer);
755 btrfs_free_block_groups(root->fs_info);
756 del_fs_roots(fs_info);
757 extent_map_tree_empty_lru(&BTRFS_I(fs_info->btree_inode)->extent_tree);
758 truncate_inode_pages(fs_info->btree_inode->i_mapping, 0);
759 iput(fs_info->btree_inode);
761 while(!list_empty(&fs_info->hashers)) {
762 struct btrfs_hasher *hasher;
763 hasher = list_entry(fs_info->hashers.next, struct btrfs_hasher,
765 list_del(&hasher->hashers);
766 crypto_free_hash(&fs_info->hash_tfm);
770 kfree(fs_info->extent_root);
771 kfree(fs_info->tree_root);
775 int btrfs_buffer_uptodate(struct extent_buffer *buf)
777 struct inode *btree_inode = buf->first_page->mapping->host;
778 return extent_buffer_uptodate(&BTRFS_I(btree_inode)->extent_tree, buf);
781 int btrfs_set_buffer_uptodate(struct extent_buffer *buf)
783 struct inode *btree_inode = buf->first_page->mapping->host;
784 return set_extent_buffer_uptodate(&BTRFS_I(btree_inode)->extent_tree,
788 void btrfs_mark_buffer_dirty(struct extent_buffer *buf)
790 struct btrfs_root *root = BTRFS_I(buf->first_page->mapping->host)->root;
791 u64 transid = btrfs_header_generation(buf);
792 struct inode *btree_inode = root->fs_info->btree_inode;
794 if (transid != root->fs_info->generation) {
795 printk(KERN_CRIT "transid mismatch buffer %llu, found %Lu running %Lu\n",
796 (unsigned long long)buf->start,
797 transid, root->fs_info->generation);
800 set_extent_buffer_dirty(&BTRFS_I(btree_inode)->extent_tree, buf);
803 void btrfs_btree_balance_dirty(struct btrfs_root *root, unsigned long nr)
805 balance_dirty_pages_ratelimited_nr(
806 root->fs_info->btree_inode->i_mapping, 1);
809 void btrfs_set_buffer_defrag(struct extent_buffer *buf)
811 struct btrfs_root *root = BTRFS_I(buf->first_page->mapping->host)->root;
812 struct inode *btree_inode = root->fs_info->btree_inode;
813 set_extent_bits(&BTRFS_I(btree_inode)->extent_tree, buf->start,
814 buf->start + buf->len - 1, EXTENT_DEFRAG, GFP_NOFS);
817 void btrfs_set_buffer_defrag_done(struct extent_buffer *buf)
819 struct btrfs_root *root = BTRFS_I(buf->first_page->mapping->host)->root;
820 struct inode *btree_inode = root->fs_info->btree_inode;
821 set_extent_bits(&BTRFS_I(btree_inode)->extent_tree, buf->start,
822 buf->start + buf->len - 1, EXTENT_DEFRAG_DONE,
826 int btrfs_buffer_defrag(struct extent_buffer *buf)
828 struct btrfs_root *root = BTRFS_I(buf->first_page->mapping->host)->root;
829 struct inode *btree_inode = root->fs_info->btree_inode;
830 return test_range_bit(&BTRFS_I(btree_inode)->extent_tree,
831 buf->start, buf->start + buf->len - 1, EXTENT_DEFRAG, 0);
834 int btrfs_buffer_defrag_done(struct extent_buffer *buf)
836 struct btrfs_root *root = BTRFS_I(buf->first_page->mapping->host)->root;
837 struct inode *btree_inode = root->fs_info->btree_inode;
838 return test_range_bit(&BTRFS_I(btree_inode)->extent_tree,
839 buf->start, buf->start + buf->len - 1,
840 EXTENT_DEFRAG_DONE, 0);
843 int btrfs_clear_buffer_defrag_done(struct extent_buffer *buf)
845 struct btrfs_root *root = BTRFS_I(buf->first_page->mapping->host)->root;
846 struct inode *btree_inode = root->fs_info->btree_inode;
847 return clear_extent_bits(&BTRFS_I(btree_inode)->extent_tree,
848 buf->start, buf->start + buf->len - 1,
849 EXTENT_DEFRAG_DONE, GFP_NOFS);
852 int btrfs_clear_buffer_defrag(struct extent_buffer *buf)
854 struct btrfs_root *root = BTRFS_I(buf->first_page->mapping->host)->root;
855 struct inode *btree_inode = root->fs_info->btree_inode;
856 return clear_extent_bits(&BTRFS_I(btree_inode)->extent_tree,
857 buf->start, buf->start + buf->len - 1,
858 EXTENT_DEFRAG, GFP_NOFS);
861 int btrfs_read_buffer(struct extent_buffer *buf)
863 struct btrfs_root *root = BTRFS_I(buf->first_page->mapping->host)->root;
864 struct inode *btree_inode = root->fs_info->btree_inode;
865 return read_extent_buffer_pages(&BTRFS_I(btree_inode)->extent_tree,
869 static struct extent_map_ops btree_extent_map_ops = {
870 .writepage_io_hook = btree_writepage_io_hook,