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 struct extent_buffer *btrfs_find_tree_block(struct btrfs_root *root,
47 u64 bytenr, u32 blocksize)
49 struct inode *btree_inode = root->fs_info->btree_inode;
50 struct extent_buffer *eb;
51 eb = find_extent_buffer(&BTRFS_I(btree_inode)->extent_tree,
52 bytenr, blocksize, GFP_NOFS);
56 struct extent_buffer *btrfs_find_create_tree_block(struct btrfs_root *root,
57 u64 bytenr, u32 blocksize)
59 struct inode *btree_inode = root->fs_info->btree_inode;
60 struct extent_buffer *eb;
62 eb = alloc_extent_buffer(&BTRFS_I(btree_inode)->extent_tree,
63 bytenr, blocksize, GFP_NOFS);
67 struct extent_map *btree_get_extent(struct inode *inode, struct page *page,
68 size_t page_offset, u64 start, u64 end,
71 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
72 struct extent_map *em;
76 em = lookup_extent_mapping(em_tree, start, end);
80 em = alloc_extent_map(GFP_NOFS);
82 em = ERR_PTR(-ENOMEM);
86 em->end = (i_size_read(inode) & ~((u64)PAGE_CACHE_SIZE -1)) - 1;
88 em->block_end = em->end;
89 em->bdev = inode->i_sb->s_bdev;
90 ret = add_extent_mapping(em_tree, em);
102 static int btree_writepage(struct page *page, struct writeback_control *wbc)
104 struct extent_map_tree *tree;
105 tree = &BTRFS_I(page->mapping->host)->extent_tree;
106 return extent_write_full_page(tree, page, btree_get_extent, wbc);
108 int btree_readpage(struct file *file, struct page *page)
110 struct extent_map_tree *tree;
111 tree = &BTRFS_I(page->mapping->host)->extent_tree;
112 return extent_read_full_page(tree, page, btree_get_extent);
115 static int btree_releasepage(struct page *page, gfp_t unused_gfp_flags)
117 struct extent_map_tree *tree;
120 BUG_ON(page->private != 1);
121 tree = &BTRFS_I(page->mapping->host)->extent_tree;
122 ret = try_release_extent_mapping(tree, page);
124 ClearPagePrivate(page);
125 set_page_private(page, 0);
126 page_cache_release(page);
131 static void btree_invalidatepage(struct page *page, unsigned long offset)
133 struct extent_map_tree *tree;
134 tree = &BTRFS_I(page->mapping->host)->extent_tree;
135 extent_invalidatepage(tree, page, offset);
136 btree_releasepage(page, GFP_NOFS);
139 int btrfs_csum_data(struct btrfs_root * root, char *data, size_t len,
145 crc = crc32c(0, data, len);
146 memcpy(result, &crc, BTRFS_CRC32_SIZE);
152 static int csum_tree_block(struct btrfs_root *root, struct extent_buffer *buf,
156 char result[BTRFS_CRC32_SIZE];
158 struct btrfs_node *node;
160 ret = btrfs_csum_data(root, bh->b_data + BTRFS_CSUM_SIZE,
161 bh->b_size - BTRFS_CSUM_SIZE, result);
165 if (memcmp(bh->b_data, result, BTRFS_CRC32_SIZE)) {
166 printk("btrfs: %s checksum verify failed on %llu\n",
167 root->fs_info->sb->s_id,
168 (unsigned long long)bh_blocknr(bh));
172 node = btrfs_buffer_node(bh);
173 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);
200 static struct address_space_operations btree_aops = {
201 .readpage = btree_readpage,
202 .writepage = btree_writepage,
203 .releasepage = btree_releasepage,
204 .invalidatepage = btree_invalidatepage,
205 .sync_page = block_sync_page,
208 int readahead_tree_block(struct btrfs_root *root, u64 bytenr, u32 blocksize)
210 struct extent_buffer *buf = NULL;
211 struct inode *btree_inode = root->fs_info->btree_inode;
214 buf = btrfs_find_create_tree_block(root, bytenr, blocksize);
217 read_extent_buffer_pages(&BTRFS_I(btree_inode)->extent_tree,
219 free_extent_buffer(buf);
223 struct extent_buffer *read_tree_block(struct btrfs_root *root, u64 bytenr,
226 struct extent_buffer *buf = NULL;
227 struct inode *btree_inode = root->fs_info->btree_inode;
229 buf = btrfs_find_create_tree_block(root, bytenr, blocksize);
232 read_extent_buffer_pages(&BTRFS_I(btree_inode)->extent_tree,
237 int clean_tree_block(struct btrfs_trans_handle *trans, struct btrfs_root *root,
238 struct extent_buffer *buf)
240 struct inode *btree_inode = root->fs_info->btree_inode;
241 clear_extent_buffer_dirty(&BTRFS_I(btree_inode)->extent_tree, buf);
245 int wait_on_tree_block_writeback(struct btrfs_root *root,
246 struct extent_buffer *buf)
248 struct inode *btree_inode = root->fs_info->btree_inode;
249 wait_on_extent_buffer_writeback(&BTRFS_I(btree_inode)->extent_tree,
254 int set_tree_block_dirty(struct btrfs_root *root, struct extent_buffer *buf)
256 struct inode *btree_inode = root->fs_info->btree_inode;
257 set_extent_buffer_dirty(&BTRFS_I(btree_inode)->extent_tree, buf);
261 static int __setup_root(u32 nodesize, u32 leafsize, u32 sectorsize,
262 struct btrfs_root *root,
263 struct btrfs_fs_info *fs_info,
268 root->commit_root = NULL;
269 root->sectorsize = sectorsize;
270 root->nodesize = nodesize;
271 root->leafsize = leafsize;
273 root->fs_info = fs_info;
274 root->objectid = objectid;
275 root->last_trans = 0;
276 root->highest_inode = 0;
277 root->last_inode_alloc = 0;
279 memset(&root->root_key, 0, sizeof(root->root_key));
280 memset(&root->root_item, 0, sizeof(root->root_item));
281 memset(&root->defrag_progress, 0, sizeof(root->defrag_progress));
282 memset(&root->root_kobj, 0, sizeof(root->root_kobj));
283 init_completion(&root->kobj_unregister);
284 init_rwsem(&root->snap_sem);
285 root->defrag_running = 0;
286 root->defrag_level = 0;
287 root->root_key.objectid = objectid;
291 static int find_and_setup_root(struct btrfs_root *tree_root,
292 struct btrfs_fs_info *fs_info,
294 struct btrfs_root *root)
299 __setup_root(tree_root->nodesize, tree_root->leafsize,
300 tree_root->sectorsize, root, fs_info, objectid);
301 ret = btrfs_find_last_root(tree_root, objectid,
302 &root->root_item, &root->root_key);
305 blocksize = btrfs_level_size(root, btrfs_root_level(&root->root_item));
306 root->node = read_tree_block(root, btrfs_root_bytenr(&root->root_item),
312 struct btrfs_root *btrfs_read_fs_root_no_radix(struct btrfs_fs_info *fs_info,
313 struct btrfs_key *location)
315 struct btrfs_root *root;
316 struct btrfs_root *tree_root = fs_info->tree_root;
317 struct btrfs_path *path;
318 struct extent_buffer *l;
323 root = kzalloc(sizeof(*root), GFP_NOFS);
325 return ERR_PTR(-ENOMEM);
326 if (location->offset == (u64)-1) {
327 ret = find_and_setup_root(tree_root, fs_info,
328 location->objectid, root);
336 __setup_root(tree_root->nodesize, tree_root->leafsize,
337 tree_root->sectorsize, root, fs_info,
340 path = btrfs_alloc_path();
342 ret = btrfs_search_slot(NULL, tree_root, location, path, 0, 0);
349 read_extent_buffer(l, &root->root_item,
350 btrfs_item_ptr_offset(l, path->slots[0]),
351 sizeof(root->root_item));
354 btrfs_release_path(root, path);
355 btrfs_free_path(path);
360 blocksize = btrfs_level_size(root, btrfs_root_level(&root->root_item));
361 root->node = read_tree_block(root, btrfs_root_bytenr(&root->root_item),
366 ret = btrfs_find_highest_inode(root, &highest_inode);
368 root->highest_inode = highest_inode;
369 root->last_inode_alloc = highest_inode;
374 struct btrfs_root *btrfs_read_fs_root(struct btrfs_fs_info *fs_info,
375 struct btrfs_key *location,
376 const char *name, int namelen)
378 struct btrfs_root *root;
381 root = radix_tree_lookup(&fs_info->fs_roots_radix,
382 (unsigned long)location->objectid);
386 root = btrfs_read_fs_root_no_radix(fs_info, location);
389 ret = radix_tree_insert(&fs_info->fs_roots_radix,
390 (unsigned long)root->root_key.objectid,
393 free_extent_buffer(root->node);
398 ret = btrfs_set_root_name(root, name, namelen);
400 free_extent_buffer(root->node);
405 ret = btrfs_sysfs_add_root(root);
407 free_extent_buffer(root->node);
413 ret = btrfs_find_dead_roots(fs_info->tree_root,
414 root->root_key.objectid, root);
420 struct btrfs_root *open_ctree(struct super_block *sb)
426 struct btrfs_root *extent_root = kmalloc(sizeof(struct btrfs_root),
428 struct btrfs_root *tree_root = kmalloc(sizeof(struct btrfs_root),
430 struct btrfs_fs_info *fs_info = kmalloc(sizeof(*fs_info),
434 struct btrfs_super_block *disk_super;
436 if (!extent_root || !tree_root || !fs_info) {
440 INIT_RADIX_TREE(&fs_info->fs_roots_radix, GFP_NOFS);
441 INIT_LIST_HEAD(&fs_info->trans_list);
442 INIT_LIST_HEAD(&fs_info->dead_roots);
443 memset(&fs_info->super_kobj, 0, sizeof(fs_info->super_kobj));
444 init_completion(&fs_info->kobj_unregister);
445 sb_set_blocksize(sb, 4096);
446 fs_info->running_transaction = NULL;
447 fs_info->last_trans_committed = 0;
448 fs_info->tree_root = tree_root;
449 fs_info->extent_root = extent_root;
451 fs_info->btree_inode = new_inode(sb);
452 fs_info->btree_inode->i_ino = 1;
453 fs_info->btree_inode->i_nlink = 1;
454 fs_info->btree_inode->i_size = sb->s_bdev->bd_inode->i_size;
455 fs_info->btree_inode->i_mapping->a_ops = &btree_aops;
456 extent_map_tree_init(&BTRFS_I(fs_info->btree_inode)->extent_tree,
457 fs_info->btree_inode->i_mapping,
459 extent_map_tree_init(&fs_info->free_space_cache,
460 fs_info->btree_inode->i_mapping, GFP_NOFS);
461 extent_map_tree_init(&fs_info->block_group_cache,
462 fs_info->btree_inode->i_mapping, GFP_NOFS);
463 extent_map_tree_init(&fs_info->pinned_extents,
464 fs_info->btree_inode->i_mapping, GFP_NOFS);
465 extent_map_tree_init(&fs_info->pending_del,
466 fs_info->btree_inode->i_mapping, GFP_NOFS);
467 extent_map_tree_init(&fs_info->extent_ins,
468 fs_info->btree_inode->i_mapping, GFP_NOFS);
469 fs_info->do_barriers = 1;
470 fs_info->closing = 0;
472 INIT_DELAYED_WORK(&fs_info->trans_work, btrfs_transaction_cleaner);
473 BTRFS_I(fs_info->btree_inode)->root = tree_root;
474 memset(&BTRFS_I(fs_info->btree_inode)->location, 0,
475 sizeof(struct btrfs_key));
476 insert_inode_hash(fs_info->btree_inode);
477 mapping_set_gfp_mask(fs_info->btree_inode->i_mapping, GFP_NOFS);
479 mutex_init(&fs_info->trans_mutex);
480 mutex_init(&fs_info->fs_mutex);
482 __setup_root(512, 512, 512, tree_root,
483 fs_info, BTRFS_ROOT_TREE_OBJECTID);
485 fs_info->sb_buffer = read_tree_block(tree_root,
486 BTRFS_SUPER_INFO_OFFSET,
489 if (!fs_info->sb_buffer)
492 read_extent_buffer(fs_info->sb_buffer, &fs_info->super_copy, 0,
493 sizeof(fs_info->super_copy));
495 read_extent_buffer(fs_info->sb_buffer, fs_info->fsid,
496 (unsigned long)btrfs_super_fsid(fs_info->sb_buffer),
498 disk_super = &fs_info->super_copy;
499 if (!btrfs_super_root(disk_super))
502 nodesize = btrfs_super_nodesize(disk_super);
503 leafsize = btrfs_super_leafsize(disk_super);
504 sectorsize = btrfs_super_sectorsize(disk_super);
505 tree_root->nodesize = nodesize;
506 tree_root->leafsize = leafsize;
507 tree_root->sectorsize = sectorsize;
509 i_size_write(fs_info->btree_inode,
510 btrfs_super_total_bytes(disk_super));
513 if (strncmp((char *)(&disk_super->magic), BTRFS_MAGIC,
514 sizeof(disk_super->magic))) {
515 printk("btrfs: valid FS not found on %s\n", sb->s_id);
518 blocksize = btrfs_level_size(tree_root,
519 btrfs_super_root_level(disk_super));
520 tree_root->node = read_tree_block(tree_root,
521 btrfs_super_root(disk_super),
523 if (!tree_root->node)
527 btrfs_print_leaf(tree_root, tree_root->node);
531 mutex_lock(&fs_info->fs_mutex);
533 ret = find_and_setup_root(tree_root, fs_info,
534 BTRFS_EXTENT_TREE_OBJECTID, extent_root);
536 mutex_unlock(&fs_info->fs_mutex);
540 btrfs_read_block_groups(extent_root);
542 fs_info->generation = btrfs_super_generation(disk_super) + 1;
543 mutex_unlock(&fs_info->fs_mutex);
547 free_extent_buffer(tree_root->node);
549 free_extent_buffer(fs_info->sb_buffer);
551 iput(fs_info->btree_inode);
559 int write_ctree_super(struct btrfs_trans_handle *trans, struct btrfs_root
563 struct extent_buffer *super = root->fs_info->sb_buffer;
564 struct inode *btree_inode = root->fs_info->btree_inode;
566 set_extent_buffer_dirty(&BTRFS_I(btree_inode)->extent_tree, super);
567 ret = sync_page_range_nolock(btree_inode, btree_inode->i_mapping,
568 super->start, super->len);
572 int btrfs_free_fs_root(struct btrfs_fs_info *fs_info, struct btrfs_root *root)
574 radix_tree_delete(&fs_info->fs_roots_radix,
575 (unsigned long)root->root_key.objectid);
576 btrfs_sysfs_del_root(root);
580 free_extent_buffer(root->node);
581 if (root->commit_root)
582 free_extent_buffer(root->commit_root);
589 static int del_fs_roots(struct btrfs_fs_info *fs_info)
592 struct btrfs_root *gang[8];
596 ret = radix_tree_gang_lookup(&fs_info->fs_roots_radix,
601 for (i = 0; i < ret; i++)
602 btrfs_free_fs_root(fs_info, gang[i]);
607 int close_ctree(struct btrfs_root *root)
610 struct btrfs_trans_handle *trans;
611 struct btrfs_fs_info *fs_info = root->fs_info;
613 fs_info->closing = 1;
614 btrfs_transaction_flush_work(root);
615 mutex_lock(&fs_info->fs_mutex);
616 btrfs_defrag_dirty_roots(root->fs_info);
617 trans = btrfs_start_transaction(root, 1);
618 ret = btrfs_commit_transaction(trans, root);
619 /* run commit again to drop the original snapshot */
620 trans = btrfs_start_transaction(root, 1);
621 btrfs_commit_transaction(trans, root);
622 ret = btrfs_write_and_wait_transaction(NULL, root);
624 write_ctree_super(NULL, root);
625 mutex_unlock(&fs_info->fs_mutex);
627 if (fs_info->extent_root->node)
628 free_extent_buffer(fs_info->extent_root->node);
630 if (fs_info->tree_root->node)
631 free_extent_buffer(fs_info->tree_root->node);
633 free_extent_buffer(fs_info->sb_buffer);
635 btrfs_free_block_groups(root->fs_info);
636 del_fs_roots(fs_info);
637 extent_map_tree_cleanup(&BTRFS_I(fs_info->btree_inode)->extent_tree);
638 truncate_inode_pages(fs_info->btree_inode->i_mapping, 0);
639 iput(fs_info->btree_inode);
640 kfree(fs_info->extent_root);
641 kfree(fs_info->tree_root);
645 int btrfs_buffer_uptodate(struct extent_buffer *buf)
647 struct inode *btree_inode = buf->last_page->mapping->host;
648 return extent_buffer_uptodate(&BTRFS_I(btree_inode)->extent_tree, buf);
651 int btrfs_set_buffer_uptodate(struct extent_buffer *buf)
653 struct inode *btree_inode = buf->last_page->mapping->host;
654 return set_extent_buffer_uptodate(&BTRFS_I(btree_inode)->extent_tree,
658 void btrfs_mark_buffer_dirty(struct extent_buffer *buf)
660 struct btrfs_root *root = BTRFS_I(buf->last_page->mapping->host)->root;
661 u64 transid = btrfs_header_generation(buf);
662 struct inode *btree_inode = root->fs_info->btree_inode;
664 if (transid != root->fs_info->generation) {
665 printk(KERN_CRIT "transid mismatch buffer %llu, found %Lu running %Lu\n",
666 (unsigned long long)buf->start,
667 transid, root->fs_info->generation);
670 set_extent_buffer_dirty(&BTRFS_I(btree_inode)->extent_tree, buf);
673 void btrfs_btree_balance_dirty(struct btrfs_root *root, unsigned long nr)
675 balance_dirty_pages_ratelimited_nr(
676 root->fs_info->btree_inode->i_mapping, nr);
679 void btrfs_set_buffer_defrag(struct extent_buffer *buf)
681 struct btrfs_root *root = BTRFS_I(buf->last_page->mapping->host)->root;
682 struct inode *btree_inode = root->fs_info->btree_inode;
683 set_extent_bits(&BTRFS_I(btree_inode)->extent_tree, buf->start,
684 buf->start + buf->len - 1, EXTENT_DEFRAG, GFP_NOFS);
687 void btrfs_set_buffer_defrag_done(struct extent_buffer *buf)
689 struct btrfs_root *root = BTRFS_I(buf->last_page->mapping->host)->root;
690 struct inode *btree_inode = root->fs_info->btree_inode;
691 set_extent_bits(&BTRFS_I(btree_inode)->extent_tree, buf->start,
692 buf->start + buf->len - 1, EXTENT_DEFRAG_DONE,
696 int btrfs_buffer_defrag(struct extent_buffer *buf)
698 struct btrfs_root *root = BTRFS_I(buf->last_page->mapping->host)->root;
699 struct inode *btree_inode = root->fs_info->btree_inode;
700 return test_range_bit(&BTRFS_I(btree_inode)->extent_tree,
701 buf->start, buf->start + buf->len - 1, EXTENT_DEFRAG, 0);
704 int btrfs_buffer_defrag_done(struct extent_buffer *buf)
706 struct btrfs_root *root = BTRFS_I(buf->last_page->mapping->host)->root;
707 struct inode *btree_inode = root->fs_info->btree_inode;
708 return test_range_bit(&BTRFS_I(btree_inode)->extent_tree,
709 buf->start, buf->start + buf->len - 1,
710 EXTENT_DEFRAG_DONE, 0);
713 int btrfs_clear_buffer_defrag_done(struct extent_buffer *buf)
715 struct btrfs_root *root = BTRFS_I(buf->last_page->mapping->host)->root;
716 struct inode *btree_inode = root->fs_info->btree_inode;
717 return clear_extent_bits(&BTRFS_I(btree_inode)->extent_tree,
718 buf->start, buf->start + buf->len - 1,
719 EXTENT_DEFRAG_DONE, GFP_NOFS);
722 int btrfs_clear_buffer_defrag(struct extent_buffer *buf)
724 struct btrfs_root *root = BTRFS_I(buf->last_page->mapping->host)->root;
725 struct inode *btree_inode = root->fs_info->btree_inode;
726 return clear_extent_bits(&BTRFS_I(btree_inode)->extent_tree,
727 buf->start, buf->start + buf->len - 1,
728 EXTENT_DEFRAG, GFP_NOFS);
731 int btrfs_read_buffer(struct extent_buffer *buf)
733 struct btrfs_root *root = BTRFS_I(buf->last_page->mapping->host)->root;
734 struct inode *btree_inode = root->fs_info->btree_inode;
735 return read_extent_buffer_pages(&BTRFS_I(btree_inode)->extent_tree,