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>
28 #include "transaction.h"
29 #include "btrfs_inode.h"
31 u64
bh_blocknr(struct buffer_head
*bh
)
36 static int check_tree_block(struct btrfs_root
*root
, struct buffer_head
*buf
)
38 struct btrfs_node
*node
= btrfs_buffer_node(buf
);
39 if (bh_blocknr(buf
) != btrfs_header_blocknr(&node
->header
)) {
40 printk(KERN_CRIT
"bh_blocknr(buf) is %llu, header is %llu\n",
41 (unsigned long long)bh_blocknr(buf
),
42 (unsigned long long)btrfs_header_blocknr(&node
->header
));
48 struct buffer_head
*btrfs_find_tree_block(struct btrfs_root
*root
, u64 blocknr
)
50 struct address_space
*mapping
= root
->fs_info
->btree_inode
->i_mapping
;
51 int blockbits
= root
->fs_info
->sb
->s_blocksize_bits
;
52 unsigned long index
= blocknr
>> (PAGE_CACHE_SHIFT
- blockbits
);
54 struct buffer_head
*bh
;
55 struct buffer_head
*head
;
56 struct buffer_head
*ret
= NULL
;
59 page
= find_lock_page(mapping
, index
);
63 if (!page_has_buffers(page
))
66 head
= page_buffers(page
);
69 if (buffer_mapped(bh
) && bh_blocknr(bh
) == blocknr
) {
78 page_cache_release(page
);
82 int btrfs_map_bh_to_logical(struct btrfs_root
*root
, struct buffer_head
*bh
,
88 set_buffer_mapped(bh
);
90 map_bh(bh
, root
->fs_info
->sb
, logical
);
95 struct buffer_head
*btrfs_find_create_tree_block(struct btrfs_root
*root
,
98 struct address_space
*mapping
= root
->fs_info
->btree_inode
->i_mapping
;
99 int blockbits
= root
->fs_info
->sb
->s_blocksize_bits
;
100 unsigned long index
= blocknr
>> (PAGE_CACHE_SHIFT
- blockbits
);
102 struct buffer_head
*bh
;
103 struct buffer_head
*head
;
104 struct buffer_head
*ret
= NULL
;
106 u64 first_block
= index
<< (PAGE_CACHE_SHIFT
- blockbits
);
108 page
= find_or_create_page(mapping
, index
, GFP_NOFS
);
112 if (!page_has_buffers(page
))
113 create_empty_buffers(page
, root
->fs_info
->sb
->s_blocksize
, 0);
114 head
= page_buffers(page
);
117 if (!buffer_mapped(bh
)) {
118 err
= btrfs_map_bh_to_logical(root
, bh
, first_block
);
121 if (bh_blocknr(bh
) == blocknr
) {
126 bh
= bh
->b_this_page
;
128 } while (bh
!= head
);
133 page_cache_release(page
);
137 static int btree_get_block(struct inode
*inode
, sector_t iblock
,
138 struct buffer_head
*bh
, int create
)
141 struct btrfs_root
*root
= BTRFS_I(bh
->b_page
->mapping
->host
)->root
;
142 err
= btrfs_map_bh_to_logical(root
, bh
, iblock
);
146 int btrfs_csum_data(struct btrfs_root
* root
, char *data
, size_t len
,
150 crc
= crc32c(0, data
, len
);
151 memcpy(result
, &crc
, BTRFS_CRC32_SIZE
);
155 static int csum_tree_block(struct btrfs_root
*root
, struct buffer_head
*bh
,
158 char result
[BTRFS_CRC32_SIZE
];
160 struct btrfs_node
*node
;
162 ret
= btrfs_csum_data(root
, bh
->b_data
+ BTRFS_CSUM_SIZE
,
163 bh
->b_size
- BTRFS_CSUM_SIZE
, result
);
167 if (memcmp(bh
->b_data
, result
, BTRFS_CRC32_SIZE
)) {
168 printk("btrfs: %s checksum verify failed on %llu\n",
169 root
->fs_info
->sb
->s_id
,
170 (unsigned long long)bh_blocknr(bh
));
174 node
= btrfs_buffer_node(bh
);
175 memcpy(node
->header
.csum
, result
, BTRFS_CRC32_SIZE
);
180 static int btree_writepage(struct page
*page
, struct writeback_control
*wbc
)
182 struct buffer_head
*bh
;
183 struct btrfs_root
*root
= BTRFS_I(page
->mapping
->host
)->root
;
184 struct buffer_head
*head
;
185 if (!page_has_buffers(page
)) {
186 create_empty_buffers(page
, root
->fs_info
->sb
->s_blocksize
,
187 (1 << BH_Dirty
)|(1 << BH_Uptodate
));
189 head
= page_buffers(page
);
192 if (buffer_dirty(bh
))
193 csum_tree_block(root
, bh
, 0);
194 bh
= bh
->b_this_page
;
195 } while (bh
!= head
);
196 return block_write_full_page(page
, btree_get_block
, wbc
);
199 static int btree_readpage(struct file
* file
, struct page
* page
)
201 return block_read_full_page(page
, btree_get_block
);
204 static struct address_space_operations btree_aops
= {
205 .readpage
= btree_readpage
,
206 .writepage
= btree_writepage
,
207 .sync_page
= block_sync_page
,
210 int readahead_tree_block(struct btrfs_root
*root
, u64 blocknr
)
212 struct buffer_head
*bh
= NULL
;
215 bh
= btrfs_find_create_tree_block(root
, blocknr
);
218 if (buffer_uptodate(bh
)) {
222 if (test_set_buffer_locked(bh
)) {
226 if (!buffer_uptodate(bh
)) {
228 bh
->b_end_io
= end_buffer_read_sync
;
239 struct buffer_head
*read_tree_block(struct btrfs_root
*root
, u64 blocknr
)
241 struct buffer_head
*bh
= NULL
;
243 bh
= btrfs_find_create_tree_block(root
, blocknr
);
246 if (buffer_uptodate(bh
))
249 if (!buffer_uptodate(bh
)) {
251 bh
->b_end_io
= end_buffer_read_sync
;
254 if (!buffer_uptodate(bh
))
260 if (!buffer_checked(bh
)) {
261 csum_tree_block(root
, bh
, 1);
262 set_buffer_checked(bh
);
264 if (check_tree_block(root
, bh
))
272 int clean_tree_block(struct btrfs_trans_handle
*trans
, struct btrfs_root
*root
,
273 struct buffer_head
*buf
)
275 WARN_ON(atomic_read(&buf
->b_count
) == 0);
277 clear_buffer_dirty(buf
);
282 static int __setup_root(int blocksize
,
283 struct btrfs_root
*root
,
284 struct btrfs_fs_info
*fs_info
,
289 root
->commit_root
= NULL
;
290 root
->blocksize
= blocksize
;
292 root
->fs_info
= fs_info
;
293 root
->objectid
= objectid
;
294 root
->last_trans
= 0;
295 root
->highest_inode
= 0;
296 root
->last_inode_alloc
= 0;
297 memset(&root
->root_key
, 0, sizeof(root
->root_key
));
298 memset(&root
->root_item
, 0, sizeof(root
->root_item
));
299 memset(&root
->defrag_progress
, 0, sizeof(root
->defrag_progress
));
300 root
->defrag_running
= 0;
301 root
->defrag_level
= 0;
302 root
->root_key
.objectid
= objectid
;
306 static int find_and_setup_root(int blocksize
,
307 struct btrfs_root
*tree_root
,
308 struct btrfs_fs_info
*fs_info
,
310 struct btrfs_root
*root
)
314 __setup_root(blocksize
, root
, fs_info
, objectid
);
315 ret
= btrfs_find_last_root(tree_root
, objectid
,
316 &root
->root_item
, &root
->root_key
);
319 root
->node
= read_tree_block(root
,
320 btrfs_root_blocknr(&root
->root_item
));
325 struct btrfs_root
*btrfs_read_fs_root_no_radix(struct btrfs_fs_info
*fs_info
,
326 struct btrfs_key
*location
)
328 struct btrfs_root
*root
;
329 struct btrfs_root
*tree_root
= fs_info
->tree_root
;
330 struct btrfs_path
*path
;
331 struct btrfs_leaf
*l
;
335 root
= kzalloc(sizeof(*root
), GFP_NOFS
);
337 return ERR_PTR(-ENOMEM
);
338 if (location
->offset
== (u64
)-1) {
339 ret
= find_and_setup_root(fs_info
->sb
->s_blocksize
,
340 fs_info
->tree_root
, fs_info
,
341 location
->objectid
, root
);
349 __setup_root(fs_info
->sb
->s_blocksize
, root
, fs_info
,
352 path
= btrfs_alloc_path();
354 ret
= btrfs_search_slot(NULL
, tree_root
, location
, path
, 0, 0);
360 l
= btrfs_buffer_leaf(path
->nodes
[0]);
361 memcpy(&root
->root_item
,
362 btrfs_item_ptr(l
, path
->slots
[0], struct btrfs_root_item
),
363 sizeof(root
->root_item
));
364 memcpy(&root
->root_key
, location
, sizeof(*location
));
367 btrfs_release_path(root
, path
);
368 btrfs_free_path(path
);
373 root
->node
= read_tree_block(root
,
374 btrfs_root_blocknr(&root
->root_item
));
378 ret
= btrfs_find_highest_inode(root
, &highest_inode
);
380 root
->highest_inode
= highest_inode
;
381 root
->last_inode_alloc
= highest_inode
;
386 struct btrfs_root
*btrfs_read_fs_root(struct btrfs_fs_info
*fs_info
,
387 struct btrfs_key
*location
)
389 struct btrfs_root
*root
;
392 root
= radix_tree_lookup(&fs_info
->fs_roots_radix
,
393 (unsigned long)location
->objectid
);
397 root
= btrfs_read_fs_root_no_radix(fs_info
, location
);
400 ret
= radix_tree_insert(&fs_info
->fs_roots_radix
,
401 (unsigned long)root
->root_key
.objectid
,
411 struct btrfs_root
*open_ctree(struct super_block
*sb
)
413 struct btrfs_root
*extent_root
= kmalloc(sizeof(struct btrfs_root
),
415 struct btrfs_root
*tree_root
= kmalloc(sizeof(struct btrfs_root
),
417 struct btrfs_fs_info
*fs_info
= kmalloc(sizeof(*fs_info
),
421 struct btrfs_super_block
*disk_super
;
423 if (!extent_root
|| !tree_root
|| !fs_info
) {
427 init_bit_radix(&fs_info
->pinned_radix
);
428 init_bit_radix(&fs_info
->pending_del_radix
);
429 init_bit_radix(&fs_info
->extent_map_radix
);
430 INIT_RADIX_TREE(&fs_info
->fs_roots_radix
, GFP_NOFS
);
431 INIT_RADIX_TREE(&fs_info
->block_group_radix
, GFP_KERNEL
);
432 INIT_RADIX_TREE(&fs_info
->block_group_data_radix
, GFP_KERNEL
);
433 INIT_LIST_HEAD(&fs_info
->trans_list
);
434 INIT_LIST_HEAD(&fs_info
->dead_roots
);
435 sb_set_blocksize(sb
, 4096);
436 fs_info
->running_transaction
= NULL
;
437 fs_info
->tree_root
= tree_root
;
438 fs_info
->extent_root
= extent_root
;
440 fs_info
->btree_inode
= new_inode(sb
);
441 fs_info
->btree_inode
->i_ino
= 1;
442 fs_info
->btree_inode
->i_nlink
= 1;
443 fs_info
->btree_inode
->i_size
= sb
->s_bdev
->bd_inode
->i_size
;
444 fs_info
->btree_inode
->i_mapping
->a_ops
= &btree_aops
;
445 fs_info
->do_barriers
= 1;
446 fs_info
->extent_tree_insert_nr
= 0;
447 fs_info
->extent_tree_prealloc_nr
= 0;
448 fs_info
->closing
= 0;
450 INIT_DELAYED_WORK(&fs_info
->trans_work
, btrfs_transaction_cleaner
);
451 BTRFS_I(fs_info
->btree_inode
)->root
= tree_root
;
452 memset(&BTRFS_I(fs_info
->btree_inode
)->location
, 0,
453 sizeof(struct btrfs_key
));
454 insert_inode_hash(fs_info
->btree_inode
);
455 mapping_set_gfp_mask(fs_info
->btree_inode
->i_mapping
, GFP_NOFS
);
457 mutex_init(&fs_info
->trans_mutex
);
458 mutex_init(&fs_info
->fs_mutex
);
460 __setup_root(sb
->s_blocksize
, tree_root
,
461 fs_info
, BTRFS_ROOT_TREE_OBJECTID
);
463 fs_info
->sb_buffer
= read_tree_block(tree_root
,
464 BTRFS_SUPER_INFO_OFFSET
/
467 if (!fs_info
->sb_buffer
)
469 disk_super
= (struct btrfs_super_block
*)fs_info
->sb_buffer
->b_data
;
470 fs_info
->disk_super
= disk_super
;
471 memcpy(&fs_info
->super_copy
, disk_super
, sizeof(fs_info
->super_copy
));
473 if (!btrfs_super_root(disk_super
))
476 i_size_write(fs_info
->btree_inode
,
477 btrfs_super_total_blocks(disk_super
) <<
478 fs_info
->btree_inode
->i_blkbits
);
481 if (strncmp((char *)(&disk_super
->magic
), BTRFS_MAGIC
,
482 sizeof(disk_super
->magic
))) {
483 printk("btrfs: valid FS not found on %s\n", sb
->s_id
);
486 tree_root
->node
= read_tree_block(tree_root
,
487 btrfs_super_root(disk_super
));
488 if (!tree_root
->node
)
491 mutex_lock(&fs_info
->fs_mutex
);
492 ret
= find_and_setup_root(sb
->s_blocksize
, tree_root
, fs_info
,
493 BTRFS_EXTENT_TREE_OBJECTID
, extent_root
);
495 mutex_unlock(&fs_info
->fs_mutex
);
499 btrfs_read_block_groups(extent_root
);
501 fs_info
->generation
= btrfs_super_generation(disk_super
) + 1;
502 ret
= btrfs_find_dead_roots(tree_root
);
505 mutex_unlock(&fs_info
->fs_mutex
);
509 btrfs_block_release(tree_root
, tree_root
->node
);
511 btrfs_block_release(tree_root
, fs_info
->sb_buffer
);
513 iput(fs_info
->btree_inode
);
521 int write_ctree_super(struct btrfs_trans_handle
*trans
, struct btrfs_root
525 struct buffer_head
*bh
= root
->fs_info
->sb_buffer
;
528 WARN_ON(atomic_read(&bh
->b_count
) < 1);
529 clear_buffer_dirty(bh
);
530 csum_tree_block(root
, bh
, 0);
531 bh
->b_end_io
= end_buffer_write_sync
;
533 if (root
->fs_info
->do_barriers
)
534 ret
= submit_bh(WRITE_BARRIER
, bh
);
536 ret
= submit_bh(WRITE
, bh
);
537 if (ret
== -EOPNOTSUPP
) {
540 set_buffer_uptodate(bh
);
541 root
->fs_info
->do_barriers
= 0;
542 ret
= submit_bh(WRITE
, bh
);
545 if (!buffer_uptodate(bh
)) {
552 int btrfs_free_fs_root(struct btrfs_fs_info
*fs_info
, struct btrfs_root
*root
)
554 radix_tree_delete(&fs_info
->fs_roots_radix
,
555 (unsigned long)root
->root_key
.objectid
);
560 if (root
->commit_root
)
561 brelse(root
->commit_root
);
566 static int del_fs_roots(struct btrfs_fs_info
*fs_info
)
569 struct btrfs_root
*gang
[8];
573 ret
= radix_tree_gang_lookup(&fs_info
->fs_roots_radix
,
578 for (i
= 0; i
< ret
; i
++)
579 btrfs_free_fs_root(fs_info
, gang
[i
]);
584 int close_ctree(struct btrfs_root
*root
)
587 struct btrfs_trans_handle
*trans
;
588 struct btrfs_fs_info
*fs_info
= root
->fs_info
;
590 fs_info
->closing
= 1;
591 btrfs_transaction_flush_work(root
);
592 mutex_lock(&fs_info
->fs_mutex
);
593 btrfs_defrag_dirty_roots(root
->fs_info
);
594 trans
= btrfs_start_transaction(root
, 1);
595 ret
= btrfs_commit_transaction(trans
, root
);
596 /* run commit again to drop the original snapshot */
597 trans
= btrfs_start_transaction(root
, 1);
598 btrfs_commit_transaction(trans
, root
);
599 ret
= btrfs_write_and_wait_transaction(NULL
, root
);
601 write_ctree_super(NULL
, root
);
602 mutex_unlock(&fs_info
->fs_mutex
);
604 if (fs_info
->extent_root
->node
)
605 btrfs_block_release(fs_info
->extent_root
,
606 fs_info
->extent_root
->node
);
607 if (fs_info
->tree_root
->node
)
608 btrfs_block_release(fs_info
->tree_root
,
609 fs_info
->tree_root
->node
);
610 btrfs_block_release(root
, fs_info
->sb_buffer
);
611 truncate_inode_pages(fs_info
->btree_inode
->i_mapping
, 0);
612 iput(fs_info
->btree_inode
);
614 btrfs_free_block_groups(root
->fs_info
);
615 del_fs_roots(fs_info
);
616 kfree(fs_info
->extent_root
);
617 kfree(fs_info
->tree_root
);
621 void btrfs_mark_buffer_dirty(struct buffer_head
*bh
)
623 struct btrfs_root
*root
= BTRFS_I(bh
->b_page
->mapping
->host
)->root
;
624 u64 transid
= btrfs_header_generation(btrfs_buffer_header(bh
));
626 WARN_ON(!atomic_read(&bh
->b_count
));
628 if (transid
!= root
->fs_info
->generation
) {
629 printk(KERN_CRIT
"transid mismatch buffer %llu, found %Lu running %Lu\n",
630 (unsigned long long)bh
->b_blocknr
,
631 transid
, root
->fs_info
->generation
);
634 mark_buffer_dirty(bh
);
637 void btrfs_block_release(struct btrfs_root
*root
, struct buffer_head
*buf
)
642 void btrfs_btree_balance_dirty(struct btrfs_root
*root
)
644 balance_dirty_pages_ratelimited(root
->fs_info
->btree_inode
->i_mapping
);