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 ((wait == 1 && !root->fs_info->open_ioctl_trans) || wait == 2)
165 wait_current_trans(root);
166 ret = join_transaction(root);
169 btrfs_record_root_in_trans(root);
170 h->transid = root->fs_info->running_transaction->transid;
171 h->transaction = root->fs_info->running_transaction;
172 h->blocks_reserved = num_blocks;
174 h->block_group = NULL;
175 h->alloc_exclude_nr = 0;
176 h->alloc_exclude_start = 0;
177 root->fs_info->running_transaction->use_count++;
178 mutex_unlock(&root->fs_info->trans_mutex);
182 struct btrfs_trans_handle *btrfs_start_transaction(struct btrfs_root *root,
185 return start_transaction(root, num_blocks, 1);
187 struct btrfs_trans_handle *btrfs_join_transaction(struct btrfs_root *root,
190 return start_transaction(root, num_blocks, 0);
193 struct btrfs_trans_handle *btrfs_start_ioctl_transaction(struct btrfs_root *r,
196 return start_transaction(r, num_blocks, 2);
200 static noinline int wait_for_commit(struct btrfs_root *root,
201 struct btrfs_transaction *commit)
204 mutex_lock(&root->fs_info->trans_mutex);
205 while(!commit->commit_done) {
206 prepare_to_wait(&commit->commit_wait, &wait,
207 TASK_UNINTERRUPTIBLE);
208 if (commit->commit_done)
210 mutex_unlock(&root->fs_info->trans_mutex);
212 mutex_lock(&root->fs_info->trans_mutex);
214 mutex_unlock(&root->fs_info->trans_mutex);
215 finish_wait(&commit->commit_wait, &wait);
219 static void throttle_on_drops(struct btrfs_root *root)
221 struct btrfs_fs_info *info = root->fs_info;
222 int harder_count = 0;
225 if (atomic_read(&info->throttles)) {
228 thr = atomic_read(&info->throttle_gen);
231 prepare_to_wait(&info->transaction_throttle,
232 &wait, TASK_UNINTERRUPTIBLE);
233 if (!atomic_read(&info->throttles)) {
234 finish_wait(&info->transaction_throttle, &wait);
238 finish_wait(&info->transaction_throttle, &wait);
239 } while (thr == atomic_read(&info->throttle_gen));
242 if (root->fs_info->total_ref_cache_size > 1 * 1024 * 1024 &&
246 if (root->fs_info->total_ref_cache_size > 5 * 1024 * 1024 &&
250 if (root->fs_info->total_ref_cache_size > 10 * 1024 * 1024 &&
256 void btrfs_throttle(struct btrfs_root *root)
258 mutex_lock(&root->fs_info->trans_mutex);
259 if (!root->fs_info->open_ioctl_trans)
260 wait_current_trans(root);
261 mutex_unlock(&root->fs_info->trans_mutex);
263 throttle_on_drops(root);
266 static int __btrfs_end_transaction(struct btrfs_trans_handle *trans,
267 struct btrfs_root *root, int throttle)
269 struct btrfs_transaction *cur_trans;
270 struct btrfs_fs_info *info = root->fs_info;
272 mutex_lock(&info->trans_mutex);
273 cur_trans = info->running_transaction;
274 WARN_ON(cur_trans != trans->transaction);
275 WARN_ON(cur_trans->num_writers < 1);
276 cur_trans->num_writers--;
278 if (waitqueue_active(&cur_trans->writer_wait))
279 wake_up(&cur_trans->writer_wait);
280 put_transaction(cur_trans);
281 mutex_unlock(&info->trans_mutex);
282 memset(trans, 0, sizeof(*trans));
283 kmem_cache_free(btrfs_trans_handle_cachep, trans);
286 throttle_on_drops(root);
291 int btrfs_end_transaction(struct btrfs_trans_handle *trans,
292 struct btrfs_root *root)
294 return __btrfs_end_transaction(trans, root, 0);
297 int btrfs_end_transaction_throttle(struct btrfs_trans_handle *trans,
298 struct btrfs_root *root)
300 return __btrfs_end_transaction(trans, root, 1);
304 int btrfs_write_and_wait_transaction(struct btrfs_trans_handle *trans,
305 struct btrfs_root *root)
310 struct extent_io_tree *dirty_pages;
312 struct inode *btree_inode = root->fs_info->btree_inode;
317 if (!trans || !trans->transaction) {
318 return filemap_write_and_wait(btree_inode->i_mapping);
320 dirty_pages = &trans->transaction->dirty_pages;
322 ret = find_first_extent_bit(dirty_pages, start, &start, &end,
326 while(start <= end) {
329 index = start >> PAGE_CACHE_SHIFT;
330 start = (u64)(index + 1) << PAGE_CACHE_SHIFT;
331 page = find_lock_page(btree_inode->i_mapping, index);
334 if (PageWriteback(page)) {
336 wait_on_page_writeback(page);
339 page_cache_release(page);
343 err = write_one_page(page, 0);
346 page_cache_release(page);
350 ret = find_first_extent_bit(dirty_pages, 0, &start, &end,
355 clear_extent_dirty(dirty_pages, start, end, GFP_NOFS);
356 while(start <= end) {
357 index = start >> PAGE_CACHE_SHIFT;
358 start = (u64)(index + 1) << PAGE_CACHE_SHIFT;
359 page = find_get_page(btree_inode->i_mapping, index);
362 if (PageDirty(page)) {
364 err = write_one_page(page, 0);
368 wait_on_page_writeback(page);
369 page_cache_release(page);
378 static int update_cowonly_root(struct btrfs_trans_handle *trans,
379 struct btrfs_root *root)
383 struct btrfs_root *tree_root = root->fs_info->tree_root;
385 btrfs_write_dirty_block_groups(trans, root);
387 old_root_bytenr = btrfs_root_bytenr(&root->root_item);
388 if (old_root_bytenr == root->node->start)
390 btrfs_set_root_bytenr(&root->root_item,
392 btrfs_set_root_level(&root->root_item,
393 btrfs_header_level(root->node));
394 ret = btrfs_update_root(trans, tree_root,
398 btrfs_write_dirty_block_groups(trans, root);
403 int btrfs_commit_tree_roots(struct btrfs_trans_handle *trans,
404 struct btrfs_root *root)
406 struct btrfs_fs_info *fs_info = root->fs_info;
407 struct list_head *next;
409 while(!list_empty(&fs_info->dirty_cowonly_roots)) {
410 next = fs_info->dirty_cowonly_roots.next;
412 root = list_entry(next, struct btrfs_root, dirty_list);
413 update_cowonly_root(trans, root);
418 int btrfs_add_dead_root(struct btrfs_root *root, struct btrfs_root *latest)
420 struct btrfs_dirty_root *dirty;
422 dirty = kmalloc(sizeof(*dirty), GFP_NOFS);
426 dirty->latest_root = latest;
428 mutex_lock(&root->fs_info->trans_mutex);
429 list_add(&dirty->list, &latest->fs_info->dead_roots);
430 mutex_unlock(&root->fs_info->trans_mutex);
434 static noinline int add_dirty_roots(struct btrfs_trans_handle *trans,
435 struct radix_tree_root *radix,
436 struct list_head *list)
438 struct btrfs_dirty_root *dirty;
439 struct btrfs_root *gang[8];
440 struct btrfs_root *root;
447 ret = radix_tree_gang_lookup_tag(radix, (void **)gang, 0,
449 BTRFS_ROOT_TRANS_TAG);
452 for (i = 0; i < ret; i++) {
454 radix_tree_tag_clear(radix,
455 (unsigned long)root->root_key.objectid,
456 BTRFS_ROOT_TRANS_TAG);
458 BUG_ON(!root->ref_tree);
459 dirty = root->dirty_root;
461 btrfs_free_log(trans, root);
463 if (root->commit_root == root->node) {
464 WARN_ON(root->node->start !=
465 btrfs_root_bytenr(&root->root_item));
467 free_extent_buffer(root->commit_root);
468 root->commit_root = NULL;
469 root->dirty_root = NULL;
471 spin_lock(&root->list_lock);
472 list_del_init(&dirty->root->dead_list);
473 spin_unlock(&root->list_lock);
478 /* make sure to update the root on disk
479 * so we get any updates to the block used
482 err = btrfs_update_root(trans,
483 root->fs_info->tree_root,
489 memset(&root->root_item.drop_progress, 0,
490 sizeof(struct btrfs_disk_key));
491 root->root_item.drop_level = 0;
492 root->commit_root = NULL;
493 root->dirty_root = NULL;
494 root->root_key.offset = root->fs_info->generation;
495 btrfs_set_root_bytenr(&root->root_item,
497 btrfs_set_root_level(&root->root_item,
498 btrfs_header_level(root->node));
499 err = btrfs_insert_root(trans, root->fs_info->tree_root,
505 refs = btrfs_root_refs(&dirty->root->root_item);
506 btrfs_set_root_refs(&dirty->root->root_item, refs - 1);
507 err = btrfs_update_root(trans, root->fs_info->tree_root,
508 &dirty->root->root_key,
509 &dirty->root->root_item);
513 list_add(&dirty->list, list);
516 free_extent_buffer(dirty->root->node);
525 int btrfs_defrag_root(struct btrfs_root *root, int cacheonly)
527 struct btrfs_fs_info *info = root->fs_info;
529 struct btrfs_trans_handle *trans;
533 if (root->defrag_running)
535 trans = btrfs_start_transaction(root, 1);
537 root->defrag_running = 1;
538 ret = btrfs_defrag_leaves(trans, root, cacheonly);
539 nr = trans->blocks_used;
540 btrfs_end_transaction(trans, root);
541 btrfs_btree_balance_dirty(info->tree_root, nr);
544 trans = btrfs_start_transaction(root, 1);
545 if (root->fs_info->closing || ret != -EAGAIN)
548 root->defrag_running = 0;
550 btrfs_end_transaction(trans, root);
554 static noinline int drop_dirty_roots(struct btrfs_root *tree_root,
555 struct list_head *list)
557 struct btrfs_dirty_root *dirty;
558 struct btrfs_trans_handle *trans;
566 while(!list_empty(list)) {
567 struct btrfs_root *root;
569 dirty = list_entry(list->prev, struct btrfs_dirty_root, list);
570 list_del_init(&dirty->list);
572 num_bytes = btrfs_root_used(&dirty->root->root_item);
573 root = dirty->latest_root;
574 atomic_inc(&root->fs_info->throttles);
576 mutex_lock(&root->fs_info->drop_mutex);
578 trans = btrfs_start_transaction(tree_root, 1);
579 ret = btrfs_drop_snapshot(trans, dirty->root);
580 if (ret != -EAGAIN) {
584 err = btrfs_update_root(trans,
586 &dirty->root->root_key,
587 &dirty->root->root_item);
590 nr = trans->blocks_used;
591 ret = btrfs_end_transaction(trans, tree_root);
594 mutex_unlock(&root->fs_info->drop_mutex);
595 btrfs_btree_balance_dirty(tree_root, nr);
597 mutex_lock(&root->fs_info->drop_mutex);
600 atomic_dec(&root->fs_info->throttles);
601 wake_up(&root->fs_info->transaction_throttle);
603 mutex_lock(&root->fs_info->alloc_mutex);
604 num_bytes -= btrfs_root_used(&dirty->root->root_item);
605 bytes_used = btrfs_root_used(&root->root_item);
607 btrfs_record_root_in_trans(root);
608 btrfs_set_root_used(&root->root_item,
609 bytes_used - num_bytes);
611 mutex_unlock(&root->fs_info->alloc_mutex);
613 ret = btrfs_del_root(trans, tree_root, &dirty->root->root_key);
618 mutex_unlock(&root->fs_info->drop_mutex);
620 spin_lock(&root->list_lock);
621 list_del_init(&dirty->root->dead_list);
622 if (!list_empty(&root->dead_list)) {
623 struct btrfs_root *oldest;
624 oldest = list_entry(root->dead_list.prev,
625 struct btrfs_root, dead_list);
626 max_useless = oldest->root_key.offset - 1;
628 max_useless = root->root_key.offset - 1;
630 spin_unlock(&root->list_lock);
632 nr = trans->blocks_used;
633 ret = btrfs_end_transaction(trans, tree_root);
636 ret = btrfs_remove_leaf_refs(root, max_useless);
639 free_extent_buffer(dirty->root->node);
643 btrfs_btree_balance_dirty(tree_root, nr);
649 static noinline int create_pending_snapshot(struct btrfs_trans_handle *trans,
650 struct btrfs_fs_info *fs_info,
651 struct btrfs_pending_snapshot *pending)
653 struct btrfs_key key;
654 struct btrfs_root_item *new_root_item;
655 struct btrfs_root *tree_root = fs_info->tree_root;
656 struct btrfs_root *root = pending->root;
657 struct extent_buffer *tmp;
658 struct extent_buffer *old;
663 new_root_item = kmalloc(sizeof(*new_root_item), GFP_NOFS);
664 if (!new_root_item) {
668 ret = btrfs_find_free_objectid(trans, tree_root, 0, &objectid);
672 memcpy(new_root_item, &root->root_item, sizeof(*new_root_item));
674 key.objectid = objectid;
676 btrfs_set_key_type(&key, BTRFS_ROOT_ITEM_KEY);
678 old = btrfs_lock_root_node(root);
679 btrfs_cow_block(trans, root, old, NULL, 0, &old, 0);
681 btrfs_copy_root(trans, root, old, &tmp, objectid);
682 btrfs_tree_unlock(old);
683 free_extent_buffer(old);
685 btrfs_set_root_bytenr(new_root_item, tmp->start);
686 btrfs_set_root_level(new_root_item, btrfs_header_level(tmp));
687 ret = btrfs_insert_root(trans, root->fs_info->tree_root, &key,
689 btrfs_tree_unlock(tmp);
690 free_extent_buffer(tmp);
695 * insert the directory item
697 key.offset = (u64)-1;
698 namelen = strlen(pending->name);
699 ret = btrfs_insert_dir_item(trans, root->fs_info->tree_root,
700 pending->name, namelen,
701 root->fs_info->sb->s_root->d_inode->i_ino,
702 &key, BTRFS_FT_DIR, 0);
707 ret = btrfs_insert_inode_ref(trans, root->fs_info->tree_root,
708 pending->name, strlen(pending->name), objectid,
709 root->fs_info->sb->s_root->d_inode->i_ino, 0);
711 /* Invalidate existing dcache entry for new snapshot. */
712 btrfs_invalidate_dcache_root(root, pending->name, namelen);
715 kfree(new_root_item);
719 static noinline int create_pending_snapshots(struct btrfs_trans_handle *trans,
720 struct btrfs_fs_info *fs_info)
722 struct btrfs_pending_snapshot *pending;
723 struct list_head *head = &trans->transaction->pending_snapshots;
726 while(!list_empty(head)) {
727 pending = list_entry(head->next,
728 struct btrfs_pending_snapshot, list);
729 ret = create_pending_snapshot(trans, fs_info, pending);
731 list_del(&pending->list);
732 kfree(pending->name);
738 int btrfs_commit_transaction(struct btrfs_trans_handle *trans,
739 struct btrfs_root *root)
741 unsigned long joined = 0;
742 unsigned long timeout = 1;
743 struct btrfs_transaction *cur_trans;
744 struct btrfs_transaction *prev_trans = NULL;
745 struct btrfs_root *chunk_root = root->fs_info->chunk_root;
746 struct list_head dirty_fs_roots;
747 struct extent_io_tree *pinned_copy;
751 INIT_LIST_HEAD(&dirty_fs_roots);
752 mutex_lock(&root->fs_info->trans_mutex);
753 if (trans->transaction->in_commit) {
754 cur_trans = trans->transaction;
755 trans->transaction->use_count++;
756 mutex_unlock(&root->fs_info->trans_mutex);
757 btrfs_end_transaction(trans, root);
759 ret = wait_for_commit(root, cur_trans);
762 mutex_lock(&root->fs_info->trans_mutex);
763 put_transaction(cur_trans);
764 mutex_unlock(&root->fs_info->trans_mutex);
769 pinned_copy = kmalloc(sizeof(*pinned_copy), GFP_NOFS);
773 extent_io_tree_init(pinned_copy,
774 root->fs_info->btree_inode->i_mapping, GFP_NOFS);
776 trans->transaction->in_commit = 1;
777 trans->transaction->blocked = 1;
778 cur_trans = trans->transaction;
779 if (cur_trans->list.prev != &root->fs_info->trans_list) {
780 prev_trans = list_entry(cur_trans->list.prev,
781 struct btrfs_transaction, list);
782 if (!prev_trans->commit_done) {
783 prev_trans->use_count++;
784 mutex_unlock(&root->fs_info->trans_mutex);
786 wait_for_commit(root, prev_trans);
788 mutex_lock(&root->fs_info->trans_mutex);
789 put_transaction(prev_trans);
794 int snap_pending = 0;
795 joined = cur_trans->num_joined;
796 if (!list_empty(&trans->transaction->pending_snapshots))
799 WARN_ON(cur_trans != trans->transaction);
800 prepare_to_wait(&cur_trans->writer_wait, &wait,
801 TASK_UNINTERRUPTIBLE);
803 if (cur_trans->num_writers > 1)
804 timeout = MAX_SCHEDULE_TIMEOUT;
808 mutex_unlock(&root->fs_info->trans_mutex);
811 ret = btrfs_wait_ordered_extents(root, 1);
815 schedule_timeout(timeout);
817 mutex_lock(&root->fs_info->trans_mutex);
818 finish_wait(&cur_trans->writer_wait, &wait);
819 } while (cur_trans->num_writers > 1 ||
820 (cur_trans->num_joined != joined));
822 ret = create_pending_snapshots(trans, root->fs_info);
825 WARN_ON(cur_trans != trans->transaction);
827 /* btrfs_commit_tree_roots is responsible for getting the
828 * various roots consistent with each other. Every pointer
829 * in the tree of tree roots has to point to the most up to date
830 * root for every subvolume and other tree. So, we have to keep
831 * the tree logging code from jumping in and changing any
834 * At this point in the commit, there can't be any tree-log
835 * writers, but a little lower down we drop the trans mutex
836 * and let new people in. By holding the tree_log_mutex
837 * from now until after the super is written, we avoid races
838 * with the tree-log code.
840 mutex_lock(&root->fs_info->tree_log_mutex);
842 ret = add_dirty_roots(trans, &root->fs_info->fs_roots_radix,
846 /* add_dirty_roots gets rid of all the tree log roots, it is now
847 * safe to free the root of tree log roots
849 btrfs_free_log_root_tree(trans, root->fs_info);
851 ret = btrfs_commit_tree_roots(trans, root);
854 cur_trans = root->fs_info->running_transaction;
855 spin_lock(&root->fs_info->new_trans_lock);
856 root->fs_info->running_transaction = NULL;
857 spin_unlock(&root->fs_info->new_trans_lock);
858 btrfs_set_super_generation(&root->fs_info->super_copy,
860 btrfs_set_super_root(&root->fs_info->super_copy,
861 root->fs_info->tree_root->node->start);
862 btrfs_set_super_root_level(&root->fs_info->super_copy,
863 btrfs_header_level(root->fs_info->tree_root->node));
865 btrfs_set_super_chunk_root(&root->fs_info->super_copy,
866 chunk_root->node->start);
867 btrfs_set_super_chunk_root_level(&root->fs_info->super_copy,
868 btrfs_header_level(chunk_root->node));
870 if (!root->fs_info->log_root_recovering) {
871 btrfs_set_super_log_root(&root->fs_info->super_copy, 0);
872 btrfs_set_super_log_root_level(&root->fs_info->super_copy, 0);
875 memcpy(&root->fs_info->super_for_commit, &root->fs_info->super_copy,
876 sizeof(root->fs_info->super_copy));
878 btrfs_copy_pinned(root, pinned_copy);
880 trans->transaction->blocked = 0;
881 wake_up(&root->fs_info->transaction_throttle);
882 wake_up(&root->fs_info->transaction_wait);
884 mutex_unlock(&root->fs_info->trans_mutex);
885 ret = btrfs_write_and_wait_transaction(trans, root);
887 write_ctree_super(trans, root);
890 * the super is written, we can safely allow the tree-loggers
891 * to go about their business
893 mutex_unlock(&root->fs_info->tree_log_mutex);
895 btrfs_finish_extent_commit(trans, root, pinned_copy);
896 mutex_lock(&root->fs_info->trans_mutex);
900 cur_trans->commit_done = 1;
901 root->fs_info->last_trans_committed = cur_trans->transid;
902 wake_up(&cur_trans->commit_wait);
903 put_transaction(cur_trans);
904 put_transaction(cur_trans);
906 list_splice_init(&dirty_fs_roots, &root->fs_info->dead_roots);
907 if (root->fs_info->closing)
908 list_splice_init(&root->fs_info->dead_roots, &dirty_fs_roots);
910 mutex_unlock(&root->fs_info->trans_mutex);
911 kmem_cache_free(btrfs_trans_handle_cachep, trans);
913 if (root->fs_info->closing) {
914 drop_dirty_roots(root->fs_info->tree_root, &dirty_fs_roots);
919 int btrfs_clean_old_snapshots(struct btrfs_root *root)
921 struct list_head dirty_roots;
922 INIT_LIST_HEAD(&dirty_roots);
924 mutex_lock(&root->fs_info->trans_mutex);
925 list_splice_init(&root->fs_info->dead_roots, &dirty_roots);
926 mutex_unlock(&root->fs_info->trans_mutex);
928 if (!list_empty(&dirty_roots)) {
929 drop_dirty_roots(root, &dirty_roots);