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"
32 #include "print-tree.h"
35 static int check_tree_block(struct btrfs_root *root, struct extent_buffer *buf)
37 if (extent_buffer_blocknr(buf) != btrfs_header_blocknr(buf)) {
38 printk(KERN_CRIT "buf blocknr(buf) is %llu, header is %llu\n",
39 (unsigned long long)extent_buffer_blocknr(buf),
40 (unsigned long long)btrfs_header_blocknr(buf));
47 static struct extent_io_ops btree_extent_io_ops;
49 struct extent_map *btree_get_extent(struct inode *inode, struct page *page,
50 size_t page_offset, u64 start, u64 len,
53 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
54 struct extent_map *em;
58 spin_lock(&em_tree->lock);
59 em = lookup_extent_mapping(em_tree, start, len);
60 spin_unlock(&em_tree->lock);
64 em = alloc_extent_map(GFP_NOFS);
66 em = ERR_PTR(-ENOMEM);
70 em->len = i_size_read(inode);
72 em->bdev = inode->i_sb->s_bdev;
74 spin_lock(&em_tree->lock);
75 ret = add_extent_mapping(em_tree, em);
76 spin_unlock(&em_tree->lock);
89 u32 btrfs_csum_data(struct btrfs_root *root, char *data, u32 seed, size_t len)
91 return crc32c(seed, data, len);
94 void btrfs_csum_final(u32 crc, char *result)
96 *(__le32 *)result = ~cpu_to_le32(crc);
99 static int csum_tree_block(struct btrfs_root *root, struct extent_buffer *buf,
102 char result[BTRFS_CRC32_SIZE];
104 unsigned long cur_len;
105 unsigned long offset = BTRFS_CSUM_SIZE;
106 char *map_token = NULL;
108 unsigned long map_start;
109 unsigned long map_len;
113 len = buf->len - offset;
115 err = map_private_extent_buffer(buf, offset, 32,
117 &map_start, &map_len, KM_USER0);
119 printk("failed to map extent buffer! %lu\n",
123 cur_len = min(len, map_len - (offset - map_start));
124 crc = btrfs_csum_data(root, kaddr + offset - map_start,
128 unmap_extent_buffer(buf, map_token, KM_USER0);
130 btrfs_csum_final(crc, result);
133 int from_this_trans = 0;
135 if (root->fs_info->running_transaction &&
136 btrfs_header_generation(buf) ==
137 root->fs_info->running_transaction->transid)
140 /* FIXME, this is not good */
141 if (memcmp_extent_buffer(buf, result, 0, BTRFS_CRC32_SIZE)) {
144 memcpy(&found, result, BTRFS_CRC32_SIZE);
146 read_extent_buffer(buf, &val, 0, BTRFS_CRC32_SIZE);
148 printk("btrfs: %s checksum verify failed on %llu "
149 "wanted %X found %X from_this_trans %d "
151 root->fs_info->sb->s_id,
152 buf->start, val, found, from_this_trans,
153 btrfs_header_level(buf));
157 write_extent_buffer(buf, result, 0, BTRFS_CRC32_SIZE);
163 int csum_dirty_buffer(struct btrfs_root *root, struct page *page)
165 struct extent_io_tree *tree;
166 u64 start = (u64)page->index << PAGE_CACHE_SHIFT;
170 struct extent_buffer *eb;
171 tree = &BTRFS_I(page->mapping->host)->io_tree;
173 if (page->private == EXTENT_PAGE_PRIVATE)
177 len = page->private >> 2;
181 eb = alloc_extent_buffer(tree, start, len, page, GFP_NOFS);
182 read_extent_buffer_pages(tree, eb, start + PAGE_CACHE_SIZE, 1,
184 btrfs_clear_buffer_defrag(eb);
185 found_start = btrfs_header_bytenr(eb);
186 if (found_start != start) {
187 printk("warning: eb start incorrect %Lu buffer %Lu len %lu\n",
188 start, found_start, len);
192 if (eb->first_page != page) {
193 printk("bad first page %lu %lu\n", eb->first_page->index,
198 if (!PageUptodate(page)) {
199 printk("csum not up to date page %lu\n", page->index);
203 found_level = btrfs_header_level(eb);
204 spin_lock(&root->fs_info->hash_lock);
205 btrfs_set_header_flag(eb, BTRFS_HEADER_FLAG_WRITTEN);
206 spin_unlock(&root->fs_info->hash_lock);
207 csum_tree_block(root, eb, 0);
209 free_extent_buffer(eb);
214 static int btree_writepage_io_hook(struct page *page, u64 start, u64 end)
216 struct btrfs_root *root = BTRFS_I(page->mapping->host)->root;
218 csum_dirty_buffer(root, page);
222 static int btree_submit_bio_hook(struct inode *inode, int rw, struct bio *bio)
224 struct btrfs_root *root = BTRFS_I(inode)->root;
226 offset = bio->bi_sector << 9;
227 if (offset == BTRFS_SUPER_INFO_OFFSET) {
228 bio->bi_bdev = root->fs_info->sb->s_bdev;
232 return btrfs_map_bio(BTRFS_I(inode)->root, rw, bio);
235 static int btree_writepage(struct page *page, struct writeback_control *wbc)
237 struct extent_io_tree *tree;
238 tree = &BTRFS_I(page->mapping->host)->io_tree;
239 return extent_write_full_page(tree, page, btree_get_extent, wbc);
242 static int btree_writepages(struct address_space *mapping,
243 struct writeback_control *wbc)
245 struct extent_io_tree *tree;
246 tree = &BTRFS_I(mapping->host)->io_tree;
247 if (wbc->sync_mode == WB_SYNC_NONE) {
250 unsigned long thresh = 96 * 1024 * 1024;
252 if (wbc->for_kupdate)
255 if (current_is_pdflush()) {
256 thresh = 96 * 1024 * 1024;
258 thresh = 8 * 1024 * 1024;
260 num_dirty = count_range_bits(tree, &start, (u64)-1,
261 thresh, EXTENT_DIRTY);
262 if (num_dirty < thresh) {
266 return extent_writepages(tree, mapping, btree_get_extent, wbc);
269 int btree_readpage(struct file *file, struct page *page)
271 struct extent_io_tree *tree;
272 tree = &BTRFS_I(page->mapping->host)->io_tree;
273 return extent_read_full_page(tree, page, btree_get_extent);
276 static int btree_releasepage(struct page *page, gfp_t gfp_flags)
278 struct extent_io_tree *tree;
279 struct extent_map_tree *map;
282 tree = &BTRFS_I(page->mapping->host)->io_tree;
283 map = &BTRFS_I(page->mapping->host)->extent_tree;
284 ret = try_release_extent_mapping(map, tree, page, gfp_flags);
286 ClearPagePrivate(page);
287 set_page_private(page, 0);
288 page_cache_release(page);
293 static void btree_invalidatepage(struct page *page, unsigned long offset)
295 struct extent_io_tree *tree;
296 tree = &BTRFS_I(page->mapping->host)->io_tree;
297 extent_invalidatepage(tree, page, offset);
298 btree_releasepage(page, GFP_NOFS);
302 static int btree_writepage(struct page *page, struct writeback_control *wbc)
304 struct buffer_head *bh;
305 struct btrfs_root *root = BTRFS_I(page->mapping->host)->root;
306 struct buffer_head *head;
307 if (!page_has_buffers(page)) {
308 create_empty_buffers(page, root->fs_info->sb->s_blocksize,
309 (1 << BH_Dirty)|(1 << BH_Uptodate));
311 head = page_buffers(page);
314 if (buffer_dirty(bh))
315 csum_tree_block(root, bh, 0);
316 bh = bh->b_this_page;
317 } while (bh != head);
318 return block_write_full_page(page, btree_get_block, wbc);
322 static struct address_space_operations btree_aops = {
323 .readpage = btree_readpage,
324 .writepage = btree_writepage,
325 .writepages = btree_writepages,
326 .releasepage = btree_releasepage,
327 .invalidatepage = btree_invalidatepage,
328 .sync_page = block_sync_page,
331 int readahead_tree_block(struct btrfs_root *root, u64 bytenr, u32 blocksize)
333 struct extent_buffer *buf = NULL;
334 struct inode *btree_inode = root->fs_info->btree_inode;
337 buf = btrfs_find_create_tree_block(root, bytenr, blocksize);
340 read_extent_buffer_pages(&BTRFS_I(btree_inode)->io_tree,
341 buf, 0, 0, btree_get_extent);
342 free_extent_buffer(buf);
346 static int close_all_devices(struct btrfs_fs_info *fs_info)
348 struct list_head *list;
349 struct list_head *next;
350 struct btrfs_device *device;
352 list = &fs_info->fs_devices->devices;
353 list_for_each(next, list) {
354 device = list_entry(next, struct btrfs_device, dev_list);
355 if (device->bdev && device->bdev != fs_info->sb->s_bdev)
356 close_bdev_excl(device->bdev);
362 int btrfs_verify_block_csum(struct btrfs_root *root,
363 struct extent_buffer *buf)
365 struct extent_io_tree *io_tree;
369 io_tree = &BTRFS_I(root->fs_info->btree_inode)->io_tree;
370 if (buf->flags & EXTENT_CSUM)
373 end = min_t(u64, buf->len, PAGE_CACHE_SIZE);
374 end = buf->start + end - 1;
375 if (test_range_bit(io_tree, buf->start, end, EXTENT_CSUM, 1)) {
376 buf->flags |= EXTENT_CSUM;
380 lock_extent(io_tree, buf->start, end, GFP_NOFS);
382 if (test_range_bit(io_tree, buf->start, end, EXTENT_CSUM, 1)) {
383 buf->flags |= EXTENT_CSUM;
388 ret = csum_tree_block(root, buf, 1);
389 set_extent_bits(io_tree, buf->start, end, EXTENT_CSUM, GFP_NOFS);
390 buf->flags |= EXTENT_CSUM;
393 unlock_extent(io_tree, buf->start, end, GFP_NOFS);
397 struct extent_buffer *btrfs_find_tree_block(struct btrfs_root *root,
398 u64 bytenr, u32 blocksize)
400 struct inode *btree_inode = root->fs_info->btree_inode;
401 struct extent_buffer *eb;
402 eb = find_extent_buffer(&BTRFS_I(btree_inode)->io_tree,
403 bytenr, blocksize, GFP_NOFS);
407 struct extent_buffer *btrfs_find_create_tree_block(struct btrfs_root *root,
408 u64 bytenr, u32 blocksize)
410 struct inode *btree_inode = root->fs_info->btree_inode;
411 struct extent_buffer *eb;
413 eb = alloc_extent_buffer(&BTRFS_I(btree_inode)->io_tree,
414 bytenr, blocksize, NULL, GFP_NOFS);
419 struct extent_buffer *read_tree_block(struct btrfs_root *root, u64 bytenr,
422 struct extent_buffer *buf = NULL;
423 struct inode *btree_inode = root->fs_info->btree_inode;
424 struct extent_io_tree *io_tree;
427 io_tree = &BTRFS_I(btree_inode)->io_tree;
429 buf = btrfs_find_create_tree_block(root, bytenr, blocksize);
432 read_extent_buffer_pages(&BTRFS_I(btree_inode)->io_tree, buf, 0, 1,
435 ret = btrfs_verify_block_csum(root, buf);
439 int clean_tree_block(struct btrfs_trans_handle *trans, struct btrfs_root *root,
440 struct extent_buffer *buf)
442 struct inode *btree_inode = root->fs_info->btree_inode;
443 if (btrfs_header_generation(buf) ==
444 root->fs_info->running_transaction->transid)
445 clear_extent_buffer_dirty(&BTRFS_I(btree_inode)->io_tree,
450 int wait_on_tree_block_writeback(struct btrfs_root *root,
451 struct extent_buffer *buf)
453 struct inode *btree_inode = root->fs_info->btree_inode;
454 wait_on_extent_buffer_writeback(&BTRFS_I(btree_inode)->io_tree,
459 static int __setup_root(u32 nodesize, u32 leafsize, u32 sectorsize,
460 u32 stripesize, struct btrfs_root *root,
461 struct btrfs_fs_info *fs_info,
466 root->commit_root = NULL;
467 root->sectorsize = sectorsize;
468 root->nodesize = nodesize;
469 root->leafsize = leafsize;
470 root->stripesize = stripesize;
472 root->track_dirty = 0;
474 root->fs_info = fs_info;
475 root->objectid = objectid;
476 root->last_trans = 0;
477 root->highest_inode = 0;
478 root->last_inode_alloc = 0;
482 INIT_LIST_HEAD(&root->dirty_list);
483 memset(&root->root_key, 0, sizeof(root->root_key));
484 memset(&root->root_item, 0, sizeof(root->root_item));
485 memset(&root->defrag_progress, 0, sizeof(root->defrag_progress));
486 memset(&root->root_kobj, 0, sizeof(root->root_kobj));
487 init_completion(&root->kobj_unregister);
488 root->defrag_running = 0;
489 root->defrag_level = 0;
490 root->root_key.objectid = objectid;
494 static int find_and_setup_root(struct btrfs_root *tree_root,
495 struct btrfs_fs_info *fs_info,
497 struct btrfs_root *root)
502 __setup_root(tree_root->nodesize, tree_root->leafsize,
503 tree_root->sectorsize, tree_root->stripesize,
504 root, fs_info, objectid);
505 ret = btrfs_find_last_root(tree_root, objectid,
506 &root->root_item, &root->root_key);
509 blocksize = btrfs_level_size(root, btrfs_root_level(&root->root_item));
510 root->node = read_tree_block(root, btrfs_root_bytenr(&root->root_item),
516 struct btrfs_root *btrfs_read_fs_root_no_radix(struct btrfs_fs_info *fs_info,
517 struct btrfs_key *location)
519 struct btrfs_root *root;
520 struct btrfs_root *tree_root = fs_info->tree_root;
521 struct btrfs_path *path;
522 struct extent_buffer *l;
527 root = kzalloc(sizeof(*root), GFP_NOFS);
529 return ERR_PTR(-ENOMEM);
530 if (location->offset == (u64)-1) {
531 ret = find_and_setup_root(tree_root, fs_info,
532 location->objectid, root);
540 __setup_root(tree_root->nodesize, tree_root->leafsize,
541 tree_root->sectorsize, tree_root->stripesize,
542 root, fs_info, location->objectid);
544 path = btrfs_alloc_path();
546 ret = btrfs_search_slot(NULL, tree_root, location, path, 0, 0);
553 read_extent_buffer(l, &root->root_item,
554 btrfs_item_ptr_offset(l, path->slots[0]),
555 sizeof(root->root_item));
556 memcpy(&root->root_key, location, sizeof(*location));
559 btrfs_release_path(root, path);
560 btrfs_free_path(path);
565 blocksize = btrfs_level_size(root, btrfs_root_level(&root->root_item));
566 root->node = read_tree_block(root, btrfs_root_bytenr(&root->root_item),
571 ret = btrfs_find_highest_inode(root, &highest_inode);
573 root->highest_inode = highest_inode;
574 root->last_inode_alloc = highest_inode;
579 struct btrfs_root *btrfs_lookup_fs_root(struct btrfs_fs_info *fs_info,
582 struct btrfs_root *root;
584 if (root_objectid == BTRFS_ROOT_TREE_OBJECTID)
585 return fs_info->tree_root;
586 if (root_objectid == BTRFS_EXTENT_TREE_OBJECTID)
587 return fs_info->extent_root;
589 root = radix_tree_lookup(&fs_info->fs_roots_radix,
590 (unsigned long)root_objectid);
594 struct btrfs_root *btrfs_read_fs_root_no_name(struct btrfs_fs_info *fs_info,
595 struct btrfs_key *location)
597 struct btrfs_root *root;
600 if (location->objectid == BTRFS_ROOT_TREE_OBJECTID)
601 return fs_info->tree_root;
602 if (location->objectid == BTRFS_EXTENT_TREE_OBJECTID)
603 return fs_info->extent_root;
605 root = radix_tree_lookup(&fs_info->fs_roots_radix,
606 (unsigned long)location->objectid);
610 root = btrfs_read_fs_root_no_radix(fs_info, location);
613 ret = radix_tree_insert(&fs_info->fs_roots_radix,
614 (unsigned long)root->root_key.objectid,
617 free_extent_buffer(root->node);
621 ret = btrfs_find_dead_roots(fs_info->tree_root,
622 root->root_key.objectid, root);
628 struct btrfs_root *btrfs_read_fs_root(struct btrfs_fs_info *fs_info,
629 struct btrfs_key *location,
630 const char *name, int namelen)
632 struct btrfs_root *root;
635 root = btrfs_read_fs_root_no_name(fs_info, location);
642 ret = btrfs_set_root_name(root, name, namelen);
644 free_extent_buffer(root->node);
649 ret = btrfs_sysfs_add_root(root);
651 free_extent_buffer(root->node);
660 static int add_hasher(struct btrfs_fs_info *info, char *type) {
661 struct btrfs_hasher *hasher;
663 hasher = kmalloc(sizeof(*hasher), GFP_NOFS);
666 hasher->hash_tfm = crypto_alloc_hash(type, 0, CRYPTO_ALG_ASYNC);
667 if (!hasher->hash_tfm) {
671 spin_lock(&info->hash_lock);
672 list_add(&hasher->list, &info->hashers);
673 spin_unlock(&info->hash_lock);
678 static int btrfs_congested_fn(void *congested_data, int bdi_bits)
680 struct btrfs_fs_info *info = (struct btrfs_fs_info *)congested_data;
682 struct list_head *cur;
683 struct btrfs_device *device;
684 struct backing_dev_info *bdi;
686 list_for_each(cur, &info->fs_devices->devices) {
687 device = list_entry(cur, struct btrfs_device, dev_list);
688 bdi = blk_get_backing_dev_info(device->bdev);
689 if (bdi && bdi_congested(bdi, bdi_bits)) {
697 void btrfs_unplug_io_fn(struct backing_dev_info *bdi, struct page *page)
699 struct list_head *cur;
700 struct btrfs_device *device;
701 struct btrfs_fs_info *info;
703 info = (struct btrfs_fs_info *)bdi->unplug_io_data;
704 list_for_each(cur, &info->fs_devices->devices) {
705 device = list_entry(cur, struct btrfs_device, dev_list);
706 bdi = blk_get_backing_dev_info(device->bdev);
707 if (bdi->unplug_io_fn) {
708 bdi->unplug_io_fn(bdi, page);
713 static int setup_bdi(struct btrfs_fs_info *info, struct backing_dev_info *bdi)
716 bdi->ra_pages = default_backing_dev_info.ra_pages * 4;
718 bdi->capabilities = default_backing_dev_info.capabilities;
719 bdi->unplug_io_fn = btrfs_unplug_io_fn;
720 bdi->unplug_io_data = info;
721 bdi->congested_fn = btrfs_congested_fn;
722 bdi->congested_data = info;
726 struct btrfs_root *open_ctree(struct super_block *sb,
727 struct btrfs_fs_devices *fs_devices)
734 struct btrfs_root *extent_root = kmalloc(sizeof(struct btrfs_root),
736 struct btrfs_root *tree_root = kmalloc(sizeof(struct btrfs_root),
738 struct btrfs_fs_info *fs_info = kmalloc(sizeof(*fs_info),
740 struct btrfs_root *chunk_root = kmalloc(sizeof(struct btrfs_root),
742 struct btrfs_root *dev_root = kmalloc(sizeof(struct btrfs_root),
746 struct btrfs_super_block *disk_super;
747 if (!extent_root || !tree_root || !fs_info) {
751 INIT_RADIX_TREE(&fs_info->fs_roots_radix, GFP_NOFS);
752 INIT_LIST_HEAD(&fs_info->trans_list);
753 INIT_LIST_HEAD(&fs_info->dead_roots);
754 INIT_LIST_HEAD(&fs_info->hashers);
755 spin_lock_init(&fs_info->hash_lock);
756 spin_lock_init(&fs_info->delalloc_lock);
757 spin_lock_init(&fs_info->new_trans_lock);
759 memset(&fs_info->super_kobj, 0, sizeof(fs_info->super_kobj));
760 init_completion(&fs_info->kobj_unregister);
761 sb_set_blocksize(sb, 4096);
762 fs_info->running_transaction = NULL;
763 fs_info->last_trans_committed = 0;
764 fs_info->tree_root = tree_root;
765 fs_info->extent_root = extent_root;
766 fs_info->chunk_root = chunk_root;
767 fs_info->dev_root = dev_root;
768 fs_info->fs_devices = fs_devices;
769 INIT_LIST_HEAD(&fs_info->dirty_cowonly_roots);
770 INIT_LIST_HEAD(&fs_info->space_info);
771 btrfs_mapping_init(&fs_info->mapping_tree);
773 fs_info->throttles = 0;
774 fs_info->mount_opt = 0;
775 fs_info->max_extent = (u64)-1;
776 fs_info->max_inline = 8192 * 1024;
777 fs_info->delalloc_bytes = 0;
778 setup_bdi(fs_info, &fs_info->bdi);
779 fs_info->btree_inode = new_inode(sb);
780 fs_info->btree_inode->i_ino = 1;
781 fs_info->btree_inode->i_nlink = 1;
782 fs_info->btree_inode->i_size = sb->s_bdev->bd_inode->i_size;
783 fs_info->btree_inode->i_mapping->a_ops = &btree_aops;
784 fs_info->btree_inode->i_mapping->backing_dev_info = &fs_info->bdi;
786 extent_io_tree_init(&BTRFS_I(fs_info->btree_inode)->io_tree,
787 fs_info->btree_inode->i_mapping,
789 extent_map_tree_init(&BTRFS_I(fs_info->btree_inode)->extent_tree,
792 BTRFS_I(fs_info->btree_inode)->io_tree.ops = &btree_extent_io_ops;
794 extent_io_tree_init(&fs_info->free_space_cache,
795 fs_info->btree_inode->i_mapping, GFP_NOFS);
796 extent_io_tree_init(&fs_info->block_group_cache,
797 fs_info->btree_inode->i_mapping, GFP_NOFS);
798 extent_io_tree_init(&fs_info->pinned_extents,
799 fs_info->btree_inode->i_mapping, GFP_NOFS);
800 extent_io_tree_init(&fs_info->pending_del,
801 fs_info->btree_inode->i_mapping, GFP_NOFS);
802 extent_io_tree_init(&fs_info->extent_ins,
803 fs_info->btree_inode->i_mapping, GFP_NOFS);
804 fs_info->do_barriers = 1;
805 fs_info->closing = 0;
806 fs_info->total_pinned = 0;
807 fs_info->last_alloc = 0;
808 fs_info->last_data_alloc = 0;
809 fs_info->extra_alloc_bits = 0;
810 fs_info->extra_data_alloc_bits = 0;
812 #if LINUX_VERSION_CODE <= KERNEL_VERSION(2,6,18)
813 INIT_WORK(&fs_info->trans_work, btrfs_transaction_cleaner, fs_info);
815 INIT_DELAYED_WORK(&fs_info->trans_work, btrfs_transaction_cleaner);
817 BTRFS_I(fs_info->btree_inode)->root = tree_root;
818 memset(&BTRFS_I(fs_info->btree_inode)->location, 0,
819 sizeof(struct btrfs_key));
820 insert_inode_hash(fs_info->btree_inode);
821 mapping_set_gfp_mask(fs_info->btree_inode->i_mapping, GFP_NOFS);
823 mutex_init(&fs_info->trans_mutex);
824 mutex_init(&fs_info->fs_mutex);
827 ret = add_hasher(fs_info, "crc32c");
829 printk("btrfs: failed hash setup, modprobe cryptomgr?\n");
834 __setup_root(4096, 4096, 4096, 4096, tree_root,
835 fs_info, BTRFS_ROOT_TREE_OBJECTID);
837 fs_info->sb_buffer = read_tree_block(tree_root,
838 BTRFS_SUPER_INFO_OFFSET,
841 if (!fs_info->sb_buffer)
844 read_extent_buffer(fs_info->sb_buffer, &fs_info->super_copy, 0,
845 sizeof(fs_info->super_copy));
847 read_extent_buffer(fs_info->sb_buffer, fs_info->fsid,
848 (unsigned long)btrfs_super_fsid(fs_info->sb_buffer),
851 disk_super = &fs_info->super_copy;
852 if (!btrfs_super_root(disk_super))
855 if (btrfs_super_num_devices(disk_super) != fs_devices->num_devices) {
856 printk("Btrfs: wanted %llu devices, but found %llu\n",
857 (unsigned long long)btrfs_super_num_devices(disk_super),
858 (unsigned long long)fs_devices->num_devices);
861 nodesize = btrfs_super_nodesize(disk_super);
862 leafsize = btrfs_super_leafsize(disk_super);
863 sectorsize = btrfs_super_sectorsize(disk_super);
864 stripesize = btrfs_super_stripesize(disk_super);
865 tree_root->nodesize = nodesize;
866 tree_root->leafsize = leafsize;
867 tree_root->sectorsize = sectorsize;
868 tree_root->stripesize = stripesize;
869 sb_set_blocksize(sb, sectorsize);
871 i_size_write(fs_info->btree_inode,
872 btrfs_super_total_bytes(disk_super));
874 if (strncmp((char *)(&disk_super->magic), BTRFS_MAGIC,
875 sizeof(disk_super->magic))) {
876 printk("btrfs: valid FS not found on %s\n", sb->s_id);
880 mutex_lock(&fs_info->fs_mutex);
882 ret = btrfs_read_sys_array(tree_root);
885 blocksize = btrfs_level_size(tree_root,
886 btrfs_super_chunk_root_level(disk_super));
888 __setup_root(nodesize, leafsize, sectorsize, stripesize,
889 chunk_root, fs_info, BTRFS_CHUNK_TREE_OBJECTID);
891 chunk_root->node = read_tree_block(chunk_root,
892 btrfs_super_chunk_root(disk_super),
894 BUG_ON(!chunk_root->node);
896 ret = btrfs_read_chunk_tree(chunk_root);
899 blocksize = btrfs_level_size(tree_root,
900 btrfs_super_root_level(disk_super));
903 tree_root->node = read_tree_block(tree_root,
904 btrfs_super_root(disk_super),
906 if (!tree_root->node)
910 ret = find_and_setup_root(tree_root, fs_info,
911 BTRFS_EXTENT_TREE_OBJECTID, extent_root);
914 extent_root->track_dirty = 1;
916 ret = find_and_setup_root(tree_root, fs_info,
917 BTRFS_DEV_TREE_OBJECTID, dev_root);
918 dev_root->track_dirty = 1;
921 goto fail_extent_root;
923 btrfs_read_block_groups(extent_root);
925 fs_info->generation = btrfs_super_generation(disk_super) + 1;
926 mutex_unlock(&fs_info->fs_mutex);
930 free_extent_buffer(extent_root->node);
932 mutex_unlock(&fs_info->fs_mutex);
933 free_extent_buffer(tree_root->node);
935 free_extent_buffer(fs_info->sb_buffer);
936 extent_io_tree_empty_lru(&BTRFS_I(fs_info->btree_inode)->io_tree);
938 iput(fs_info->btree_inode);
940 close_all_devices(fs_info);
943 bdi_destroy(&fs_info->bdi);
948 int write_ctree_super(struct btrfs_trans_handle *trans, struct btrfs_root
952 struct extent_buffer *super = root->fs_info->sb_buffer;
953 struct inode *btree_inode = root->fs_info->btree_inode;
954 struct super_block *sb = root->fs_info->sb;
956 if (!btrfs_test_opt(root, NOBARRIER))
957 blkdev_issue_flush(sb->s_bdev, NULL);
958 set_extent_buffer_dirty(&BTRFS_I(btree_inode)->io_tree, super);
959 ret = sync_page_range_nolock(btree_inode, btree_inode->i_mapping,
960 super->start, super->len);
961 if (!btrfs_test_opt(root, NOBARRIER))
962 blkdev_issue_flush(sb->s_bdev, NULL);
966 int btrfs_free_fs_root(struct btrfs_fs_info *fs_info, struct btrfs_root *root)
968 radix_tree_delete(&fs_info->fs_roots_radix,
969 (unsigned long)root->root_key.objectid);
971 btrfs_sysfs_del_root(root);
975 free_extent_buffer(root->node);
976 if (root->commit_root)
977 free_extent_buffer(root->commit_root);
984 static int del_fs_roots(struct btrfs_fs_info *fs_info)
987 struct btrfs_root *gang[8];
991 ret = radix_tree_gang_lookup(&fs_info->fs_roots_radix,
996 for (i = 0; i < ret; i++)
997 btrfs_free_fs_root(fs_info, gang[i]);
1002 int close_ctree(struct btrfs_root *root)
1005 struct btrfs_trans_handle *trans;
1006 struct btrfs_fs_info *fs_info = root->fs_info;
1008 fs_info->closing = 1;
1009 btrfs_transaction_flush_work(root);
1010 mutex_lock(&fs_info->fs_mutex);
1011 btrfs_defrag_dirty_roots(root->fs_info);
1012 trans = btrfs_start_transaction(root, 1);
1013 ret = btrfs_commit_transaction(trans, root);
1014 /* run commit again to drop the original snapshot */
1015 trans = btrfs_start_transaction(root, 1);
1016 btrfs_commit_transaction(trans, root);
1017 ret = btrfs_write_and_wait_transaction(NULL, root);
1019 write_ctree_super(NULL, root);
1020 mutex_unlock(&fs_info->fs_mutex);
1022 if (fs_info->delalloc_bytes) {
1023 printk("btrfs: at unmount delalloc count %Lu\n",
1024 fs_info->delalloc_bytes);
1026 if (fs_info->extent_root->node)
1027 free_extent_buffer(fs_info->extent_root->node);
1029 if (fs_info->tree_root->node)
1030 free_extent_buffer(fs_info->tree_root->node);
1032 if (root->fs_info->chunk_root->node);
1033 free_extent_buffer(root->fs_info->chunk_root->node);
1035 if (root->fs_info->dev_root->node);
1036 free_extent_buffer(root->fs_info->dev_root->node);
1038 free_extent_buffer(fs_info->sb_buffer);
1040 btrfs_free_block_groups(root->fs_info);
1041 del_fs_roots(fs_info);
1043 filemap_write_and_wait(fs_info->btree_inode->i_mapping);
1045 extent_io_tree_empty_lru(&fs_info->free_space_cache);
1046 extent_io_tree_empty_lru(&fs_info->block_group_cache);
1047 extent_io_tree_empty_lru(&fs_info->pinned_extents);
1048 extent_io_tree_empty_lru(&fs_info->pending_del);
1049 extent_io_tree_empty_lru(&fs_info->extent_ins);
1050 extent_io_tree_empty_lru(&BTRFS_I(fs_info->btree_inode)->io_tree);
1052 truncate_inode_pages(fs_info->btree_inode->i_mapping, 0);
1054 iput(fs_info->btree_inode);
1056 while(!list_empty(&fs_info->hashers)) {
1057 struct btrfs_hasher *hasher;
1058 hasher = list_entry(fs_info->hashers.next, struct btrfs_hasher,
1060 list_del(&hasher->hashers);
1061 crypto_free_hash(&fs_info->hash_tfm);
1065 close_all_devices(fs_info);
1066 btrfs_mapping_tree_free(&fs_info->mapping_tree);
1067 bdi_destroy(&fs_info->bdi);
1069 kfree(fs_info->extent_root);
1070 kfree(fs_info->tree_root);
1071 kfree(fs_info->chunk_root);
1072 kfree(fs_info->dev_root);
1076 int btrfs_buffer_uptodate(struct extent_buffer *buf)
1078 struct inode *btree_inode = buf->first_page->mapping->host;
1079 return extent_buffer_uptodate(&BTRFS_I(btree_inode)->io_tree, buf);
1082 int btrfs_set_buffer_uptodate(struct extent_buffer *buf)
1084 struct inode *btree_inode = buf->first_page->mapping->host;
1085 return set_extent_buffer_uptodate(&BTRFS_I(btree_inode)->io_tree,
1089 void btrfs_mark_buffer_dirty(struct extent_buffer *buf)
1091 struct btrfs_root *root = BTRFS_I(buf->first_page->mapping->host)->root;
1092 u64 transid = btrfs_header_generation(buf);
1093 struct inode *btree_inode = root->fs_info->btree_inode;
1095 if (transid != root->fs_info->generation) {
1096 printk(KERN_CRIT "transid mismatch buffer %llu, found %Lu running %Lu\n",
1097 (unsigned long long)buf->start,
1098 transid, root->fs_info->generation);
1101 set_extent_buffer_dirty(&BTRFS_I(btree_inode)->io_tree, buf);
1104 void btrfs_throttle(struct btrfs_root *root)
1106 struct backing_dev_info *bdi;
1108 bdi = root->fs_info->sb->s_bdev->bd_inode->i_mapping->backing_dev_info;
1109 if (root->fs_info->throttles && bdi_write_congested(bdi)) {
1110 #if LINUX_VERSION_CODE > KERNEL_VERSION(2,6,18)
1111 congestion_wait(WRITE, HZ/20);
1113 blk_congestion_wait(WRITE, HZ/20);
1118 void btrfs_btree_balance_dirty(struct btrfs_root *root, unsigned long nr)
1120 balance_dirty_pages_ratelimited_nr(
1121 root->fs_info->btree_inode->i_mapping, 1);
1124 void btrfs_set_buffer_defrag(struct extent_buffer *buf)
1126 struct btrfs_root *root = BTRFS_I(buf->first_page->mapping->host)->root;
1127 struct inode *btree_inode = root->fs_info->btree_inode;
1128 set_extent_bits(&BTRFS_I(btree_inode)->io_tree, buf->start,
1129 buf->start + buf->len - 1, EXTENT_DEFRAG, GFP_NOFS);
1132 void btrfs_set_buffer_defrag_done(struct extent_buffer *buf)
1134 struct btrfs_root *root = BTRFS_I(buf->first_page->mapping->host)->root;
1135 struct inode *btree_inode = root->fs_info->btree_inode;
1136 set_extent_bits(&BTRFS_I(btree_inode)->io_tree, buf->start,
1137 buf->start + buf->len - 1, EXTENT_DEFRAG_DONE,
1141 int btrfs_buffer_defrag(struct extent_buffer *buf)
1143 struct btrfs_root *root = BTRFS_I(buf->first_page->mapping->host)->root;
1144 struct inode *btree_inode = root->fs_info->btree_inode;
1145 return test_range_bit(&BTRFS_I(btree_inode)->io_tree,
1146 buf->start, buf->start + buf->len - 1, EXTENT_DEFRAG, 0);
1149 int btrfs_buffer_defrag_done(struct extent_buffer *buf)
1151 struct btrfs_root *root = BTRFS_I(buf->first_page->mapping->host)->root;
1152 struct inode *btree_inode = root->fs_info->btree_inode;
1153 return test_range_bit(&BTRFS_I(btree_inode)->io_tree,
1154 buf->start, buf->start + buf->len - 1,
1155 EXTENT_DEFRAG_DONE, 0);
1158 int btrfs_clear_buffer_defrag_done(struct extent_buffer *buf)
1160 struct btrfs_root *root = BTRFS_I(buf->first_page->mapping->host)->root;
1161 struct inode *btree_inode = root->fs_info->btree_inode;
1162 return clear_extent_bits(&BTRFS_I(btree_inode)->io_tree,
1163 buf->start, buf->start + buf->len - 1,
1164 EXTENT_DEFRAG_DONE, GFP_NOFS);
1167 int btrfs_clear_buffer_defrag(struct extent_buffer *buf)
1169 struct btrfs_root *root = BTRFS_I(buf->first_page->mapping->host)->root;
1170 struct inode *btree_inode = root->fs_info->btree_inode;
1171 return clear_extent_bits(&BTRFS_I(btree_inode)->io_tree,
1172 buf->start, buf->start + buf->len - 1,
1173 EXTENT_DEFRAG, GFP_NOFS);
1176 int btrfs_read_buffer(struct extent_buffer *buf)
1178 struct btrfs_root *root = BTRFS_I(buf->first_page->mapping->host)->root;
1179 struct inode *btree_inode = root->fs_info->btree_inode;
1180 return read_extent_buffer_pages(&BTRFS_I(btree_inode)->io_tree,
1181 buf, 0, 1, btree_get_extent);
1184 static struct extent_io_ops btree_extent_io_ops = {
1185 .writepage_io_hook = btree_writepage_io_hook,
1186 .submit_bio_hook = btree_submit_bio_hook,
1187 /* note we're sharing with inode.c for the merge bio hook */
1188 .merge_bio_hook = btrfs_merge_bio_hook,