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 mutex_init(&dirty->root->log_mutex);
113 INIT_LIST_HEAD(&dirty->root->dead_list);
114 dirty->root->node = root->commit_root;
115 dirty->root->commit_root = NULL;
117 spin_lock(&root->list_lock);
118 list_add(&dirty->root->dead_list, &root->dead_list);
119 spin_unlock(&root->list_lock);
121 root->dirty_root = dirty;
125 root->last_trans = running_trans_id;
130 static void wait_current_trans(struct btrfs_root *root)
132 struct btrfs_transaction *cur_trans;
134 cur_trans = root->fs_info->running_transaction;
135 if (cur_trans && cur_trans->blocked) {
137 cur_trans->use_count++;
139 prepare_to_wait(&root->fs_info->transaction_wait, &wait,
140 TASK_UNINTERRUPTIBLE);
141 if (cur_trans->blocked) {
142 mutex_unlock(&root->fs_info->trans_mutex);
144 mutex_lock(&root->fs_info->trans_mutex);
145 finish_wait(&root->fs_info->transaction_wait,
148 finish_wait(&root->fs_info->transaction_wait,
153 put_transaction(cur_trans);
157 static struct btrfs_trans_handle *start_transaction(struct btrfs_root *root,
158 int num_blocks, int wait)
160 struct btrfs_trans_handle *h =
161 kmem_cache_alloc(btrfs_trans_handle_cachep, GFP_NOFS);
164 mutex_lock(&root->fs_info->trans_mutex);
165 if (!root->fs_info->log_root_recovering &&
166 ((wait == 1 && !root->fs_info->open_ioctl_trans) || wait == 2))
167 wait_current_trans(root);
168 ret = join_transaction(root);
171 btrfs_record_root_in_trans(root);
172 h->transid = root->fs_info->running_transaction->transid;
173 h->transaction = root->fs_info->running_transaction;
174 h->blocks_reserved = num_blocks;
176 h->block_group = NULL;
177 h->alloc_exclude_nr = 0;
178 h->alloc_exclude_start = 0;
179 root->fs_info->running_transaction->use_count++;
180 mutex_unlock(&root->fs_info->trans_mutex);
184 struct btrfs_trans_handle *btrfs_start_transaction(struct btrfs_root *root,
187 return start_transaction(root, num_blocks, 1);
189 struct btrfs_trans_handle *btrfs_join_transaction(struct btrfs_root *root,
192 return start_transaction(root, num_blocks, 0);
195 struct btrfs_trans_handle *btrfs_start_ioctl_transaction(struct btrfs_root *r,
198 return start_transaction(r, num_blocks, 2);
202 static noinline int wait_for_commit(struct btrfs_root *root,
203 struct btrfs_transaction *commit)
206 mutex_lock(&root->fs_info->trans_mutex);
207 while(!commit->commit_done) {
208 prepare_to_wait(&commit->commit_wait, &wait,
209 TASK_UNINTERRUPTIBLE);
210 if (commit->commit_done)
212 mutex_unlock(&root->fs_info->trans_mutex);
214 mutex_lock(&root->fs_info->trans_mutex);
216 mutex_unlock(&root->fs_info->trans_mutex);
217 finish_wait(&commit->commit_wait, &wait);
221 static void throttle_on_drops(struct btrfs_root *root)
223 struct btrfs_fs_info *info = root->fs_info;
224 int harder_count = 0;
227 if (atomic_read(&info->throttles)) {
230 thr = atomic_read(&info->throttle_gen);
233 prepare_to_wait(&info->transaction_throttle,
234 &wait, TASK_UNINTERRUPTIBLE);
235 if (!atomic_read(&info->throttles)) {
236 finish_wait(&info->transaction_throttle, &wait);
240 finish_wait(&info->transaction_throttle, &wait);
241 } while (thr == atomic_read(&info->throttle_gen));
244 if (root->fs_info->total_ref_cache_size > 1 * 1024 * 1024 &&
248 if (root->fs_info->total_ref_cache_size > 5 * 1024 * 1024 &&
252 if (root->fs_info->total_ref_cache_size > 10 * 1024 * 1024 &&
258 void btrfs_throttle(struct btrfs_root *root)
260 mutex_lock(&root->fs_info->trans_mutex);
261 if (!root->fs_info->open_ioctl_trans)
262 wait_current_trans(root);
263 mutex_unlock(&root->fs_info->trans_mutex);
265 throttle_on_drops(root);
268 static int __btrfs_end_transaction(struct btrfs_trans_handle *trans,
269 struct btrfs_root *root, int throttle)
271 struct btrfs_transaction *cur_trans;
272 struct btrfs_fs_info *info = root->fs_info;
274 mutex_lock(&info->trans_mutex);
275 cur_trans = info->running_transaction;
276 WARN_ON(cur_trans != trans->transaction);
277 WARN_ON(cur_trans->num_writers < 1);
278 cur_trans->num_writers--;
280 if (waitqueue_active(&cur_trans->writer_wait))
281 wake_up(&cur_trans->writer_wait);
282 put_transaction(cur_trans);
283 mutex_unlock(&info->trans_mutex);
284 memset(trans, 0, sizeof(*trans));
285 kmem_cache_free(btrfs_trans_handle_cachep, trans);
288 throttle_on_drops(root);
293 int btrfs_end_transaction(struct btrfs_trans_handle *trans,
294 struct btrfs_root *root)
296 return __btrfs_end_transaction(trans, root, 0);
299 int btrfs_end_transaction_throttle(struct btrfs_trans_handle *trans,
300 struct btrfs_root *root)
302 return __btrfs_end_transaction(trans, root, 1);
306 int btrfs_write_and_wait_marked_extents(struct btrfs_root *root,
307 struct extent_io_tree *dirty_pages)
313 struct inode *btree_inode = root->fs_info->btree_inode;
319 ret = find_first_extent_bit(dirty_pages, start, &start, &end,
323 while(start <= end) {
326 index = start >> PAGE_CACHE_SHIFT;
327 start = (u64)(index + 1) << PAGE_CACHE_SHIFT;
328 page = find_get_page(btree_inode->i_mapping, index);
332 btree_lock_page_hook(page);
333 if (!page->mapping) {
335 page_cache_release(page);
339 if (PageWriteback(page)) {
341 wait_on_page_writeback(page);
344 page_cache_release(page);
348 err = write_one_page(page, 0);
351 page_cache_release(page);
355 ret = find_first_extent_bit(dirty_pages, 0, &start, &end,
360 clear_extent_dirty(dirty_pages, start, end, GFP_NOFS);
361 while(start <= end) {
362 index = start >> PAGE_CACHE_SHIFT;
363 start = (u64)(index + 1) << PAGE_CACHE_SHIFT;
364 page = find_get_page(btree_inode->i_mapping, index);
367 if (PageDirty(page)) {
368 btree_lock_page_hook(page);
369 wait_on_page_writeback(page);
370 err = write_one_page(page, 0);
374 wait_on_page_writeback(page);
375 page_cache_release(page);
384 int btrfs_write_and_wait_transaction(struct btrfs_trans_handle *trans,
385 struct btrfs_root *root)
387 if (!trans || !trans->transaction) {
388 struct inode *btree_inode;
389 btree_inode = root->fs_info->btree_inode;
390 return filemap_write_and_wait(btree_inode->i_mapping);
392 return btrfs_write_and_wait_marked_extents(root,
393 &trans->transaction->dirty_pages);
396 static int update_cowonly_root(struct btrfs_trans_handle *trans,
397 struct btrfs_root *root)
401 struct btrfs_root *tree_root = root->fs_info->tree_root;
403 btrfs_write_dirty_block_groups(trans, root);
405 old_root_bytenr = btrfs_root_bytenr(&root->root_item);
406 if (old_root_bytenr == root->node->start)
408 btrfs_set_root_bytenr(&root->root_item,
410 btrfs_set_root_level(&root->root_item,
411 btrfs_header_level(root->node));
412 ret = btrfs_update_root(trans, tree_root,
416 btrfs_write_dirty_block_groups(trans, root);
421 int btrfs_commit_tree_roots(struct btrfs_trans_handle *trans,
422 struct btrfs_root *root)
424 struct btrfs_fs_info *fs_info = root->fs_info;
425 struct list_head *next;
427 while(!list_empty(&fs_info->dirty_cowonly_roots)) {
428 next = fs_info->dirty_cowonly_roots.next;
430 root = list_entry(next, struct btrfs_root, dirty_list);
431 update_cowonly_root(trans, root);
436 int btrfs_add_dead_root(struct btrfs_root *root, struct btrfs_root *latest)
438 struct btrfs_dirty_root *dirty;
440 dirty = kmalloc(sizeof(*dirty), GFP_NOFS);
444 dirty->latest_root = latest;
446 mutex_lock(&root->fs_info->trans_mutex);
447 list_add(&dirty->list, &latest->fs_info->dead_roots);
448 mutex_unlock(&root->fs_info->trans_mutex);
452 static noinline int add_dirty_roots(struct btrfs_trans_handle *trans,
453 struct radix_tree_root *radix,
454 struct list_head *list)
456 struct btrfs_dirty_root *dirty;
457 struct btrfs_root *gang[8];
458 struct btrfs_root *root;
465 ret = radix_tree_gang_lookup_tag(radix, (void **)gang, 0,
467 BTRFS_ROOT_TRANS_TAG);
470 for (i = 0; i < ret; i++) {
472 radix_tree_tag_clear(radix,
473 (unsigned long)root->root_key.objectid,
474 BTRFS_ROOT_TRANS_TAG);
476 BUG_ON(!root->ref_tree);
477 dirty = root->dirty_root;
479 btrfs_free_log(trans, root);
480 btrfs_free_reloc_root(root);
482 if (root->commit_root == root->node) {
483 WARN_ON(root->node->start !=
484 btrfs_root_bytenr(&root->root_item));
486 free_extent_buffer(root->commit_root);
487 root->commit_root = NULL;
488 root->dirty_root = NULL;
490 spin_lock(&root->list_lock);
491 list_del_init(&dirty->root->dead_list);
492 spin_unlock(&root->list_lock);
497 /* make sure to update the root on disk
498 * so we get any updates to the block used
501 err = btrfs_update_root(trans,
502 root->fs_info->tree_root,
508 memset(&root->root_item.drop_progress, 0,
509 sizeof(struct btrfs_disk_key));
510 root->root_item.drop_level = 0;
511 root->commit_root = NULL;
512 root->dirty_root = NULL;
513 root->root_key.offset = root->fs_info->generation;
514 btrfs_set_root_bytenr(&root->root_item,
516 btrfs_set_root_level(&root->root_item,
517 btrfs_header_level(root->node));
518 err = btrfs_insert_root(trans, root->fs_info->tree_root,
524 refs = btrfs_root_refs(&dirty->root->root_item);
525 btrfs_set_root_refs(&dirty->root->root_item, refs - 1);
526 err = btrfs_update_root(trans, root->fs_info->tree_root,
527 &dirty->root->root_key,
528 &dirty->root->root_item);
532 list_add(&dirty->list, list);
535 free_extent_buffer(dirty->root->node);
544 int btrfs_defrag_root(struct btrfs_root *root, int cacheonly)
546 struct btrfs_fs_info *info = root->fs_info;
548 struct btrfs_trans_handle *trans;
552 if (root->defrag_running)
554 trans = btrfs_start_transaction(root, 1);
556 root->defrag_running = 1;
557 ret = btrfs_defrag_leaves(trans, root, cacheonly);
558 nr = trans->blocks_used;
559 btrfs_end_transaction(trans, root);
560 btrfs_btree_balance_dirty(info->tree_root, nr);
563 trans = btrfs_start_transaction(root, 1);
564 if (root->fs_info->closing || ret != -EAGAIN)
567 root->defrag_running = 0;
569 btrfs_end_transaction(trans, root);
573 static noinline int drop_dirty_roots(struct btrfs_root *tree_root,
574 struct list_head *list)
576 struct btrfs_dirty_root *dirty;
577 struct btrfs_trans_handle *trans;
585 while(!list_empty(list)) {
586 struct btrfs_root *root;
588 dirty = list_entry(list->prev, struct btrfs_dirty_root, list);
589 list_del_init(&dirty->list);
591 num_bytes = btrfs_root_used(&dirty->root->root_item);
592 root = dirty->latest_root;
593 atomic_inc(&root->fs_info->throttles);
596 trans = btrfs_start_transaction(tree_root, 1);
597 mutex_lock(&root->fs_info->drop_mutex);
598 ret = btrfs_drop_snapshot(trans, dirty->root);
599 if (ret != -EAGAIN) {
602 mutex_unlock(&root->fs_info->drop_mutex);
604 err = btrfs_update_root(trans,
606 &dirty->root->root_key,
607 &dirty->root->root_item);
610 nr = trans->blocks_used;
611 ret = btrfs_end_transaction(trans, tree_root);
614 btrfs_btree_balance_dirty(tree_root, nr);
618 atomic_dec(&root->fs_info->throttles);
619 wake_up(&root->fs_info->transaction_throttle);
621 mutex_lock(&root->fs_info->alloc_mutex);
622 num_bytes -= btrfs_root_used(&dirty->root->root_item);
623 bytes_used = btrfs_root_used(&root->root_item);
625 btrfs_record_root_in_trans(root);
626 btrfs_set_root_used(&root->root_item,
627 bytes_used - num_bytes);
629 mutex_unlock(&root->fs_info->alloc_mutex);
631 ret = btrfs_del_root(trans, tree_root, &dirty->root->root_key);
636 mutex_unlock(&root->fs_info->drop_mutex);
638 spin_lock(&root->list_lock);
639 list_del_init(&dirty->root->dead_list);
640 if (!list_empty(&root->dead_list)) {
641 struct btrfs_root *oldest;
642 oldest = list_entry(root->dead_list.prev,
643 struct btrfs_root, dead_list);
644 max_useless = oldest->root_key.offset - 1;
646 max_useless = root->root_key.offset - 1;
648 spin_unlock(&root->list_lock);
650 nr = trans->blocks_used;
651 ret = btrfs_end_transaction(trans, tree_root);
654 ret = btrfs_remove_leaf_refs(root, max_useless, 0);
657 free_extent_buffer(dirty->root->node);
661 btrfs_btree_balance_dirty(tree_root, nr);
667 static noinline int create_pending_snapshot(struct btrfs_trans_handle *trans,
668 struct btrfs_fs_info *fs_info,
669 struct btrfs_pending_snapshot *pending)
671 struct btrfs_key key;
672 struct btrfs_root_item *new_root_item;
673 struct btrfs_root *tree_root = fs_info->tree_root;
674 struct btrfs_root *root = pending->root;
675 struct extent_buffer *tmp;
676 struct extent_buffer *old;
681 new_root_item = kmalloc(sizeof(*new_root_item), GFP_NOFS);
682 if (!new_root_item) {
686 ret = btrfs_find_free_objectid(trans, tree_root, 0, &objectid);
690 memcpy(new_root_item, &root->root_item, sizeof(*new_root_item));
692 key.objectid = objectid;
693 key.offset = trans->transid;
694 btrfs_set_key_type(&key, BTRFS_ROOT_ITEM_KEY);
696 old = btrfs_lock_root_node(root);
697 btrfs_cow_block(trans, root, old, NULL, 0, &old, 0);
699 btrfs_copy_root(trans, root, old, &tmp, objectid);
700 btrfs_tree_unlock(old);
701 free_extent_buffer(old);
703 btrfs_set_root_bytenr(new_root_item, tmp->start);
704 btrfs_set_root_level(new_root_item, btrfs_header_level(tmp));
705 ret = btrfs_insert_root(trans, root->fs_info->tree_root, &key,
707 btrfs_tree_unlock(tmp);
708 free_extent_buffer(tmp);
713 * insert the directory item
715 key.offset = (u64)-1;
716 namelen = strlen(pending->name);
717 ret = btrfs_insert_dir_item(trans, root->fs_info->tree_root,
718 pending->name, namelen,
719 root->fs_info->sb->s_root->d_inode->i_ino,
720 &key, BTRFS_FT_DIR, 0);
725 ret = btrfs_insert_inode_ref(trans, root->fs_info->tree_root,
726 pending->name, strlen(pending->name), objectid,
727 root->fs_info->sb->s_root->d_inode->i_ino, 0);
729 /* Invalidate existing dcache entry for new snapshot. */
730 btrfs_invalidate_dcache_root(root, pending->name, namelen);
733 kfree(new_root_item);
737 static noinline int create_pending_snapshots(struct btrfs_trans_handle *trans,
738 struct btrfs_fs_info *fs_info)
740 struct btrfs_pending_snapshot *pending;
741 struct list_head *head = &trans->transaction->pending_snapshots;
744 while(!list_empty(head)) {
745 pending = list_entry(head->next,
746 struct btrfs_pending_snapshot, list);
747 ret = create_pending_snapshot(trans, fs_info, pending);
749 list_del(&pending->list);
750 kfree(pending->name);
756 int btrfs_commit_transaction(struct btrfs_trans_handle *trans,
757 struct btrfs_root *root)
759 unsigned long joined = 0;
760 unsigned long timeout = 1;
761 struct btrfs_transaction *cur_trans;
762 struct btrfs_transaction *prev_trans = NULL;
763 struct btrfs_root *chunk_root = root->fs_info->chunk_root;
764 struct list_head dirty_fs_roots;
765 struct extent_io_tree *pinned_copy;
769 INIT_LIST_HEAD(&dirty_fs_roots);
770 mutex_lock(&root->fs_info->trans_mutex);
771 if (trans->transaction->in_commit) {
772 cur_trans = trans->transaction;
773 trans->transaction->use_count++;
774 mutex_unlock(&root->fs_info->trans_mutex);
775 btrfs_end_transaction(trans, root);
777 ret = wait_for_commit(root, cur_trans);
780 mutex_lock(&root->fs_info->trans_mutex);
781 put_transaction(cur_trans);
782 mutex_unlock(&root->fs_info->trans_mutex);
787 pinned_copy = kmalloc(sizeof(*pinned_copy), GFP_NOFS);
791 extent_io_tree_init(pinned_copy,
792 root->fs_info->btree_inode->i_mapping, GFP_NOFS);
794 trans->transaction->in_commit = 1;
795 trans->transaction->blocked = 1;
796 cur_trans = trans->transaction;
797 if (cur_trans->list.prev != &root->fs_info->trans_list) {
798 prev_trans = list_entry(cur_trans->list.prev,
799 struct btrfs_transaction, list);
800 if (!prev_trans->commit_done) {
801 prev_trans->use_count++;
802 mutex_unlock(&root->fs_info->trans_mutex);
804 wait_for_commit(root, prev_trans);
806 mutex_lock(&root->fs_info->trans_mutex);
807 put_transaction(prev_trans);
812 int snap_pending = 0;
813 joined = cur_trans->num_joined;
814 if (!list_empty(&trans->transaction->pending_snapshots))
817 WARN_ON(cur_trans != trans->transaction);
818 prepare_to_wait(&cur_trans->writer_wait, &wait,
819 TASK_UNINTERRUPTIBLE);
821 if (cur_trans->num_writers > 1)
822 timeout = MAX_SCHEDULE_TIMEOUT;
826 mutex_unlock(&root->fs_info->trans_mutex);
829 ret = btrfs_wait_ordered_extents(root, 1);
833 schedule_timeout(timeout);
835 mutex_lock(&root->fs_info->trans_mutex);
836 finish_wait(&cur_trans->writer_wait, &wait);
837 } while (cur_trans->num_writers > 1 ||
838 (cur_trans->num_joined != joined));
840 ret = create_pending_snapshots(trans, root->fs_info);
843 WARN_ON(cur_trans != trans->transaction);
845 /* btrfs_commit_tree_roots is responsible for getting the
846 * various roots consistent with each other. Every pointer
847 * in the tree of tree roots has to point to the most up to date
848 * root for every subvolume and other tree. So, we have to keep
849 * the tree logging code from jumping in and changing any
852 * At this point in the commit, there can't be any tree-log
853 * writers, but a little lower down we drop the trans mutex
854 * and let new people in. By holding the tree_log_mutex
855 * from now until after the super is written, we avoid races
856 * with the tree-log code.
858 mutex_lock(&root->fs_info->tree_log_mutex);
860 * keep tree reloc code from adding new reloc trees
862 mutex_lock(&root->fs_info->tree_reloc_mutex);
865 ret = add_dirty_roots(trans, &root->fs_info->fs_roots_radix,
869 /* add_dirty_roots gets rid of all the tree log roots, it is now
870 * safe to free the root of tree log roots
872 btrfs_free_log_root_tree(trans, root->fs_info);
874 btrfs_free_reloc_mappings(root);
876 ret = btrfs_commit_tree_roots(trans, root);
879 cur_trans = root->fs_info->running_transaction;
880 spin_lock(&root->fs_info->new_trans_lock);
881 root->fs_info->running_transaction = NULL;
882 spin_unlock(&root->fs_info->new_trans_lock);
883 btrfs_set_super_generation(&root->fs_info->super_copy,
885 btrfs_set_super_root(&root->fs_info->super_copy,
886 root->fs_info->tree_root->node->start);
887 btrfs_set_super_root_level(&root->fs_info->super_copy,
888 btrfs_header_level(root->fs_info->tree_root->node));
890 btrfs_set_super_chunk_root(&root->fs_info->super_copy,
891 chunk_root->node->start);
892 btrfs_set_super_chunk_root_level(&root->fs_info->super_copy,
893 btrfs_header_level(chunk_root->node));
895 if (!root->fs_info->log_root_recovering) {
896 btrfs_set_super_log_root(&root->fs_info->super_copy, 0);
897 btrfs_set_super_log_root_level(&root->fs_info->super_copy, 0);
900 memcpy(&root->fs_info->super_for_commit, &root->fs_info->super_copy,
901 sizeof(root->fs_info->super_copy));
903 btrfs_copy_pinned(root, pinned_copy);
905 trans->transaction->blocked = 0;
906 wake_up(&root->fs_info->transaction_throttle);
907 wake_up(&root->fs_info->transaction_wait);
909 mutex_unlock(&root->fs_info->trans_mutex);
910 ret = btrfs_write_and_wait_transaction(trans, root);
912 write_ctree_super(trans, root);
915 * the super is written, we can safely allow the tree-loggers
916 * to go about their business
918 mutex_unlock(&root->fs_info->tree_log_mutex);
920 btrfs_finish_extent_commit(trans, root, pinned_copy);
923 btrfs_drop_dead_reloc_roots(root);
924 mutex_unlock(&root->fs_info->tree_reloc_mutex);
926 mutex_lock(&root->fs_info->trans_mutex);
928 cur_trans->commit_done = 1;
929 root->fs_info->last_trans_committed = cur_trans->transid;
930 wake_up(&cur_trans->commit_wait);
931 put_transaction(cur_trans);
932 put_transaction(cur_trans);
934 list_splice_init(&dirty_fs_roots, &root->fs_info->dead_roots);
935 if (root->fs_info->closing)
936 list_splice_init(&root->fs_info->dead_roots, &dirty_fs_roots);
938 mutex_unlock(&root->fs_info->trans_mutex);
939 kmem_cache_free(btrfs_trans_handle_cachep, trans);
941 if (root->fs_info->closing) {
942 drop_dirty_roots(root->fs_info->tree_root, &dirty_fs_roots);
947 int btrfs_clean_old_snapshots(struct btrfs_root *root)
949 struct list_head dirty_roots;
950 INIT_LIST_HEAD(&dirty_roots);
952 mutex_lock(&root->fs_info->trans_mutex);
953 list_splice_init(&root->fs_info->dead_roots, &dirty_roots);
954 mutex_unlock(&root->fs_info->trans_mutex);
956 if (!list_empty(&dirty_roots)) {
957 drop_dirty_roots(root, &dirty_roots);