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);
50 * either allocate a new transaction or hop into the existing one
52 static noinline int join_transaction(struct btrfs_root *root)
54 struct btrfs_transaction *cur_trans;
55 cur_trans = root->fs_info->running_transaction;
57 cur_trans = kmem_cache_alloc(btrfs_transaction_cachep,
61 root->fs_info->generation++;
62 root->fs_info->last_alloc = 0;
63 root->fs_info->last_data_alloc = 0;
64 cur_trans->num_writers = 1;
65 cur_trans->num_joined = 0;
66 cur_trans->transid = root->fs_info->generation;
67 init_waitqueue_head(&cur_trans->writer_wait);
68 init_waitqueue_head(&cur_trans->commit_wait);
69 cur_trans->in_commit = 0;
70 cur_trans->blocked = 0;
71 cur_trans->use_count = 1;
72 cur_trans->commit_done = 0;
73 cur_trans->start_time = get_seconds();
74 INIT_LIST_HEAD(&cur_trans->pending_snapshots);
75 list_add_tail(&cur_trans->list, &root->fs_info->trans_list);
76 extent_io_tree_init(&cur_trans->dirty_pages,
77 root->fs_info->btree_inode->i_mapping,
79 spin_lock(&root->fs_info->new_trans_lock);
80 root->fs_info->running_transaction = cur_trans;
81 spin_unlock(&root->fs_info->new_trans_lock);
83 cur_trans->num_writers++;
84 cur_trans->num_joined++;
91 * this does all the record keeping required to make sure that a
92 * reference counted root is properly recorded in a given transaction.
93 * This is required to make sure the old root from before we joined the transaction
94 * is deleted when the transaction commits
96 noinline int btrfs_record_root_in_trans(struct btrfs_root *root)
98 struct btrfs_dirty_root *dirty;
99 u64 running_trans_id = root->fs_info->running_transaction->transid;
100 if (root->ref_cows && root->last_trans < running_trans_id) {
101 WARN_ON(root == root->fs_info->extent_root);
102 if (root->root_item.refs != 0) {
103 radix_tree_tag_set(&root->fs_info->fs_roots_radix,
104 (unsigned long)root->root_key.objectid,
105 BTRFS_ROOT_TRANS_TAG);
107 dirty = kmalloc(sizeof(*dirty), GFP_NOFS);
109 dirty->root = kmalloc(sizeof(*dirty->root), GFP_NOFS);
110 BUG_ON(!dirty->root);
111 dirty->latest_root = root;
112 INIT_LIST_HEAD(&dirty->list);
114 root->commit_root = btrfs_root_node(root);
116 memcpy(dirty->root, root, sizeof(*root));
117 spin_lock_init(&dirty->root->node_lock);
118 spin_lock_init(&dirty->root->list_lock);
119 mutex_init(&dirty->root->objectid_mutex);
120 mutex_init(&dirty->root->log_mutex);
121 INIT_LIST_HEAD(&dirty->root->dead_list);
122 dirty->root->node = root->commit_root;
123 dirty->root->commit_root = NULL;
125 spin_lock(&root->list_lock);
126 list_add(&dirty->root->dead_list, &root->dead_list);
127 spin_unlock(&root->list_lock);
129 root->dirty_root = dirty;
133 root->last_trans = running_trans_id;
138 /* wait for commit against the current transaction to become unblocked
139 * when this is done, it is safe to start a new transaction, but the current
140 * transaction might not be fully on disk.
142 static void wait_current_trans(struct btrfs_root *root)
144 struct btrfs_transaction *cur_trans;
146 cur_trans = root->fs_info->running_transaction;
147 if (cur_trans && cur_trans->blocked) {
149 cur_trans->use_count++;
151 prepare_to_wait(&root->fs_info->transaction_wait, &wait,
152 TASK_UNINTERRUPTIBLE);
153 if (cur_trans->blocked) {
154 mutex_unlock(&root->fs_info->trans_mutex);
156 mutex_lock(&root->fs_info->trans_mutex);
157 finish_wait(&root->fs_info->transaction_wait,
160 finish_wait(&root->fs_info->transaction_wait,
165 put_transaction(cur_trans);
169 static struct btrfs_trans_handle *start_transaction(struct btrfs_root *root,
170 int num_blocks, int wait)
172 struct btrfs_trans_handle *h =
173 kmem_cache_alloc(btrfs_trans_handle_cachep, GFP_NOFS);
176 mutex_lock(&root->fs_info->trans_mutex);
177 if (!root->fs_info->log_root_recovering &&
178 ((wait == 1 && !root->fs_info->open_ioctl_trans) || wait == 2))
179 wait_current_trans(root);
180 ret = join_transaction(root);
183 btrfs_record_root_in_trans(root);
184 h->transid = root->fs_info->running_transaction->transid;
185 h->transaction = root->fs_info->running_transaction;
186 h->blocks_reserved = num_blocks;
188 h->block_group = NULL;
189 h->alloc_exclude_nr = 0;
190 h->alloc_exclude_start = 0;
191 root->fs_info->running_transaction->use_count++;
192 mutex_unlock(&root->fs_info->trans_mutex);
196 struct btrfs_trans_handle *btrfs_start_transaction(struct btrfs_root *root,
199 return start_transaction(root, num_blocks, 1);
201 struct btrfs_trans_handle *btrfs_join_transaction(struct btrfs_root *root,
204 return start_transaction(root, num_blocks, 0);
207 struct btrfs_trans_handle *btrfs_start_ioctl_transaction(struct btrfs_root *r,
210 return start_transaction(r, num_blocks, 2);
213 /* wait for a transaction commit to be fully complete */
214 static noinline int wait_for_commit(struct btrfs_root *root,
215 struct btrfs_transaction *commit)
218 mutex_lock(&root->fs_info->trans_mutex);
219 while(!commit->commit_done) {
220 prepare_to_wait(&commit->commit_wait, &wait,
221 TASK_UNINTERRUPTIBLE);
222 if (commit->commit_done)
224 mutex_unlock(&root->fs_info->trans_mutex);
226 mutex_lock(&root->fs_info->trans_mutex);
228 mutex_unlock(&root->fs_info->trans_mutex);
229 finish_wait(&commit->commit_wait, &wait);
234 * rate limit against the drop_snapshot code. This helps to slow down new operations
235 * if the drop_snapshot code isn't able to keep up.
237 static void throttle_on_drops(struct btrfs_root *root)
239 struct btrfs_fs_info *info = root->fs_info;
240 int harder_count = 0;
243 if (atomic_read(&info->throttles)) {
246 thr = atomic_read(&info->throttle_gen);
249 prepare_to_wait(&info->transaction_throttle,
250 &wait, TASK_UNINTERRUPTIBLE);
251 if (!atomic_read(&info->throttles)) {
252 finish_wait(&info->transaction_throttle, &wait);
256 finish_wait(&info->transaction_throttle, &wait);
257 } while (thr == atomic_read(&info->throttle_gen));
260 if (root->fs_info->total_ref_cache_size > 1 * 1024 * 1024 &&
264 if (root->fs_info->total_ref_cache_size > 5 * 1024 * 1024 &&
268 if (root->fs_info->total_ref_cache_size > 10 * 1024 * 1024 &&
274 void btrfs_throttle(struct btrfs_root *root)
276 mutex_lock(&root->fs_info->trans_mutex);
277 if (!root->fs_info->open_ioctl_trans)
278 wait_current_trans(root);
279 mutex_unlock(&root->fs_info->trans_mutex);
281 throttle_on_drops(root);
284 static int __btrfs_end_transaction(struct btrfs_trans_handle *trans,
285 struct btrfs_root *root, int throttle)
287 struct btrfs_transaction *cur_trans;
288 struct btrfs_fs_info *info = root->fs_info;
290 mutex_lock(&info->trans_mutex);
291 cur_trans = info->running_transaction;
292 WARN_ON(cur_trans != trans->transaction);
293 WARN_ON(cur_trans->num_writers < 1);
294 cur_trans->num_writers--;
296 if (waitqueue_active(&cur_trans->writer_wait))
297 wake_up(&cur_trans->writer_wait);
298 put_transaction(cur_trans);
299 mutex_unlock(&info->trans_mutex);
300 memset(trans, 0, sizeof(*trans));
301 kmem_cache_free(btrfs_trans_handle_cachep, trans);
304 throttle_on_drops(root);
309 int btrfs_end_transaction(struct btrfs_trans_handle *trans,
310 struct btrfs_root *root)
312 return __btrfs_end_transaction(trans, root, 0);
315 int btrfs_end_transaction_throttle(struct btrfs_trans_handle *trans,
316 struct btrfs_root *root)
318 return __btrfs_end_transaction(trans, root, 1);
322 * when btree blocks are allocated, they have some corresponding bits set for
323 * them in one of two extent_io trees. This is used to make sure all of
324 * those extents are on disk for transaction or log commit
326 int btrfs_write_and_wait_marked_extents(struct btrfs_root *root,
327 struct extent_io_tree *dirty_pages)
333 struct inode *btree_inode = root->fs_info->btree_inode;
339 ret = find_first_extent_bit(dirty_pages, start, &start, &end,
343 while(start <= end) {
346 index = start >> PAGE_CACHE_SHIFT;
347 start = (u64)(index + 1) << PAGE_CACHE_SHIFT;
348 page = find_get_page(btree_inode->i_mapping, index);
352 btree_lock_page_hook(page);
353 if (!page->mapping) {
355 page_cache_release(page);
359 if (PageWriteback(page)) {
361 wait_on_page_writeback(page);
364 page_cache_release(page);
368 err = write_one_page(page, 0);
371 page_cache_release(page);
375 ret = find_first_extent_bit(dirty_pages, 0, &start, &end,
380 clear_extent_dirty(dirty_pages, start, end, GFP_NOFS);
381 while(start <= end) {
382 index = start >> PAGE_CACHE_SHIFT;
383 start = (u64)(index + 1) << PAGE_CACHE_SHIFT;
384 page = find_get_page(btree_inode->i_mapping, index);
387 if (PageDirty(page)) {
388 btree_lock_page_hook(page);
389 wait_on_page_writeback(page);
390 err = write_one_page(page, 0);
394 wait_on_page_writeback(page);
395 page_cache_release(page);
404 int btrfs_write_and_wait_transaction(struct btrfs_trans_handle *trans,
405 struct btrfs_root *root)
407 if (!trans || !trans->transaction) {
408 struct inode *btree_inode;
409 btree_inode = root->fs_info->btree_inode;
410 return filemap_write_and_wait(btree_inode->i_mapping);
412 return btrfs_write_and_wait_marked_extents(root,
413 &trans->transaction->dirty_pages);
417 * this is used to update the root pointer in the tree of tree roots.
419 * But, in the case of the extent allocation tree, updating the root
420 * pointer may allocate blocks which may change the root of the extent
423 * So, this loops and repeats and makes sure the cowonly root didn't
424 * change while the root pointer was being updated in the metadata.
426 static int update_cowonly_root(struct btrfs_trans_handle *trans,
427 struct btrfs_root *root)
431 struct btrfs_root *tree_root = root->fs_info->tree_root;
433 btrfs_extent_post_op(trans, root);
434 btrfs_write_dirty_block_groups(trans, root);
435 btrfs_extent_post_op(trans, root);
438 old_root_bytenr = btrfs_root_bytenr(&root->root_item);
439 if (old_root_bytenr == root->node->start)
441 btrfs_set_root_bytenr(&root->root_item,
443 btrfs_set_root_level(&root->root_item,
444 btrfs_header_level(root->node));
445 btrfs_set_root_generation(&root->root_item, trans->transid);
447 btrfs_extent_post_op(trans, root);
449 ret = btrfs_update_root(trans, tree_root,
453 btrfs_write_dirty_block_groups(trans, root);
454 btrfs_extent_post_op(trans, root);
460 * update all the cowonly tree roots on disk
462 int btrfs_commit_tree_roots(struct btrfs_trans_handle *trans,
463 struct btrfs_root *root)
465 struct btrfs_fs_info *fs_info = root->fs_info;
466 struct list_head *next;
467 struct extent_buffer *eb;
469 btrfs_extent_post_op(trans, fs_info->tree_root);
471 eb = btrfs_lock_root_node(fs_info->tree_root);
472 btrfs_cow_block(trans, fs_info->tree_root, eb, NULL, 0, &eb, 0);
473 btrfs_tree_unlock(eb);
474 free_extent_buffer(eb);
476 btrfs_extent_post_op(trans, fs_info->tree_root);
478 while(!list_empty(&fs_info->dirty_cowonly_roots)) {
479 next = fs_info->dirty_cowonly_roots.next;
481 root = list_entry(next, struct btrfs_root, dirty_list);
483 update_cowonly_root(trans, root);
489 * dead roots are old snapshots that need to be deleted. This allocates
490 * a dirty root struct and adds it into the list of dead roots that need to
493 int btrfs_add_dead_root(struct btrfs_root *root, struct btrfs_root *latest)
495 struct btrfs_dirty_root *dirty;
497 dirty = kmalloc(sizeof(*dirty), GFP_NOFS);
501 dirty->latest_root = latest;
503 mutex_lock(&root->fs_info->trans_mutex);
504 list_add(&dirty->list, &latest->fs_info->dead_roots);
505 mutex_unlock(&root->fs_info->trans_mutex);
510 * at transaction commit time we need to schedule the old roots for
511 * deletion via btrfs_drop_snapshot. This runs through all the
512 * reference counted roots that were modified in the current
513 * transaction and puts them into the drop list
515 static noinline int add_dirty_roots(struct btrfs_trans_handle *trans,
516 struct radix_tree_root *radix,
517 struct list_head *list)
519 struct btrfs_dirty_root *dirty;
520 struct btrfs_root *gang[8];
521 struct btrfs_root *root;
528 ret = radix_tree_gang_lookup_tag(radix, (void **)gang, 0,
530 BTRFS_ROOT_TRANS_TAG);
533 for (i = 0; i < ret; i++) {
535 radix_tree_tag_clear(radix,
536 (unsigned long)root->root_key.objectid,
537 BTRFS_ROOT_TRANS_TAG);
539 BUG_ON(!root->ref_tree);
540 dirty = root->dirty_root;
542 btrfs_free_log(trans, root);
543 btrfs_free_reloc_root(trans, root);
545 if (root->commit_root == root->node) {
546 WARN_ON(root->node->start !=
547 btrfs_root_bytenr(&root->root_item));
549 free_extent_buffer(root->commit_root);
550 root->commit_root = NULL;
551 root->dirty_root = NULL;
553 spin_lock(&root->list_lock);
554 list_del_init(&dirty->root->dead_list);
555 spin_unlock(&root->list_lock);
560 /* make sure to update the root on disk
561 * so we get any updates to the block used
564 err = btrfs_update_root(trans,
565 root->fs_info->tree_root,
571 memset(&root->root_item.drop_progress, 0,
572 sizeof(struct btrfs_disk_key));
573 root->root_item.drop_level = 0;
574 root->commit_root = NULL;
575 root->dirty_root = NULL;
576 root->root_key.offset = root->fs_info->generation;
577 btrfs_set_root_bytenr(&root->root_item,
579 btrfs_set_root_level(&root->root_item,
580 btrfs_header_level(root->node));
581 btrfs_set_root_generation(&root->root_item,
582 root->root_key.offset);
584 err = btrfs_insert_root(trans, root->fs_info->tree_root,
590 refs = btrfs_root_refs(&dirty->root->root_item);
591 btrfs_set_root_refs(&dirty->root->root_item, refs - 1);
592 err = btrfs_update_root(trans, root->fs_info->tree_root,
593 &dirty->root->root_key,
594 &dirty->root->root_item);
598 list_add(&dirty->list, list);
601 free_extent_buffer(dirty->root->node);
611 * defrag a given btree. If cacheonly == 1, this won't read from the disk,
612 * otherwise every leaf in the btree is read and defragged.
614 int btrfs_defrag_root(struct btrfs_root *root, int cacheonly)
616 struct btrfs_fs_info *info = root->fs_info;
618 struct btrfs_trans_handle *trans;
622 if (root->defrag_running)
624 trans = btrfs_start_transaction(root, 1);
626 root->defrag_running = 1;
627 ret = btrfs_defrag_leaves(trans, root, cacheonly);
628 nr = trans->blocks_used;
629 btrfs_end_transaction(trans, root);
630 btrfs_btree_balance_dirty(info->tree_root, nr);
633 trans = btrfs_start_transaction(root, 1);
634 if (root->fs_info->closing || ret != -EAGAIN)
637 root->defrag_running = 0;
639 btrfs_end_transaction(trans, root);
644 * Given a list of roots that need to be deleted, call btrfs_drop_snapshot on
647 static noinline int drop_dirty_roots(struct btrfs_root *tree_root,
648 struct list_head *list)
650 struct btrfs_dirty_root *dirty;
651 struct btrfs_trans_handle *trans;
659 while(!list_empty(list)) {
660 struct btrfs_root *root;
662 dirty = list_entry(list->prev, struct btrfs_dirty_root, list);
663 list_del_init(&dirty->list);
665 num_bytes = btrfs_root_used(&dirty->root->root_item);
666 root = dirty->latest_root;
667 atomic_inc(&root->fs_info->throttles);
670 trans = btrfs_start_transaction(tree_root, 1);
671 mutex_lock(&root->fs_info->drop_mutex);
672 ret = btrfs_drop_snapshot(trans, dirty->root);
673 if (ret != -EAGAIN) {
676 mutex_unlock(&root->fs_info->drop_mutex);
678 err = btrfs_update_root(trans,
680 &dirty->root->root_key,
681 &dirty->root->root_item);
684 nr = trans->blocks_used;
685 ret = btrfs_end_transaction(trans, tree_root);
688 btrfs_btree_balance_dirty(tree_root, nr);
692 atomic_dec(&root->fs_info->throttles);
693 wake_up(&root->fs_info->transaction_throttle);
695 num_bytes -= btrfs_root_used(&dirty->root->root_item);
696 bytes_used = btrfs_root_used(&root->root_item);
698 btrfs_record_root_in_trans(root);
699 btrfs_set_root_used(&root->root_item,
700 bytes_used - num_bytes);
703 ret = btrfs_del_root(trans, tree_root, &dirty->root->root_key);
708 mutex_unlock(&root->fs_info->drop_mutex);
710 spin_lock(&root->list_lock);
711 list_del_init(&dirty->root->dead_list);
712 if (!list_empty(&root->dead_list)) {
713 struct btrfs_root *oldest;
714 oldest = list_entry(root->dead_list.prev,
715 struct btrfs_root, dead_list);
716 max_useless = oldest->root_key.offset - 1;
718 max_useless = root->root_key.offset - 1;
720 spin_unlock(&root->list_lock);
722 nr = trans->blocks_used;
723 ret = btrfs_end_transaction(trans, tree_root);
726 ret = btrfs_remove_leaf_refs(root, max_useless, 0);
729 free_extent_buffer(dirty->root->node);
733 btrfs_btree_balance_dirty(tree_root, nr);
740 * new snapshots need to be created at a very specific time in the
741 * transaction commit. This does the actual creation
743 static noinline int create_pending_snapshot(struct btrfs_trans_handle *trans,
744 struct btrfs_fs_info *fs_info,
745 struct btrfs_pending_snapshot *pending)
747 struct btrfs_key key;
748 struct btrfs_root_item *new_root_item;
749 struct btrfs_root *tree_root = fs_info->tree_root;
750 struct btrfs_root *root = pending->root;
751 struct extent_buffer *tmp;
752 struct extent_buffer *old;
757 new_root_item = kmalloc(sizeof(*new_root_item), GFP_NOFS);
758 if (!new_root_item) {
762 ret = btrfs_find_free_objectid(trans, tree_root, 0, &objectid);
766 memcpy(new_root_item, &root->root_item, sizeof(*new_root_item));
768 key.objectid = objectid;
769 key.offset = trans->transid;
770 btrfs_set_key_type(&key, BTRFS_ROOT_ITEM_KEY);
772 old = btrfs_lock_root_node(root);
773 btrfs_cow_block(trans, root, old, NULL, 0, &old, 0);
775 btrfs_copy_root(trans, root, old, &tmp, objectid);
776 btrfs_tree_unlock(old);
777 free_extent_buffer(old);
779 btrfs_set_root_bytenr(new_root_item, tmp->start);
780 btrfs_set_root_level(new_root_item, btrfs_header_level(tmp));
781 btrfs_set_root_generation(new_root_item, trans->transid);
782 ret = btrfs_insert_root(trans, root->fs_info->tree_root, &key,
784 btrfs_tree_unlock(tmp);
785 free_extent_buffer(tmp);
790 * insert the directory item
792 key.offset = (u64)-1;
793 namelen = strlen(pending->name);
794 ret = btrfs_insert_dir_item(trans, root->fs_info->tree_root,
795 pending->name, namelen,
796 root->fs_info->sb->s_root->d_inode->i_ino,
797 &key, BTRFS_FT_DIR, 0);
802 ret = btrfs_insert_inode_ref(trans, root->fs_info->tree_root,
803 pending->name, strlen(pending->name), objectid,
804 root->fs_info->sb->s_root->d_inode->i_ino, 0);
806 /* Invalidate existing dcache entry for new snapshot. */
807 btrfs_invalidate_dcache_root(root, pending->name, namelen);
810 kfree(new_root_item);
815 * create all the snapshots we've scheduled for creation
817 static noinline int create_pending_snapshots(struct btrfs_trans_handle *trans,
818 struct btrfs_fs_info *fs_info)
820 struct btrfs_pending_snapshot *pending;
821 struct list_head *head = &trans->transaction->pending_snapshots;
824 while(!list_empty(head)) {
825 pending = list_entry(head->next,
826 struct btrfs_pending_snapshot, list);
827 ret = create_pending_snapshot(trans, fs_info, pending);
829 list_del(&pending->list);
830 kfree(pending->name);
836 int btrfs_commit_transaction(struct btrfs_trans_handle *trans,
837 struct btrfs_root *root)
839 unsigned long joined = 0;
840 unsigned long timeout = 1;
841 struct btrfs_transaction *cur_trans;
842 struct btrfs_transaction *prev_trans = NULL;
843 struct btrfs_root *chunk_root = root->fs_info->chunk_root;
844 struct list_head dirty_fs_roots;
845 struct extent_io_tree *pinned_copy;
849 INIT_LIST_HEAD(&dirty_fs_roots);
850 mutex_lock(&root->fs_info->trans_mutex);
851 if (trans->transaction->in_commit) {
852 cur_trans = trans->transaction;
853 trans->transaction->use_count++;
854 mutex_unlock(&root->fs_info->trans_mutex);
855 btrfs_end_transaction(trans, root);
857 ret = wait_for_commit(root, cur_trans);
860 mutex_lock(&root->fs_info->trans_mutex);
861 put_transaction(cur_trans);
862 mutex_unlock(&root->fs_info->trans_mutex);
867 pinned_copy = kmalloc(sizeof(*pinned_copy), GFP_NOFS);
871 extent_io_tree_init(pinned_copy,
872 root->fs_info->btree_inode->i_mapping, GFP_NOFS);
874 trans->transaction->in_commit = 1;
875 trans->transaction->blocked = 1;
876 cur_trans = trans->transaction;
877 if (cur_trans->list.prev != &root->fs_info->trans_list) {
878 prev_trans = list_entry(cur_trans->list.prev,
879 struct btrfs_transaction, list);
880 if (!prev_trans->commit_done) {
881 prev_trans->use_count++;
882 mutex_unlock(&root->fs_info->trans_mutex);
884 wait_for_commit(root, prev_trans);
886 mutex_lock(&root->fs_info->trans_mutex);
887 put_transaction(prev_trans);
892 int snap_pending = 0;
893 joined = cur_trans->num_joined;
894 if (!list_empty(&trans->transaction->pending_snapshots))
897 WARN_ON(cur_trans != trans->transaction);
898 prepare_to_wait(&cur_trans->writer_wait, &wait,
899 TASK_UNINTERRUPTIBLE);
901 if (cur_trans->num_writers > 1)
902 timeout = MAX_SCHEDULE_TIMEOUT;
906 mutex_unlock(&root->fs_info->trans_mutex);
909 ret = btrfs_wait_ordered_extents(root, 1);
913 schedule_timeout(timeout);
915 mutex_lock(&root->fs_info->trans_mutex);
916 finish_wait(&cur_trans->writer_wait, &wait);
917 } while (cur_trans->num_writers > 1 ||
918 (cur_trans->num_joined != joined));
920 ret = create_pending_snapshots(trans, root->fs_info);
923 WARN_ON(cur_trans != trans->transaction);
925 /* btrfs_commit_tree_roots is responsible for getting the
926 * various roots consistent with each other. Every pointer
927 * in the tree of tree roots has to point to the most up to date
928 * root for every subvolume and other tree. So, we have to keep
929 * the tree logging code from jumping in and changing any
932 * At this point in the commit, there can't be any tree-log
933 * writers, but a little lower down we drop the trans mutex
934 * and let new people in. By holding the tree_log_mutex
935 * from now until after the super is written, we avoid races
936 * with the tree-log code.
938 mutex_lock(&root->fs_info->tree_log_mutex);
940 * keep tree reloc code from adding new reloc trees
942 mutex_lock(&root->fs_info->tree_reloc_mutex);
945 ret = add_dirty_roots(trans, &root->fs_info->fs_roots_radix,
949 /* add_dirty_roots gets rid of all the tree log roots, it is now
950 * safe to free the root of tree log roots
952 btrfs_free_log_root_tree(trans, root->fs_info);
954 ret = btrfs_commit_tree_roots(trans, root);
957 cur_trans = root->fs_info->running_transaction;
958 spin_lock(&root->fs_info->new_trans_lock);
959 root->fs_info->running_transaction = NULL;
960 spin_unlock(&root->fs_info->new_trans_lock);
961 btrfs_set_super_generation(&root->fs_info->super_copy,
963 btrfs_set_super_root(&root->fs_info->super_copy,
964 root->fs_info->tree_root->node->start);
965 btrfs_set_super_root_level(&root->fs_info->super_copy,
966 btrfs_header_level(root->fs_info->tree_root->node));
968 btrfs_set_super_chunk_root(&root->fs_info->super_copy,
969 chunk_root->node->start);
970 btrfs_set_super_chunk_root_level(&root->fs_info->super_copy,
971 btrfs_header_level(chunk_root->node));
972 btrfs_set_super_chunk_root_generation(&root->fs_info->super_copy,
973 btrfs_header_generation(chunk_root->node));
975 if (!root->fs_info->log_root_recovering) {
976 btrfs_set_super_log_root(&root->fs_info->super_copy, 0);
977 btrfs_set_super_log_root_level(&root->fs_info->super_copy, 0);
980 memcpy(&root->fs_info->super_for_commit, &root->fs_info->super_copy,
981 sizeof(root->fs_info->super_copy));
983 btrfs_copy_pinned(root, pinned_copy);
985 trans->transaction->blocked = 0;
986 wake_up(&root->fs_info->transaction_throttle);
987 wake_up(&root->fs_info->transaction_wait);
989 mutex_unlock(&root->fs_info->trans_mutex);
990 ret = btrfs_write_and_wait_transaction(trans, root);
992 write_ctree_super(trans, root);
995 * the super is written, we can safely allow the tree-loggers
996 * to go about their business
998 mutex_unlock(&root->fs_info->tree_log_mutex);
1000 btrfs_finish_extent_commit(trans, root, pinned_copy);
1003 btrfs_drop_dead_reloc_roots(root);
1004 mutex_unlock(&root->fs_info->tree_reloc_mutex);
1006 mutex_lock(&root->fs_info->trans_mutex);
1008 cur_trans->commit_done = 1;
1009 root->fs_info->last_trans_committed = cur_trans->transid;
1010 wake_up(&cur_trans->commit_wait);
1011 put_transaction(cur_trans);
1012 put_transaction(cur_trans);
1014 list_splice_init(&dirty_fs_roots, &root->fs_info->dead_roots);
1015 if (root->fs_info->closing)
1016 list_splice_init(&root->fs_info->dead_roots, &dirty_fs_roots);
1018 mutex_unlock(&root->fs_info->trans_mutex);
1019 kmem_cache_free(btrfs_trans_handle_cachep, trans);
1021 if (root->fs_info->closing) {
1022 drop_dirty_roots(root->fs_info->tree_root, &dirty_fs_roots);
1028 * interface function to delete all the snapshots we have scheduled for deletion
1030 int btrfs_clean_old_snapshots(struct btrfs_root *root)
1032 struct list_head dirty_roots;
1033 INIT_LIST_HEAD(&dirty_roots);
1035 mutex_lock(&root->fs_info->trans_mutex);
1036 list_splice_init(&root->fs_info->dead_roots, &dirty_roots);
1037 mutex_unlock(&root->fs_info->trans_mutex);
1039 if (!list_empty(&dirty_roots)) {
1040 drop_dirty_roots(root, &dirty_roots);