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"
33 static int check_tree_block(struct btrfs_root *root, struct extent_buffer *buf)
35 if (extent_buffer_blocknr(buf) != btrfs_header_blocknr(buf)) {
36 printk(KERN_CRIT "buf blocknr(buf) is %llu, header is %llu\n",
37 (unsigned long long)extent_buffer_blocknr(buf),
38 (unsigned long long)btrfs_header_blocknr(buf));
45 struct extent_buffer *btrfs_find_tree_block(struct btrfs_root *root,
48 struct inode *btree_inode = root->fs_info->btree_inode;
49 return find_extent_buffer(&BTRFS_I(btree_inode)->extent_tree,
50 blocknr * root->sectorsize,
51 root->sectorsize, GFP_NOFS);
54 struct extent_buffer *btrfs_find_create_tree_block(struct btrfs_root *root,
57 struct inode *btree_inode = root->fs_info->btree_inode;
58 return alloc_extent_buffer(&BTRFS_I(btree_inode)->extent_tree,
59 blocknr * root->sectorsize,
60 root->sectorsize, GFP_NOFS);
63 struct extent_map *btree_get_extent(struct inode *inode, struct page *page,
64 size_t page_offset, u64 start, u64 end,
67 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
68 struct extent_map *em;
72 em = lookup_extent_mapping(em_tree, start, end);
76 em = alloc_extent_map(GFP_NOFS);
78 em = ERR_PTR(-ENOMEM);
82 em->end = (i_size_read(inode) & ~((u64)PAGE_CACHE_SIZE -1)) - 1;
84 em->block_end = em->end;
85 em->bdev = inode->i_sb->s_bdev;
86 ret = add_extent_mapping(em_tree, em);
98 static int btree_writepage(struct page *page, struct writeback_control *wbc)
100 struct extent_map_tree *tree;
101 tree = &BTRFS_I(page->mapping->host)->extent_tree;
102 return extent_write_full_page(tree, page, btree_get_extent, wbc);
104 int btree_readpage(struct file *file, struct page *page)
106 struct extent_map_tree *tree;
107 tree = &BTRFS_I(page->mapping->host)->extent_tree;
108 return extent_read_full_page(tree, page, btree_get_extent);
111 static int btree_releasepage(struct page *page, gfp_t unused_gfp_flags)
113 struct extent_map_tree *tree;
116 BUG_ON(page->private != 1);
117 tree = &BTRFS_I(page->mapping->host)->extent_tree;
118 ret = try_release_extent_mapping(tree, page);
120 ClearPagePrivate(page);
121 set_page_private(page, 0);
122 page_cache_release(page);
127 static void btree_invalidatepage(struct page *page, unsigned long offset)
129 struct extent_map_tree *tree;
130 tree = &BTRFS_I(page->mapping->host)->extent_tree;
131 extent_invalidatepage(tree, page, offset);
132 btree_releasepage(page, GFP_NOFS);
135 int btrfs_csum_data(struct btrfs_root * root, char *data, size_t len,
141 crc = crc32c(0, data, len);
142 memcpy(result, &crc, BTRFS_CRC32_SIZE);
148 static int csum_tree_block(struct btrfs_root *root, struct extent_buffer *buf,
152 char result[BTRFS_CRC32_SIZE];
154 struct btrfs_node *node;
156 ret = btrfs_csum_data(root, bh->b_data + BTRFS_CSUM_SIZE,
157 bh->b_size - BTRFS_CSUM_SIZE, result);
161 if (memcmp(bh->b_data, result, BTRFS_CRC32_SIZE)) {
162 printk("btrfs: %s checksum verify failed on %llu\n",
163 root->fs_info->sb->s_id,
164 (unsigned long long)bh_blocknr(bh));
168 node = btrfs_buffer_node(bh);
169 memcpy(node->header.csum, result, BTRFS_CRC32_SIZE);
176 static int btree_writepage(struct page *page, struct writeback_control *wbc)
178 struct buffer_head *bh;
179 struct btrfs_root *root = BTRFS_I(page->mapping->host)->root;
180 struct buffer_head *head;
181 if (!page_has_buffers(page)) {
182 create_empty_buffers(page, root->fs_info->sb->s_blocksize,
183 (1 << BH_Dirty)|(1 << BH_Uptodate));
185 head = page_buffers(page);
188 if (buffer_dirty(bh))
189 csum_tree_block(root, bh, 0);
190 bh = bh->b_this_page;
191 } while (bh != head);
192 return block_write_full_page(page, btree_get_block, wbc);
196 static struct address_space_operations btree_aops = {
197 .readpage = btree_readpage,
198 .writepage = btree_writepage,
199 .releasepage = btree_releasepage,
200 .invalidatepage = btree_invalidatepage,
201 .sync_page = block_sync_page,
204 int readahead_tree_block(struct btrfs_root *root, u64 blocknr)
206 struct extent_buffer *buf = NULL;
207 struct inode *btree_inode = root->fs_info->btree_inode;
210 buf = btrfs_find_create_tree_block(root, blocknr);
213 read_extent_buffer_pages(&BTRFS_I(btree_inode)->extent_tree,
215 free_extent_buffer(buf);
219 struct extent_buffer *read_tree_block(struct btrfs_root *root, u64 blocknr)
221 struct extent_buffer *buf = NULL;
222 struct inode *btree_inode = root->fs_info->btree_inode;
224 buf = btrfs_find_create_tree_block(root, blocknr);
227 read_extent_buffer_pages(&BTRFS_I(btree_inode)->extent_tree,
232 int clean_tree_block(struct btrfs_trans_handle *trans, struct btrfs_root *root,
233 struct extent_buffer *buf)
235 struct inode *btree_inode = root->fs_info->btree_inode;
236 clear_extent_buffer_dirty(&BTRFS_I(btree_inode)->extent_tree, buf);
240 int wait_on_tree_block_writeback(struct btrfs_root *root,
241 struct extent_buffer *buf)
243 struct inode *btree_inode = root->fs_info->btree_inode;
244 wait_on_extent_buffer_writeback(&BTRFS_I(btree_inode)->extent_tree,
249 int set_tree_block_dirty(struct btrfs_root *root, struct extent_buffer *buf)
251 struct inode *btree_inode = root->fs_info->btree_inode;
252 set_extent_buffer_dirty(&BTRFS_I(btree_inode)->extent_tree, buf);
256 static int __setup_root(int blocksize,
257 struct btrfs_root *root,
258 struct btrfs_fs_info *fs_info,
263 root->commit_root = NULL;
264 root->sectorsize = blocksize;
265 root->nodesize = blocksize;
266 root->leafsize = blocksize;
268 root->fs_info = fs_info;
269 root->objectid = objectid;
270 root->last_trans = 0;
271 root->highest_inode = 0;
272 root->last_inode_alloc = 0;
274 memset(&root->root_key, 0, sizeof(root->root_key));
275 memset(&root->root_item, 0, sizeof(root->root_item));
276 memset(&root->defrag_progress, 0, sizeof(root->defrag_progress));
277 memset(&root->root_kobj, 0, sizeof(root->root_kobj));
278 init_completion(&root->kobj_unregister);
279 init_rwsem(&root->snap_sem);
280 root->defrag_running = 0;
281 root->defrag_level = 0;
282 root->root_key.objectid = objectid;
286 static int find_and_setup_root(int blocksize,
287 struct btrfs_root *tree_root,
288 struct btrfs_fs_info *fs_info,
290 struct btrfs_root *root)
294 __setup_root(blocksize, root, fs_info, objectid);
295 ret = btrfs_find_last_root(tree_root, objectid,
296 &root->root_item, &root->root_key);
299 root->node = read_tree_block(root,
300 btrfs_root_blocknr(&root->root_item));
305 struct btrfs_root *btrfs_read_fs_root_no_radix(struct btrfs_fs_info *fs_info,
306 struct btrfs_key *location)
308 struct btrfs_root *root;
309 struct btrfs_root *tree_root = fs_info->tree_root;
310 struct btrfs_path *path;
311 struct extent_buffer *l;
315 root = kzalloc(sizeof(*root), GFP_NOFS);
317 return ERR_PTR(-ENOMEM);
318 if (location->offset == (u64)-1) {
319 ret = find_and_setup_root(fs_info->sb->s_blocksize,
320 fs_info->tree_root, fs_info,
321 location->objectid, root);
329 __setup_root(fs_info->sb->s_blocksize, root, fs_info,
332 path = btrfs_alloc_path();
334 ret = btrfs_search_slot(NULL, tree_root, location, path, 0, 0);
341 read_extent_buffer(l, &root->root_item,
342 btrfs_item_ptr_offset(l, path->slots[0]),
343 sizeof(root->root_item));
346 btrfs_release_path(root, path);
347 btrfs_free_path(path);
352 root->node = read_tree_block(root,
353 btrfs_root_blocknr(&root->root_item));
357 ret = btrfs_find_highest_inode(root, &highest_inode);
359 root->highest_inode = highest_inode;
360 root->last_inode_alloc = highest_inode;
365 struct btrfs_root *btrfs_read_fs_root(struct btrfs_fs_info *fs_info,
366 struct btrfs_key *location,
367 const char *name, int namelen)
369 struct btrfs_root *root;
372 root = radix_tree_lookup(&fs_info->fs_roots_radix,
373 (unsigned long)location->objectid);
377 root = btrfs_read_fs_root_no_radix(fs_info, location);
380 ret = radix_tree_insert(&fs_info->fs_roots_radix,
381 (unsigned long)root->root_key.objectid,
384 free_extent_buffer(root->node);
389 ret = btrfs_set_root_name(root, name, namelen);
391 free_extent_buffer(root->node);
396 ret = btrfs_sysfs_add_root(root);
398 free_extent_buffer(root->node);
404 ret = btrfs_find_dead_roots(fs_info->tree_root,
405 root->root_key.objectid, root);
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_bit_radix(&fs_info->extent_ins_radix);
431 INIT_RADIX_TREE(&fs_info->fs_roots_radix, GFP_NOFS);
432 INIT_RADIX_TREE(&fs_info->block_group_radix, GFP_KERNEL);
433 INIT_RADIX_TREE(&fs_info->block_group_data_radix, GFP_KERNEL);
434 INIT_LIST_HEAD(&fs_info->trans_list);
435 INIT_LIST_HEAD(&fs_info->dead_roots);
436 memset(&fs_info->super_kobj, 0, sizeof(fs_info->super_kobj));
437 init_completion(&fs_info->kobj_unregister);
438 sb_set_blocksize(sb, 4096);
439 fs_info->running_transaction = NULL;
440 fs_info->last_trans_committed = 0;
441 fs_info->tree_root = tree_root;
442 fs_info->extent_root = extent_root;
444 fs_info->btree_inode = new_inode(sb);
445 fs_info->btree_inode->i_ino = 1;
446 fs_info->btree_inode->i_nlink = 1;
447 fs_info->btree_inode->i_size = sb->s_bdev->bd_inode->i_size;
448 fs_info->btree_inode->i_mapping->a_ops = &btree_aops;
449 extent_map_tree_init(&BTRFS_I(fs_info->btree_inode)->extent_tree,
450 fs_info->btree_inode->i_mapping,
452 fs_info->do_barriers = 1;
453 fs_info->closing = 0;
455 INIT_DELAYED_WORK(&fs_info->trans_work, btrfs_transaction_cleaner);
456 BTRFS_I(fs_info->btree_inode)->root = tree_root;
457 memset(&BTRFS_I(fs_info->btree_inode)->location, 0,
458 sizeof(struct btrfs_key));
459 insert_inode_hash(fs_info->btree_inode);
460 mapping_set_gfp_mask(fs_info->btree_inode->i_mapping, GFP_NOFS);
462 mutex_init(&fs_info->trans_mutex);
463 mutex_init(&fs_info->fs_mutex);
465 __setup_root(sb->s_blocksize, tree_root,
466 fs_info, BTRFS_ROOT_TREE_OBJECTID);
468 fs_info->sb_buffer = read_tree_block(tree_root,
469 BTRFS_SUPER_INFO_OFFSET /
472 if (!fs_info->sb_buffer)
475 read_extent_buffer(fs_info->sb_buffer, &fs_info->super_copy, 0,
476 sizeof(fs_info->super_copy));
478 read_extent_buffer(fs_info->sb_buffer, fs_info->fsid,
479 (unsigned long)btrfs_super_fsid(fs_info->sb_buffer),
481 disk_super = &fs_info->super_copy;
482 if (!btrfs_super_root(disk_super))
485 i_size_write(fs_info->btree_inode,
486 btrfs_super_total_blocks(disk_super) <<
487 fs_info->btree_inode->i_blkbits);
490 if (strncmp((char *)(&disk_super->magic), BTRFS_MAGIC,
491 sizeof(disk_super->magic))) {
492 printk("btrfs: valid FS not found on %s\n", sb->s_id);
495 tree_root->node = read_tree_block(tree_root,
496 btrfs_super_root(disk_super));
497 if (!tree_root->node)
500 mutex_lock(&fs_info->fs_mutex);
501 ret = find_and_setup_root(sb->s_blocksize, tree_root, fs_info,
502 BTRFS_EXTENT_TREE_OBJECTID, extent_root);
504 mutex_unlock(&fs_info->fs_mutex);
508 btrfs_read_block_groups(extent_root);
510 fs_info->generation = btrfs_super_generation(disk_super) + 1;
511 mutex_unlock(&fs_info->fs_mutex);
515 free_extent_buffer(tree_root->node);
517 free_extent_buffer(fs_info->sb_buffer);
519 iput(fs_info->btree_inode);
527 int write_ctree_super(struct btrfs_trans_handle *trans, struct btrfs_root
531 struct extent_buffer *super = root->fs_info->sb_buffer;
532 struct inode *btree_inode = root->fs_info->btree_inode;
534 set_extent_buffer_dirty(&BTRFS_I(btree_inode)->extent_tree, super);
535 ret = sync_page_range_nolock(btree_inode, btree_inode->i_mapping,
536 super->start, super->len);
540 int btrfs_free_fs_root(struct btrfs_fs_info *fs_info, struct btrfs_root *root)
542 radix_tree_delete(&fs_info->fs_roots_radix,
543 (unsigned long)root->root_key.objectid);
544 btrfs_sysfs_del_root(root);
548 free_extent_buffer(root->node);
549 if (root->commit_root)
550 free_extent_buffer(root->commit_root);
557 static int del_fs_roots(struct btrfs_fs_info *fs_info)
560 struct btrfs_root *gang[8];
564 ret = radix_tree_gang_lookup(&fs_info->fs_roots_radix,
569 for (i = 0; i < ret; i++)
570 btrfs_free_fs_root(fs_info, gang[i]);
575 int close_ctree(struct btrfs_root *root)
578 struct btrfs_trans_handle *trans;
579 struct btrfs_fs_info *fs_info = root->fs_info;
581 fs_info->closing = 1;
582 btrfs_transaction_flush_work(root);
583 mutex_lock(&fs_info->fs_mutex);
584 btrfs_defrag_dirty_roots(root->fs_info);
585 trans = btrfs_start_transaction(root, 1);
586 ret = btrfs_commit_transaction(trans, root);
587 /* run commit again to drop the original snapshot */
588 trans = btrfs_start_transaction(root, 1);
589 btrfs_commit_transaction(trans, root);
590 ret = btrfs_write_and_wait_transaction(NULL, root);
592 write_ctree_super(NULL, root);
593 mutex_unlock(&fs_info->fs_mutex);
595 if (fs_info->extent_root->node)
596 free_extent_buffer(fs_info->extent_root->node);
597 if (fs_info->tree_root->node)
598 free_extent_buffer(fs_info->tree_root->node);
599 free_extent_buffer(fs_info->sb_buffer);
600 truncate_inode_pages(fs_info->btree_inode->i_mapping, 0);
601 iput(fs_info->btree_inode);
603 btrfs_free_block_groups(root->fs_info);
604 del_fs_roots(fs_info);
605 kfree(fs_info->extent_root);
606 kfree(fs_info->tree_root);
610 int btrfs_buffer_uptodate(struct extent_buffer *buf)
612 struct inode *btree_inode = buf->pages[0]->mapping->host;
613 return extent_buffer_uptodate(&BTRFS_I(btree_inode)->extent_tree, buf);
616 int btrfs_set_buffer_uptodate(struct extent_buffer *buf)
618 struct inode *btree_inode = buf->pages[0]->mapping->host;
619 return set_extent_buffer_uptodate(&BTRFS_I(btree_inode)->extent_tree,
623 void btrfs_mark_buffer_dirty(struct extent_buffer *buf)
625 struct btrfs_root *root = BTRFS_I(buf->pages[0]->mapping->host)->root;
626 u64 transid = btrfs_header_generation(buf);
627 struct inode *btree_inode = root->fs_info->btree_inode;
629 if (transid != root->fs_info->generation) {
630 printk(KERN_CRIT "transid mismatch buffer %llu, found %Lu running %Lu\n",
631 (unsigned long long)extent_buffer_blocknr(buf),
632 transid, root->fs_info->generation);
635 set_extent_buffer_dirty(&BTRFS_I(btree_inode)->extent_tree, buf);
638 void btrfs_btree_balance_dirty(struct btrfs_root *root, unsigned long nr)
640 balance_dirty_pages_ratelimited_nr(
641 root->fs_info->btree_inode->i_mapping, nr);