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/sched.h>
21 #include <linux/writeback.h>
22 #include <linux/pagemap.h>
25 #include "transaction.h"
27 #include "ref-cache.h"
30 static int total_trans = 0;
31 extern struct kmem_cache *btrfs_trans_handle_cachep;
32 extern struct kmem_cache *btrfs_transaction_cachep;
34 #define BTRFS_ROOT_TRANS_TAG 0
36 static noinline void put_transaction(struct btrfs_transaction *transaction)
38 WARN_ON(transaction->use_count == 0);
39 transaction->use_count--;
40 if (transaction->use_count == 0) {
41 WARN_ON(total_trans == 0);
43 list_del_init(&transaction->list);
44 memset(transaction, 0, sizeof(*transaction));
45 kmem_cache_free(btrfs_transaction_cachep, transaction);
49 static noinline int join_transaction(struct btrfs_root *root)
51 struct btrfs_transaction *cur_trans;
52 cur_trans = root->fs_info->running_transaction;
54 cur_trans = kmem_cache_alloc(btrfs_transaction_cachep,
58 root->fs_info->generation++;
59 root->fs_info->last_alloc = 0;
60 root->fs_info->last_data_alloc = 0;
61 root->fs_info->last_log_alloc = 0;
62 cur_trans->num_writers = 1;
63 cur_trans->num_joined = 0;
64 cur_trans->transid = root->fs_info->generation;
65 init_waitqueue_head(&cur_trans->writer_wait);
66 init_waitqueue_head(&cur_trans->commit_wait);
67 cur_trans->in_commit = 0;
68 cur_trans->blocked = 0;
69 cur_trans->use_count = 1;
70 cur_trans->commit_done = 0;
71 cur_trans->start_time = get_seconds();
72 INIT_LIST_HEAD(&cur_trans->pending_snapshots);
73 list_add_tail(&cur_trans->list, &root->fs_info->trans_list);
74 extent_io_tree_init(&cur_trans->dirty_pages,
75 root->fs_info->btree_inode->i_mapping,
77 spin_lock(&root->fs_info->new_trans_lock);
78 root->fs_info->running_transaction = cur_trans;
79 spin_unlock(&root->fs_info->new_trans_lock);
81 cur_trans->num_writers++;
82 cur_trans->num_joined++;
88 noinline int btrfs_record_root_in_trans(struct btrfs_root *root)
90 struct btrfs_dirty_root *dirty;
91 u64 running_trans_id = root->fs_info->running_transaction->transid;
92 if (root->ref_cows && root->last_trans < running_trans_id) {
93 WARN_ON(root == root->fs_info->extent_root);
94 if (root->root_item.refs != 0) {
95 radix_tree_tag_set(&root->fs_info->fs_roots_radix,
96 (unsigned long)root->root_key.objectid,
97 BTRFS_ROOT_TRANS_TAG);
99 dirty = kmalloc(sizeof(*dirty), GFP_NOFS);
101 dirty->root = kmalloc(sizeof(*dirty->root), GFP_NOFS);
102 BUG_ON(!dirty->root);
103 dirty->latest_root = root;
104 INIT_LIST_HEAD(&dirty->list);
106 root->commit_root = btrfs_root_node(root);
108 memcpy(dirty->root, root, sizeof(*root));
109 spin_lock_init(&dirty->root->node_lock);
110 spin_lock_init(&dirty->root->list_lock);
111 mutex_init(&dirty->root->objectid_mutex);
112 INIT_LIST_HEAD(&dirty->root->dead_list);
113 dirty->root->node = root->commit_root;
114 dirty->root->commit_root = NULL;
116 spin_lock(&root->list_lock);
117 list_add(&dirty->root->dead_list, &root->dead_list);
118 spin_unlock(&root->list_lock);
120 root->dirty_root = dirty;
124 root->last_trans = running_trans_id;
129 static void wait_current_trans(struct btrfs_root *root)
131 struct btrfs_transaction *cur_trans;
133 cur_trans = root->fs_info->running_transaction;
134 if (cur_trans && cur_trans->blocked) {
136 cur_trans->use_count++;
138 prepare_to_wait(&root->fs_info->transaction_wait, &wait,
139 TASK_UNINTERRUPTIBLE);
140 if (cur_trans->blocked) {
141 mutex_unlock(&root->fs_info->trans_mutex);
143 mutex_lock(&root->fs_info->trans_mutex);
144 finish_wait(&root->fs_info->transaction_wait,
147 finish_wait(&root->fs_info->transaction_wait,
152 put_transaction(cur_trans);
156 static struct btrfs_trans_handle *start_transaction(struct btrfs_root *root,
157 int num_blocks, int wait)
159 struct btrfs_trans_handle *h =
160 kmem_cache_alloc(btrfs_trans_handle_cachep, GFP_NOFS);
163 mutex_lock(&root->fs_info->trans_mutex);
164 if (!root->fs_info->log_root_recovering &&
165 ((wait == 1 && !root->fs_info->open_ioctl_trans) || wait == 2))
166 wait_current_trans(root);
167 ret = join_transaction(root);
170 btrfs_record_root_in_trans(root);
171 h->transid = root->fs_info->running_transaction->transid;
172 h->transaction = root->fs_info->running_transaction;
173 h->blocks_reserved = num_blocks;
175 h->block_group = NULL;
176 h->alloc_exclude_nr = 0;
177 h->alloc_exclude_start = 0;
178 root->fs_info->running_transaction->use_count++;
179 mutex_unlock(&root->fs_info->trans_mutex);
183 struct btrfs_trans_handle *btrfs_start_transaction(struct btrfs_root *root,
186 return start_transaction(root, num_blocks, 1);
188 struct btrfs_trans_handle *btrfs_join_transaction(struct btrfs_root *root,
191 return start_transaction(root, num_blocks, 0);
194 struct btrfs_trans_handle *btrfs_start_ioctl_transaction(struct btrfs_root *r,
197 return start_transaction(r, num_blocks, 2);
201 static noinline int wait_for_commit(struct btrfs_root *root,
202 struct btrfs_transaction *commit)
205 mutex_lock(&root->fs_info->trans_mutex);
206 while(!commit->commit_done) {
207 prepare_to_wait(&commit->commit_wait, &wait,
208 TASK_UNINTERRUPTIBLE);
209 if (commit->commit_done)
211 mutex_unlock(&root->fs_info->trans_mutex);
213 mutex_lock(&root->fs_info->trans_mutex);
215 mutex_unlock(&root->fs_info->trans_mutex);
216 finish_wait(&commit->commit_wait, &wait);
220 static void throttle_on_drops(struct btrfs_root *root)
222 struct btrfs_fs_info *info = root->fs_info;
223 int harder_count = 0;
226 if (atomic_read(&info->throttles)) {
229 thr = atomic_read(&info->throttle_gen);
232 prepare_to_wait(&info->transaction_throttle,
233 &wait, TASK_UNINTERRUPTIBLE);
234 if (!atomic_read(&info->throttles)) {
235 finish_wait(&info->transaction_throttle, &wait);
239 finish_wait(&info->transaction_throttle, &wait);
240 } while (thr == atomic_read(&info->throttle_gen));
243 if (root->fs_info->total_ref_cache_size > 1 * 1024 * 1024 &&
247 if (root->fs_info->total_ref_cache_size > 5 * 1024 * 1024 &&
251 if (root->fs_info->total_ref_cache_size > 10 * 1024 * 1024 &&
257 void btrfs_throttle(struct btrfs_root *root)
259 mutex_lock(&root->fs_info->trans_mutex);
260 if (!root->fs_info->open_ioctl_trans)
261 wait_current_trans(root);
262 mutex_unlock(&root->fs_info->trans_mutex);
264 throttle_on_drops(root);
267 static int __btrfs_end_transaction(struct btrfs_trans_handle *trans,
268 struct btrfs_root *root, int throttle)
270 struct btrfs_transaction *cur_trans;
271 struct btrfs_fs_info *info = root->fs_info;
273 mutex_lock(&info->trans_mutex);
274 cur_trans = info->running_transaction;
275 WARN_ON(cur_trans != trans->transaction);
276 WARN_ON(cur_trans->num_writers < 1);
277 cur_trans->num_writers--;
279 if (waitqueue_active(&cur_trans->writer_wait))
280 wake_up(&cur_trans->writer_wait);
281 put_transaction(cur_trans);
282 mutex_unlock(&info->trans_mutex);
283 memset(trans, 0, sizeof(*trans));
284 kmem_cache_free(btrfs_trans_handle_cachep, trans);
287 throttle_on_drops(root);
292 int btrfs_end_transaction(struct btrfs_trans_handle *trans,
293 struct btrfs_root *root)
295 return __btrfs_end_transaction(trans, root, 0);
298 int btrfs_end_transaction_throttle(struct btrfs_trans_handle *trans,
299 struct btrfs_root *root)
301 return __btrfs_end_transaction(trans, root, 1);
305 int btrfs_write_and_wait_transaction(struct btrfs_trans_handle *trans,
306 struct btrfs_root *root)
311 struct extent_io_tree *dirty_pages;
313 struct inode *btree_inode = root->fs_info->btree_inode;
318 if (!trans || !trans->transaction) {
319 return filemap_write_and_wait(btree_inode->i_mapping);
321 dirty_pages = &trans->transaction->dirty_pages;
323 ret = find_first_extent_bit(dirty_pages, start, &start, &end,
327 while(start <= end) {
330 index = start >> PAGE_CACHE_SHIFT;
331 start = (u64)(index + 1) << PAGE_CACHE_SHIFT;
332 page = find_get_page(btree_inode->i_mapping, index);
336 btree_lock_page_hook(page);
337 if (!page->mapping) {
339 page_cache_release(page);
343 if (PageWriteback(page)) {
345 wait_on_page_writeback(page);
348 page_cache_release(page);
352 err = write_one_page(page, 0);
355 page_cache_release(page);
359 ret = find_first_extent_bit(dirty_pages, 0, &start, &end,
364 clear_extent_dirty(dirty_pages, start, end, GFP_NOFS);
365 while(start <= end) {
366 index = start >> PAGE_CACHE_SHIFT;
367 start = (u64)(index + 1) << PAGE_CACHE_SHIFT;
368 page = find_get_page(btree_inode->i_mapping, index);
371 if (PageDirty(page)) {
372 btree_lock_page_hook(page);
373 wait_on_page_writeback(page);
374 err = write_one_page(page, 0);
378 wait_on_page_writeback(page);
379 page_cache_release(page);
388 static int update_cowonly_root(struct btrfs_trans_handle *trans,
389 struct btrfs_root *root)
393 struct btrfs_root *tree_root = root->fs_info->tree_root;
395 btrfs_write_dirty_block_groups(trans, root);
397 old_root_bytenr = btrfs_root_bytenr(&root->root_item);
398 if (old_root_bytenr == root->node->start)
400 btrfs_set_root_bytenr(&root->root_item,
402 btrfs_set_root_level(&root->root_item,
403 btrfs_header_level(root->node));
404 ret = btrfs_update_root(trans, tree_root,
408 btrfs_write_dirty_block_groups(trans, root);
413 int btrfs_commit_tree_roots(struct btrfs_trans_handle *trans,
414 struct btrfs_root *root)
416 struct btrfs_fs_info *fs_info = root->fs_info;
417 struct list_head *next;
419 while(!list_empty(&fs_info->dirty_cowonly_roots)) {
420 next = fs_info->dirty_cowonly_roots.next;
422 root = list_entry(next, struct btrfs_root, dirty_list);
423 update_cowonly_root(trans, root);
428 int btrfs_add_dead_root(struct btrfs_root *root, struct btrfs_root *latest)
430 struct btrfs_dirty_root *dirty;
432 dirty = kmalloc(sizeof(*dirty), GFP_NOFS);
436 dirty->latest_root = latest;
438 mutex_lock(&root->fs_info->trans_mutex);
439 list_add(&dirty->list, &latest->fs_info->dead_roots);
440 mutex_unlock(&root->fs_info->trans_mutex);
444 static noinline int add_dirty_roots(struct btrfs_trans_handle *trans,
445 struct radix_tree_root *radix,
446 struct list_head *list)
448 struct btrfs_dirty_root *dirty;
449 struct btrfs_root *gang[8];
450 struct btrfs_root *root;
457 ret = radix_tree_gang_lookup_tag(radix, (void **)gang, 0,
459 BTRFS_ROOT_TRANS_TAG);
462 for (i = 0; i < ret; i++) {
464 radix_tree_tag_clear(radix,
465 (unsigned long)root->root_key.objectid,
466 BTRFS_ROOT_TRANS_TAG);
468 BUG_ON(!root->ref_tree);
469 dirty = root->dirty_root;
471 btrfs_free_log(trans, root);
473 if (root->commit_root == root->node) {
474 WARN_ON(root->node->start !=
475 btrfs_root_bytenr(&root->root_item));
477 free_extent_buffer(root->commit_root);
478 root->commit_root = NULL;
479 root->dirty_root = NULL;
481 spin_lock(&root->list_lock);
482 list_del_init(&dirty->root->dead_list);
483 spin_unlock(&root->list_lock);
488 /* make sure to update the root on disk
489 * so we get any updates to the block used
492 err = btrfs_update_root(trans,
493 root->fs_info->tree_root,
499 memset(&root->root_item.drop_progress, 0,
500 sizeof(struct btrfs_disk_key));
501 root->root_item.drop_level = 0;
502 root->commit_root = NULL;
503 root->dirty_root = NULL;
504 root->root_key.offset = root->fs_info->generation;
505 btrfs_set_root_bytenr(&root->root_item,
507 btrfs_set_root_level(&root->root_item,
508 btrfs_header_level(root->node));
509 err = btrfs_insert_root(trans, root->fs_info->tree_root,
515 refs = btrfs_root_refs(&dirty->root->root_item);
516 btrfs_set_root_refs(&dirty->root->root_item, refs - 1);
517 err = btrfs_update_root(trans, root->fs_info->tree_root,
518 &dirty->root->root_key,
519 &dirty->root->root_item);
523 list_add(&dirty->list, list);
526 free_extent_buffer(dirty->root->node);
535 int btrfs_defrag_root(struct btrfs_root *root, int cacheonly)
537 struct btrfs_fs_info *info = root->fs_info;
539 struct btrfs_trans_handle *trans;
543 if (root->defrag_running)
545 trans = btrfs_start_transaction(root, 1);
547 root->defrag_running = 1;
548 ret = btrfs_defrag_leaves(trans, root, cacheonly);
549 nr = trans->blocks_used;
550 btrfs_end_transaction(trans, root);
551 btrfs_btree_balance_dirty(info->tree_root, nr);
554 trans = btrfs_start_transaction(root, 1);
555 if (root->fs_info->closing || ret != -EAGAIN)
558 root->defrag_running = 0;
560 btrfs_end_transaction(trans, root);
564 static noinline int drop_dirty_roots(struct btrfs_root *tree_root,
565 struct list_head *list)
567 struct btrfs_dirty_root *dirty;
568 struct btrfs_trans_handle *trans;
576 while(!list_empty(list)) {
577 struct btrfs_root *root;
579 dirty = list_entry(list->prev, struct btrfs_dirty_root, list);
580 list_del_init(&dirty->list);
582 num_bytes = btrfs_root_used(&dirty->root->root_item);
583 root = dirty->latest_root;
584 atomic_inc(&root->fs_info->throttles);
586 mutex_lock(&root->fs_info->drop_mutex);
588 trans = btrfs_start_transaction(tree_root, 1);
589 ret = btrfs_drop_snapshot(trans, dirty->root);
590 if (ret != -EAGAIN) {
594 err = btrfs_update_root(trans,
596 &dirty->root->root_key,
597 &dirty->root->root_item);
600 nr = trans->blocks_used;
601 ret = btrfs_end_transaction(trans, tree_root);
604 mutex_unlock(&root->fs_info->drop_mutex);
605 btrfs_btree_balance_dirty(tree_root, nr);
607 mutex_lock(&root->fs_info->drop_mutex);
610 atomic_dec(&root->fs_info->throttles);
611 wake_up(&root->fs_info->transaction_throttle);
613 mutex_lock(&root->fs_info->alloc_mutex);
614 num_bytes -= btrfs_root_used(&dirty->root->root_item);
615 bytes_used = btrfs_root_used(&root->root_item);
617 btrfs_record_root_in_trans(root);
618 btrfs_set_root_used(&root->root_item,
619 bytes_used - num_bytes);
621 mutex_unlock(&root->fs_info->alloc_mutex);
623 ret = btrfs_del_root(trans, tree_root, &dirty->root->root_key);
628 mutex_unlock(&root->fs_info->drop_mutex);
630 spin_lock(&root->list_lock);
631 list_del_init(&dirty->root->dead_list);
632 if (!list_empty(&root->dead_list)) {
633 struct btrfs_root *oldest;
634 oldest = list_entry(root->dead_list.prev,
635 struct btrfs_root, dead_list);
636 max_useless = oldest->root_key.offset - 1;
638 max_useless = root->root_key.offset - 1;
640 spin_unlock(&root->list_lock);
642 nr = trans->blocks_used;
643 ret = btrfs_end_transaction(trans, tree_root);
646 ret = btrfs_remove_leaf_refs(root, max_useless);
649 free_extent_buffer(dirty->root->node);
653 btrfs_btree_balance_dirty(tree_root, nr);
659 static noinline int create_pending_snapshot(struct btrfs_trans_handle *trans,
660 struct btrfs_fs_info *fs_info,
661 struct btrfs_pending_snapshot *pending)
663 struct btrfs_key key;
664 struct btrfs_root_item *new_root_item;
665 struct btrfs_root *tree_root = fs_info->tree_root;
666 struct btrfs_root *root = pending->root;
667 struct extent_buffer *tmp;
668 struct extent_buffer *old;
673 new_root_item = kmalloc(sizeof(*new_root_item), GFP_NOFS);
674 if (!new_root_item) {
678 ret = btrfs_find_free_objectid(trans, tree_root, 0, &objectid);
682 memcpy(new_root_item, &root->root_item, sizeof(*new_root_item));
684 key.objectid = objectid;
686 btrfs_set_key_type(&key, BTRFS_ROOT_ITEM_KEY);
688 old = btrfs_lock_root_node(root);
689 btrfs_cow_block(trans, root, old, NULL, 0, &old, 0);
691 btrfs_copy_root(trans, root, old, &tmp, objectid);
692 btrfs_tree_unlock(old);
693 free_extent_buffer(old);
695 btrfs_set_root_bytenr(new_root_item, tmp->start);
696 btrfs_set_root_level(new_root_item, btrfs_header_level(tmp));
697 ret = btrfs_insert_root(trans, root->fs_info->tree_root, &key,
699 btrfs_tree_unlock(tmp);
700 free_extent_buffer(tmp);
705 * insert the directory item
707 key.offset = (u64)-1;
708 namelen = strlen(pending->name);
709 ret = btrfs_insert_dir_item(trans, root->fs_info->tree_root,
710 pending->name, namelen,
711 root->fs_info->sb->s_root->d_inode->i_ino,
712 &key, BTRFS_FT_DIR, 0);
717 ret = btrfs_insert_inode_ref(trans, root->fs_info->tree_root,
718 pending->name, strlen(pending->name), objectid,
719 root->fs_info->sb->s_root->d_inode->i_ino, 0);
721 /* Invalidate existing dcache entry for new snapshot. */
722 btrfs_invalidate_dcache_root(root, pending->name, namelen);
725 kfree(new_root_item);
729 static noinline int create_pending_snapshots(struct btrfs_trans_handle *trans,
730 struct btrfs_fs_info *fs_info)
732 struct btrfs_pending_snapshot *pending;
733 struct list_head *head = &trans->transaction->pending_snapshots;
736 while(!list_empty(head)) {
737 pending = list_entry(head->next,
738 struct btrfs_pending_snapshot, list);
739 ret = create_pending_snapshot(trans, fs_info, pending);
741 list_del(&pending->list);
742 kfree(pending->name);
748 int btrfs_commit_transaction(struct btrfs_trans_handle *trans,
749 struct btrfs_root *root)
751 unsigned long joined = 0;
752 unsigned long timeout = 1;
753 struct btrfs_transaction *cur_trans;
754 struct btrfs_transaction *prev_trans = NULL;
755 struct btrfs_root *chunk_root = root->fs_info->chunk_root;
756 struct list_head dirty_fs_roots;
757 struct extent_io_tree *pinned_copy;
761 INIT_LIST_HEAD(&dirty_fs_roots);
762 mutex_lock(&root->fs_info->trans_mutex);
763 if (trans->transaction->in_commit) {
764 cur_trans = trans->transaction;
765 trans->transaction->use_count++;
766 mutex_unlock(&root->fs_info->trans_mutex);
767 btrfs_end_transaction(trans, root);
769 ret = wait_for_commit(root, cur_trans);
772 mutex_lock(&root->fs_info->trans_mutex);
773 put_transaction(cur_trans);
774 mutex_unlock(&root->fs_info->trans_mutex);
779 pinned_copy = kmalloc(sizeof(*pinned_copy), GFP_NOFS);
783 extent_io_tree_init(pinned_copy,
784 root->fs_info->btree_inode->i_mapping, GFP_NOFS);
786 trans->transaction->in_commit = 1;
787 trans->transaction->blocked = 1;
788 cur_trans = trans->transaction;
789 if (cur_trans->list.prev != &root->fs_info->trans_list) {
790 prev_trans = list_entry(cur_trans->list.prev,
791 struct btrfs_transaction, list);
792 if (!prev_trans->commit_done) {
793 prev_trans->use_count++;
794 mutex_unlock(&root->fs_info->trans_mutex);
796 wait_for_commit(root, prev_trans);
798 mutex_lock(&root->fs_info->trans_mutex);
799 put_transaction(prev_trans);
804 int snap_pending = 0;
805 joined = cur_trans->num_joined;
806 if (!list_empty(&trans->transaction->pending_snapshots))
809 WARN_ON(cur_trans != trans->transaction);
810 prepare_to_wait(&cur_trans->writer_wait, &wait,
811 TASK_UNINTERRUPTIBLE);
813 if (cur_trans->num_writers > 1)
814 timeout = MAX_SCHEDULE_TIMEOUT;
818 mutex_unlock(&root->fs_info->trans_mutex);
821 ret = btrfs_wait_ordered_extents(root, 1);
825 schedule_timeout(timeout);
827 mutex_lock(&root->fs_info->trans_mutex);
828 finish_wait(&cur_trans->writer_wait, &wait);
829 } while (cur_trans->num_writers > 1 ||
830 (cur_trans->num_joined != joined));
832 ret = create_pending_snapshots(trans, root->fs_info);
835 WARN_ON(cur_trans != trans->transaction);
837 /* btrfs_commit_tree_roots is responsible for getting the
838 * various roots consistent with each other. Every pointer
839 * in the tree of tree roots has to point to the most up to date
840 * root for every subvolume and other tree. So, we have to keep
841 * the tree logging code from jumping in and changing any
844 * At this point in the commit, there can't be any tree-log
845 * writers, but a little lower down we drop the trans mutex
846 * and let new people in. By holding the tree_log_mutex
847 * from now until after the super is written, we avoid races
848 * with the tree-log code.
850 mutex_lock(&root->fs_info->tree_log_mutex);
852 ret = add_dirty_roots(trans, &root->fs_info->fs_roots_radix,
856 /* add_dirty_roots gets rid of all the tree log roots, it is now
857 * safe to free the root of tree log roots
859 btrfs_free_log_root_tree(trans, root->fs_info);
861 ret = btrfs_commit_tree_roots(trans, root);
864 cur_trans = root->fs_info->running_transaction;
865 spin_lock(&root->fs_info->new_trans_lock);
866 root->fs_info->running_transaction = NULL;
867 spin_unlock(&root->fs_info->new_trans_lock);
868 btrfs_set_super_generation(&root->fs_info->super_copy,
870 btrfs_set_super_root(&root->fs_info->super_copy,
871 root->fs_info->tree_root->node->start);
872 btrfs_set_super_root_level(&root->fs_info->super_copy,
873 btrfs_header_level(root->fs_info->tree_root->node));
875 btrfs_set_super_chunk_root(&root->fs_info->super_copy,
876 chunk_root->node->start);
877 btrfs_set_super_chunk_root_level(&root->fs_info->super_copy,
878 btrfs_header_level(chunk_root->node));
880 if (!root->fs_info->log_root_recovering) {
881 btrfs_set_super_log_root(&root->fs_info->super_copy, 0);
882 btrfs_set_super_log_root_level(&root->fs_info->super_copy, 0);
885 memcpy(&root->fs_info->super_for_commit, &root->fs_info->super_copy,
886 sizeof(root->fs_info->super_copy));
888 btrfs_copy_pinned(root, pinned_copy);
890 trans->transaction->blocked = 0;
891 wake_up(&root->fs_info->transaction_throttle);
892 wake_up(&root->fs_info->transaction_wait);
894 mutex_unlock(&root->fs_info->trans_mutex);
895 ret = btrfs_write_and_wait_transaction(trans, root);
897 write_ctree_super(trans, root);
900 * the super is written, we can safely allow the tree-loggers
901 * to go about their business
903 mutex_unlock(&root->fs_info->tree_log_mutex);
905 btrfs_finish_extent_commit(trans, root, pinned_copy);
906 mutex_lock(&root->fs_info->trans_mutex);
910 cur_trans->commit_done = 1;
911 root->fs_info->last_trans_committed = cur_trans->transid;
912 wake_up(&cur_trans->commit_wait);
913 put_transaction(cur_trans);
914 put_transaction(cur_trans);
916 list_splice_init(&dirty_fs_roots, &root->fs_info->dead_roots);
917 if (root->fs_info->closing)
918 list_splice_init(&root->fs_info->dead_roots, &dirty_fs_roots);
920 mutex_unlock(&root->fs_info->trans_mutex);
921 kmem_cache_free(btrfs_trans_handle_cachep, trans);
923 if (root->fs_info->closing) {
924 drop_dirty_roots(root->fs_info->tree_root, &dirty_fs_roots);
929 int btrfs_clean_old_snapshots(struct btrfs_root *root)
931 struct list_head dirty_roots;
932 INIT_LIST_HEAD(&dirty_roots);
934 mutex_lock(&root->fs_info->trans_mutex);
935 list_splice_init(&root->fs_info->dead_roots, &dirty_roots);
936 mutex_unlock(&root->fs_info->trans_mutex);
938 if (!list_empty(&dirty_roots)) {
939 drop_dirty_roots(root, &dirty_roots);