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/scatterlist.h>
22 #include <linux/swap.h>
23 #include <linux/radix-tree.h>
24 #include <linux/writeback.h>
25 #include <linux/buffer_head.h>
26 #include <linux/workqueue.h>
27 #include <linux/kthread.h>
28 #include <linux/freezer.h>
29 #include <linux/crc32c.h>
30 #include <linux/slab.h>
31 #include <linux/migrate.h>
32 #include <asm/unaligned.h>
36 #include "transaction.h"
37 #include "btrfs_inode.h"
39 #include "print-tree.h"
40 #include "async-thread.h"
43 #include "free-space-cache.h"
44 #include "inode-map.h"
46 static struct extent_io_ops btree_extent_io_ops;
47 static void end_workqueue_fn(struct btrfs_work *work);
48 static void free_fs_root(struct btrfs_root *root);
49 static void btrfs_check_super_valid(struct btrfs_fs_info *fs_info,
51 static int btrfs_destroy_ordered_operations(struct btrfs_root *root);
52 static int btrfs_destroy_ordered_extents(struct btrfs_root *root);
53 static int btrfs_destroy_delayed_refs(struct btrfs_transaction *trans,
54 struct btrfs_root *root);
55 static int btrfs_destroy_pending_snapshots(struct btrfs_transaction *t);
56 static int btrfs_destroy_delalloc_inodes(struct btrfs_root *root);
57 static int btrfs_destroy_marked_extents(struct btrfs_root *root,
58 struct extent_io_tree *dirty_pages,
60 static int btrfs_destroy_pinned_extent(struct btrfs_root *root,
61 struct extent_io_tree *pinned_extents);
62 static int btrfs_cleanup_transaction(struct btrfs_root *root);
65 * end_io_wq structs are used to do processing in task context when an IO is
66 * complete. This is used during reads to verify checksums, and it is used
67 * by writes to insert metadata for new file extents after IO is complete.
73 struct btrfs_fs_info *info;
76 struct list_head list;
77 struct btrfs_work work;
81 * async submit bios are used to offload expensive checksumming
82 * onto the worker threads. They checksum file and metadata bios
83 * just before they are sent down the IO stack.
85 struct async_submit_bio {
88 struct list_head list;
89 extent_submit_bio_hook_t *submit_bio_start;
90 extent_submit_bio_hook_t *submit_bio_done;
93 unsigned long bio_flags;
95 * bio_offset is optional, can be used if the pages in the bio
96 * can't tell us where in the file the bio should go
99 struct btrfs_work work;
102 /* These are used to set the lockdep class on the extent buffer locks.
103 * The class is set by the readpage_end_io_hook after the buffer has
104 * passed csum validation but before the pages are unlocked.
106 * The lockdep class is also set by btrfs_init_new_buffer on freshly
109 * The class is based on the level in the tree block, which allows lockdep
110 * to know that lower nodes nest inside the locks of higher nodes.
112 * We also add a check to make sure the highest level of the tree is
113 * the same as our lockdep setup here. If BTRFS_MAX_LEVEL changes, this
114 * code needs update as well.
116 #ifdef CONFIG_DEBUG_LOCK_ALLOC
117 # if BTRFS_MAX_LEVEL != 8
120 static struct lock_class_key btrfs_eb_class[BTRFS_MAX_LEVEL + 1];
121 static const char *btrfs_eb_name[BTRFS_MAX_LEVEL + 1] = {
131 /* highest possible level */
137 * extents on the btree inode are pretty simple, there's one extent
138 * that covers the entire device
140 static struct extent_map *btree_get_extent(struct inode *inode,
141 struct page *page, size_t page_offset, u64 start, u64 len,
144 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
145 struct extent_map *em;
148 read_lock(&em_tree->lock);
149 em = lookup_extent_mapping(em_tree, start, len);
152 BTRFS_I(inode)->root->fs_info->fs_devices->latest_bdev;
153 read_unlock(&em_tree->lock);
156 read_unlock(&em_tree->lock);
158 em = alloc_extent_map(GFP_NOFS);
160 em = ERR_PTR(-ENOMEM);
165 em->block_len = (u64)-1;
167 em->bdev = BTRFS_I(inode)->root->fs_info->fs_devices->latest_bdev;
169 write_lock(&em_tree->lock);
170 ret = add_extent_mapping(em_tree, em);
171 if (ret == -EEXIST) {
172 u64 failed_start = em->start;
173 u64 failed_len = em->len;
176 em = lookup_extent_mapping(em_tree, start, len);
180 em = lookup_extent_mapping(em_tree, failed_start,
188 write_unlock(&em_tree->lock);
196 u32 btrfs_csum_data(struct btrfs_root *root, char *data, u32 seed, size_t len)
198 return crc32c(seed, data, len);
201 void btrfs_csum_final(u32 crc, char *result)
203 put_unaligned_le32(~crc, result);
207 * compute the csum for a btree block, and either verify it or write it
208 * into the csum field of the block.
210 static int csum_tree_block(struct btrfs_root *root, struct extent_buffer *buf,
214 btrfs_super_csum_size(&root->fs_info->super_copy);
217 unsigned long cur_len;
218 unsigned long offset = BTRFS_CSUM_SIZE;
219 char *map_token = NULL;
221 unsigned long map_start;
222 unsigned long map_len;
225 unsigned long inline_result;
227 len = buf->len - offset;
229 err = map_private_extent_buffer(buf, offset, 32,
231 &map_start, &map_len, KM_USER0);
234 cur_len = min(len, map_len - (offset - map_start));
235 crc = btrfs_csum_data(root, kaddr + offset - map_start,
239 unmap_extent_buffer(buf, map_token, KM_USER0);
241 if (csum_size > sizeof(inline_result)) {
242 result = kzalloc(csum_size * sizeof(char), GFP_NOFS);
246 result = (char *)&inline_result;
249 btrfs_csum_final(crc, result);
252 if (memcmp_extent_buffer(buf, result, 0, csum_size)) {
255 memcpy(&found, result, csum_size);
257 read_extent_buffer(buf, &val, 0, csum_size);
258 if (printk_ratelimit()) {
259 printk(KERN_INFO "btrfs: %s checksum verify "
260 "failed on %llu wanted %X found %X "
262 root->fs_info->sb->s_id,
263 (unsigned long long)buf->start, val, found,
264 btrfs_header_level(buf));
266 if (result != (char *)&inline_result)
271 write_extent_buffer(buf, result, 0, csum_size);
273 if (result != (char *)&inline_result)
279 * we can't consider a given block up to date unless the transid of the
280 * block matches the transid in the parent node's pointer. This is how we
281 * detect blocks that either didn't get written at all or got written
282 * in the wrong place.
284 static int verify_parent_transid(struct extent_io_tree *io_tree,
285 struct extent_buffer *eb, u64 parent_transid)
287 struct extent_state *cached_state = NULL;
290 if (!parent_transid || btrfs_header_generation(eb) == parent_transid)
293 lock_extent_bits(io_tree, eb->start, eb->start + eb->len - 1,
294 0, &cached_state, GFP_NOFS);
295 if (extent_buffer_uptodate(io_tree, eb, cached_state) &&
296 btrfs_header_generation(eb) == parent_transid) {
300 if (printk_ratelimit()) {
301 printk("parent transid verify failed on %llu wanted %llu "
303 (unsigned long long)eb->start,
304 (unsigned long long)parent_transid,
305 (unsigned long long)btrfs_header_generation(eb));
308 clear_extent_buffer_uptodate(io_tree, eb, &cached_state);
310 unlock_extent_cached(io_tree, eb->start, eb->start + eb->len - 1,
311 &cached_state, GFP_NOFS);
316 * helper to read a given tree block, doing retries as required when
317 * the checksums don't match and we have alternate mirrors to try.
319 static int btree_read_extent_buffer_pages(struct btrfs_root *root,
320 struct extent_buffer *eb,
321 u64 start, u64 parent_transid)
323 struct extent_io_tree *io_tree;
328 clear_bit(EXTENT_BUFFER_CORRUPT, &eb->bflags);
329 io_tree = &BTRFS_I(root->fs_info->btree_inode)->io_tree;
331 ret = read_extent_buffer_pages(io_tree, eb, start, 1,
332 btree_get_extent, mirror_num);
334 !verify_parent_transid(io_tree, eb, parent_transid))
338 * This buffer's crc is fine, but its contents are corrupted, so
339 * there is no reason to read the other copies, they won't be
342 if (test_bit(EXTENT_BUFFER_CORRUPT, &eb->bflags))
345 num_copies = btrfs_num_copies(&root->fs_info->mapping_tree,
351 if (mirror_num > num_copies)
358 * checksum a dirty tree block before IO. This has extra checks to make sure
359 * we only fill in the checksum field in the first page of a multi-page block
362 static int csum_dirty_buffer(struct btrfs_root *root, struct page *page)
364 struct extent_io_tree *tree;
365 u64 start = (u64)page->index << PAGE_CACHE_SHIFT;
368 struct extent_buffer *eb;
371 tree = &BTRFS_I(page->mapping->host)->io_tree;
373 if (page->private == EXTENT_PAGE_PRIVATE) {
377 if (!page->private) {
381 len = page->private >> 2;
384 eb = alloc_extent_buffer(tree, start, len, page, GFP_NOFS);
389 ret = btree_read_extent_buffer_pages(root, eb, start + PAGE_CACHE_SIZE,
390 btrfs_header_generation(eb));
392 WARN_ON(!btrfs_header_flag(eb, BTRFS_HEADER_FLAG_WRITTEN));
394 found_start = btrfs_header_bytenr(eb);
395 if (found_start != start) {
399 if (eb->first_page != page) {
403 if (!PageUptodate(page)) {
407 csum_tree_block(root, eb, 0);
409 free_extent_buffer(eb);
414 static int check_tree_block_fsid(struct btrfs_root *root,
415 struct extent_buffer *eb)
417 struct btrfs_fs_devices *fs_devices = root->fs_info->fs_devices;
418 u8 fsid[BTRFS_UUID_SIZE];
421 read_extent_buffer(eb, fsid, (unsigned long)btrfs_header_fsid(eb),
424 if (!memcmp(fsid, fs_devices->fsid, BTRFS_FSID_SIZE)) {
428 fs_devices = fs_devices->seed;
433 #define CORRUPT(reason, eb, root, slot) \
434 printk(KERN_CRIT "btrfs: corrupt leaf, %s: block=%llu," \
435 "root=%llu, slot=%d\n", reason, \
436 (unsigned long long)btrfs_header_bytenr(eb), \
437 (unsigned long long)root->objectid, slot)
439 static noinline int check_leaf(struct btrfs_root *root,
440 struct extent_buffer *leaf)
442 struct btrfs_key key;
443 struct btrfs_key leaf_key;
444 u32 nritems = btrfs_header_nritems(leaf);
450 /* Check the 0 item */
451 if (btrfs_item_offset_nr(leaf, 0) + btrfs_item_size_nr(leaf, 0) !=
452 BTRFS_LEAF_DATA_SIZE(root)) {
453 CORRUPT("invalid item offset size pair", leaf, root, 0);
458 * Check to make sure each items keys are in the correct order and their
459 * offsets make sense. We only have to loop through nritems-1 because
460 * we check the current slot against the next slot, which verifies the
461 * next slot's offset+size makes sense and that the current's slot
464 for (slot = 0; slot < nritems - 1; slot++) {
465 btrfs_item_key_to_cpu(leaf, &leaf_key, slot);
466 btrfs_item_key_to_cpu(leaf, &key, slot + 1);
468 /* Make sure the keys are in the right order */
469 if (btrfs_comp_cpu_keys(&leaf_key, &key) >= 0) {
470 CORRUPT("bad key order", leaf, root, slot);
475 * Make sure the offset and ends are right, remember that the
476 * item data starts at the end of the leaf and grows towards the
479 if (btrfs_item_offset_nr(leaf, slot) !=
480 btrfs_item_end_nr(leaf, slot + 1)) {
481 CORRUPT("slot offset bad", leaf, root, slot);
486 * Check to make sure that we don't point outside of the leaf,
487 * just incase all the items are consistent to eachother, but
488 * all point outside of the leaf.
490 if (btrfs_item_end_nr(leaf, slot) >
491 BTRFS_LEAF_DATA_SIZE(root)) {
492 CORRUPT("slot end outside of leaf", leaf, root, slot);
500 #ifdef CONFIG_DEBUG_LOCK_ALLOC
501 void btrfs_set_buffer_lockdep_class(struct extent_buffer *eb, int level)
503 lockdep_set_class_and_name(&eb->lock,
504 &btrfs_eb_class[level],
505 btrfs_eb_name[level]);
509 static int btree_readpage_end_io_hook(struct page *page, u64 start, u64 end,
510 struct extent_state *state)
512 struct extent_io_tree *tree;
516 struct extent_buffer *eb;
517 struct btrfs_root *root = BTRFS_I(page->mapping->host)->root;
520 tree = &BTRFS_I(page->mapping->host)->io_tree;
521 if (page->private == EXTENT_PAGE_PRIVATE)
526 len = page->private >> 2;
529 eb = alloc_extent_buffer(tree, start, len, page, GFP_NOFS);
535 found_start = btrfs_header_bytenr(eb);
536 if (found_start != start) {
537 if (printk_ratelimit()) {
538 printk(KERN_INFO "btrfs bad tree block start "
540 (unsigned long long)found_start,
541 (unsigned long long)eb->start);
546 if (eb->first_page != page) {
547 printk(KERN_INFO "btrfs bad first page %lu %lu\n",
548 eb->first_page->index, page->index);
553 if (check_tree_block_fsid(root, eb)) {
554 if (printk_ratelimit()) {
555 printk(KERN_INFO "btrfs bad fsid on block %llu\n",
556 (unsigned long long)eb->start);
561 found_level = btrfs_header_level(eb);
563 btrfs_set_buffer_lockdep_class(eb, found_level);
565 ret = csum_tree_block(root, eb, 1);
572 * If this is a leaf block and it is corrupt, set the corrupt bit so
573 * that we don't try and read the other copies of this block, just
576 if (found_level == 0 && check_leaf(root, eb)) {
577 set_bit(EXTENT_BUFFER_CORRUPT, &eb->bflags);
581 end = min_t(u64, eb->len, PAGE_CACHE_SIZE);
582 end = eb->start + end - 1;
584 free_extent_buffer(eb);
589 static void end_workqueue_bio(struct bio *bio, int err)
591 struct end_io_wq *end_io_wq = bio->bi_private;
592 struct btrfs_fs_info *fs_info;
594 fs_info = end_io_wq->info;
595 end_io_wq->error = err;
596 end_io_wq->work.func = end_workqueue_fn;
597 end_io_wq->work.flags = 0;
599 if (bio->bi_rw & REQ_WRITE) {
600 if (end_io_wq->metadata == 1)
601 btrfs_queue_worker(&fs_info->endio_meta_write_workers,
603 else if (end_io_wq->metadata == 2)
604 btrfs_queue_worker(&fs_info->endio_freespace_worker,
607 btrfs_queue_worker(&fs_info->endio_write_workers,
610 if (end_io_wq->metadata)
611 btrfs_queue_worker(&fs_info->endio_meta_workers,
614 btrfs_queue_worker(&fs_info->endio_workers,
620 * For the metadata arg you want
623 * 1 - if normal metadta
624 * 2 - if writing to the free space cache area
626 int btrfs_bio_wq_end_io(struct btrfs_fs_info *info, struct bio *bio,
629 struct end_io_wq *end_io_wq;
630 end_io_wq = kmalloc(sizeof(*end_io_wq), GFP_NOFS);
634 end_io_wq->private = bio->bi_private;
635 end_io_wq->end_io = bio->bi_end_io;
636 end_io_wq->info = info;
637 end_io_wq->error = 0;
638 end_io_wq->bio = bio;
639 end_io_wq->metadata = metadata;
641 bio->bi_private = end_io_wq;
642 bio->bi_end_io = end_workqueue_bio;
646 unsigned long btrfs_async_submit_limit(struct btrfs_fs_info *info)
648 unsigned long limit = min_t(unsigned long,
649 info->workers.max_workers,
650 info->fs_devices->open_devices);
654 int btrfs_congested_async(struct btrfs_fs_info *info, int iodone)
656 return atomic_read(&info->nr_async_bios) >
657 btrfs_async_submit_limit(info);
660 static void run_one_async_start(struct btrfs_work *work)
662 struct async_submit_bio *async;
664 async = container_of(work, struct async_submit_bio, work);
665 async->submit_bio_start(async->inode, async->rw, async->bio,
666 async->mirror_num, async->bio_flags,
670 static void run_one_async_done(struct btrfs_work *work)
672 struct btrfs_fs_info *fs_info;
673 struct async_submit_bio *async;
676 async = container_of(work, struct async_submit_bio, work);
677 fs_info = BTRFS_I(async->inode)->root->fs_info;
679 limit = btrfs_async_submit_limit(fs_info);
680 limit = limit * 2 / 3;
682 atomic_dec(&fs_info->nr_async_submits);
684 if (atomic_read(&fs_info->nr_async_submits) < limit &&
685 waitqueue_active(&fs_info->async_submit_wait))
686 wake_up(&fs_info->async_submit_wait);
688 async->submit_bio_done(async->inode, async->rw, async->bio,
689 async->mirror_num, async->bio_flags,
693 static void run_one_async_free(struct btrfs_work *work)
695 struct async_submit_bio *async;
697 async = container_of(work, struct async_submit_bio, work);
701 int btrfs_wq_submit_bio(struct btrfs_fs_info *fs_info, struct inode *inode,
702 int rw, struct bio *bio, int mirror_num,
703 unsigned long bio_flags,
705 extent_submit_bio_hook_t *submit_bio_start,
706 extent_submit_bio_hook_t *submit_bio_done)
708 struct async_submit_bio *async;
710 async = kmalloc(sizeof(*async), GFP_NOFS);
714 async->inode = inode;
717 async->mirror_num = mirror_num;
718 async->submit_bio_start = submit_bio_start;
719 async->submit_bio_done = submit_bio_done;
721 async->work.func = run_one_async_start;
722 async->work.ordered_func = run_one_async_done;
723 async->work.ordered_free = run_one_async_free;
725 async->work.flags = 0;
726 async->bio_flags = bio_flags;
727 async->bio_offset = bio_offset;
729 atomic_inc(&fs_info->nr_async_submits);
732 btrfs_set_work_high_prio(&async->work);
734 btrfs_queue_worker(&fs_info->workers, &async->work);
736 while (atomic_read(&fs_info->async_submit_draining) &&
737 atomic_read(&fs_info->nr_async_submits)) {
738 wait_event(fs_info->async_submit_wait,
739 (atomic_read(&fs_info->nr_async_submits) == 0));
745 static int btree_csum_one_bio(struct bio *bio)
747 struct bio_vec *bvec = bio->bi_io_vec;
749 struct btrfs_root *root;
751 WARN_ON(bio->bi_vcnt <= 0);
752 while (bio_index < bio->bi_vcnt) {
753 root = BTRFS_I(bvec->bv_page->mapping->host)->root;
754 csum_dirty_buffer(root, bvec->bv_page);
761 static int __btree_submit_bio_start(struct inode *inode, int rw,
762 struct bio *bio, int mirror_num,
763 unsigned long bio_flags,
767 * when we're called for a write, we're already in the async
768 * submission context. Just jump into btrfs_map_bio
770 btree_csum_one_bio(bio);
774 static int __btree_submit_bio_done(struct inode *inode, int rw, struct bio *bio,
775 int mirror_num, unsigned long bio_flags,
779 * when we're called for a write, we're already in the async
780 * submission context. Just jump into btrfs_map_bio
782 return btrfs_map_bio(BTRFS_I(inode)->root, rw, bio, mirror_num, 1);
785 static int btree_submit_bio_hook(struct inode *inode, int rw, struct bio *bio,
786 int mirror_num, unsigned long bio_flags,
791 ret = btrfs_bio_wq_end_io(BTRFS_I(inode)->root->fs_info,
795 if (!(rw & REQ_WRITE)) {
797 * called for a read, do the setup so that checksum validation
798 * can happen in the async kernel threads
800 return btrfs_map_bio(BTRFS_I(inode)->root, rw, bio,
805 * kthread helpers are used to submit writes so that checksumming
806 * can happen in parallel across all CPUs
808 return btrfs_wq_submit_bio(BTRFS_I(inode)->root->fs_info,
809 inode, rw, bio, mirror_num, 0,
811 __btree_submit_bio_start,
812 __btree_submit_bio_done);
815 #ifdef CONFIG_MIGRATION
816 static int btree_migratepage(struct address_space *mapping,
817 struct page *newpage, struct page *page)
820 * we can't safely write a btree page from here,
821 * we haven't done the locking hook
826 * Buffers may be managed in a filesystem specific way.
827 * We must have no buffers or drop them.
829 if (page_has_private(page) &&
830 !try_to_release_page(page, GFP_KERNEL))
832 return migrate_page(mapping, newpage, page);
836 static int btree_writepage(struct page *page, struct writeback_control *wbc)
838 struct extent_io_tree *tree;
839 struct btrfs_root *root = BTRFS_I(page->mapping->host)->root;
840 struct extent_buffer *eb;
843 tree = &BTRFS_I(page->mapping->host)->io_tree;
844 if (!(current->flags & PF_MEMALLOC)) {
845 return extent_write_full_page(tree, page,
846 btree_get_extent, wbc);
849 redirty_page_for_writepage(wbc, page);
850 eb = btrfs_find_tree_block(root, page_offset(page), PAGE_CACHE_SIZE);
853 was_dirty = test_and_set_bit(EXTENT_BUFFER_DIRTY, &eb->bflags);
855 spin_lock(&root->fs_info->delalloc_lock);
856 root->fs_info->dirty_metadata_bytes += PAGE_CACHE_SIZE;
857 spin_unlock(&root->fs_info->delalloc_lock);
859 free_extent_buffer(eb);
865 static int btree_writepages(struct address_space *mapping,
866 struct writeback_control *wbc)
868 struct extent_io_tree *tree;
869 tree = &BTRFS_I(mapping->host)->io_tree;
870 if (wbc->sync_mode == WB_SYNC_NONE) {
871 struct btrfs_root *root = BTRFS_I(mapping->host)->root;
873 unsigned long thresh = 32 * 1024 * 1024;
875 if (wbc->for_kupdate)
878 /* this is a bit racy, but that's ok */
879 num_dirty = root->fs_info->dirty_metadata_bytes;
880 if (num_dirty < thresh)
883 return extent_writepages(tree, mapping, btree_get_extent, wbc);
886 static int btree_readpage(struct file *file, struct page *page)
888 struct extent_io_tree *tree;
889 tree = &BTRFS_I(page->mapping->host)->io_tree;
890 return extent_read_full_page(tree, page, btree_get_extent);
893 static int btree_releasepage(struct page *page, gfp_t gfp_flags)
895 struct extent_io_tree *tree;
896 struct extent_map_tree *map;
899 if (PageWriteback(page) || PageDirty(page))
902 tree = &BTRFS_I(page->mapping->host)->io_tree;
903 map = &BTRFS_I(page->mapping->host)->extent_tree;
905 ret = try_release_extent_state(map, tree, page, gfp_flags);
909 ret = try_release_extent_buffer(tree, page);
911 ClearPagePrivate(page);
912 set_page_private(page, 0);
913 page_cache_release(page);
919 static void btree_invalidatepage(struct page *page, unsigned long offset)
921 struct extent_io_tree *tree;
922 tree = &BTRFS_I(page->mapping->host)->io_tree;
923 extent_invalidatepage(tree, page, offset);
924 btree_releasepage(page, GFP_NOFS);
925 if (PagePrivate(page)) {
926 printk(KERN_WARNING "btrfs warning page private not zero "
927 "on page %llu\n", (unsigned long long)page_offset(page));
928 ClearPagePrivate(page);
929 set_page_private(page, 0);
930 page_cache_release(page);
934 static const struct address_space_operations btree_aops = {
935 .readpage = btree_readpage,
936 .writepage = btree_writepage,
937 .writepages = btree_writepages,
938 .releasepage = btree_releasepage,
939 .invalidatepage = btree_invalidatepage,
940 .sync_page = block_sync_page,
941 #ifdef CONFIG_MIGRATION
942 .migratepage = btree_migratepage,
946 int readahead_tree_block(struct btrfs_root *root, u64 bytenr, u32 blocksize,
949 struct extent_buffer *buf = NULL;
950 struct inode *btree_inode = root->fs_info->btree_inode;
953 buf = btrfs_find_create_tree_block(root, bytenr, blocksize);
956 read_extent_buffer_pages(&BTRFS_I(btree_inode)->io_tree,
957 buf, 0, 0, btree_get_extent, 0);
958 free_extent_buffer(buf);
962 struct extent_buffer *btrfs_find_tree_block(struct btrfs_root *root,
963 u64 bytenr, u32 blocksize)
965 struct inode *btree_inode = root->fs_info->btree_inode;
966 struct extent_buffer *eb;
967 eb = find_extent_buffer(&BTRFS_I(btree_inode)->io_tree,
968 bytenr, blocksize, GFP_NOFS);
972 struct extent_buffer *btrfs_find_create_tree_block(struct btrfs_root *root,
973 u64 bytenr, u32 blocksize)
975 struct inode *btree_inode = root->fs_info->btree_inode;
976 struct extent_buffer *eb;
978 eb = alloc_extent_buffer(&BTRFS_I(btree_inode)->io_tree,
979 bytenr, blocksize, NULL, GFP_NOFS);
984 int btrfs_write_tree_block(struct extent_buffer *buf)
986 return filemap_fdatawrite_range(buf->first_page->mapping, buf->start,
987 buf->start + buf->len - 1);
990 int btrfs_wait_tree_block_writeback(struct extent_buffer *buf)
992 return filemap_fdatawait_range(buf->first_page->mapping,
993 buf->start, buf->start + buf->len - 1);
996 struct extent_buffer *read_tree_block(struct btrfs_root *root, u64 bytenr,
997 u32 blocksize, u64 parent_transid)
999 struct extent_buffer *buf = NULL;
1002 buf = btrfs_find_create_tree_block(root, bytenr, blocksize);
1006 ret = btree_read_extent_buffer_pages(root, buf, 0, parent_transid);
1009 set_bit(EXTENT_BUFFER_UPTODATE, &buf->bflags);
1014 int clean_tree_block(struct btrfs_trans_handle *trans, struct btrfs_root *root,
1015 struct extent_buffer *buf)
1017 struct inode *btree_inode = root->fs_info->btree_inode;
1018 if (btrfs_header_generation(buf) ==
1019 root->fs_info->running_transaction->transid) {
1020 btrfs_assert_tree_locked(buf);
1022 if (test_and_clear_bit(EXTENT_BUFFER_DIRTY, &buf->bflags)) {
1023 spin_lock(&root->fs_info->delalloc_lock);
1024 if (root->fs_info->dirty_metadata_bytes >= buf->len)
1025 root->fs_info->dirty_metadata_bytes -= buf->len;
1028 spin_unlock(&root->fs_info->delalloc_lock);
1031 /* ugh, clear_extent_buffer_dirty needs to lock the page */
1032 btrfs_set_lock_blocking(buf);
1033 clear_extent_buffer_dirty(&BTRFS_I(btree_inode)->io_tree,
1039 static int __setup_root(u32 nodesize, u32 leafsize, u32 sectorsize,
1040 u32 stripesize, struct btrfs_root *root,
1041 struct btrfs_fs_info *fs_info,
1045 root->commit_root = NULL;
1046 root->sectorsize = sectorsize;
1047 root->nodesize = nodesize;
1048 root->leafsize = leafsize;
1049 root->stripesize = stripesize;
1051 root->track_dirty = 0;
1053 root->orphan_item_inserted = 0;
1054 root->orphan_cleanup_state = 0;
1056 root->fs_info = fs_info;
1057 root->objectid = objectid;
1058 root->last_trans = 0;
1059 root->highest_objectid = 0;
1062 root->inode_tree = RB_ROOT;
1063 root->block_rsv = NULL;
1064 root->orphan_block_rsv = NULL;
1066 INIT_LIST_HEAD(&root->dirty_list);
1067 INIT_LIST_HEAD(&root->orphan_list);
1068 INIT_LIST_HEAD(&root->root_list);
1069 spin_lock_init(&root->node_lock);
1070 spin_lock_init(&root->orphan_lock);
1071 spin_lock_init(&root->inode_lock);
1072 spin_lock_init(&root->accounting_lock);
1073 mutex_init(&root->objectid_mutex);
1074 mutex_init(&root->log_mutex);
1075 init_waitqueue_head(&root->log_writer_wait);
1076 init_waitqueue_head(&root->log_commit_wait[0]);
1077 init_waitqueue_head(&root->log_commit_wait[1]);
1078 atomic_set(&root->log_commit[0], 0);
1079 atomic_set(&root->log_commit[1], 0);
1080 atomic_set(&root->log_writers, 0);
1081 root->log_batch = 0;
1082 root->log_transid = 0;
1083 root->last_log_commit = 0;
1084 extent_io_tree_init(&root->dirty_log_pages,
1085 fs_info->btree_inode->i_mapping, GFP_NOFS);
1087 memset(&root->root_key, 0, sizeof(root->root_key));
1088 memset(&root->root_item, 0, sizeof(root->root_item));
1089 memset(&root->defrag_progress, 0, sizeof(root->defrag_progress));
1090 memset(&root->root_kobj, 0, sizeof(root->root_kobj));
1091 root->defrag_trans_start = fs_info->generation;
1092 init_completion(&root->kobj_unregister);
1093 root->defrag_running = 0;
1094 root->root_key.objectid = objectid;
1095 root->anon_super.s_root = NULL;
1096 root->anon_super.s_dev = 0;
1097 INIT_LIST_HEAD(&root->anon_super.s_list);
1098 INIT_LIST_HEAD(&root->anon_super.s_instances);
1099 init_rwsem(&root->anon_super.s_umount);
1104 static int find_and_setup_root(struct btrfs_root *tree_root,
1105 struct btrfs_fs_info *fs_info,
1107 struct btrfs_root *root)
1113 __setup_root(tree_root->nodesize, tree_root->leafsize,
1114 tree_root->sectorsize, tree_root->stripesize,
1115 root, fs_info, objectid);
1116 ret = btrfs_find_last_root(tree_root, objectid,
1117 &root->root_item, &root->root_key);
1122 generation = btrfs_root_generation(&root->root_item);
1123 blocksize = btrfs_level_size(root, btrfs_root_level(&root->root_item));
1124 root->node = read_tree_block(root, btrfs_root_bytenr(&root->root_item),
1125 blocksize, generation);
1126 if (!root->node || !btrfs_buffer_uptodate(root->node, generation)) {
1127 free_extent_buffer(root->node);
1130 root->commit_root = btrfs_root_node(root);
1134 static struct btrfs_root *alloc_log_tree(struct btrfs_trans_handle *trans,
1135 struct btrfs_fs_info *fs_info)
1137 struct btrfs_root *root;
1138 struct btrfs_root *tree_root = fs_info->tree_root;
1139 struct extent_buffer *leaf;
1141 root = kzalloc(sizeof(*root), GFP_NOFS);
1143 return ERR_PTR(-ENOMEM);
1145 __setup_root(tree_root->nodesize, tree_root->leafsize,
1146 tree_root->sectorsize, tree_root->stripesize,
1147 root, fs_info, BTRFS_TREE_LOG_OBJECTID);
1149 root->root_key.objectid = BTRFS_TREE_LOG_OBJECTID;
1150 root->root_key.type = BTRFS_ROOT_ITEM_KEY;
1151 root->root_key.offset = BTRFS_TREE_LOG_OBJECTID;
1153 * log trees do not get reference counted because they go away
1154 * before a real commit is actually done. They do store pointers
1155 * to file data extents, and those reference counts still get
1156 * updated (along with back refs to the log tree).
1160 leaf = btrfs_alloc_free_block(trans, root, root->leafsize, 0,
1161 BTRFS_TREE_LOG_OBJECTID, NULL, 0, 0, 0);
1164 return ERR_CAST(leaf);
1167 memset_extent_buffer(leaf, 0, 0, sizeof(struct btrfs_header));
1168 btrfs_set_header_bytenr(leaf, leaf->start);
1169 btrfs_set_header_generation(leaf, trans->transid);
1170 btrfs_set_header_backref_rev(leaf, BTRFS_MIXED_BACKREF_REV);
1171 btrfs_set_header_owner(leaf, BTRFS_TREE_LOG_OBJECTID);
1174 write_extent_buffer(root->node, root->fs_info->fsid,
1175 (unsigned long)btrfs_header_fsid(root->node),
1177 btrfs_mark_buffer_dirty(root->node);
1178 btrfs_tree_unlock(root->node);
1182 int btrfs_init_log_root_tree(struct btrfs_trans_handle *trans,
1183 struct btrfs_fs_info *fs_info)
1185 struct btrfs_root *log_root;
1187 log_root = alloc_log_tree(trans, fs_info);
1188 if (IS_ERR(log_root))
1189 return PTR_ERR(log_root);
1190 WARN_ON(fs_info->log_root_tree);
1191 fs_info->log_root_tree = log_root;
1195 int btrfs_add_log_tree(struct btrfs_trans_handle *trans,
1196 struct btrfs_root *root)
1198 struct btrfs_root *log_root;
1199 struct btrfs_inode_item *inode_item;
1201 log_root = alloc_log_tree(trans, root->fs_info);
1202 if (IS_ERR(log_root))
1203 return PTR_ERR(log_root);
1205 log_root->last_trans = trans->transid;
1206 log_root->root_key.offset = root->root_key.objectid;
1208 inode_item = &log_root->root_item.inode;
1209 inode_item->generation = cpu_to_le64(1);
1210 inode_item->size = cpu_to_le64(3);
1211 inode_item->nlink = cpu_to_le32(1);
1212 inode_item->nbytes = cpu_to_le64(root->leafsize);
1213 inode_item->mode = cpu_to_le32(S_IFDIR | 0755);
1215 btrfs_set_root_node(&log_root->root_item, log_root->node);
1217 WARN_ON(root->log_root);
1218 root->log_root = log_root;
1219 root->log_transid = 0;
1220 root->last_log_commit = 0;
1224 struct btrfs_root *btrfs_read_fs_root_no_radix(struct btrfs_root *tree_root,
1225 struct btrfs_key *location)
1227 struct btrfs_root *root;
1228 struct btrfs_fs_info *fs_info = tree_root->fs_info;
1229 struct btrfs_path *path;
1230 struct extent_buffer *l;
1235 root = kzalloc(sizeof(*root), GFP_NOFS);
1237 return ERR_PTR(-ENOMEM);
1238 if (location->offset == (u64)-1) {
1239 ret = find_and_setup_root(tree_root, fs_info,
1240 location->objectid, root);
1243 return ERR_PTR(ret);
1248 __setup_root(tree_root->nodesize, tree_root->leafsize,
1249 tree_root->sectorsize, tree_root->stripesize,
1250 root, fs_info, location->objectid);
1252 path = btrfs_alloc_path();
1255 return ERR_PTR(-ENOMEM);
1257 ret = btrfs_search_slot(NULL, tree_root, location, path, 0, 0);
1260 read_extent_buffer(l, &root->root_item,
1261 btrfs_item_ptr_offset(l, path->slots[0]),
1262 sizeof(root->root_item));
1263 memcpy(&root->root_key, location, sizeof(*location));
1265 btrfs_free_path(path);
1270 return ERR_PTR(ret);
1273 generation = btrfs_root_generation(&root->root_item);
1274 blocksize = btrfs_level_size(root, btrfs_root_level(&root->root_item));
1275 root->node = read_tree_block(root, btrfs_root_bytenr(&root->root_item),
1276 blocksize, generation);
1277 root->commit_root = btrfs_root_node(root);
1278 BUG_ON(!root->node);
1280 if (location->objectid != BTRFS_TREE_LOG_OBJECTID) {
1282 btrfs_check_and_init_root_item(&root->root_item);
1288 struct btrfs_root *btrfs_lookup_fs_root(struct btrfs_fs_info *fs_info,
1291 struct btrfs_root *root;
1293 if (root_objectid == BTRFS_ROOT_TREE_OBJECTID)
1294 return fs_info->tree_root;
1295 if (root_objectid == BTRFS_EXTENT_TREE_OBJECTID)
1296 return fs_info->extent_root;
1298 root = radix_tree_lookup(&fs_info->fs_roots_radix,
1299 (unsigned long)root_objectid);
1303 struct btrfs_root *btrfs_read_fs_root_no_name(struct btrfs_fs_info *fs_info,
1304 struct btrfs_key *location)
1306 struct btrfs_root *root;
1309 if (location->objectid == BTRFS_ROOT_TREE_OBJECTID)
1310 return fs_info->tree_root;
1311 if (location->objectid == BTRFS_EXTENT_TREE_OBJECTID)
1312 return fs_info->extent_root;
1313 if (location->objectid == BTRFS_CHUNK_TREE_OBJECTID)
1314 return fs_info->chunk_root;
1315 if (location->objectid == BTRFS_DEV_TREE_OBJECTID)
1316 return fs_info->dev_root;
1317 if (location->objectid == BTRFS_CSUM_TREE_OBJECTID)
1318 return fs_info->csum_root;
1320 spin_lock(&fs_info->fs_roots_radix_lock);
1321 root = radix_tree_lookup(&fs_info->fs_roots_radix,
1322 (unsigned long)location->objectid);
1323 spin_unlock(&fs_info->fs_roots_radix_lock);
1327 root = btrfs_read_fs_root_no_radix(fs_info->tree_root, location);
1331 root->free_ino_ctl = kzalloc(sizeof(*root->free_ino_ctl), GFP_NOFS);
1332 if (!root->free_ino_ctl)
1334 root->free_ino_pinned = kzalloc(sizeof(*root->free_ino_pinned),
1336 if (!root->free_ino_pinned)
1339 btrfs_init_free_ino_ctl(root);
1340 mutex_init(&root->fs_commit_mutex);
1341 spin_lock_init(&root->cache_lock);
1342 init_waitqueue_head(&root->cache_wait);
1344 set_anon_super(&root->anon_super, NULL);
1346 if (btrfs_root_refs(&root->root_item) == 0) {
1351 ret = btrfs_find_orphan_item(fs_info->tree_root, location->objectid);
1355 root->orphan_item_inserted = 1;
1357 ret = radix_tree_preload(GFP_NOFS & ~__GFP_HIGHMEM);
1361 spin_lock(&fs_info->fs_roots_radix_lock);
1362 ret = radix_tree_insert(&fs_info->fs_roots_radix,
1363 (unsigned long)root->root_key.objectid,
1368 spin_unlock(&fs_info->fs_roots_radix_lock);
1369 radix_tree_preload_end();
1371 if (ret == -EEXIST) {
1378 ret = btrfs_find_dead_roots(fs_info->tree_root,
1379 root->root_key.objectid);
1384 return ERR_PTR(ret);
1387 struct btrfs_root *btrfs_read_fs_root(struct btrfs_fs_info *fs_info,
1388 struct btrfs_key *location,
1389 const char *name, int namelen)
1391 return btrfs_read_fs_root_no_name(fs_info, location);
1393 struct btrfs_root *root;
1396 root = btrfs_read_fs_root_no_name(fs_info, location);
1403 ret = btrfs_set_root_name(root, name, namelen);
1405 free_extent_buffer(root->node);
1407 return ERR_PTR(ret);
1410 ret = btrfs_sysfs_add_root(root);
1412 free_extent_buffer(root->node);
1415 return ERR_PTR(ret);
1422 static int btrfs_congested_fn(void *congested_data, int bdi_bits)
1424 struct btrfs_fs_info *info = (struct btrfs_fs_info *)congested_data;
1426 struct btrfs_device *device;
1427 struct backing_dev_info *bdi;
1429 list_for_each_entry(device, &info->fs_devices->devices, dev_list) {
1432 bdi = blk_get_backing_dev_info(device->bdev);
1433 if (bdi && bdi_congested(bdi, bdi_bits)) {
1442 * this unplugs every device on the box, and it is only used when page
1445 static void __unplug_io_fn(struct backing_dev_info *bdi, struct page *page)
1447 struct btrfs_device *device;
1448 struct btrfs_fs_info *info;
1450 info = (struct btrfs_fs_info *)bdi->unplug_io_data;
1451 list_for_each_entry(device, &info->fs_devices->devices, dev_list) {
1455 bdi = blk_get_backing_dev_info(device->bdev);
1456 if (bdi->unplug_io_fn)
1457 bdi->unplug_io_fn(bdi, page);
1461 static void btrfs_unplug_io_fn(struct backing_dev_info *bdi, struct page *page)
1463 struct inode *inode;
1464 struct extent_map_tree *em_tree;
1465 struct extent_map *em;
1466 struct address_space *mapping;
1469 /* the generic O_DIRECT read code does this */
1471 __unplug_io_fn(bdi, page);
1476 * page->mapping may change at any time. Get a consistent copy
1477 * and use that for everything below
1480 mapping = page->mapping;
1484 inode = mapping->host;
1487 * don't do the expensive searching for a small number of
1490 if (BTRFS_I(inode)->root->fs_info->fs_devices->open_devices <= 2) {
1491 __unplug_io_fn(bdi, page);
1495 offset = page_offset(page);
1497 em_tree = &BTRFS_I(inode)->extent_tree;
1498 read_lock(&em_tree->lock);
1499 em = lookup_extent_mapping(em_tree, offset, PAGE_CACHE_SIZE);
1500 read_unlock(&em_tree->lock);
1502 __unplug_io_fn(bdi, page);
1506 if (em->block_start >= EXTENT_MAP_LAST_BYTE) {
1507 free_extent_map(em);
1508 __unplug_io_fn(bdi, page);
1511 offset = offset - em->start;
1512 btrfs_unplug_page(&BTRFS_I(inode)->root->fs_info->mapping_tree,
1513 em->block_start + offset, page);
1514 free_extent_map(em);
1518 * If this fails, caller must call bdi_destroy() to get rid of the
1521 static int setup_bdi(struct btrfs_fs_info *info, struct backing_dev_info *bdi)
1525 bdi->capabilities = BDI_CAP_MAP_COPY;
1526 err = bdi_setup_and_register(bdi, "btrfs", BDI_CAP_MAP_COPY);
1530 bdi->ra_pages = default_backing_dev_info.ra_pages;
1531 bdi->unplug_io_fn = btrfs_unplug_io_fn;
1532 bdi->unplug_io_data = info;
1533 bdi->congested_fn = btrfs_congested_fn;
1534 bdi->congested_data = info;
1538 static int bio_ready_for_csum(struct bio *bio)
1544 struct extent_io_tree *io_tree = NULL;
1545 struct bio_vec *bvec;
1549 bio_for_each_segment(bvec, bio, i) {
1550 page = bvec->bv_page;
1551 if (page->private == EXTENT_PAGE_PRIVATE) {
1552 length += bvec->bv_len;
1555 if (!page->private) {
1556 length += bvec->bv_len;
1559 length = bvec->bv_len;
1560 buf_len = page->private >> 2;
1561 start = page_offset(page) + bvec->bv_offset;
1562 io_tree = &BTRFS_I(page->mapping->host)->io_tree;
1564 /* are we fully contained in this bio? */
1565 if (buf_len <= length)
1568 ret = extent_range_uptodate(io_tree, start + length,
1569 start + buf_len - 1);
1574 * called by the kthread helper functions to finally call the bio end_io
1575 * functions. This is where read checksum verification actually happens
1577 static void end_workqueue_fn(struct btrfs_work *work)
1580 struct end_io_wq *end_io_wq;
1581 struct btrfs_fs_info *fs_info;
1584 end_io_wq = container_of(work, struct end_io_wq, work);
1585 bio = end_io_wq->bio;
1586 fs_info = end_io_wq->info;
1588 /* metadata bio reads are special because the whole tree block must
1589 * be checksummed at once. This makes sure the entire block is in
1590 * ram and up to date before trying to verify things. For
1591 * blocksize <= pagesize, it is basically a noop
1593 if (!(bio->bi_rw & REQ_WRITE) && end_io_wq->metadata &&
1594 !bio_ready_for_csum(bio)) {
1595 btrfs_queue_worker(&fs_info->endio_meta_workers,
1599 error = end_io_wq->error;
1600 bio->bi_private = end_io_wq->private;
1601 bio->bi_end_io = end_io_wq->end_io;
1603 bio_endio(bio, error);
1606 static int cleaner_kthread(void *arg)
1608 struct btrfs_root *root = arg;
1611 vfs_check_frozen(root->fs_info->sb, SB_FREEZE_WRITE);
1613 if (!(root->fs_info->sb->s_flags & MS_RDONLY) &&
1614 mutex_trylock(&root->fs_info->cleaner_mutex)) {
1615 btrfs_run_delayed_iputs(root);
1616 btrfs_clean_old_snapshots(root);
1617 mutex_unlock(&root->fs_info->cleaner_mutex);
1620 if (freezing(current)) {
1623 set_current_state(TASK_INTERRUPTIBLE);
1624 if (!kthread_should_stop())
1626 __set_current_state(TASK_RUNNING);
1628 } while (!kthread_should_stop());
1632 static int transaction_kthread(void *arg)
1634 struct btrfs_root *root = arg;
1635 struct btrfs_trans_handle *trans;
1636 struct btrfs_transaction *cur;
1639 unsigned long delay;
1644 vfs_check_frozen(root->fs_info->sb, SB_FREEZE_WRITE);
1645 mutex_lock(&root->fs_info->transaction_kthread_mutex);
1647 spin_lock(&root->fs_info->new_trans_lock);
1648 cur = root->fs_info->running_transaction;
1650 spin_unlock(&root->fs_info->new_trans_lock);
1654 now = get_seconds();
1655 if (!cur->blocked &&
1656 (now < cur->start_time || now - cur->start_time < 30)) {
1657 spin_unlock(&root->fs_info->new_trans_lock);
1661 transid = cur->transid;
1662 spin_unlock(&root->fs_info->new_trans_lock);
1664 trans = btrfs_join_transaction(root, 1);
1665 BUG_ON(IS_ERR(trans));
1666 if (transid == trans->transid) {
1667 ret = btrfs_commit_transaction(trans, root);
1670 btrfs_end_transaction(trans, root);
1673 wake_up_process(root->fs_info->cleaner_kthread);
1674 mutex_unlock(&root->fs_info->transaction_kthread_mutex);
1676 if (freezing(current)) {
1679 set_current_state(TASK_INTERRUPTIBLE);
1680 if (!kthread_should_stop() &&
1681 !btrfs_transaction_blocked(root->fs_info))
1682 schedule_timeout(delay);
1683 __set_current_state(TASK_RUNNING);
1685 } while (!kthread_should_stop());
1689 struct btrfs_root *open_ctree(struct super_block *sb,
1690 struct btrfs_fs_devices *fs_devices,
1700 struct btrfs_key location;
1701 struct buffer_head *bh;
1702 struct btrfs_root *extent_root = kzalloc(sizeof(struct btrfs_root),
1704 struct btrfs_root *csum_root = kzalloc(sizeof(struct btrfs_root),
1706 struct btrfs_root *tree_root = btrfs_sb(sb);
1707 struct btrfs_fs_info *fs_info = tree_root->fs_info;
1708 struct btrfs_root *chunk_root = kzalloc(sizeof(struct btrfs_root),
1710 struct btrfs_root *dev_root = kzalloc(sizeof(struct btrfs_root),
1712 struct btrfs_root *log_tree_root;
1717 struct btrfs_super_block *disk_super;
1719 if (!extent_root || !tree_root || !fs_info ||
1720 !chunk_root || !dev_root || !csum_root) {
1725 ret = init_srcu_struct(&fs_info->subvol_srcu);
1731 ret = setup_bdi(fs_info, &fs_info->bdi);
1737 fs_info->btree_inode = new_inode(sb);
1738 if (!fs_info->btree_inode) {
1743 fs_info->btree_inode->i_mapping->flags &= ~__GFP_FS;
1745 INIT_RADIX_TREE(&fs_info->fs_roots_radix, GFP_ATOMIC);
1746 INIT_LIST_HEAD(&fs_info->trans_list);
1747 INIT_LIST_HEAD(&fs_info->dead_roots);
1748 INIT_LIST_HEAD(&fs_info->delayed_iputs);
1749 INIT_LIST_HEAD(&fs_info->hashers);
1750 INIT_LIST_HEAD(&fs_info->delalloc_inodes);
1751 INIT_LIST_HEAD(&fs_info->ordered_operations);
1752 INIT_LIST_HEAD(&fs_info->caching_block_groups);
1753 spin_lock_init(&fs_info->delalloc_lock);
1754 spin_lock_init(&fs_info->new_trans_lock);
1755 spin_lock_init(&fs_info->ref_cache_lock);
1756 spin_lock_init(&fs_info->fs_roots_radix_lock);
1757 spin_lock_init(&fs_info->delayed_iput_lock);
1759 init_completion(&fs_info->kobj_unregister);
1760 fs_info->tree_root = tree_root;
1761 fs_info->extent_root = extent_root;
1762 fs_info->csum_root = csum_root;
1763 fs_info->chunk_root = chunk_root;
1764 fs_info->dev_root = dev_root;
1765 fs_info->fs_devices = fs_devices;
1766 INIT_LIST_HEAD(&fs_info->dirty_cowonly_roots);
1767 INIT_LIST_HEAD(&fs_info->space_info);
1768 btrfs_mapping_init(&fs_info->mapping_tree);
1769 btrfs_init_block_rsv(&fs_info->global_block_rsv);
1770 btrfs_init_block_rsv(&fs_info->delalloc_block_rsv);
1771 btrfs_init_block_rsv(&fs_info->trans_block_rsv);
1772 btrfs_init_block_rsv(&fs_info->chunk_block_rsv);
1773 btrfs_init_block_rsv(&fs_info->empty_block_rsv);
1774 INIT_LIST_HEAD(&fs_info->durable_block_rsv_list);
1775 mutex_init(&fs_info->durable_block_rsv_mutex);
1776 atomic_set(&fs_info->nr_async_submits, 0);
1777 atomic_set(&fs_info->async_delalloc_pages, 0);
1778 atomic_set(&fs_info->async_submit_draining, 0);
1779 atomic_set(&fs_info->nr_async_bios, 0);
1781 fs_info->max_inline = 8192 * 1024;
1782 fs_info->metadata_ratio = 0;
1784 fs_info->thread_pool_size = min_t(unsigned long,
1785 num_online_cpus() + 2, 8);
1787 INIT_LIST_HEAD(&fs_info->ordered_extents);
1788 spin_lock_init(&fs_info->ordered_extent_lock);
1790 sb->s_blocksize = 4096;
1791 sb->s_blocksize_bits = blksize_bits(4096);
1792 sb->s_bdi = &fs_info->bdi;
1794 fs_info->btree_inode->i_ino = BTRFS_BTREE_INODE_OBJECTID;
1795 fs_info->btree_inode->i_nlink = 1;
1797 * we set the i_size on the btree inode to the max possible int.
1798 * the real end of the address space is determined by all of
1799 * the devices in the system
1801 fs_info->btree_inode->i_size = OFFSET_MAX;
1802 fs_info->btree_inode->i_mapping->a_ops = &btree_aops;
1803 fs_info->btree_inode->i_mapping->backing_dev_info = &fs_info->bdi;
1805 RB_CLEAR_NODE(&BTRFS_I(fs_info->btree_inode)->rb_node);
1806 extent_io_tree_init(&BTRFS_I(fs_info->btree_inode)->io_tree,
1807 fs_info->btree_inode->i_mapping,
1809 extent_map_tree_init(&BTRFS_I(fs_info->btree_inode)->extent_tree,
1812 BTRFS_I(fs_info->btree_inode)->io_tree.ops = &btree_extent_io_ops;
1814 BTRFS_I(fs_info->btree_inode)->root = tree_root;
1815 memset(&BTRFS_I(fs_info->btree_inode)->location, 0,
1816 sizeof(struct btrfs_key));
1817 BTRFS_I(fs_info->btree_inode)->dummy_inode = 1;
1818 insert_inode_hash(fs_info->btree_inode);
1820 spin_lock_init(&fs_info->block_group_cache_lock);
1821 fs_info->block_group_cache_tree = RB_ROOT;
1823 extent_io_tree_init(&fs_info->freed_extents[0],
1824 fs_info->btree_inode->i_mapping, GFP_NOFS);
1825 extent_io_tree_init(&fs_info->freed_extents[1],
1826 fs_info->btree_inode->i_mapping, GFP_NOFS);
1827 fs_info->pinned_extents = &fs_info->freed_extents[0];
1828 fs_info->do_barriers = 1;
1831 mutex_init(&fs_info->trans_mutex);
1832 mutex_init(&fs_info->ordered_operations_mutex);
1833 mutex_init(&fs_info->tree_log_mutex);
1834 mutex_init(&fs_info->chunk_mutex);
1835 mutex_init(&fs_info->transaction_kthread_mutex);
1836 mutex_init(&fs_info->cleaner_mutex);
1837 mutex_init(&fs_info->volume_mutex);
1838 init_rwsem(&fs_info->extent_commit_sem);
1839 init_rwsem(&fs_info->cleanup_work_sem);
1840 init_rwsem(&fs_info->subvol_sem);
1842 btrfs_init_free_cluster(&fs_info->meta_alloc_cluster);
1843 btrfs_init_free_cluster(&fs_info->data_alloc_cluster);
1845 init_waitqueue_head(&fs_info->transaction_throttle);
1846 init_waitqueue_head(&fs_info->transaction_wait);
1847 init_waitqueue_head(&fs_info->transaction_blocked_wait);
1848 init_waitqueue_head(&fs_info->async_submit_wait);
1850 __setup_root(4096, 4096, 4096, 4096, tree_root,
1851 fs_info, BTRFS_ROOT_TREE_OBJECTID);
1853 bh = btrfs_read_dev_super(fs_devices->latest_bdev);
1859 memcpy(&fs_info->super_copy, bh->b_data, sizeof(fs_info->super_copy));
1860 memcpy(&fs_info->super_for_commit, &fs_info->super_copy,
1861 sizeof(fs_info->super_for_commit));
1864 memcpy(fs_info->fsid, fs_info->super_copy.fsid, BTRFS_FSID_SIZE);
1866 disk_super = &fs_info->super_copy;
1867 if (!btrfs_super_root(disk_super))
1870 /* check FS state, whether FS is broken. */
1871 fs_info->fs_state |= btrfs_super_flags(disk_super);
1873 btrfs_check_super_valid(fs_info, sb->s_flags & MS_RDONLY);
1876 * In the long term, we'll store the compression type in the super
1877 * block, and it'll be used for per file compression control.
1879 fs_info->compress_type = BTRFS_COMPRESS_ZLIB;
1881 ret = btrfs_parse_options(tree_root, options);
1887 features = btrfs_super_incompat_flags(disk_super) &
1888 ~BTRFS_FEATURE_INCOMPAT_SUPP;
1890 printk(KERN_ERR "BTRFS: couldn't mount because of "
1891 "unsupported optional features (%Lx).\n",
1892 (unsigned long long)features);
1897 features = btrfs_super_incompat_flags(disk_super);
1898 features |= BTRFS_FEATURE_INCOMPAT_MIXED_BACKREF;
1899 if (tree_root->fs_info->compress_type & BTRFS_COMPRESS_LZO)
1900 features |= BTRFS_FEATURE_INCOMPAT_COMPRESS_LZO;
1901 btrfs_set_super_incompat_flags(disk_super, features);
1903 features = btrfs_super_compat_ro_flags(disk_super) &
1904 ~BTRFS_FEATURE_COMPAT_RO_SUPP;
1905 if (!(sb->s_flags & MS_RDONLY) && features) {
1906 printk(KERN_ERR "BTRFS: couldn't mount RDWR because of "
1907 "unsupported option features (%Lx).\n",
1908 (unsigned long long)features);
1913 btrfs_init_workers(&fs_info->generic_worker,
1914 "genwork", 1, NULL);
1916 btrfs_init_workers(&fs_info->workers, "worker",
1917 fs_info->thread_pool_size,
1918 &fs_info->generic_worker);
1920 btrfs_init_workers(&fs_info->delalloc_workers, "delalloc",
1921 fs_info->thread_pool_size,
1922 &fs_info->generic_worker);
1924 btrfs_init_workers(&fs_info->submit_workers, "submit",
1925 min_t(u64, fs_devices->num_devices,
1926 fs_info->thread_pool_size),
1927 &fs_info->generic_worker);
1929 /* a higher idle thresh on the submit workers makes it much more
1930 * likely that bios will be send down in a sane order to the
1933 fs_info->submit_workers.idle_thresh = 64;
1935 fs_info->workers.idle_thresh = 16;
1936 fs_info->workers.ordered = 1;
1938 fs_info->delalloc_workers.idle_thresh = 2;
1939 fs_info->delalloc_workers.ordered = 1;
1941 btrfs_init_workers(&fs_info->fixup_workers, "fixup", 1,
1942 &fs_info->generic_worker);
1943 btrfs_init_workers(&fs_info->endio_workers, "endio",
1944 fs_info->thread_pool_size,
1945 &fs_info->generic_worker);
1946 btrfs_init_workers(&fs_info->endio_meta_workers, "endio-meta",
1947 fs_info->thread_pool_size,
1948 &fs_info->generic_worker);
1949 btrfs_init_workers(&fs_info->endio_meta_write_workers,
1950 "endio-meta-write", fs_info->thread_pool_size,
1951 &fs_info->generic_worker);
1952 btrfs_init_workers(&fs_info->endio_write_workers, "endio-write",
1953 fs_info->thread_pool_size,
1954 &fs_info->generic_worker);
1955 btrfs_init_workers(&fs_info->endio_freespace_worker, "freespace-write",
1956 1, &fs_info->generic_worker);
1959 * endios are largely parallel and should have a very
1962 fs_info->endio_workers.idle_thresh = 4;
1963 fs_info->endio_meta_workers.idle_thresh = 4;
1965 fs_info->endio_write_workers.idle_thresh = 2;
1966 fs_info->endio_meta_write_workers.idle_thresh = 2;
1968 btrfs_start_workers(&fs_info->workers, 1);
1969 btrfs_start_workers(&fs_info->generic_worker, 1);
1970 btrfs_start_workers(&fs_info->submit_workers, 1);
1971 btrfs_start_workers(&fs_info->delalloc_workers, 1);
1972 btrfs_start_workers(&fs_info->fixup_workers, 1);
1973 btrfs_start_workers(&fs_info->endio_workers, 1);
1974 btrfs_start_workers(&fs_info->endio_meta_workers, 1);
1975 btrfs_start_workers(&fs_info->endio_meta_write_workers, 1);
1976 btrfs_start_workers(&fs_info->endio_write_workers, 1);
1977 btrfs_start_workers(&fs_info->endio_freespace_worker, 1);
1979 fs_info->bdi.ra_pages *= btrfs_super_num_devices(disk_super);
1980 fs_info->bdi.ra_pages = max(fs_info->bdi.ra_pages,
1981 4 * 1024 * 1024 / PAGE_CACHE_SIZE);
1983 nodesize = btrfs_super_nodesize(disk_super);
1984 leafsize = btrfs_super_leafsize(disk_super);
1985 sectorsize = btrfs_super_sectorsize(disk_super);
1986 stripesize = btrfs_super_stripesize(disk_super);
1987 tree_root->nodesize = nodesize;
1988 tree_root->leafsize = leafsize;
1989 tree_root->sectorsize = sectorsize;
1990 tree_root->stripesize = stripesize;
1992 sb->s_blocksize = sectorsize;
1993 sb->s_blocksize_bits = blksize_bits(sectorsize);
1995 if (strncmp((char *)(&disk_super->magic), BTRFS_MAGIC,
1996 sizeof(disk_super->magic))) {
1997 printk(KERN_INFO "btrfs: valid FS not found on %s\n", sb->s_id);
1998 goto fail_sb_buffer;
2001 mutex_lock(&fs_info->chunk_mutex);
2002 ret = btrfs_read_sys_array(tree_root);
2003 mutex_unlock(&fs_info->chunk_mutex);
2005 printk(KERN_WARNING "btrfs: failed to read the system "
2006 "array on %s\n", sb->s_id);
2007 goto fail_sb_buffer;
2010 blocksize = btrfs_level_size(tree_root,
2011 btrfs_super_chunk_root_level(disk_super));
2012 generation = btrfs_super_chunk_root_generation(disk_super);
2014 __setup_root(nodesize, leafsize, sectorsize, stripesize,
2015 chunk_root, fs_info, BTRFS_CHUNK_TREE_OBJECTID);
2017 chunk_root->node = read_tree_block(chunk_root,
2018 btrfs_super_chunk_root(disk_super),
2019 blocksize, generation);
2020 BUG_ON(!chunk_root->node);
2021 if (!test_bit(EXTENT_BUFFER_UPTODATE, &chunk_root->node->bflags)) {
2022 printk(KERN_WARNING "btrfs: failed to read chunk root on %s\n",
2024 goto fail_chunk_root;
2026 btrfs_set_root_node(&chunk_root->root_item, chunk_root->node);
2027 chunk_root->commit_root = btrfs_root_node(chunk_root);
2029 read_extent_buffer(chunk_root->node, fs_info->chunk_tree_uuid,
2030 (unsigned long)btrfs_header_chunk_tree_uuid(chunk_root->node),
2033 mutex_lock(&fs_info->chunk_mutex);
2034 ret = btrfs_read_chunk_tree(chunk_root);
2035 mutex_unlock(&fs_info->chunk_mutex);
2037 printk(KERN_WARNING "btrfs: failed to read chunk tree on %s\n",
2039 goto fail_chunk_root;
2042 btrfs_close_extra_devices(fs_devices);
2044 blocksize = btrfs_level_size(tree_root,
2045 btrfs_super_root_level(disk_super));
2046 generation = btrfs_super_generation(disk_super);
2048 tree_root->node = read_tree_block(tree_root,
2049 btrfs_super_root(disk_super),
2050 blocksize, generation);
2051 if (!tree_root->node)
2052 goto fail_chunk_root;
2053 if (!test_bit(EXTENT_BUFFER_UPTODATE, &tree_root->node->bflags)) {
2054 printk(KERN_WARNING "btrfs: failed to read tree root on %s\n",
2056 goto fail_tree_root;
2058 btrfs_set_root_node(&tree_root->root_item, tree_root->node);
2059 tree_root->commit_root = btrfs_root_node(tree_root);
2061 ret = find_and_setup_root(tree_root, fs_info,
2062 BTRFS_EXTENT_TREE_OBJECTID, extent_root);
2064 goto fail_tree_root;
2065 extent_root->track_dirty = 1;
2067 ret = find_and_setup_root(tree_root, fs_info,
2068 BTRFS_DEV_TREE_OBJECTID, dev_root);
2070 goto fail_extent_root;
2071 dev_root->track_dirty = 1;
2073 ret = find_and_setup_root(tree_root, fs_info,
2074 BTRFS_CSUM_TREE_OBJECTID, csum_root);
2078 csum_root->track_dirty = 1;
2080 fs_info->generation = generation;
2081 fs_info->last_trans_committed = generation;
2082 fs_info->data_alloc_profile = (u64)-1;
2083 fs_info->metadata_alloc_profile = (u64)-1;
2084 fs_info->system_alloc_profile = fs_info->metadata_alloc_profile;
2086 ret = btrfs_init_space_info(fs_info);
2088 printk(KERN_ERR "Failed to initial space info: %d\n", ret);
2089 goto fail_block_groups;
2092 ret = btrfs_read_block_groups(extent_root);
2094 printk(KERN_ERR "Failed to read block groups: %d\n", ret);
2095 goto fail_block_groups;
2098 fs_info->cleaner_kthread = kthread_run(cleaner_kthread, tree_root,
2100 if (IS_ERR(fs_info->cleaner_kthread))
2101 goto fail_block_groups;
2103 fs_info->transaction_kthread = kthread_run(transaction_kthread,
2105 "btrfs-transaction");
2106 if (IS_ERR(fs_info->transaction_kthread))
2109 if (!btrfs_test_opt(tree_root, SSD) &&
2110 !btrfs_test_opt(tree_root, NOSSD) &&
2111 !fs_info->fs_devices->rotating) {
2112 printk(KERN_INFO "Btrfs detected SSD devices, enabling SSD "
2114 btrfs_set_opt(fs_info->mount_opt, SSD);
2117 /* do not make disk changes in broken FS */
2118 if (btrfs_super_log_root(disk_super) != 0 &&
2119 !(fs_info->fs_state & BTRFS_SUPER_FLAG_ERROR)) {
2120 u64 bytenr = btrfs_super_log_root(disk_super);
2122 if (fs_devices->rw_devices == 0) {
2123 printk(KERN_WARNING "Btrfs log replay required "
2126 goto fail_trans_kthread;
2129 btrfs_level_size(tree_root,
2130 btrfs_super_log_root_level(disk_super));
2132 log_tree_root = kzalloc(sizeof(struct btrfs_root), GFP_NOFS);
2133 if (!log_tree_root) {
2135 goto fail_trans_kthread;
2138 __setup_root(nodesize, leafsize, sectorsize, stripesize,
2139 log_tree_root, fs_info, BTRFS_TREE_LOG_OBJECTID);
2141 log_tree_root->node = read_tree_block(tree_root, bytenr,
2144 ret = btrfs_recover_log_trees(log_tree_root);
2147 if (sb->s_flags & MS_RDONLY) {
2148 ret = btrfs_commit_super(tree_root);
2153 ret = btrfs_find_orphan_roots(tree_root);
2156 if (!(sb->s_flags & MS_RDONLY)) {
2157 ret = btrfs_cleanup_fs_roots(fs_info);
2160 ret = btrfs_recover_relocation(tree_root);
2163 "btrfs: failed to recover relocation\n");
2165 goto fail_trans_kthread;
2169 location.objectid = BTRFS_FS_TREE_OBJECTID;
2170 location.type = BTRFS_ROOT_ITEM_KEY;
2171 location.offset = (u64)-1;
2173 fs_info->fs_root = btrfs_read_fs_root_no_name(fs_info, &location);
2174 if (!fs_info->fs_root)
2175 goto fail_trans_kthread;
2176 if (IS_ERR(fs_info->fs_root)) {
2177 err = PTR_ERR(fs_info->fs_root);
2178 goto fail_trans_kthread;
2181 if (!(sb->s_flags & MS_RDONLY)) {
2182 down_read(&fs_info->cleanup_work_sem);
2183 err = btrfs_orphan_cleanup(fs_info->fs_root);
2185 err = btrfs_orphan_cleanup(fs_info->tree_root);
2186 up_read(&fs_info->cleanup_work_sem);
2188 close_ctree(tree_root);
2189 return ERR_PTR(err);
2196 kthread_stop(fs_info->transaction_kthread);
2198 kthread_stop(fs_info->cleaner_kthread);
2201 * make sure we're done with the btree inode before we stop our
2204 filemap_write_and_wait(fs_info->btree_inode->i_mapping);
2205 invalidate_inode_pages2(fs_info->btree_inode->i_mapping);
2208 btrfs_free_block_groups(fs_info);
2209 free_extent_buffer(csum_root->node);
2210 free_extent_buffer(csum_root->commit_root);
2212 free_extent_buffer(dev_root->node);
2213 free_extent_buffer(dev_root->commit_root);
2215 free_extent_buffer(extent_root->node);
2216 free_extent_buffer(extent_root->commit_root);
2218 free_extent_buffer(tree_root->node);
2219 free_extent_buffer(tree_root->commit_root);
2221 free_extent_buffer(chunk_root->node);
2222 free_extent_buffer(chunk_root->commit_root);
2224 btrfs_stop_workers(&fs_info->generic_worker);
2225 btrfs_stop_workers(&fs_info->fixup_workers);
2226 btrfs_stop_workers(&fs_info->delalloc_workers);
2227 btrfs_stop_workers(&fs_info->workers);
2228 btrfs_stop_workers(&fs_info->endio_workers);
2229 btrfs_stop_workers(&fs_info->endio_meta_workers);
2230 btrfs_stop_workers(&fs_info->endio_meta_write_workers);
2231 btrfs_stop_workers(&fs_info->endio_write_workers);
2232 btrfs_stop_workers(&fs_info->endio_freespace_worker);
2233 btrfs_stop_workers(&fs_info->submit_workers);
2235 invalidate_inode_pages2(fs_info->btree_inode->i_mapping);
2236 iput(fs_info->btree_inode);
2238 btrfs_close_devices(fs_info->fs_devices);
2239 btrfs_mapping_tree_free(&fs_info->mapping_tree);
2241 bdi_destroy(&fs_info->bdi);
2243 cleanup_srcu_struct(&fs_info->subvol_srcu);
2251 return ERR_PTR(err);
2254 static void btrfs_end_buffer_write_sync(struct buffer_head *bh, int uptodate)
2256 char b[BDEVNAME_SIZE];
2259 set_buffer_uptodate(bh);
2261 if (printk_ratelimit()) {
2262 printk(KERN_WARNING "lost page write due to "
2263 "I/O error on %s\n",
2264 bdevname(bh->b_bdev, b));
2266 /* note, we dont' set_buffer_write_io_error because we have
2267 * our own ways of dealing with the IO errors
2269 clear_buffer_uptodate(bh);
2275 struct buffer_head *btrfs_read_dev_super(struct block_device *bdev)
2277 struct buffer_head *bh;
2278 struct buffer_head *latest = NULL;
2279 struct btrfs_super_block *super;
2284 /* we would like to check all the supers, but that would make
2285 * a btrfs mount succeed after a mkfs from a different FS.
2286 * So, we need to add a special mount option to scan for
2287 * later supers, using BTRFS_SUPER_MIRROR_MAX instead
2289 for (i = 0; i < 1; i++) {
2290 bytenr = btrfs_sb_offset(i);
2291 if (bytenr + 4096 >= i_size_read(bdev->bd_inode))
2293 bh = __bread(bdev, bytenr / 4096, 4096);
2297 super = (struct btrfs_super_block *)bh->b_data;
2298 if (btrfs_super_bytenr(super) != bytenr ||
2299 strncmp((char *)(&super->magic), BTRFS_MAGIC,
2300 sizeof(super->magic))) {
2305 if (!latest || btrfs_super_generation(super) > transid) {
2308 transid = btrfs_super_generation(super);
2317 * this should be called twice, once with wait == 0 and
2318 * once with wait == 1. When wait == 0 is done, all the buffer heads
2319 * we write are pinned.
2321 * They are released when wait == 1 is done.
2322 * max_mirrors must be the same for both runs, and it indicates how
2323 * many supers on this one device should be written.
2325 * max_mirrors == 0 means to write them all.
2327 static int write_dev_supers(struct btrfs_device *device,
2328 struct btrfs_super_block *sb,
2329 int do_barriers, int wait, int max_mirrors)
2331 struct buffer_head *bh;
2337 int last_barrier = 0;
2339 if (max_mirrors == 0)
2340 max_mirrors = BTRFS_SUPER_MIRROR_MAX;
2342 /* make sure only the last submit_bh does a barrier */
2344 for (i = 0; i < max_mirrors; i++) {
2345 bytenr = btrfs_sb_offset(i);
2346 if (bytenr + BTRFS_SUPER_INFO_SIZE >=
2347 device->total_bytes)
2353 for (i = 0; i < max_mirrors; i++) {
2354 bytenr = btrfs_sb_offset(i);
2355 if (bytenr + BTRFS_SUPER_INFO_SIZE >= device->total_bytes)
2359 bh = __find_get_block(device->bdev, bytenr / 4096,
2360 BTRFS_SUPER_INFO_SIZE);
2363 if (!buffer_uptodate(bh))
2366 /* drop our reference */
2369 /* drop the reference from the wait == 0 run */
2373 btrfs_set_super_bytenr(sb, bytenr);
2376 crc = btrfs_csum_data(NULL, (char *)sb +
2377 BTRFS_CSUM_SIZE, crc,
2378 BTRFS_SUPER_INFO_SIZE -
2380 btrfs_csum_final(crc, sb->csum);
2383 * one reference for us, and we leave it for the
2386 bh = __getblk(device->bdev, bytenr / 4096,
2387 BTRFS_SUPER_INFO_SIZE);
2388 memcpy(bh->b_data, sb, BTRFS_SUPER_INFO_SIZE);
2390 /* one reference for submit_bh */
2393 set_buffer_uptodate(bh);
2395 bh->b_end_io = btrfs_end_buffer_write_sync;
2398 if (i == last_barrier && do_barriers)
2399 ret = submit_bh(WRITE_FLUSH_FUA, bh);
2401 ret = submit_bh(WRITE_SYNC, bh);
2406 return errors < i ? 0 : -1;
2409 int write_all_supers(struct btrfs_root *root, int max_mirrors)
2411 struct list_head *head;
2412 struct btrfs_device *dev;
2413 struct btrfs_super_block *sb;
2414 struct btrfs_dev_item *dev_item;
2418 int total_errors = 0;
2421 max_errors = btrfs_super_num_devices(&root->fs_info->super_copy) - 1;
2422 do_barriers = !btrfs_test_opt(root, NOBARRIER);
2424 sb = &root->fs_info->super_for_commit;
2425 dev_item = &sb->dev_item;
2427 mutex_lock(&root->fs_info->fs_devices->device_list_mutex);
2428 head = &root->fs_info->fs_devices->devices;
2429 list_for_each_entry(dev, head, dev_list) {
2434 if (!dev->in_fs_metadata || !dev->writeable)
2437 btrfs_set_stack_device_generation(dev_item, 0);
2438 btrfs_set_stack_device_type(dev_item, dev->type);
2439 btrfs_set_stack_device_id(dev_item, dev->devid);
2440 btrfs_set_stack_device_total_bytes(dev_item, dev->total_bytes);
2441 btrfs_set_stack_device_bytes_used(dev_item, dev->bytes_used);
2442 btrfs_set_stack_device_io_align(dev_item, dev->io_align);
2443 btrfs_set_stack_device_io_width(dev_item, dev->io_width);
2444 btrfs_set_stack_device_sector_size(dev_item, dev->sector_size);
2445 memcpy(dev_item->uuid, dev->uuid, BTRFS_UUID_SIZE);
2446 memcpy(dev_item->fsid, dev->fs_devices->fsid, BTRFS_UUID_SIZE);
2448 flags = btrfs_super_flags(sb);
2449 btrfs_set_super_flags(sb, flags | BTRFS_HEADER_FLAG_WRITTEN);
2451 ret = write_dev_supers(dev, sb, do_barriers, 0, max_mirrors);
2455 if (total_errors > max_errors) {
2456 printk(KERN_ERR "btrfs: %d errors while writing supers\n",
2462 list_for_each_entry(dev, head, dev_list) {
2465 if (!dev->in_fs_metadata || !dev->writeable)
2468 ret = write_dev_supers(dev, sb, do_barriers, 1, max_mirrors);
2472 mutex_unlock(&root->fs_info->fs_devices->device_list_mutex);
2473 if (total_errors > max_errors) {
2474 printk(KERN_ERR "btrfs: %d errors while writing supers\n",
2481 int write_ctree_super(struct btrfs_trans_handle *trans,
2482 struct btrfs_root *root, int max_mirrors)
2486 ret = write_all_supers(root, max_mirrors);
2490 int btrfs_free_fs_root(struct btrfs_fs_info *fs_info, struct btrfs_root *root)
2492 spin_lock(&fs_info->fs_roots_radix_lock);
2493 radix_tree_delete(&fs_info->fs_roots_radix,
2494 (unsigned long)root->root_key.objectid);
2495 spin_unlock(&fs_info->fs_roots_radix_lock);
2497 if (btrfs_root_refs(&root->root_item) == 0)
2498 synchronize_srcu(&fs_info->subvol_srcu);
2500 __btrfs_remove_free_space_cache(root->free_ino_pinned);
2501 __btrfs_remove_free_space_cache(root->free_ino_ctl);
2506 static void free_fs_root(struct btrfs_root *root)
2508 WARN_ON(!RB_EMPTY_ROOT(&root->inode_tree));
2509 if (root->anon_super.s_dev) {
2510 down_write(&root->anon_super.s_umount);
2511 kill_anon_super(&root->anon_super);
2513 free_extent_buffer(root->node);
2514 free_extent_buffer(root->commit_root);
2515 kfree(root->free_ino_ctl);
2516 kfree(root->free_ino_pinned);
2521 static int del_fs_roots(struct btrfs_fs_info *fs_info)
2524 struct btrfs_root *gang[8];
2527 while (!list_empty(&fs_info->dead_roots)) {
2528 gang[0] = list_entry(fs_info->dead_roots.next,
2529 struct btrfs_root, root_list);
2530 list_del(&gang[0]->root_list);
2532 if (gang[0]->in_radix) {
2533 btrfs_free_fs_root(fs_info, gang[0]);
2535 free_extent_buffer(gang[0]->node);
2536 free_extent_buffer(gang[0]->commit_root);
2542 ret = radix_tree_gang_lookup(&fs_info->fs_roots_radix,
2547 for (i = 0; i < ret; i++)
2548 btrfs_free_fs_root(fs_info, gang[i]);
2553 int btrfs_cleanup_fs_roots(struct btrfs_fs_info *fs_info)
2555 u64 root_objectid = 0;
2556 struct btrfs_root *gang[8];
2561 ret = radix_tree_gang_lookup(&fs_info->fs_roots_radix,
2562 (void **)gang, root_objectid,
2567 root_objectid = gang[ret - 1]->root_key.objectid + 1;
2568 for (i = 0; i < ret; i++) {
2571 root_objectid = gang[i]->root_key.objectid;
2572 err = btrfs_orphan_cleanup(gang[i]);
2581 int btrfs_commit_super(struct btrfs_root *root)
2583 struct btrfs_trans_handle *trans;
2586 mutex_lock(&root->fs_info->cleaner_mutex);
2587 btrfs_run_delayed_iputs(root);
2588 btrfs_clean_old_snapshots(root);
2589 mutex_unlock(&root->fs_info->cleaner_mutex);
2591 /* wait until ongoing cleanup work done */
2592 down_write(&root->fs_info->cleanup_work_sem);
2593 up_write(&root->fs_info->cleanup_work_sem);
2595 trans = btrfs_join_transaction(root, 1);
2597 return PTR_ERR(trans);
2598 ret = btrfs_commit_transaction(trans, root);
2600 /* run commit again to drop the original snapshot */
2601 trans = btrfs_join_transaction(root, 1);
2603 return PTR_ERR(trans);
2604 btrfs_commit_transaction(trans, root);
2605 ret = btrfs_write_and_wait_transaction(NULL, root);
2608 ret = write_ctree_super(NULL, root, 0);
2612 int close_ctree(struct btrfs_root *root)
2614 struct btrfs_fs_info *fs_info = root->fs_info;
2617 fs_info->closing = 1;
2620 btrfs_put_block_group_cache(fs_info);
2623 * Here come 2 situations when btrfs is broken to flip readonly:
2625 * 1. when btrfs flips readonly somewhere else before
2626 * btrfs_commit_super, sb->s_flags has MS_RDONLY flag,
2627 * and btrfs will skip to write sb directly to keep
2628 * ERROR state on disk.
2630 * 2. when btrfs flips readonly just in btrfs_commit_super,
2631 * and in such case, btrfs cannnot write sb via btrfs_commit_super,
2632 * and since fs_state has been set BTRFS_SUPER_FLAG_ERROR flag,
2633 * btrfs will cleanup all FS resources first and write sb then.
2635 if (!(fs_info->sb->s_flags & MS_RDONLY)) {
2636 ret = btrfs_commit_super(root);
2638 printk(KERN_ERR "btrfs: commit super ret %d\n", ret);
2641 if (fs_info->fs_state & BTRFS_SUPER_FLAG_ERROR) {
2642 ret = btrfs_error_commit_super(root);
2644 printk(KERN_ERR "btrfs: commit super ret %d\n", ret);
2647 kthread_stop(root->fs_info->transaction_kthread);
2648 kthread_stop(root->fs_info->cleaner_kthread);
2650 fs_info->closing = 2;
2653 if (fs_info->delalloc_bytes) {
2654 printk(KERN_INFO "btrfs: at unmount delalloc count %llu\n",
2655 (unsigned long long)fs_info->delalloc_bytes);
2657 if (fs_info->total_ref_cache_size) {
2658 printk(KERN_INFO "btrfs: at umount reference cache size %llu\n",
2659 (unsigned long long)fs_info->total_ref_cache_size);
2662 free_extent_buffer(fs_info->extent_root->node);
2663 free_extent_buffer(fs_info->extent_root->commit_root);
2664 free_extent_buffer(fs_info->tree_root->node);
2665 free_extent_buffer(fs_info->tree_root->commit_root);
2666 free_extent_buffer(root->fs_info->chunk_root->node);
2667 free_extent_buffer(root->fs_info->chunk_root->commit_root);
2668 free_extent_buffer(root->fs_info->dev_root->node);
2669 free_extent_buffer(root->fs_info->dev_root->commit_root);
2670 free_extent_buffer(root->fs_info->csum_root->node);
2671 free_extent_buffer(root->fs_info->csum_root->commit_root);
2673 btrfs_free_block_groups(root->fs_info);
2675 del_fs_roots(fs_info);
2677 iput(fs_info->btree_inode);
2679 btrfs_stop_workers(&fs_info->generic_worker);
2680 btrfs_stop_workers(&fs_info->fixup_workers);
2681 btrfs_stop_workers(&fs_info->delalloc_workers);
2682 btrfs_stop_workers(&fs_info->workers);
2683 btrfs_stop_workers(&fs_info->endio_workers);
2684 btrfs_stop_workers(&fs_info->endio_meta_workers);
2685 btrfs_stop_workers(&fs_info->endio_meta_write_workers);
2686 btrfs_stop_workers(&fs_info->endio_write_workers);
2687 btrfs_stop_workers(&fs_info->endio_freespace_worker);
2688 btrfs_stop_workers(&fs_info->submit_workers);
2690 btrfs_close_devices(fs_info->fs_devices);
2691 btrfs_mapping_tree_free(&fs_info->mapping_tree);
2693 bdi_destroy(&fs_info->bdi);
2694 cleanup_srcu_struct(&fs_info->subvol_srcu);
2696 kfree(fs_info->extent_root);
2697 kfree(fs_info->tree_root);
2698 kfree(fs_info->chunk_root);
2699 kfree(fs_info->dev_root);
2700 kfree(fs_info->csum_root);
2706 int btrfs_buffer_uptodate(struct extent_buffer *buf, u64 parent_transid)
2709 struct inode *btree_inode = buf->first_page->mapping->host;
2711 ret = extent_buffer_uptodate(&BTRFS_I(btree_inode)->io_tree, buf,
2716 ret = verify_parent_transid(&BTRFS_I(btree_inode)->io_tree, buf,
2721 int btrfs_set_buffer_uptodate(struct extent_buffer *buf)
2723 struct inode *btree_inode = buf->first_page->mapping->host;
2724 return set_extent_buffer_uptodate(&BTRFS_I(btree_inode)->io_tree,
2728 void btrfs_mark_buffer_dirty(struct extent_buffer *buf)
2730 struct btrfs_root *root = BTRFS_I(buf->first_page->mapping->host)->root;
2731 u64 transid = btrfs_header_generation(buf);
2732 struct inode *btree_inode = root->fs_info->btree_inode;
2735 btrfs_assert_tree_locked(buf);
2736 if (transid != root->fs_info->generation) {
2737 printk(KERN_CRIT "btrfs transid mismatch buffer %llu, "
2738 "found %llu running %llu\n",
2739 (unsigned long long)buf->start,
2740 (unsigned long long)transid,
2741 (unsigned long long)root->fs_info->generation);
2744 was_dirty = set_extent_buffer_dirty(&BTRFS_I(btree_inode)->io_tree,
2747 spin_lock(&root->fs_info->delalloc_lock);
2748 root->fs_info->dirty_metadata_bytes += buf->len;
2749 spin_unlock(&root->fs_info->delalloc_lock);
2753 void btrfs_btree_balance_dirty(struct btrfs_root *root, unsigned long nr)
2756 * looks as though older kernels can get into trouble with
2757 * this code, they end up stuck in balance_dirty_pages forever
2760 unsigned long thresh = 32 * 1024 * 1024;
2762 if (current->flags & PF_MEMALLOC)
2765 num_dirty = root->fs_info->dirty_metadata_bytes;
2767 if (num_dirty > thresh) {
2768 balance_dirty_pages_ratelimited_nr(
2769 root->fs_info->btree_inode->i_mapping, 1);
2774 int btrfs_read_buffer(struct extent_buffer *buf, u64 parent_transid)
2776 struct btrfs_root *root = BTRFS_I(buf->first_page->mapping->host)->root;
2778 ret = btree_read_extent_buffer_pages(root, buf, 0, parent_transid);
2780 set_bit(EXTENT_BUFFER_UPTODATE, &buf->bflags);
2784 int btree_lock_page_hook(struct page *page)
2786 struct inode *inode = page->mapping->host;
2787 struct btrfs_root *root = BTRFS_I(inode)->root;
2788 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
2789 struct extent_buffer *eb;
2791 u64 bytenr = page_offset(page);
2793 if (page->private == EXTENT_PAGE_PRIVATE)
2796 len = page->private >> 2;
2797 eb = find_extent_buffer(io_tree, bytenr, len, GFP_NOFS);
2801 btrfs_tree_lock(eb);
2802 btrfs_set_header_flag(eb, BTRFS_HEADER_FLAG_WRITTEN);
2804 if (test_and_clear_bit(EXTENT_BUFFER_DIRTY, &eb->bflags)) {
2805 spin_lock(&root->fs_info->delalloc_lock);
2806 if (root->fs_info->dirty_metadata_bytes >= eb->len)
2807 root->fs_info->dirty_metadata_bytes -= eb->len;
2810 spin_unlock(&root->fs_info->delalloc_lock);
2813 btrfs_tree_unlock(eb);
2814 free_extent_buffer(eb);
2820 static void btrfs_check_super_valid(struct btrfs_fs_info *fs_info,
2826 if (fs_info->fs_state & BTRFS_SUPER_FLAG_ERROR)
2827 printk(KERN_WARNING "warning: mount fs with errors, "
2828 "running btrfsck is recommended\n");
2831 int btrfs_error_commit_super(struct btrfs_root *root)
2835 mutex_lock(&root->fs_info->cleaner_mutex);
2836 btrfs_run_delayed_iputs(root);
2837 mutex_unlock(&root->fs_info->cleaner_mutex);
2839 down_write(&root->fs_info->cleanup_work_sem);
2840 up_write(&root->fs_info->cleanup_work_sem);
2842 /* cleanup FS via transaction */
2843 btrfs_cleanup_transaction(root);
2845 ret = write_ctree_super(NULL, root, 0);
2850 static int btrfs_destroy_ordered_operations(struct btrfs_root *root)
2852 struct btrfs_inode *btrfs_inode;
2853 struct list_head splice;
2855 INIT_LIST_HEAD(&splice);
2857 mutex_lock(&root->fs_info->ordered_operations_mutex);
2858 spin_lock(&root->fs_info->ordered_extent_lock);
2860 list_splice_init(&root->fs_info->ordered_operations, &splice);
2861 while (!list_empty(&splice)) {
2862 btrfs_inode = list_entry(splice.next, struct btrfs_inode,
2863 ordered_operations);
2865 list_del_init(&btrfs_inode->ordered_operations);
2867 btrfs_invalidate_inodes(btrfs_inode->root);
2870 spin_unlock(&root->fs_info->ordered_extent_lock);
2871 mutex_unlock(&root->fs_info->ordered_operations_mutex);
2876 static int btrfs_destroy_ordered_extents(struct btrfs_root *root)
2878 struct list_head splice;
2879 struct btrfs_ordered_extent *ordered;
2880 struct inode *inode;
2882 INIT_LIST_HEAD(&splice);
2884 spin_lock(&root->fs_info->ordered_extent_lock);
2886 list_splice_init(&root->fs_info->ordered_extents, &splice);
2887 while (!list_empty(&splice)) {
2888 ordered = list_entry(splice.next, struct btrfs_ordered_extent,
2891 list_del_init(&ordered->root_extent_list);
2892 atomic_inc(&ordered->refs);
2894 /* the inode may be getting freed (in sys_unlink path). */
2895 inode = igrab(ordered->inode);
2897 spin_unlock(&root->fs_info->ordered_extent_lock);
2901 atomic_set(&ordered->refs, 1);
2902 btrfs_put_ordered_extent(ordered);
2904 spin_lock(&root->fs_info->ordered_extent_lock);
2907 spin_unlock(&root->fs_info->ordered_extent_lock);
2912 static int btrfs_destroy_delayed_refs(struct btrfs_transaction *trans,
2913 struct btrfs_root *root)
2915 struct rb_node *node;
2916 struct btrfs_delayed_ref_root *delayed_refs;
2917 struct btrfs_delayed_ref_node *ref;
2920 delayed_refs = &trans->delayed_refs;
2922 spin_lock(&delayed_refs->lock);
2923 if (delayed_refs->num_entries == 0) {
2924 printk(KERN_INFO "delayed_refs has NO entry\n");
2928 node = rb_first(&delayed_refs->root);
2930 ref = rb_entry(node, struct btrfs_delayed_ref_node, rb_node);
2931 node = rb_next(node);
2934 rb_erase(&ref->rb_node, &delayed_refs->root);
2935 delayed_refs->num_entries--;
2937 atomic_set(&ref->refs, 1);
2938 if (btrfs_delayed_ref_is_head(ref)) {
2939 struct btrfs_delayed_ref_head *head;
2941 head = btrfs_delayed_node_to_head(ref);
2942 mutex_lock(&head->mutex);
2943 kfree(head->extent_op);
2944 delayed_refs->num_heads--;
2945 if (list_empty(&head->cluster))
2946 delayed_refs->num_heads_ready--;
2947 list_del_init(&head->cluster);
2948 mutex_unlock(&head->mutex);
2951 spin_unlock(&delayed_refs->lock);
2952 btrfs_put_delayed_ref(ref);
2955 spin_lock(&delayed_refs->lock);
2958 spin_unlock(&delayed_refs->lock);
2963 static int btrfs_destroy_pending_snapshots(struct btrfs_transaction *t)
2965 struct btrfs_pending_snapshot *snapshot;
2966 struct list_head splice;
2968 INIT_LIST_HEAD(&splice);
2970 list_splice_init(&t->pending_snapshots, &splice);
2972 while (!list_empty(&splice)) {
2973 snapshot = list_entry(splice.next,
2974 struct btrfs_pending_snapshot,
2977 list_del_init(&snapshot->list);
2985 static int btrfs_destroy_delalloc_inodes(struct btrfs_root *root)
2987 struct btrfs_inode *btrfs_inode;
2988 struct list_head splice;
2990 INIT_LIST_HEAD(&splice);
2992 list_splice_init(&root->fs_info->delalloc_inodes, &splice);
2994 spin_lock(&root->fs_info->delalloc_lock);
2996 while (!list_empty(&splice)) {
2997 btrfs_inode = list_entry(splice.next, struct btrfs_inode,
3000 list_del_init(&btrfs_inode->delalloc_inodes);
3002 btrfs_invalidate_inodes(btrfs_inode->root);
3005 spin_unlock(&root->fs_info->delalloc_lock);
3010 static int btrfs_destroy_marked_extents(struct btrfs_root *root,
3011 struct extent_io_tree *dirty_pages,
3016 struct inode *btree_inode = root->fs_info->btree_inode;
3017 struct extent_buffer *eb;
3021 unsigned long index;
3024 ret = find_first_extent_bit(dirty_pages, start, &start, &end,
3029 clear_extent_bits(dirty_pages, start, end, mark, GFP_NOFS);
3030 while (start <= end) {
3031 index = start >> PAGE_CACHE_SHIFT;
3032 start = (u64)(index + 1) << PAGE_CACHE_SHIFT;
3033 page = find_get_page(btree_inode->i_mapping, index);
3036 offset = page_offset(page);
3038 spin_lock(&dirty_pages->buffer_lock);
3039 eb = radix_tree_lookup(
3040 &(&BTRFS_I(page->mapping->host)->io_tree)->buffer,
3041 offset >> PAGE_CACHE_SHIFT);
3042 spin_unlock(&dirty_pages->buffer_lock);
3044 ret = test_and_clear_bit(EXTENT_BUFFER_DIRTY,
3046 atomic_set(&eb->refs, 1);
3048 if (PageWriteback(page))
3049 end_page_writeback(page);
3052 if (PageDirty(page)) {
3053 clear_page_dirty_for_io(page);
3054 spin_lock_irq(&page->mapping->tree_lock);
3055 radix_tree_tag_clear(&page->mapping->page_tree,
3057 PAGECACHE_TAG_DIRTY);
3058 spin_unlock_irq(&page->mapping->tree_lock);
3061 page->mapping->a_ops->invalidatepage(page, 0);
3069 static int btrfs_destroy_pinned_extent(struct btrfs_root *root,
3070 struct extent_io_tree *pinned_extents)
3072 struct extent_io_tree *unpin;
3077 unpin = pinned_extents;
3079 ret = find_first_extent_bit(unpin, 0, &start, &end,
3085 if (btrfs_test_opt(root, DISCARD))
3086 ret = btrfs_error_discard_extent(root, start,
3090 clear_extent_dirty(unpin, start, end, GFP_NOFS);
3091 btrfs_error_unpin_extent_range(root, start, end);
3098 static int btrfs_cleanup_transaction(struct btrfs_root *root)
3100 struct btrfs_transaction *t;
3105 mutex_lock(&root->fs_info->trans_mutex);
3106 mutex_lock(&root->fs_info->transaction_kthread_mutex);
3108 list_splice_init(&root->fs_info->trans_list, &list);
3109 while (!list_empty(&list)) {
3110 t = list_entry(list.next, struct btrfs_transaction, list);
3114 btrfs_destroy_ordered_operations(root);
3116 btrfs_destroy_ordered_extents(root);
3118 btrfs_destroy_delayed_refs(t, root);
3120 btrfs_block_rsv_release(root,
3121 &root->fs_info->trans_block_rsv,
3122 t->dirty_pages.dirty_bytes);
3124 /* FIXME: cleanup wait for commit */
3127 if (waitqueue_active(&root->fs_info->transaction_blocked_wait))
3128 wake_up(&root->fs_info->transaction_blocked_wait);
3131 if (waitqueue_active(&root->fs_info->transaction_wait))
3132 wake_up(&root->fs_info->transaction_wait);
3133 mutex_unlock(&root->fs_info->trans_mutex);
3135 mutex_lock(&root->fs_info->trans_mutex);
3137 if (waitqueue_active(&t->commit_wait))
3138 wake_up(&t->commit_wait);
3139 mutex_unlock(&root->fs_info->trans_mutex);
3141 mutex_lock(&root->fs_info->trans_mutex);
3143 btrfs_destroy_pending_snapshots(t);
3145 btrfs_destroy_delalloc_inodes(root);
3147 spin_lock(&root->fs_info->new_trans_lock);
3148 root->fs_info->running_transaction = NULL;
3149 spin_unlock(&root->fs_info->new_trans_lock);
3151 btrfs_destroy_marked_extents(root, &t->dirty_pages,
3154 btrfs_destroy_pinned_extent(root,
3155 root->fs_info->pinned_extents);
3157 atomic_set(&t->use_count, 0);
3158 list_del_init(&t->list);
3159 memset(t, 0, sizeof(*t));
3160 kmem_cache_free(btrfs_transaction_cachep, t);
3163 mutex_unlock(&root->fs_info->transaction_kthread_mutex);
3164 mutex_unlock(&root->fs_info->trans_mutex);
3169 static struct extent_io_ops btree_extent_io_ops = {
3170 .write_cache_pages_lock_hook = btree_lock_page_hook,
3171 .readpage_end_io_hook = btree_readpage_end_io_hook,
3172 .submit_bio_hook = btree_submit_bio_hook,
3173 /* note we're sharing with inode.c for the merge bio hook */
3174 .merge_bio_hook = btrfs_merge_bio_hook,