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
27 #include <linux/workqueue.h>
30 #include "transaction.h"
31 #include "btrfs_inode.h"
33 #include "print-tree.h"
36 static int check_tree_block(struct btrfs_root
*root
, struct extent_buffer
*buf
)
38 if (extent_buffer_blocknr(buf
) != btrfs_header_blocknr(buf
)) {
39 printk(KERN_CRIT
"buf blocknr(buf) is %llu, header is %llu\n",
40 (unsigned long long)extent_buffer_blocknr(buf
),
41 (unsigned long long)btrfs_header_blocknr(buf
));
48 static struct extent_io_ops btree_extent_io_ops
;
49 static struct workqueue_struct
*end_io_workqueue
;
55 struct btrfs_fs_info
*info
;
57 struct list_head list
;
60 struct extent_map
*btree_get_extent(struct inode
*inode
, struct page
*page
,
61 size_t page_offset
, u64 start
, u64 len
,
64 struct extent_map_tree
*em_tree
= &BTRFS_I(inode
)->extent_tree
;
65 struct extent_map
*em
;
69 spin_lock(&em_tree
->lock
);
70 em
= lookup_extent_mapping(em_tree
, start
, len
);
71 spin_unlock(&em_tree
->lock
);
75 em
= alloc_extent_map(GFP_NOFS
);
77 em
= ERR_PTR(-ENOMEM
);
81 em
->len
= i_size_read(inode
);
83 em
->bdev
= inode
->i_sb
->s_bdev
;
85 spin_lock(&em_tree
->lock
);
86 ret
= add_extent_mapping(em_tree
, em
);
87 spin_unlock(&em_tree
->lock
);
100 u32
btrfs_csum_data(struct btrfs_root
*root
, char *data
, u32 seed
, size_t len
)
102 return crc32c(seed
, data
, len
);
105 void btrfs_csum_final(u32 crc
, char *result
)
107 *(__le32
*)result
= ~cpu_to_le32(crc
);
110 static int csum_tree_block(struct btrfs_root
*root
, struct extent_buffer
*buf
,
113 char result
[BTRFS_CRC32_SIZE
];
115 unsigned long cur_len
;
116 unsigned long offset
= BTRFS_CSUM_SIZE
;
117 char *map_token
= NULL
;
119 unsigned long map_start
;
120 unsigned long map_len
;
124 len
= buf
->len
- offset
;
126 err
= map_private_extent_buffer(buf
, offset
, 32,
128 &map_start
, &map_len
, KM_USER0
);
130 printk("failed to map extent buffer! %lu\n",
134 cur_len
= min(len
, map_len
- (offset
- map_start
));
135 crc
= btrfs_csum_data(root
, kaddr
+ offset
- map_start
,
139 unmap_extent_buffer(buf
, map_token
, KM_USER0
);
141 btrfs_csum_final(crc
, result
);
144 int from_this_trans
= 0;
146 if (root
->fs_info
->running_transaction
&&
147 btrfs_header_generation(buf
) ==
148 root
->fs_info
->running_transaction
->transid
)
151 /* FIXME, this is not good */
152 if (memcmp_extent_buffer(buf
, result
, 0, BTRFS_CRC32_SIZE
)) {
155 memcpy(&found
, result
, BTRFS_CRC32_SIZE
);
157 read_extent_buffer(buf
, &val
, 0, BTRFS_CRC32_SIZE
);
159 printk("btrfs: %s checksum verify failed on %llu "
160 "wanted %X found %X from_this_trans %d "
162 root
->fs_info
->sb
->s_id
,
163 buf
->start
, val
, found
, from_this_trans
,
164 btrfs_header_level(buf
));
168 write_extent_buffer(buf
, result
, 0, BTRFS_CRC32_SIZE
);
174 int csum_dirty_buffer(struct btrfs_root
*root
, struct page
*page
)
176 struct extent_io_tree
*tree
;
177 u64 start
= (u64
)page
->index
<< PAGE_CACHE_SHIFT
;
181 struct extent_buffer
*eb
;
182 tree
= &BTRFS_I(page
->mapping
->host
)->io_tree
;
184 if (page
->private == EXTENT_PAGE_PRIVATE
)
188 len
= page
->private >> 2;
192 eb
= alloc_extent_buffer(tree
, start
, len
, page
, GFP_NOFS
);
193 read_extent_buffer_pages(tree
, eb
, start
+ PAGE_CACHE_SIZE
, 1,
195 btrfs_clear_buffer_defrag(eb
);
196 found_start
= btrfs_header_bytenr(eb
);
197 if (found_start
!= start
) {
198 printk("warning: eb start incorrect %Lu buffer %Lu len %lu\n",
199 start
, found_start
, len
);
203 if (eb
->first_page
!= page
) {
204 printk("bad first page %lu %lu\n", eb
->first_page
->index
,
209 if (!PageUptodate(page
)) {
210 printk("csum not up to date page %lu\n", page
->index
);
214 found_level
= btrfs_header_level(eb
);
215 spin_lock(&root
->fs_info
->hash_lock
);
216 btrfs_set_header_flag(eb
, BTRFS_HEADER_FLAG_WRITTEN
);
217 spin_unlock(&root
->fs_info
->hash_lock
);
218 csum_tree_block(root
, eb
, 0);
220 free_extent_buffer(eb
);
225 static int btree_writepage_io_hook(struct page
*page
, u64 start
, u64 end
)
227 struct btrfs_root
*root
= BTRFS_I(page
->mapping
->host
)->root
;
229 csum_dirty_buffer(root
, page
);
233 int btree_readpage_end_io_hook(struct page
*page
, u64 start
, u64 end
,
234 struct extent_state
*state
)
236 struct extent_io_tree
*tree
;
240 struct extent_buffer
*eb
;
241 struct btrfs_root
*root
= BTRFS_I(page
->mapping
->host
)->root
;
244 tree
= &BTRFS_I(page
->mapping
->host
)->io_tree
;
245 if (page
->private == EXTENT_PAGE_PRIVATE
)
249 len
= page
->private >> 2;
253 eb
= alloc_extent_buffer(tree
, start
, len
, page
, GFP_NOFS
);
254 read_extent_buffer_pages(tree
, eb
, start
+ PAGE_CACHE_SIZE
, 1,
256 btrfs_clear_buffer_defrag(eb
);
257 found_start
= btrfs_header_bytenr(eb
);
258 if (found_start
!= start
) {
259 printk("warning: eb start incorrect %Lu buffer %Lu len %lu\n",
260 start
, found_start
, len
);
264 if (eb
->first_page
!= page
) {
265 printk("bad first page %lu %lu\n", eb
->first_page
->index
,
270 found_level
= btrfs_header_level(eb
);
272 ret
= csum_tree_block(root
, eb
, 1);
274 end
= min_t(u64
, eb
->len
, PAGE_CACHE_SIZE
);
275 end
= eb
->start
+ end
- 1;
276 release_extent_buffer_tail_pages(eb
);
278 free_extent_buffer(eb
);
283 #if LINUX_VERSION_CODE > KERNEL_VERSION(2,6,23)
284 static void end_workqueue_bio(struct bio
*bio
, int err
)
286 static int end_workqueue_bio(struct bio
*bio
,
287 unsigned int bytes_done
, int err
)
290 struct end_io_wq
*end_io_wq
= bio
->bi_private
;
291 struct btrfs_fs_info
*fs_info
;
294 #if LINUX_VERSION_CODE <= KERNEL_VERSION(2,6,23)
299 fs_info
= end_io_wq
->info
;
300 spin_lock_irqsave(&fs_info
->end_io_work_lock
, flags
);
301 end_io_wq
->error
= err
;
302 list_add_tail(&end_io_wq
->list
, &fs_info
->end_io_work_list
);
303 spin_unlock_irqrestore(&fs_info
->end_io_work_lock
, flags
);
304 queue_work(end_io_workqueue
, &fs_info
->end_io_work
);
306 #if LINUX_VERSION_CODE <= KERNEL_VERSION(2,6,23)
311 static int btree_submit_bio_hook(struct inode
*inode
, int rw
, struct bio
*bio
)
313 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
314 struct end_io_wq
*end_io_wq
;
316 offset
= bio
->bi_sector
<< 9;
318 if (rw
& (1 << BIO_RW
)) {
319 return btrfs_map_bio(BTRFS_I(inode
)->root
, rw
, bio
);
322 end_io_wq
= kmalloc(sizeof(*end_io_wq
), GFP_NOFS
);
326 end_io_wq
->private = bio
->bi_private
;
327 end_io_wq
->end_io
= bio
->bi_end_io
;
328 end_io_wq
->info
= root
->fs_info
;
329 end_io_wq
->error
= 0;
330 end_io_wq
->bio
= bio
;
332 bio
->bi_private
= end_io_wq
;
333 bio
->bi_end_io
= end_workqueue_bio
;
335 if (offset
== BTRFS_SUPER_INFO_OFFSET
) {
336 bio
->bi_bdev
= root
->fs_info
->sb
->s_bdev
;
340 return btrfs_map_bio(BTRFS_I(inode
)->root
, rw
, bio
);
343 static int btree_writepage(struct page
*page
, struct writeback_control
*wbc
)
345 struct extent_io_tree
*tree
;
346 tree
= &BTRFS_I(page
->mapping
->host
)->io_tree
;
347 return extent_write_full_page(tree
, page
, btree_get_extent
, wbc
);
350 static int btree_writepages(struct address_space
*mapping
,
351 struct writeback_control
*wbc
)
353 struct extent_io_tree
*tree
;
354 tree
= &BTRFS_I(mapping
->host
)->io_tree
;
355 if (wbc
->sync_mode
== WB_SYNC_NONE
) {
358 unsigned long thresh
= 96 * 1024 * 1024;
360 if (wbc
->for_kupdate
)
363 if (current_is_pdflush()) {
364 thresh
= 96 * 1024 * 1024;
366 thresh
= 8 * 1024 * 1024;
368 num_dirty
= count_range_bits(tree
, &start
, (u64
)-1,
369 thresh
, EXTENT_DIRTY
);
370 if (num_dirty
< thresh
) {
374 return extent_writepages(tree
, mapping
, btree_get_extent
, wbc
);
377 int btree_readpage(struct file
*file
, struct page
*page
)
379 struct extent_io_tree
*tree
;
380 tree
= &BTRFS_I(page
->mapping
->host
)->io_tree
;
381 return extent_read_full_page(tree
, page
, btree_get_extent
);
384 static int btree_releasepage(struct page
*page
, gfp_t gfp_flags
)
386 struct extent_io_tree
*tree
;
387 struct extent_map_tree
*map
;
390 tree
= &BTRFS_I(page
->mapping
->host
)->io_tree
;
391 map
= &BTRFS_I(page
->mapping
->host
)->extent_tree
;
392 ret
= try_release_extent_mapping(map
, tree
, page
, gfp_flags
);
394 invalidate_extent_lru(tree
, page_offset(page
), PAGE_CACHE_SIZE
);
395 ClearPagePrivate(page
);
396 set_page_private(page
, 0);
397 page_cache_release(page
);
402 static void btree_invalidatepage(struct page
*page
, unsigned long offset
)
404 struct extent_io_tree
*tree
;
405 tree
= &BTRFS_I(page
->mapping
->host
)->io_tree
;
406 extent_invalidatepage(tree
, page
, offset
);
407 btree_releasepage(page
, GFP_NOFS
);
411 static int btree_writepage(struct page
*page
, struct writeback_control
*wbc
)
413 struct buffer_head
*bh
;
414 struct btrfs_root
*root
= BTRFS_I(page
->mapping
->host
)->root
;
415 struct buffer_head
*head
;
416 if (!page_has_buffers(page
)) {
417 create_empty_buffers(page
, root
->fs_info
->sb
->s_blocksize
,
418 (1 << BH_Dirty
)|(1 << BH_Uptodate
));
420 head
= page_buffers(page
);
423 if (buffer_dirty(bh
))
424 csum_tree_block(root
, bh
, 0);
425 bh
= bh
->b_this_page
;
426 } while (bh
!= head
);
427 return block_write_full_page(page
, btree_get_block
, wbc
);
431 static struct address_space_operations btree_aops
= {
432 .readpage
= btree_readpage
,
433 .writepage
= btree_writepage
,
434 .writepages
= btree_writepages
,
435 .releasepage
= btree_releasepage
,
436 .invalidatepage
= btree_invalidatepage
,
437 .sync_page
= block_sync_page
,
440 int readahead_tree_block(struct btrfs_root
*root
, u64 bytenr
, u32 blocksize
)
442 struct extent_buffer
*buf
= NULL
;
443 struct inode
*btree_inode
= root
->fs_info
->btree_inode
;
446 buf
= btrfs_find_create_tree_block(root
, bytenr
, blocksize
);
449 read_extent_buffer_pages(&BTRFS_I(btree_inode
)->io_tree
,
450 buf
, 0, 0, btree_get_extent
);
451 free_extent_buffer(buf
);
455 static int close_all_devices(struct btrfs_fs_info
*fs_info
)
457 struct list_head
*list
;
458 struct list_head
*next
;
459 struct btrfs_device
*device
;
461 list
= &fs_info
->fs_devices
->devices
;
462 list_for_each(next
, list
) {
463 device
= list_entry(next
, struct btrfs_device
, dev_list
);
464 if (device
->bdev
&& device
->bdev
!= fs_info
->sb
->s_bdev
)
465 close_bdev_excl(device
->bdev
);
471 int btrfs_verify_block_csum(struct btrfs_root
*root
,
472 struct extent_buffer
*buf
)
474 return btrfs_buffer_uptodate(buf
);
477 struct extent_buffer
*btrfs_find_tree_block(struct btrfs_root
*root
,
478 u64 bytenr
, u32 blocksize
)
480 struct inode
*btree_inode
= root
->fs_info
->btree_inode
;
481 struct extent_buffer
*eb
;
482 eb
= find_extent_buffer(&BTRFS_I(btree_inode
)->io_tree
,
483 bytenr
, blocksize
, GFP_NOFS
);
487 struct extent_buffer
*btrfs_find_create_tree_block(struct btrfs_root
*root
,
488 u64 bytenr
, u32 blocksize
)
490 struct inode
*btree_inode
= root
->fs_info
->btree_inode
;
491 struct extent_buffer
*eb
;
493 eb
= alloc_extent_buffer(&BTRFS_I(btree_inode
)->io_tree
,
494 bytenr
, blocksize
, NULL
, GFP_NOFS
);
499 struct extent_buffer
*read_tree_block(struct btrfs_root
*root
, u64 bytenr
,
502 struct extent_buffer
*buf
= NULL
;
503 struct inode
*btree_inode
= root
->fs_info
->btree_inode
;
504 struct extent_io_tree
*io_tree
;
507 io_tree
= &BTRFS_I(btree_inode
)->io_tree
;
509 buf
= btrfs_find_create_tree_block(root
, bytenr
, blocksize
);
513 ret
= read_extent_buffer_pages(&BTRFS_I(btree_inode
)->io_tree
, buf
, 0,
514 1, btree_get_extent
);
517 buf
->flags
|= EXTENT_UPTODATE
;
523 int clean_tree_block(struct btrfs_trans_handle
*trans
, struct btrfs_root
*root
,
524 struct extent_buffer
*buf
)
526 struct inode
*btree_inode
= root
->fs_info
->btree_inode
;
527 if (btrfs_header_generation(buf
) ==
528 root
->fs_info
->running_transaction
->transid
)
529 clear_extent_buffer_dirty(&BTRFS_I(btree_inode
)->io_tree
,
534 int wait_on_tree_block_writeback(struct btrfs_root
*root
,
535 struct extent_buffer
*buf
)
537 struct inode
*btree_inode
= root
->fs_info
->btree_inode
;
538 wait_on_extent_buffer_writeback(&BTRFS_I(btree_inode
)->io_tree
,
543 static int __setup_root(u32 nodesize
, u32 leafsize
, u32 sectorsize
,
544 u32 stripesize
, struct btrfs_root
*root
,
545 struct btrfs_fs_info
*fs_info
,
550 root
->commit_root
= NULL
;
551 root
->sectorsize
= sectorsize
;
552 root
->nodesize
= nodesize
;
553 root
->leafsize
= leafsize
;
554 root
->stripesize
= stripesize
;
556 root
->track_dirty
= 0;
558 root
->fs_info
= fs_info
;
559 root
->objectid
= objectid
;
560 root
->last_trans
= 0;
561 root
->highest_inode
= 0;
562 root
->last_inode_alloc
= 0;
566 INIT_LIST_HEAD(&root
->dirty_list
);
567 memset(&root
->root_key
, 0, sizeof(root
->root_key
));
568 memset(&root
->root_item
, 0, sizeof(root
->root_item
));
569 memset(&root
->defrag_progress
, 0, sizeof(root
->defrag_progress
));
570 memset(&root
->root_kobj
, 0, sizeof(root
->root_kobj
));
571 init_completion(&root
->kobj_unregister
);
572 root
->defrag_running
= 0;
573 root
->defrag_level
= 0;
574 root
->root_key
.objectid
= objectid
;
578 static int find_and_setup_root(struct btrfs_root
*tree_root
,
579 struct btrfs_fs_info
*fs_info
,
581 struct btrfs_root
*root
)
586 __setup_root(tree_root
->nodesize
, tree_root
->leafsize
,
587 tree_root
->sectorsize
, tree_root
->stripesize
,
588 root
, fs_info
, objectid
);
589 ret
= btrfs_find_last_root(tree_root
, objectid
,
590 &root
->root_item
, &root
->root_key
);
593 blocksize
= btrfs_level_size(root
, btrfs_root_level(&root
->root_item
));
594 root
->node
= read_tree_block(root
, btrfs_root_bytenr(&root
->root_item
),
600 struct btrfs_root
*btrfs_read_fs_root_no_radix(struct btrfs_fs_info
*fs_info
,
601 struct btrfs_key
*location
)
603 struct btrfs_root
*root
;
604 struct btrfs_root
*tree_root
= fs_info
->tree_root
;
605 struct btrfs_path
*path
;
606 struct extent_buffer
*l
;
611 root
= kzalloc(sizeof(*root
), GFP_NOFS
);
613 return ERR_PTR(-ENOMEM
);
614 if (location
->offset
== (u64
)-1) {
615 ret
= find_and_setup_root(tree_root
, fs_info
,
616 location
->objectid
, root
);
624 __setup_root(tree_root
->nodesize
, tree_root
->leafsize
,
625 tree_root
->sectorsize
, tree_root
->stripesize
,
626 root
, fs_info
, location
->objectid
);
628 path
= btrfs_alloc_path();
630 ret
= btrfs_search_slot(NULL
, tree_root
, location
, path
, 0, 0);
637 read_extent_buffer(l
, &root
->root_item
,
638 btrfs_item_ptr_offset(l
, path
->slots
[0]),
639 sizeof(root
->root_item
));
640 memcpy(&root
->root_key
, location
, sizeof(*location
));
643 btrfs_release_path(root
, path
);
644 btrfs_free_path(path
);
649 blocksize
= btrfs_level_size(root
, btrfs_root_level(&root
->root_item
));
650 root
->node
= read_tree_block(root
, btrfs_root_bytenr(&root
->root_item
),
655 ret
= btrfs_find_highest_inode(root
, &highest_inode
);
657 root
->highest_inode
= highest_inode
;
658 root
->last_inode_alloc
= highest_inode
;
663 struct btrfs_root
*btrfs_lookup_fs_root(struct btrfs_fs_info
*fs_info
,
666 struct btrfs_root
*root
;
668 if (root_objectid
== BTRFS_ROOT_TREE_OBJECTID
)
669 return fs_info
->tree_root
;
670 if (root_objectid
== BTRFS_EXTENT_TREE_OBJECTID
)
671 return fs_info
->extent_root
;
673 root
= radix_tree_lookup(&fs_info
->fs_roots_radix
,
674 (unsigned long)root_objectid
);
678 struct btrfs_root
*btrfs_read_fs_root_no_name(struct btrfs_fs_info
*fs_info
,
679 struct btrfs_key
*location
)
681 struct btrfs_root
*root
;
684 if (location
->objectid
== BTRFS_ROOT_TREE_OBJECTID
)
685 return fs_info
->tree_root
;
686 if (location
->objectid
== BTRFS_EXTENT_TREE_OBJECTID
)
687 return fs_info
->extent_root
;
689 root
= radix_tree_lookup(&fs_info
->fs_roots_radix
,
690 (unsigned long)location
->objectid
);
694 root
= btrfs_read_fs_root_no_radix(fs_info
, location
);
697 ret
= radix_tree_insert(&fs_info
->fs_roots_radix
,
698 (unsigned long)root
->root_key
.objectid
,
701 free_extent_buffer(root
->node
);
705 ret
= btrfs_find_dead_roots(fs_info
->tree_root
,
706 root
->root_key
.objectid
, root
);
712 struct btrfs_root
*btrfs_read_fs_root(struct btrfs_fs_info
*fs_info
,
713 struct btrfs_key
*location
,
714 const char *name
, int namelen
)
716 struct btrfs_root
*root
;
719 root
= btrfs_read_fs_root_no_name(fs_info
, location
);
726 ret
= btrfs_set_root_name(root
, name
, namelen
);
728 free_extent_buffer(root
->node
);
733 ret
= btrfs_sysfs_add_root(root
);
735 free_extent_buffer(root
->node
);
744 static int add_hasher(struct btrfs_fs_info
*info
, char *type
) {
745 struct btrfs_hasher
*hasher
;
747 hasher
= kmalloc(sizeof(*hasher
), GFP_NOFS
);
750 hasher
->hash_tfm
= crypto_alloc_hash(type
, 0, CRYPTO_ALG_ASYNC
);
751 if (!hasher
->hash_tfm
) {
755 spin_lock(&info
->hash_lock
);
756 list_add(&hasher
->list
, &info
->hashers
);
757 spin_unlock(&info
->hash_lock
);
762 static int btrfs_congested_fn(void *congested_data
, int bdi_bits
)
764 struct btrfs_fs_info
*info
= (struct btrfs_fs_info
*)congested_data
;
766 struct list_head
*cur
;
767 struct btrfs_device
*device
;
768 struct backing_dev_info
*bdi
;
770 list_for_each(cur
, &info
->fs_devices
->devices
) {
771 device
= list_entry(cur
, struct btrfs_device
, dev_list
);
772 bdi
= blk_get_backing_dev_info(device
->bdev
);
773 if (bdi
&& bdi_congested(bdi
, bdi_bits
)) {
781 void btrfs_unplug_io_fn(struct backing_dev_info
*bdi
, struct page
*page
)
783 struct list_head
*cur
;
784 struct btrfs_device
*device
;
785 struct btrfs_fs_info
*info
;
787 info
= (struct btrfs_fs_info
*)bdi
->unplug_io_data
;
788 list_for_each(cur
, &info
->fs_devices
->devices
) {
789 device
= list_entry(cur
, struct btrfs_device
, dev_list
);
790 bdi
= blk_get_backing_dev_info(device
->bdev
);
791 if (bdi
->unplug_io_fn
) {
792 bdi
->unplug_io_fn(bdi
, page
);
797 static int setup_bdi(struct btrfs_fs_info
*info
, struct backing_dev_info
*bdi
)
800 bdi
->ra_pages
= default_backing_dev_info
.ra_pages
* 4;
802 bdi
->capabilities
= default_backing_dev_info
.capabilities
;
803 bdi
->unplug_io_fn
= btrfs_unplug_io_fn
;
804 bdi
->unplug_io_data
= info
;
805 bdi
->congested_fn
= btrfs_congested_fn
;
806 bdi
->congested_data
= info
;
810 static int bio_ready_for_csum(struct bio
*bio
)
816 struct extent_io_tree
*io_tree
= NULL
;
817 struct btrfs_fs_info
*info
= NULL
;
818 struct bio_vec
*bvec
;
822 bio_for_each_segment(bvec
, bio
, i
) {
823 page
= bvec
->bv_page
;
824 if (page
->private == EXTENT_PAGE_PRIVATE
) {
825 length
+= bvec
->bv_len
;
828 if (!page
->private) {
829 length
+= bvec
->bv_len
;
832 length
= bvec
->bv_len
;
833 buf_len
= page
->private >> 2;
834 start
= page_offset(page
) + bvec
->bv_offset
;
835 io_tree
= &BTRFS_I(page
->mapping
->host
)->io_tree
;
836 info
= BTRFS_I(page
->mapping
->host
)->root
->fs_info
;
838 /* are we fully contained in this bio? */
839 if (buf_len
<= length
)
842 ret
= extent_range_uptodate(io_tree
, start
+ length
,
843 start
+ buf_len
- 1);
849 #if LINUX_VERSION_CODE <= KERNEL_VERSION(2,6,18)
850 void btrfs_end_io_csum(void *p
)
852 void btrfs_end_io_csum(struct work_struct
*work
)
855 #if LINUX_VERSION_CODE <= KERNEL_VERSION(2,6,18)
856 struct btrfs_fs_info
*fs_info
= p
;
858 struct btrfs_fs_info
*fs_info
= container_of(work
,
859 struct btrfs_fs_info
,
863 struct end_io_wq
*end_io_wq
;
865 struct list_head
*next
;
870 spin_lock_irqsave(&fs_info
->end_io_work_lock
, flags
);
871 if (list_empty(&fs_info
->end_io_work_list
)) {
872 spin_unlock_irqrestore(&fs_info
->end_io_work_lock
,
876 next
= fs_info
->end_io_work_list
.next
;
878 spin_unlock_irqrestore(&fs_info
->end_io_work_lock
, flags
);
880 end_io_wq
= list_entry(next
, struct end_io_wq
, list
);
882 bio
= end_io_wq
->bio
;
883 if (!bio_ready_for_csum(bio
)) {
884 spin_lock_irqsave(&fs_info
->end_io_work_lock
, flags
);
885 was_empty
= list_empty(&fs_info
->end_io_work_list
);
886 list_add_tail(&end_io_wq
->list
,
887 &fs_info
->end_io_work_list
);
888 spin_unlock_irqrestore(&fs_info
->end_io_work_lock
,
894 error
= end_io_wq
->error
;
895 bio
->bi_private
= end_io_wq
->private;
896 bio
->bi_end_io
= end_io_wq
->end_io
;
898 bio_endio(bio
, error
);
903 struct btrfs_root
*open_ctree(struct super_block
*sb
,
904 struct btrfs_fs_devices
*fs_devices
)
911 struct btrfs_root
*extent_root
= kmalloc(sizeof(struct btrfs_root
),
913 struct btrfs_root
*tree_root
= kmalloc(sizeof(struct btrfs_root
),
915 struct btrfs_fs_info
*fs_info
= kzalloc(sizeof(*fs_info
),
917 struct btrfs_root
*chunk_root
= kmalloc(sizeof(struct btrfs_root
),
919 struct btrfs_root
*dev_root
= kmalloc(sizeof(struct btrfs_root
),
923 struct btrfs_super_block
*disk_super
;
925 if (!extent_root
|| !tree_root
|| !fs_info
) {
929 end_io_workqueue
= create_workqueue("btrfs-end-io");
930 BUG_ON(!end_io_workqueue
);
932 INIT_RADIX_TREE(&fs_info
->fs_roots_radix
, GFP_NOFS
);
933 INIT_LIST_HEAD(&fs_info
->trans_list
);
934 INIT_LIST_HEAD(&fs_info
->dead_roots
);
935 INIT_LIST_HEAD(&fs_info
->hashers
);
936 INIT_LIST_HEAD(&fs_info
->end_io_work_list
);
937 spin_lock_init(&fs_info
->hash_lock
);
938 spin_lock_init(&fs_info
->end_io_work_lock
);
939 spin_lock_init(&fs_info
->delalloc_lock
);
940 spin_lock_init(&fs_info
->new_trans_lock
);
942 init_completion(&fs_info
->kobj_unregister
);
943 sb_set_blocksize(sb
, 4096);
944 fs_info
->tree_root
= tree_root
;
945 fs_info
->extent_root
= extent_root
;
946 fs_info
->chunk_root
= chunk_root
;
947 fs_info
->dev_root
= dev_root
;
948 fs_info
->fs_devices
= fs_devices
;
949 INIT_LIST_HEAD(&fs_info
->dirty_cowonly_roots
);
950 INIT_LIST_HEAD(&fs_info
->space_info
);
951 btrfs_mapping_init(&fs_info
->mapping_tree
);
953 fs_info
->max_extent
= (u64
)-1;
954 fs_info
->max_inline
= 8192 * 1024;
955 setup_bdi(fs_info
, &fs_info
->bdi
);
956 fs_info
->btree_inode
= new_inode(sb
);
957 fs_info
->btree_inode
->i_ino
= 1;
958 fs_info
->btree_inode
->i_nlink
= 1;
959 fs_info
->btree_inode
->i_size
= sb
->s_bdev
->bd_inode
->i_size
;
960 fs_info
->btree_inode
->i_mapping
->a_ops
= &btree_aops
;
961 fs_info
->btree_inode
->i_mapping
->backing_dev_info
= &fs_info
->bdi
;
963 extent_io_tree_init(&BTRFS_I(fs_info
->btree_inode
)->io_tree
,
964 fs_info
->btree_inode
->i_mapping
,
966 extent_map_tree_init(&BTRFS_I(fs_info
->btree_inode
)->extent_tree
,
969 BTRFS_I(fs_info
->btree_inode
)->io_tree
.ops
= &btree_extent_io_ops
;
971 extent_io_tree_init(&fs_info
->free_space_cache
,
972 fs_info
->btree_inode
->i_mapping
, GFP_NOFS
);
973 extent_io_tree_init(&fs_info
->block_group_cache
,
974 fs_info
->btree_inode
->i_mapping
, GFP_NOFS
);
975 extent_io_tree_init(&fs_info
->pinned_extents
,
976 fs_info
->btree_inode
->i_mapping
, GFP_NOFS
);
977 extent_io_tree_init(&fs_info
->pending_del
,
978 fs_info
->btree_inode
->i_mapping
, GFP_NOFS
);
979 extent_io_tree_init(&fs_info
->extent_ins
,
980 fs_info
->btree_inode
->i_mapping
, GFP_NOFS
);
981 fs_info
->do_barriers
= 1;
983 INIT_WORK(&fs_info
->end_io_work
, btrfs_end_io_csum
);
984 #if LINUX_VERSION_CODE <= KERNEL_VERSION(2,6,18)
985 INIT_WORK(&fs_info
->trans_work
, btrfs_transaction_cleaner
, fs_info
);
987 INIT_DELAYED_WORK(&fs_info
->trans_work
, btrfs_transaction_cleaner
);
989 BTRFS_I(fs_info
->btree_inode
)->root
= tree_root
;
990 memset(&BTRFS_I(fs_info
->btree_inode
)->location
, 0,
991 sizeof(struct btrfs_key
));
992 insert_inode_hash(fs_info
->btree_inode
);
993 mapping_set_gfp_mask(fs_info
->btree_inode
->i_mapping
, GFP_NOFS
);
995 mutex_init(&fs_info
->trans_mutex
);
996 mutex_init(&fs_info
->fs_mutex
);
999 ret
= add_hasher(fs_info
, "crc32c");
1001 printk("btrfs: failed hash setup, modprobe cryptomgr?\n");
1006 __setup_root(4096, 4096, 4096, 4096, tree_root
,
1007 fs_info
, BTRFS_ROOT_TREE_OBJECTID
);
1009 fs_info
->sb_buffer
= read_tree_block(tree_root
,
1010 BTRFS_SUPER_INFO_OFFSET
,
1013 if (!fs_info
->sb_buffer
)
1016 read_extent_buffer(fs_info
->sb_buffer
, &fs_info
->super_copy
, 0,
1017 sizeof(fs_info
->super_copy
));
1019 read_extent_buffer(fs_info
->sb_buffer
, fs_info
->fsid
,
1020 (unsigned long)btrfs_super_fsid(fs_info
->sb_buffer
),
1023 disk_super
= &fs_info
->super_copy
;
1024 if (!btrfs_super_root(disk_super
))
1025 goto fail_sb_buffer
;
1027 if (btrfs_super_num_devices(disk_super
) != fs_devices
->num_devices
) {
1028 printk("Btrfs: wanted %llu devices, but found %llu\n",
1029 (unsigned long long)btrfs_super_num_devices(disk_super
),
1030 (unsigned long long)fs_devices
->num_devices
);
1031 goto fail_sb_buffer
;
1033 nodesize
= btrfs_super_nodesize(disk_super
);
1034 leafsize
= btrfs_super_leafsize(disk_super
);
1035 sectorsize
= btrfs_super_sectorsize(disk_super
);
1036 stripesize
= btrfs_super_stripesize(disk_super
);
1037 tree_root
->nodesize
= nodesize
;
1038 tree_root
->leafsize
= leafsize
;
1039 tree_root
->sectorsize
= sectorsize
;
1040 tree_root
->stripesize
= stripesize
;
1041 sb_set_blocksize(sb
, sectorsize
);
1043 i_size_write(fs_info
->btree_inode
,
1044 btrfs_super_total_bytes(disk_super
));
1046 if (strncmp((char *)(&disk_super
->magic
), BTRFS_MAGIC
,
1047 sizeof(disk_super
->magic
))) {
1048 printk("btrfs: valid FS not found on %s\n", sb
->s_id
);
1049 goto fail_sb_buffer
;
1052 mutex_lock(&fs_info
->fs_mutex
);
1054 ret
= btrfs_read_sys_array(tree_root
);
1057 blocksize
= btrfs_level_size(tree_root
,
1058 btrfs_super_chunk_root_level(disk_super
));
1060 __setup_root(nodesize
, leafsize
, sectorsize
, stripesize
,
1061 chunk_root
, fs_info
, BTRFS_CHUNK_TREE_OBJECTID
);
1063 chunk_root
->node
= read_tree_block(chunk_root
,
1064 btrfs_super_chunk_root(disk_super
),
1066 BUG_ON(!chunk_root
->node
);
1068 ret
= btrfs_read_chunk_tree(chunk_root
);
1071 blocksize
= btrfs_level_size(tree_root
,
1072 btrfs_super_root_level(disk_super
));
1075 tree_root
->node
= read_tree_block(tree_root
,
1076 btrfs_super_root(disk_super
),
1078 if (!tree_root
->node
)
1079 goto fail_sb_buffer
;
1082 ret
= find_and_setup_root(tree_root
, fs_info
,
1083 BTRFS_EXTENT_TREE_OBJECTID
, extent_root
);
1085 goto fail_tree_root
;
1086 extent_root
->track_dirty
= 1;
1088 ret
= find_and_setup_root(tree_root
, fs_info
,
1089 BTRFS_DEV_TREE_OBJECTID
, dev_root
);
1090 dev_root
->track_dirty
= 1;
1093 goto fail_extent_root
;
1095 btrfs_read_block_groups(extent_root
);
1097 fs_info
->generation
= btrfs_super_generation(disk_super
) + 1;
1098 fs_info
->data_alloc_profile
= (u64
)-1;
1099 fs_info
->metadata_alloc_profile
= (u64
)-1;
1100 fs_info
->system_alloc_profile
= fs_info
->metadata_alloc_profile
;
1102 mutex_unlock(&fs_info
->fs_mutex
);
1106 free_extent_buffer(extent_root
->node
);
1108 mutex_unlock(&fs_info
->fs_mutex
);
1109 free_extent_buffer(tree_root
->node
);
1111 free_extent_buffer(fs_info
->sb_buffer
);
1112 extent_io_tree_empty_lru(&BTRFS_I(fs_info
->btree_inode
)->io_tree
);
1114 iput(fs_info
->btree_inode
);
1116 close_all_devices(fs_info
);
1119 bdi_destroy(&fs_info
->bdi
);
1121 return ERR_PTR(err
);
1124 int write_ctree_super(struct btrfs_trans_handle
*trans
, struct btrfs_root
1128 struct extent_buffer
*super
= root
->fs_info
->sb_buffer
;
1129 struct inode
*btree_inode
= root
->fs_info
->btree_inode
;
1130 struct super_block
*sb
= root
->fs_info
->sb
;
1132 if (!btrfs_test_opt(root
, NOBARRIER
))
1133 blkdev_issue_flush(sb
->s_bdev
, NULL
);
1134 set_extent_buffer_dirty(&BTRFS_I(btree_inode
)->io_tree
, super
);
1135 ret
= sync_page_range_nolock(btree_inode
, btree_inode
->i_mapping
,
1136 super
->start
, super
->len
);
1137 if (!btrfs_test_opt(root
, NOBARRIER
))
1138 blkdev_issue_flush(sb
->s_bdev
, NULL
);
1142 int btrfs_free_fs_root(struct btrfs_fs_info
*fs_info
, struct btrfs_root
*root
)
1144 radix_tree_delete(&fs_info
->fs_roots_radix
,
1145 (unsigned long)root
->root_key
.objectid
);
1147 btrfs_sysfs_del_root(root
);
1151 free_extent_buffer(root
->node
);
1152 if (root
->commit_root
)
1153 free_extent_buffer(root
->commit_root
);
1160 static int del_fs_roots(struct btrfs_fs_info
*fs_info
)
1163 struct btrfs_root
*gang
[8];
1167 ret
= radix_tree_gang_lookup(&fs_info
->fs_roots_radix
,
1172 for (i
= 0; i
< ret
; i
++)
1173 btrfs_free_fs_root(fs_info
, gang
[i
]);
1178 int close_ctree(struct btrfs_root
*root
)
1181 struct btrfs_trans_handle
*trans
;
1182 struct btrfs_fs_info
*fs_info
= root
->fs_info
;
1184 fs_info
->closing
= 1;
1185 btrfs_transaction_flush_work(root
);
1186 mutex_lock(&fs_info
->fs_mutex
);
1187 btrfs_defrag_dirty_roots(root
->fs_info
);
1188 trans
= btrfs_start_transaction(root
, 1);
1189 ret
= btrfs_commit_transaction(trans
, root
);
1190 /* run commit again to drop the original snapshot */
1191 trans
= btrfs_start_transaction(root
, 1);
1192 btrfs_commit_transaction(trans
, root
);
1193 ret
= btrfs_write_and_wait_transaction(NULL
, root
);
1195 write_ctree_super(NULL
, root
);
1196 mutex_unlock(&fs_info
->fs_mutex
);
1198 if (fs_info
->delalloc_bytes
) {
1199 printk("btrfs: at unmount delalloc count %Lu\n",
1200 fs_info
->delalloc_bytes
);
1202 if (fs_info
->extent_root
->node
)
1203 free_extent_buffer(fs_info
->extent_root
->node
);
1205 if (fs_info
->tree_root
->node
)
1206 free_extent_buffer(fs_info
->tree_root
->node
);
1208 if (root
->fs_info
->chunk_root
->node
);
1209 free_extent_buffer(root
->fs_info
->chunk_root
->node
);
1211 if (root
->fs_info
->dev_root
->node
);
1212 free_extent_buffer(root
->fs_info
->dev_root
->node
);
1214 free_extent_buffer(fs_info
->sb_buffer
);
1216 btrfs_free_block_groups(root
->fs_info
);
1217 del_fs_roots(fs_info
);
1219 filemap_write_and_wait(fs_info
->btree_inode
->i_mapping
);
1221 extent_io_tree_empty_lru(&fs_info
->free_space_cache
);
1222 extent_io_tree_empty_lru(&fs_info
->block_group_cache
);
1223 extent_io_tree_empty_lru(&fs_info
->pinned_extents
);
1224 extent_io_tree_empty_lru(&fs_info
->pending_del
);
1225 extent_io_tree_empty_lru(&fs_info
->extent_ins
);
1226 extent_io_tree_empty_lru(&BTRFS_I(fs_info
->btree_inode
)->io_tree
);
1228 truncate_inode_pages(fs_info
->btree_inode
->i_mapping
, 0);
1229 flush_workqueue(end_io_workqueue
);
1230 destroy_workqueue(end_io_workqueue
);
1232 iput(fs_info
->btree_inode
);
1234 while(!list_empty(&fs_info
->hashers
)) {
1235 struct btrfs_hasher
*hasher
;
1236 hasher
= list_entry(fs_info
->hashers
.next
, struct btrfs_hasher
,
1238 list_del(&hasher
->hashers
);
1239 crypto_free_hash(&fs_info
->hash_tfm
);
1243 close_all_devices(fs_info
);
1244 btrfs_mapping_tree_free(&fs_info
->mapping_tree
);
1245 bdi_destroy(&fs_info
->bdi
);
1247 kfree(fs_info
->extent_root
);
1248 kfree(fs_info
->tree_root
);
1249 kfree(fs_info
->chunk_root
);
1250 kfree(fs_info
->dev_root
);
1254 int btrfs_buffer_uptodate(struct extent_buffer
*buf
)
1256 struct inode
*btree_inode
= buf
->first_page
->mapping
->host
;
1257 return extent_buffer_uptodate(&BTRFS_I(btree_inode
)->io_tree
, buf
);
1260 int btrfs_set_buffer_uptodate(struct extent_buffer
*buf
)
1262 struct inode
*btree_inode
= buf
->first_page
->mapping
->host
;
1263 return set_extent_buffer_uptodate(&BTRFS_I(btree_inode
)->io_tree
,
1267 void btrfs_mark_buffer_dirty(struct extent_buffer
*buf
)
1269 struct btrfs_root
*root
= BTRFS_I(buf
->first_page
->mapping
->host
)->root
;
1270 u64 transid
= btrfs_header_generation(buf
);
1271 struct inode
*btree_inode
= root
->fs_info
->btree_inode
;
1273 if (transid
!= root
->fs_info
->generation
) {
1274 printk(KERN_CRIT
"transid mismatch buffer %llu, found %Lu running %Lu\n",
1275 (unsigned long long)buf
->start
,
1276 transid
, root
->fs_info
->generation
);
1279 set_extent_buffer_dirty(&BTRFS_I(btree_inode
)->io_tree
, buf
);
1282 void btrfs_throttle(struct btrfs_root
*root
)
1284 struct backing_dev_info
*bdi
;
1286 bdi
= root
->fs_info
->sb
->s_bdev
->bd_inode
->i_mapping
->backing_dev_info
;
1287 if (root
->fs_info
->throttles
&& bdi_write_congested(bdi
)) {
1288 #if LINUX_VERSION_CODE > KERNEL_VERSION(2,6,18)
1289 congestion_wait(WRITE
, HZ
/20);
1291 blk_congestion_wait(WRITE
, HZ
/20);
1296 void btrfs_btree_balance_dirty(struct btrfs_root
*root
, unsigned long nr
)
1298 balance_dirty_pages_ratelimited_nr(
1299 root
->fs_info
->btree_inode
->i_mapping
, 1);
1302 void btrfs_set_buffer_defrag(struct extent_buffer
*buf
)
1304 struct btrfs_root
*root
= BTRFS_I(buf
->first_page
->mapping
->host
)->root
;
1305 struct inode
*btree_inode
= root
->fs_info
->btree_inode
;
1306 set_extent_bits(&BTRFS_I(btree_inode
)->io_tree
, buf
->start
,
1307 buf
->start
+ buf
->len
- 1, EXTENT_DEFRAG
, GFP_NOFS
);
1310 void btrfs_set_buffer_defrag_done(struct extent_buffer
*buf
)
1312 struct btrfs_root
*root
= BTRFS_I(buf
->first_page
->mapping
->host
)->root
;
1313 struct inode
*btree_inode
= root
->fs_info
->btree_inode
;
1314 set_extent_bits(&BTRFS_I(btree_inode
)->io_tree
, buf
->start
,
1315 buf
->start
+ buf
->len
- 1, EXTENT_DEFRAG_DONE
,
1319 int btrfs_buffer_defrag(struct extent_buffer
*buf
)
1321 struct btrfs_root
*root
= BTRFS_I(buf
->first_page
->mapping
->host
)->root
;
1322 struct inode
*btree_inode
= root
->fs_info
->btree_inode
;
1323 return test_range_bit(&BTRFS_I(btree_inode
)->io_tree
,
1324 buf
->start
, buf
->start
+ buf
->len
- 1, EXTENT_DEFRAG
, 0);
1327 int btrfs_buffer_defrag_done(struct extent_buffer
*buf
)
1329 struct btrfs_root
*root
= BTRFS_I(buf
->first_page
->mapping
->host
)->root
;
1330 struct inode
*btree_inode
= root
->fs_info
->btree_inode
;
1331 return test_range_bit(&BTRFS_I(btree_inode
)->io_tree
,
1332 buf
->start
, buf
->start
+ buf
->len
- 1,
1333 EXTENT_DEFRAG_DONE
, 0);
1336 int btrfs_clear_buffer_defrag_done(struct extent_buffer
*buf
)
1338 struct btrfs_root
*root
= BTRFS_I(buf
->first_page
->mapping
->host
)->root
;
1339 struct inode
*btree_inode
= root
->fs_info
->btree_inode
;
1340 return clear_extent_bits(&BTRFS_I(btree_inode
)->io_tree
,
1341 buf
->start
, buf
->start
+ buf
->len
- 1,
1342 EXTENT_DEFRAG_DONE
, GFP_NOFS
);
1345 int btrfs_clear_buffer_defrag(struct extent_buffer
*buf
)
1347 struct btrfs_root
*root
= BTRFS_I(buf
->first_page
->mapping
->host
)->root
;
1348 struct inode
*btree_inode
= root
->fs_info
->btree_inode
;
1349 return clear_extent_bits(&BTRFS_I(btree_inode
)->io_tree
,
1350 buf
->start
, buf
->start
+ buf
->len
- 1,
1351 EXTENT_DEFRAG
, GFP_NOFS
);
1354 int btrfs_read_buffer(struct extent_buffer
*buf
)
1356 struct btrfs_root
*root
= BTRFS_I(buf
->first_page
->mapping
->host
)->root
;
1357 struct inode
*btree_inode
= root
->fs_info
->btree_inode
;
1359 ret
= read_extent_buffer_pages(&BTRFS_I(btree_inode
)->io_tree
,
1360 buf
, 0, 1, btree_get_extent
);
1362 buf
->flags
|= EXTENT_UPTODATE
;
1367 static struct extent_io_ops btree_extent_io_ops
= {
1368 .writepage_io_hook
= btree_writepage_io_hook
,
1369 .readpage_end_io_hook
= btree_readpage_end_io_hook
,
1370 .submit_bio_hook
= btree_submit_bio_hook
,
1371 /* note we're sharing with inode.c for the merge bio hook */
1372 .merge_bio_hook
= btrfs_merge_bio_hook
,