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_marked_extents(struct btrfs_root *root,
306 struct extent_io_tree *dirty_pages)
312 struct inode *btree_inode = root->fs_info->btree_inode;
318 ret = find_first_extent_bit(dirty_pages, start, &start, &end,
322 while(start <= end) {
325 index = start >> PAGE_CACHE_SHIFT;
326 start = (u64)(index + 1) << PAGE_CACHE_SHIFT;
327 page = find_get_page(btree_inode->i_mapping, index);
331 btree_lock_page_hook(page);
332 if (!page->mapping) {
334 page_cache_release(page);
338 if (PageWriteback(page)) {
340 wait_on_page_writeback(page);
343 page_cache_release(page);
347 err = write_one_page(page, 0);
350 page_cache_release(page);
354 ret = find_first_extent_bit(dirty_pages, 0, &start, &end,
359 clear_extent_dirty(dirty_pages, start, end, GFP_NOFS);
360 while(start <= end) {
361 index = start >> PAGE_CACHE_SHIFT;
362 start = (u64)(index + 1) << PAGE_CACHE_SHIFT;
363 page = find_get_page(btree_inode->i_mapping, index);
366 if (PageDirty(page)) {
367 btree_lock_page_hook(page);
368 wait_on_page_writeback(page);
369 err = write_one_page(page, 0);
373 wait_on_page_writeback(page);
374 page_cache_release(page);
383 int btrfs_write_and_wait_transaction(struct btrfs_trans_handle *trans,
384 struct btrfs_root *root)
386 if (!trans || !trans->transaction) {
387 struct inode *btree_inode;
388 btree_inode = root->fs_info->btree_inode;
389 return filemap_write_and_wait(btree_inode->i_mapping);
391 return btrfs_write_and_wait_marked_extents(root,
392 &trans->transaction->dirty_pages);
395 static int update_cowonly_root(struct btrfs_trans_handle *trans,
396 struct btrfs_root *root)
400 struct btrfs_root *tree_root = root->fs_info->tree_root;
402 btrfs_write_dirty_block_groups(trans, root);
404 old_root_bytenr = btrfs_root_bytenr(&root->root_item);
405 if (old_root_bytenr == root->node->start)
407 btrfs_set_root_bytenr(&root->root_item,
409 btrfs_set_root_level(&root->root_item,
410 btrfs_header_level(root->node));
411 ret = btrfs_update_root(trans, tree_root,
415 btrfs_write_dirty_block_groups(trans, root);
420 int btrfs_commit_tree_roots(struct btrfs_trans_handle *trans,
421 struct btrfs_root *root)
423 struct btrfs_fs_info *fs_info = root->fs_info;
424 struct list_head *next;
426 while(!list_empty(&fs_info->dirty_cowonly_roots)) {
427 next = fs_info->dirty_cowonly_roots.next;
429 root = list_entry(next, struct btrfs_root, dirty_list);
430 update_cowonly_root(trans, root);
435 int btrfs_add_dead_root(struct btrfs_root *root, struct btrfs_root *latest)
437 struct btrfs_dirty_root *dirty;
439 dirty = kmalloc(sizeof(*dirty), GFP_NOFS);
443 dirty->latest_root = latest;
445 mutex_lock(&root->fs_info->trans_mutex);
446 list_add(&dirty->list, &latest->fs_info->dead_roots);
447 mutex_unlock(&root->fs_info->trans_mutex);
451 static noinline int add_dirty_roots(struct btrfs_trans_handle *trans,
452 struct radix_tree_root *radix,
453 struct list_head *list)
455 struct btrfs_dirty_root *dirty;
456 struct btrfs_root *gang[8];
457 struct btrfs_root *root;
464 ret = radix_tree_gang_lookup_tag(radix, (void **)gang, 0,
466 BTRFS_ROOT_TRANS_TAG);
469 for (i = 0; i < ret; i++) {
471 radix_tree_tag_clear(radix,
472 (unsigned long)root->root_key.objectid,
473 BTRFS_ROOT_TRANS_TAG);
475 BUG_ON(!root->ref_tree);
476 dirty = root->dirty_root;
478 btrfs_free_log(trans, root);
480 if (root->commit_root == root->node) {
481 WARN_ON(root->node->start !=
482 btrfs_root_bytenr(&root->root_item));
484 free_extent_buffer(root->commit_root);
485 root->commit_root = NULL;
486 root->dirty_root = NULL;
488 spin_lock(&root->list_lock);
489 list_del_init(&dirty->root->dead_list);
490 spin_unlock(&root->list_lock);
495 /* make sure to update the root on disk
496 * so we get any updates to the block used
499 err = btrfs_update_root(trans,
500 root->fs_info->tree_root,
506 memset(&root->root_item.drop_progress, 0,
507 sizeof(struct btrfs_disk_key));
508 root->root_item.drop_level = 0;
509 root->commit_root = NULL;
510 root->dirty_root = NULL;
511 root->root_key.offset = root->fs_info->generation;
512 btrfs_set_root_bytenr(&root->root_item,
514 btrfs_set_root_level(&root->root_item,
515 btrfs_header_level(root->node));
516 err = btrfs_insert_root(trans, root->fs_info->tree_root,
522 refs = btrfs_root_refs(&dirty->root->root_item);
523 btrfs_set_root_refs(&dirty->root->root_item, refs - 1);
524 err = btrfs_update_root(trans, root->fs_info->tree_root,
525 &dirty->root->root_key,
526 &dirty->root->root_item);
530 list_add(&dirty->list, list);
533 free_extent_buffer(dirty->root->node);
542 int btrfs_defrag_root(struct btrfs_root *root, int cacheonly)
544 struct btrfs_fs_info *info = root->fs_info;
546 struct btrfs_trans_handle *trans;
550 if (root->defrag_running)
552 trans = btrfs_start_transaction(root, 1);
554 root->defrag_running = 1;
555 ret = btrfs_defrag_leaves(trans, root, cacheonly);
556 nr = trans->blocks_used;
557 btrfs_end_transaction(trans, root);
558 btrfs_btree_balance_dirty(info->tree_root, nr);
561 trans = btrfs_start_transaction(root, 1);
562 if (root->fs_info->closing || ret != -EAGAIN)
565 root->defrag_running = 0;
567 btrfs_end_transaction(trans, root);
571 static noinline int drop_dirty_roots(struct btrfs_root *tree_root,
572 struct list_head *list)
574 struct btrfs_dirty_root *dirty;
575 struct btrfs_trans_handle *trans;
583 while(!list_empty(list)) {
584 struct btrfs_root *root;
586 dirty = list_entry(list->prev, struct btrfs_dirty_root, list);
587 list_del_init(&dirty->list);
589 num_bytes = btrfs_root_used(&dirty->root->root_item);
590 root = dirty->latest_root;
591 atomic_inc(&root->fs_info->throttles);
593 mutex_lock(&root->fs_info->drop_mutex);
595 trans = btrfs_start_transaction(tree_root, 1);
596 ret = btrfs_drop_snapshot(trans, dirty->root);
597 if (ret != -EAGAIN) {
601 err = btrfs_update_root(trans,
603 &dirty->root->root_key,
604 &dirty->root->root_item);
607 nr = trans->blocks_used;
608 ret = btrfs_end_transaction(trans, tree_root);
611 mutex_unlock(&root->fs_info->drop_mutex);
612 btrfs_btree_balance_dirty(tree_root, nr);
614 mutex_lock(&root->fs_info->drop_mutex);
617 atomic_dec(&root->fs_info->throttles);
618 wake_up(&root->fs_info->transaction_throttle);
620 mutex_lock(&root->fs_info->alloc_mutex);
621 num_bytes -= btrfs_root_used(&dirty->root->root_item);
622 bytes_used = btrfs_root_used(&root->root_item);
624 btrfs_record_root_in_trans(root);
625 btrfs_set_root_used(&root->root_item,
626 bytes_used - num_bytes);
628 mutex_unlock(&root->fs_info->alloc_mutex);
630 ret = btrfs_del_root(trans, tree_root, &dirty->root->root_key);
635 mutex_unlock(&root->fs_info->drop_mutex);
637 spin_lock(&root->list_lock);
638 list_del_init(&dirty->root->dead_list);
639 if (!list_empty(&root->dead_list)) {
640 struct btrfs_root *oldest;
641 oldest = list_entry(root->dead_list.prev,
642 struct btrfs_root, dead_list);
643 max_useless = oldest->root_key.offset - 1;
645 max_useless = root->root_key.offset - 1;
647 spin_unlock(&root->list_lock);
649 nr = trans->blocks_used;
650 ret = btrfs_end_transaction(trans, tree_root);
653 ret = btrfs_remove_leaf_refs(root, max_useless);
656 free_extent_buffer(dirty->root->node);
660 btrfs_btree_balance_dirty(tree_root, nr);
666 static noinline int create_pending_snapshot(struct btrfs_trans_handle *trans,
667 struct btrfs_fs_info *fs_info,
668 struct btrfs_pending_snapshot *pending)
670 struct btrfs_key key;
671 struct btrfs_root_item *new_root_item;
672 struct btrfs_root *tree_root = fs_info->tree_root;
673 struct btrfs_root *root = pending->root;
674 struct extent_buffer *tmp;
675 struct extent_buffer *old;
680 new_root_item = kmalloc(sizeof(*new_root_item), GFP_NOFS);
681 if (!new_root_item) {
685 ret = btrfs_find_free_objectid(trans, tree_root, 0, &objectid);
689 memcpy(new_root_item, &root->root_item, sizeof(*new_root_item));
691 key.objectid = objectid;
693 btrfs_set_key_type(&key, BTRFS_ROOT_ITEM_KEY);
695 old = btrfs_lock_root_node(root);
696 btrfs_cow_block(trans, root, old, NULL, 0, &old, 0);
698 btrfs_copy_root(trans, root, old, &tmp, objectid);
699 btrfs_tree_unlock(old);
700 free_extent_buffer(old);
702 btrfs_set_root_bytenr(new_root_item, tmp->start);
703 btrfs_set_root_level(new_root_item, btrfs_header_level(tmp));
704 ret = btrfs_insert_root(trans, root->fs_info->tree_root, &key,
706 btrfs_tree_unlock(tmp);
707 free_extent_buffer(tmp);
712 * insert the directory item
714 key.offset = (u64)-1;
715 namelen = strlen(pending->name);
716 ret = btrfs_insert_dir_item(trans, root->fs_info->tree_root,
717 pending->name, namelen,
718 root->fs_info->sb->s_root->d_inode->i_ino,
719 &key, BTRFS_FT_DIR, 0);
724 ret = btrfs_insert_inode_ref(trans, root->fs_info->tree_root,
725 pending->name, strlen(pending->name), objectid,
726 root->fs_info->sb->s_root->d_inode->i_ino, 0);
728 /* Invalidate existing dcache entry for new snapshot. */
729 btrfs_invalidate_dcache_root(root, pending->name, namelen);
732 kfree(new_root_item);
736 static noinline int create_pending_snapshots(struct btrfs_trans_handle *trans,
737 struct btrfs_fs_info *fs_info)
739 struct btrfs_pending_snapshot *pending;
740 struct list_head *head = &trans->transaction->pending_snapshots;
743 while(!list_empty(head)) {
744 pending = list_entry(head->next,
745 struct btrfs_pending_snapshot, list);
746 ret = create_pending_snapshot(trans, fs_info, pending);
748 list_del(&pending->list);
749 kfree(pending->name);
755 int btrfs_commit_transaction(struct btrfs_trans_handle *trans,
756 struct btrfs_root *root)
758 unsigned long joined = 0;
759 unsigned long timeout = 1;
760 struct btrfs_transaction *cur_trans;
761 struct btrfs_transaction *prev_trans = NULL;
762 struct btrfs_root *chunk_root = root->fs_info->chunk_root;
763 struct list_head dirty_fs_roots;
764 struct extent_io_tree *pinned_copy;
768 INIT_LIST_HEAD(&dirty_fs_roots);
769 mutex_lock(&root->fs_info->trans_mutex);
770 if (trans->transaction->in_commit) {
771 cur_trans = trans->transaction;
772 trans->transaction->use_count++;
773 mutex_unlock(&root->fs_info->trans_mutex);
774 btrfs_end_transaction(trans, root);
776 ret = wait_for_commit(root, cur_trans);
779 mutex_lock(&root->fs_info->trans_mutex);
780 put_transaction(cur_trans);
781 mutex_unlock(&root->fs_info->trans_mutex);
786 pinned_copy = kmalloc(sizeof(*pinned_copy), GFP_NOFS);
790 extent_io_tree_init(pinned_copy,
791 root->fs_info->btree_inode->i_mapping, GFP_NOFS);
793 trans->transaction->in_commit = 1;
794 trans->transaction->blocked = 1;
795 cur_trans = trans->transaction;
796 if (cur_trans->list.prev != &root->fs_info->trans_list) {
797 prev_trans = list_entry(cur_trans->list.prev,
798 struct btrfs_transaction, list);
799 if (!prev_trans->commit_done) {
800 prev_trans->use_count++;
801 mutex_unlock(&root->fs_info->trans_mutex);
803 wait_for_commit(root, prev_trans);
805 mutex_lock(&root->fs_info->trans_mutex);
806 put_transaction(prev_trans);
811 int snap_pending = 0;
812 joined = cur_trans->num_joined;
813 if (!list_empty(&trans->transaction->pending_snapshots))
816 WARN_ON(cur_trans != trans->transaction);
817 prepare_to_wait(&cur_trans->writer_wait, &wait,
818 TASK_UNINTERRUPTIBLE);
820 if (cur_trans->num_writers > 1)
821 timeout = MAX_SCHEDULE_TIMEOUT;
825 mutex_unlock(&root->fs_info->trans_mutex);
828 ret = btrfs_wait_ordered_extents(root, 1);
832 schedule_timeout(timeout);
834 mutex_lock(&root->fs_info->trans_mutex);
835 finish_wait(&cur_trans->writer_wait, &wait);
836 } while (cur_trans->num_writers > 1 ||
837 (cur_trans->num_joined != joined));
839 ret = create_pending_snapshots(trans, root->fs_info);
842 WARN_ON(cur_trans != trans->transaction);
844 /* btrfs_commit_tree_roots is responsible for getting the
845 * various roots consistent with each other. Every pointer
846 * in the tree of tree roots has to point to the most up to date
847 * root for every subvolume and other tree. So, we have to keep
848 * the tree logging code from jumping in and changing any
851 * At this point in the commit, there can't be any tree-log
852 * writers, but a little lower down we drop the trans mutex
853 * and let new people in. By holding the tree_log_mutex
854 * from now until after the super is written, we avoid races
855 * with the tree-log code.
857 mutex_lock(&root->fs_info->tree_log_mutex);
859 ret = add_dirty_roots(trans, &root->fs_info->fs_roots_radix,
863 /* add_dirty_roots gets rid of all the tree log roots, it is now
864 * safe to free the root of tree log roots
866 btrfs_free_log_root_tree(trans, root->fs_info);
868 ret = btrfs_commit_tree_roots(trans, root);
871 cur_trans = root->fs_info->running_transaction;
872 spin_lock(&root->fs_info->new_trans_lock);
873 root->fs_info->running_transaction = NULL;
874 spin_unlock(&root->fs_info->new_trans_lock);
875 btrfs_set_super_generation(&root->fs_info->super_copy,
877 btrfs_set_super_root(&root->fs_info->super_copy,
878 root->fs_info->tree_root->node->start);
879 btrfs_set_super_root_level(&root->fs_info->super_copy,
880 btrfs_header_level(root->fs_info->tree_root->node));
882 btrfs_set_super_chunk_root(&root->fs_info->super_copy,
883 chunk_root->node->start);
884 btrfs_set_super_chunk_root_level(&root->fs_info->super_copy,
885 btrfs_header_level(chunk_root->node));
887 if (!root->fs_info->log_root_recovering) {
888 btrfs_set_super_log_root(&root->fs_info->super_copy, 0);
889 btrfs_set_super_log_root_level(&root->fs_info->super_copy, 0);
892 memcpy(&root->fs_info->super_for_commit, &root->fs_info->super_copy,
893 sizeof(root->fs_info->super_copy));
895 btrfs_copy_pinned(root, pinned_copy);
897 trans->transaction->blocked = 0;
898 wake_up(&root->fs_info->transaction_throttle);
899 wake_up(&root->fs_info->transaction_wait);
901 mutex_unlock(&root->fs_info->trans_mutex);
902 ret = btrfs_write_and_wait_transaction(trans, root);
904 write_ctree_super(trans, root);
907 * the super is written, we can safely allow the tree-loggers
908 * to go about their business
910 mutex_unlock(&root->fs_info->tree_log_mutex);
912 btrfs_finish_extent_commit(trans, root, pinned_copy);
913 mutex_lock(&root->fs_info->trans_mutex);
917 cur_trans->commit_done = 1;
918 root->fs_info->last_trans_committed = cur_trans->transid;
919 wake_up(&cur_trans->commit_wait);
920 put_transaction(cur_trans);
921 put_transaction(cur_trans);
923 list_splice_init(&dirty_fs_roots, &root->fs_info->dead_roots);
924 if (root->fs_info->closing)
925 list_splice_init(&root->fs_info->dead_roots, &dirty_fs_roots);
927 mutex_unlock(&root->fs_info->trans_mutex);
928 kmem_cache_free(btrfs_trans_handle_cachep, trans);
930 if (root->fs_info->closing) {
931 drop_dirty_roots(root->fs_info->tree_root, &dirty_fs_roots);
936 int btrfs_clean_old_snapshots(struct btrfs_root *root)
938 struct list_head dirty_roots;
939 INIT_LIST_HEAD(&dirty_roots);
941 mutex_lock(&root->fs_info->trans_mutex);
942 list_splice_init(&root->fs_info->dead_roots, &dirty_roots);
943 mutex_unlock(&root->fs_info->trans_mutex);
945 if (!list_empty(&dirty_roots)) {
946 drop_dirty_roots(root, &dirty_roots);