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 struct extent_buffer *eb;
50 eb = find_extent_buffer(&BTRFS_I(btree_inode)->extent_tree,
51 blocknr * root->sectorsize,
52 root->sectorsize, GFP_NOFS);
54 eb->alloc_addr = (unsigned long)__builtin_return_address(0);
58 struct extent_buffer *btrfs_find_create_tree_block(struct btrfs_root *root,
61 struct inode *btree_inode = root->fs_info->btree_inode;
62 struct extent_buffer *eb;
63 eb = alloc_extent_buffer(&BTRFS_I(btree_inode)->extent_tree,
64 blocknr * root->sectorsize,
65 root->sectorsize, GFP_NOFS);
66 eb->alloc_addr = (unsigned long)__builtin_return_address(0);
70 struct extent_map *btree_get_extent(struct inode *inode, struct page *page,
71 size_t page_offset, u64 start, u64 end,
74 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
75 struct extent_map *em;
79 em = lookup_extent_mapping(em_tree, start, end);
83 em = alloc_extent_map(GFP_NOFS);
85 em = ERR_PTR(-ENOMEM);
89 em->end = (i_size_read(inode) & ~((u64)PAGE_CACHE_SIZE -1)) - 1;
91 em->block_end = em->end;
92 em->bdev = inode->i_sb->s_bdev;
93 ret = add_extent_mapping(em_tree, em);
105 static int btree_writepage(struct page *page, struct writeback_control *wbc)
107 struct extent_map_tree *tree;
108 tree = &BTRFS_I(page->mapping->host)->extent_tree;
109 return extent_write_full_page(tree, page, btree_get_extent, wbc);
111 int btree_readpage(struct file *file, struct page *page)
113 struct extent_map_tree *tree;
114 tree = &BTRFS_I(page->mapping->host)->extent_tree;
115 return extent_read_full_page(tree, page, btree_get_extent);
118 static int btree_releasepage(struct page *page, gfp_t unused_gfp_flags)
120 struct extent_map_tree *tree;
123 BUG_ON(page->private != 1);
124 tree = &BTRFS_I(page->mapping->host)->extent_tree;
125 ret = try_release_extent_mapping(tree, page);
127 ClearPagePrivate(page);
128 set_page_private(page, 0);
129 page_cache_release(page);
134 static void btree_invalidatepage(struct page *page, unsigned long offset)
136 struct extent_map_tree *tree;
137 tree = &BTRFS_I(page->mapping->host)->extent_tree;
138 extent_invalidatepage(tree, page, offset);
139 btree_releasepage(page, GFP_NOFS);
142 int btrfs_csum_data(struct btrfs_root * root, char *data, size_t len,
148 crc = crc32c(0, data, len);
149 memcpy(result, &crc, BTRFS_CRC32_SIZE);
155 static int csum_tree_block(struct btrfs_root *root, struct extent_buffer *buf,
159 char result[BTRFS_CRC32_SIZE];
161 struct btrfs_node *node;
163 ret = btrfs_csum_data(root, bh->b_data + BTRFS_CSUM_SIZE,
164 bh->b_size - BTRFS_CSUM_SIZE, result);
168 if (memcmp(bh->b_data, result, BTRFS_CRC32_SIZE)) {
169 printk("btrfs: %s checksum verify failed on %llu\n",
170 root->fs_info->sb->s_id,
171 (unsigned long long)bh_blocknr(bh));
175 node = btrfs_buffer_node(bh);
176 memcpy(node->header.csum, result, BTRFS_CRC32_SIZE);
183 static int btree_writepage(struct page *page, struct writeback_control *wbc)
185 struct buffer_head *bh;
186 struct btrfs_root *root = BTRFS_I(page->mapping->host)->root;
187 struct buffer_head *head;
188 if (!page_has_buffers(page)) {
189 create_empty_buffers(page, root->fs_info->sb->s_blocksize,
190 (1 << BH_Dirty)|(1 << BH_Uptodate));
192 head = page_buffers(page);
195 if (buffer_dirty(bh))
196 csum_tree_block(root, bh, 0);
197 bh = bh->b_this_page;
198 } while (bh != head);
199 return block_write_full_page(page, btree_get_block, wbc);
203 static struct address_space_operations btree_aops = {
204 .readpage = btree_readpage,
205 .writepage = btree_writepage,
206 .releasepage = btree_releasepage,
207 .invalidatepage = btree_invalidatepage,
208 .sync_page = block_sync_page,
211 int readahead_tree_block(struct btrfs_root *root, u64 blocknr)
213 struct extent_buffer *buf = NULL;
214 struct inode *btree_inode = root->fs_info->btree_inode;
217 buf = btrfs_find_create_tree_block(root, blocknr);
220 read_extent_buffer_pages(&BTRFS_I(btree_inode)->extent_tree,
222 free_extent_buffer(buf);
226 struct extent_buffer *read_tree_block(struct btrfs_root *root, u64 blocknr)
228 struct extent_buffer *buf = NULL;
229 struct inode *btree_inode = root->fs_info->btree_inode;
231 buf = btrfs_find_create_tree_block(root, blocknr);
234 read_extent_buffer_pages(&BTRFS_I(btree_inode)->extent_tree,
236 buf->alloc_addr = (unsigned long)__builtin_return_address(0);
240 int clean_tree_block(struct btrfs_trans_handle *trans, struct btrfs_root *root,
241 struct extent_buffer *buf)
243 struct inode *btree_inode = root->fs_info->btree_inode;
244 clear_extent_buffer_dirty(&BTRFS_I(btree_inode)->extent_tree, buf);
248 int wait_on_tree_block_writeback(struct btrfs_root *root,
249 struct extent_buffer *buf)
251 struct inode *btree_inode = root->fs_info->btree_inode;
252 wait_on_extent_buffer_writeback(&BTRFS_I(btree_inode)->extent_tree,
257 int set_tree_block_dirty(struct btrfs_root *root, struct extent_buffer *buf)
259 struct inode *btree_inode = root->fs_info->btree_inode;
260 set_extent_buffer_dirty(&BTRFS_I(btree_inode)->extent_tree, buf);
264 static int __setup_root(int blocksize,
265 struct btrfs_root *root,
266 struct btrfs_fs_info *fs_info,
271 root->commit_root = NULL;
272 root->sectorsize = blocksize;
273 root->nodesize = blocksize;
274 root->leafsize = blocksize;
276 root->fs_info = fs_info;
277 root->objectid = objectid;
278 root->last_trans = 0;
279 root->highest_inode = 0;
280 root->last_inode_alloc = 0;
282 memset(&root->root_key, 0, sizeof(root->root_key));
283 memset(&root->root_item, 0, sizeof(root->root_item));
284 memset(&root->defrag_progress, 0, sizeof(root->defrag_progress));
285 memset(&root->root_kobj, 0, sizeof(root->root_kobj));
286 init_completion(&root->kobj_unregister);
287 init_rwsem(&root->snap_sem);
288 root->defrag_running = 0;
289 root->defrag_level = 0;
290 root->root_key.objectid = objectid;
294 static int find_and_setup_root(int blocksize,
295 struct btrfs_root *tree_root,
296 struct btrfs_fs_info *fs_info,
298 struct btrfs_root *root)
302 __setup_root(blocksize, root, fs_info, objectid);
303 ret = btrfs_find_last_root(tree_root, objectid,
304 &root->root_item, &root->root_key);
307 root->node = read_tree_block(root,
308 btrfs_root_blocknr(&root->root_item));
313 struct btrfs_root *btrfs_read_fs_root_no_radix(struct btrfs_fs_info *fs_info,
314 struct btrfs_key *location)
316 struct btrfs_root *root;
317 struct btrfs_root *tree_root = fs_info->tree_root;
318 struct btrfs_path *path;
319 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(fs_info->sb->s_blocksize,
328 fs_info->tree_root, fs_info,
329 location->objectid, root);
337 __setup_root(fs_info->sb->s_blocksize, 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 root->node = read_tree_block(root,
361 btrfs_root_blocknr(&root->root_item));
365 ret = btrfs_find_highest_inode(root, &highest_inode);
367 root->highest_inode = highest_inode;
368 root->last_inode_alloc = highest_inode;
373 struct btrfs_root *btrfs_read_fs_root(struct btrfs_fs_info *fs_info,
374 struct btrfs_key *location,
375 const char *name, int namelen)
377 struct btrfs_root *root;
380 root = radix_tree_lookup(&fs_info->fs_roots_radix,
381 (unsigned long)location->objectid);
385 root = btrfs_read_fs_root_no_radix(fs_info, location);
388 ret = radix_tree_insert(&fs_info->fs_roots_radix,
389 (unsigned long)root->root_key.objectid,
392 free_extent_buffer(root->node);
397 ret = btrfs_set_root_name(root, name, namelen);
399 free_extent_buffer(root->node);
404 ret = btrfs_sysfs_add_root(root);
406 free_extent_buffer(root->node);
412 ret = btrfs_find_dead_roots(fs_info->tree_root,
413 root->root_key.objectid, root);
419 struct btrfs_root *open_ctree(struct super_block *sb)
421 struct btrfs_root *extent_root = kmalloc(sizeof(struct btrfs_root),
423 struct btrfs_root *tree_root = kmalloc(sizeof(struct btrfs_root),
425 struct btrfs_fs_info *fs_info = kmalloc(sizeof(*fs_info),
429 struct btrfs_super_block *disk_super;
431 if (!extent_root || !tree_root || !fs_info) {
435 init_bit_radix(&fs_info->pinned_radix);
436 init_bit_radix(&fs_info->pending_del_radix);
437 init_bit_radix(&fs_info->extent_ins_radix);
438 INIT_RADIX_TREE(&fs_info->fs_roots_radix, GFP_NOFS);
439 INIT_LIST_HEAD(&fs_info->trans_list);
440 INIT_LIST_HEAD(&fs_info->dead_roots);
441 memset(&fs_info->super_kobj, 0, sizeof(fs_info->super_kobj));
442 init_completion(&fs_info->kobj_unregister);
443 sb_set_blocksize(sb, 4096);
444 fs_info->running_transaction = NULL;
445 fs_info->last_trans_committed = 0;
446 fs_info->tree_root = tree_root;
447 fs_info->extent_root = extent_root;
449 fs_info->btree_inode = new_inode(sb);
450 fs_info->btree_inode->i_ino = 1;
451 fs_info->btree_inode->i_nlink = 1;
452 fs_info->btree_inode->i_size = sb->s_bdev->bd_inode->i_size;
453 fs_info->btree_inode->i_mapping->a_ops = &btree_aops;
454 extent_map_tree_init(&BTRFS_I(fs_info->btree_inode)->extent_tree,
455 fs_info->btree_inode->i_mapping,
457 extent_map_tree_init(&fs_info->free_space_cache,
458 fs_info->btree_inode->i_mapping, GFP_NOFS);
459 extent_map_tree_init(&fs_info->block_group_cache,
460 fs_info->btree_inode->i_mapping, GFP_NOFS);
461 fs_info->do_barriers = 1;
462 fs_info->closing = 0;
464 INIT_DELAYED_WORK(&fs_info->trans_work, btrfs_transaction_cleaner);
465 BTRFS_I(fs_info->btree_inode)->root = tree_root;
466 memset(&BTRFS_I(fs_info->btree_inode)->location, 0,
467 sizeof(struct btrfs_key));
468 insert_inode_hash(fs_info->btree_inode);
469 mapping_set_gfp_mask(fs_info->btree_inode->i_mapping, GFP_NOFS);
471 mutex_init(&fs_info->trans_mutex);
472 mutex_init(&fs_info->fs_mutex);
474 __setup_root(sb->s_blocksize, tree_root,
475 fs_info, BTRFS_ROOT_TREE_OBJECTID);
477 fs_info->sb_buffer = read_tree_block(tree_root,
478 BTRFS_SUPER_INFO_OFFSET /
481 if (!fs_info->sb_buffer)
484 read_extent_buffer(fs_info->sb_buffer, &fs_info->super_copy, 0,
485 sizeof(fs_info->super_copy));
487 read_extent_buffer(fs_info->sb_buffer, fs_info->fsid,
488 (unsigned long)btrfs_super_fsid(fs_info->sb_buffer),
490 disk_super = &fs_info->super_copy;
491 if (!btrfs_super_root(disk_super))
494 i_size_write(fs_info->btree_inode,
495 btrfs_super_total_blocks(disk_super) <<
496 fs_info->btree_inode->i_blkbits);
499 if (strncmp((char *)(&disk_super->magic), BTRFS_MAGIC,
500 sizeof(disk_super->magic))) {
501 printk("btrfs: valid FS not found on %s\n", sb->s_id);
504 tree_root->node = read_tree_block(tree_root,
505 btrfs_super_root(disk_super));
506 if (!tree_root->node)
509 mutex_lock(&fs_info->fs_mutex);
510 ret = find_and_setup_root(sb->s_blocksize, tree_root, fs_info,
511 BTRFS_EXTENT_TREE_OBJECTID, extent_root);
513 mutex_unlock(&fs_info->fs_mutex);
517 btrfs_read_block_groups(extent_root);
519 fs_info->generation = btrfs_super_generation(disk_super) + 1;
520 mutex_unlock(&fs_info->fs_mutex);
524 free_extent_buffer(tree_root->node);
526 free_extent_buffer(fs_info->sb_buffer);
528 iput(fs_info->btree_inode);
536 int write_ctree_super(struct btrfs_trans_handle *trans, struct btrfs_root
540 struct extent_buffer *super = root->fs_info->sb_buffer;
541 struct inode *btree_inode = root->fs_info->btree_inode;
543 set_extent_buffer_dirty(&BTRFS_I(btree_inode)->extent_tree, super);
544 ret = sync_page_range_nolock(btree_inode, btree_inode->i_mapping,
545 super->start, super->len);
549 int btrfs_free_fs_root(struct btrfs_fs_info *fs_info, struct btrfs_root *root)
551 radix_tree_delete(&fs_info->fs_roots_radix,
552 (unsigned long)root->root_key.objectid);
553 btrfs_sysfs_del_root(root);
557 free_extent_buffer(root->node);
558 if (root->commit_root)
559 free_extent_buffer(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 free_extent_buffer(fs_info->extent_root->node);
607 if (fs_info->tree_root->node)
608 free_extent_buffer(fs_info->tree_root->node);
610 free_extent_buffer(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 int btrfs_buffer_uptodate(struct extent_buffer *buf)
623 struct inode *btree_inode = buf->first_page->mapping->host;
624 return extent_buffer_uptodate(&BTRFS_I(btree_inode)->extent_tree, buf);
627 int btrfs_set_buffer_uptodate(struct extent_buffer *buf)
629 struct inode *btree_inode = buf->first_page->mapping->host;
630 return set_extent_buffer_uptodate(&BTRFS_I(btree_inode)->extent_tree,
634 void btrfs_mark_buffer_dirty(struct extent_buffer *buf)
636 struct btrfs_root *root = BTRFS_I(buf->first_page->mapping->host)->root;
637 u64 transid = btrfs_header_generation(buf);
638 struct inode *btree_inode = root->fs_info->btree_inode;
640 if (transid != root->fs_info->generation) {
641 printk(KERN_CRIT "transid mismatch buffer %llu, found %Lu running %Lu\n",
642 (unsigned long long)extent_buffer_blocknr(buf),
643 transid, root->fs_info->generation);
646 set_extent_buffer_dirty(&BTRFS_I(btree_inode)->extent_tree, buf);
649 void btrfs_btree_balance_dirty(struct btrfs_root *root, unsigned long nr)
651 balance_dirty_pages_ratelimited_nr(
652 root->fs_info->btree_inode->i_mapping, nr);