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/slab.h>
21 #include <linux/sched.h>
22 #include <linux/writeback.h>
23 #include <linux/pagemap.h>
24 #include <linux/blkdev.h>
27 #include "transaction.h"
31 #define BTRFS_ROOT_TRANS_TAG 0
33 static noinline void put_transaction(struct btrfs_transaction *transaction)
35 WARN_ON(atomic_read(&transaction->use_count) == 0);
36 if (atomic_dec_and_test(&transaction->use_count)) {
37 memset(transaction, 0, sizeof(*transaction));
38 kmem_cache_free(btrfs_transaction_cachep, transaction);
42 static noinline void switch_commit_root(struct btrfs_root *root)
44 free_extent_buffer(root->commit_root);
45 root->commit_root = btrfs_root_node(root);
49 * either allocate a new transaction or hop into the existing one
51 static noinline int join_transaction(struct btrfs_root *root)
53 struct btrfs_transaction *cur_trans;
54 cur_trans = root->fs_info->running_transaction;
56 cur_trans = kmem_cache_alloc(btrfs_transaction_cachep,
60 root->fs_info->generation++;
61 atomic_set(&cur_trans->num_writers, 1);
62 cur_trans->num_joined = 0;
63 cur_trans->transid = root->fs_info->generation;
64 init_waitqueue_head(&cur_trans->writer_wait);
65 init_waitqueue_head(&cur_trans->commit_wait);
66 cur_trans->in_commit = 0;
67 cur_trans->blocked = 0;
68 atomic_set(&cur_trans->use_count, 1);
69 cur_trans->commit_done = 0;
70 cur_trans->start_time = get_seconds();
72 cur_trans->delayed_refs.root = RB_ROOT;
73 cur_trans->delayed_refs.num_entries = 0;
74 cur_trans->delayed_refs.num_heads_ready = 0;
75 cur_trans->delayed_refs.num_heads = 0;
76 cur_trans->delayed_refs.flushing = 0;
77 cur_trans->delayed_refs.run_delayed_start = 0;
78 spin_lock_init(&cur_trans->delayed_refs.lock);
80 INIT_LIST_HEAD(&cur_trans->pending_snapshots);
81 list_add_tail(&cur_trans->list, &root->fs_info->trans_list);
82 extent_io_tree_init(&cur_trans->dirty_pages,
83 root->fs_info->btree_inode->i_mapping);
84 spin_lock(&root->fs_info->new_trans_lock);
85 root->fs_info->running_transaction = cur_trans;
86 spin_unlock(&root->fs_info->new_trans_lock);
88 atomic_inc(&cur_trans->num_writers);
89 cur_trans->num_joined++;
96 * this does all the record keeping required to make sure that a reference
97 * counted root is properly recorded in a given transaction. This is required
98 * to make sure the old root from before we joined the transaction is deleted
99 * when the transaction commits
101 static noinline int record_root_in_trans(struct btrfs_trans_handle *trans,
102 struct btrfs_root *root)
104 if (root->ref_cows && root->last_trans < trans->transid) {
105 WARN_ON(root == root->fs_info->extent_root);
106 WARN_ON(root->commit_root != root->node);
108 radix_tree_tag_set(&root->fs_info->fs_roots_radix,
109 (unsigned long)root->root_key.objectid,
110 BTRFS_ROOT_TRANS_TAG);
111 root->last_trans = trans->transid;
112 btrfs_init_reloc_root(trans, root);
117 int btrfs_record_root_in_trans(struct btrfs_trans_handle *trans,
118 struct btrfs_root *root)
123 mutex_lock(&root->fs_info->trans_mutex);
124 if (root->last_trans == trans->transid) {
125 mutex_unlock(&root->fs_info->trans_mutex);
129 record_root_in_trans(trans, root);
130 mutex_unlock(&root->fs_info->trans_mutex);
134 /* wait for commit against the current transaction to become unblocked
135 * when this is done, it is safe to start a new transaction, but the current
136 * transaction might not be fully on disk.
138 static void wait_current_trans(struct btrfs_root *root)
140 struct btrfs_transaction *cur_trans;
142 cur_trans = root->fs_info->running_transaction;
143 if (cur_trans && cur_trans->blocked) {
145 atomic_inc(&cur_trans->use_count);
147 prepare_to_wait(&root->fs_info->transaction_wait, &wait,
148 TASK_UNINTERRUPTIBLE);
149 if (!cur_trans->blocked)
151 mutex_unlock(&root->fs_info->trans_mutex);
153 mutex_lock(&root->fs_info->trans_mutex);
155 finish_wait(&root->fs_info->transaction_wait, &wait);
156 put_transaction(cur_trans);
160 enum btrfs_trans_type {
167 static int may_wait_transaction(struct btrfs_root *root, int type)
169 if (!root->fs_info->log_root_recovering &&
170 ((type == TRANS_START && !root->fs_info->open_ioctl_trans) ||
171 type == TRANS_USERSPACE))
176 static struct btrfs_trans_handle *start_transaction(struct btrfs_root *root,
177 u64 num_items, int type)
179 struct btrfs_trans_handle *h;
180 struct btrfs_transaction *cur_trans;
184 if (root->fs_info->fs_state & BTRFS_SUPER_FLAG_ERROR)
185 return ERR_PTR(-EROFS);
187 h = kmem_cache_alloc(btrfs_trans_handle_cachep, GFP_NOFS);
189 return ERR_PTR(-ENOMEM);
191 if (type != TRANS_JOIN_NOLOCK)
192 mutex_lock(&root->fs_info->trans_mutex);
193 if (may_wait_transaction(root, type))
194 wait_current_trans(root);
196 ret = join_transaction(root);
198 kmem_cache_free(btrfs_trans_handle_cachep, h);
199 if (type != TRANS_JOIN_NOLOCK)
200 mutex_unlock(&root->fs_info->trans_mutex);
204 cur_trans = root->fs_info->running_transaction;
205 atomic_inc(&cur_trans->use_count);
206 if (type != TRANS_JOIN_NOLOCK)
207 mutex_unlock(&root->fs_info->trans_mutex);
209 h->transid = cur_trans->transid;
210 h->transaction = cur_trans;
213 h->bytes_reserved = 0;
214 h->delayed_ref_updates = 0;
218 if (cur_trans->blocked && may_wait_transaction(root, type)) {
219 btrfs_commit_transaction(h, root);
224 ret = btrfs_trans_reserve_metadata(h, root, num_items);
225 if (ret == -EAGAIN && !retries) {
227 btrfs_commit_transaction(h, root);
229 } else if (ret == -EAGAIN) {
231 * We have already retried and got EAGAIN, so really we
232 * don't have space, so set ret to -ENOSPC.
238 btrfs_end_transaction(h, root);
243 if (type != TRANS_JOIN_NOLOCK)
244 mutex_lock(&root->fs_info->trans_mutex);
245 record_root_in_trans(h, root);
246 if (type != TRANS_JOIN_NOLOCK)
247 mutex_unlock(&root->fs_info->trans_mutex);
249 if (!current->journal_info && type != TRANS_USERSPACE)
250 current->journal_info = h;
254 struct btrfs_trans_handle *btrfs_start_transaction(struct btrfs_root *root,
257 return start_transaction(root, num_items, TRANS_START);
259 struct btrfs_trans_handle *btrfs_join_transaction(struct btrfs_root *root,
262 return start_transaction(root, 0, TRANS_JOIN);
265 struct btrfs_trans_handle *btrfs_join_transaction_nolock(struct btrfs_root *root,
268 return start_transaction(root, 0, TRANS_JOIN_NOLOCK);
271 struct btrfs_trans_handle *btrfs_start_ioctl_transaction(struct btrfs_root *r,
274 return start_transaction(r, 0, TRANS_USERSPACE);
277 /* wait for a transaction commit to be fully complete */
278 static noinline int wait_for_commit(struct btrfs_root *root,
279 struct btrfs_transaction *commit)
282 mutex_lock(&root->fs_info->trans_mutex);
283 while (!commit->commit_done) {
284 prepare_to_wait(&commit->commit_wait, &wait,
285 TASK_UNINTERRUPTIBLE);
286 if (commit->commit_done)
288 mutex_unlock(&root->fs_info->trans_mutex);
290 mutex_lock(&root->fs_info->trans_mutex);
292 mutex_unlock(&root->fs_info->trans_mutex);
293 finish_wait(&commit->commit_wait, &wait);
297 int btrfs_wait_for_commit(struct btrfs_root *root, u64 transid)
299 struct btrfs_transaction *cur_trans = NULL, *t;
302 mutex_lock(&root->fs_info->trans_mutex);
306 if (transid <= root->fs_info->last_trans_committed)
309 /* find specified transaction */
310 list_for_each_entry(t, &root->fs_info->trans_list, list) {
311 if (t->transid == transid) {
315 if (t->transid > transid)
320 goto out_unlock; /* bad transid */
322 /* find newest transaction that is committing | committed */
323 list_for_each_entry_reverse(t, &root->fs_info->trans_list,
333 goto out_unlock; /* nothing committing|committed */
336 atomic_inc(&cur_trans->use_count);
337 mutex_unlock(&root->fs_info->trans_mutex);
339 wait_for_commit(root, cur_trans);
341 mutex_lock(&root->fs_info->trans_mutex);
342 put_transaction(cur_trans);
345 mutex_unlock(&root->fs_info->trans_mutex);
351 * rate limit against the drop_snapshot code. This helps to slow down new
352 * operations if the drop_snapshot code isn't able to keep up.
354 static void throttle_on_drops(struct btrfs_root *root)
356 struct btrfs_fs_info *info = root->fs_info;
357 int harder_count = 0;
360 if (atomic_read(&info->throttles)) {
363 thr = atomic_read(&info->throttle_gen);
366 prepare_to_wait(&info->transaction_throttle,
367 &wait, TASK_UNINTERRUPTIBLE);
368 if (!atomic_read(&info->throttles)) {
369 finish_wait(&info->transaction_throttle, &wait);
373 finish_wait(&info->transaction_throttle, &wait);
374 } while (thr == atomic_read(&info->throttle_gen));
377 if (root->fs_info->total_ref_cache_size > 1 * 1024 * 1024 &&
381 if (root->fs_info->total_ref_cache_size > 5 * 1024 * 1024 &&
385 if (root->fs_info->total_ref_cache_size > 10 * 1024 * 1024 &&
392 void btrfs_throttle(struct btrfs_root *root)
394 mutex_lock(&root->fs_info->trans_mutex);
395 if (!root->fs_info->open_ioctl_trans)
396 wait_current_trans(root);
397 mutex_unlock(&root->fs_info->trans_mutex);
400 static int should_end_transaction(struct btrfs_trans_handle *trans,
401 struct btrfs_root *root)
404 ret = btrfs_block_rsv_check(trans, root,
405 &root->fs_info->global_block_rsv, 0, 5);
409 int btrfs_should_end_transaction(struct btrfs_trans_handle *trans,
410 struct btrfs_root *root)
412 struct btrfs_transaction *cur_trans = trans->transaction;
415 if (cur_trans->blocked || cur_trans->delayed_refs.flushing)
418 updates = trans->delayed_ref_updates;
419 trans->delayed_ref_updates = 0;
421 btrfs_run_delayed_refs(trans, root, updates);
423 return should_end_transaction(trans, root);
426 static int __btrfs_end_transaction(struct btrfs_trans_handle *trans,
427 struct btrfs_root *root, int throttle, int lock)
429 struct btrfs_transaction *cur_trans = trans->transaction;
430 struct btrfs_fs_info *info = root->fs_info;
434 unsigned long cur = trans->delayed_ref_updates;
435 trans->delayed_ref_updates = 0;
437 trans->transaction->delayed_refs.num_heads_ready > 64) {
438 trans->delayed_ref_updates = 0;
441 * do a full flush if the transaction is trying
444 if (trans->transaction->delayed_refs.flushing)
446 btrfs_run_delayed_refs(trans, root, cur);
453 btrfs_trans_release_metadata(trans, root);
455 if (lock && !root->fs_info->open_ioctl_trans &&
456 should_end_transaction(trans, root))
457 trans->transaction->blocked = 1;
459 if (lock && cur_trans->blocked && !cur_trans->in_commit) {
461 return btrfs_commit_transaction(trans, root);
463 wake_up_process(info->transaction_kthread);
466 WARN_ON(cur_trans != info->running_transaction);
467 WARN_ON(atomic_read(&cur_trans->num_writers) < 1);
468 atomic_dec(&cur_trans->num_writers);
471 if (waitqueue_active(&cur_trans->writer_wait))
472 wake_up(&cur_trans->writer_wait);
473 put_transaction(cur_trans);
475 if (current->journal_info == trans)
476 current->journal_info = NULL;
477 memset(trans, 0, sizeof(*trans));
478 kmem_cache_free(btrfs_trans_handle_cachep, trans);
481 btrfs_run_delayed_iputs(root);
486 int btrfs_end_transaction(struct btrfs_trans_handle *trans,
487 struct btrfs_root *root)
489 return __btrfs_end_transaction(trans, root, 0, 1);
492 int btrfs_end_transaction_throttle(struct btrfs_trans_handle *trans,
493 struct btrfs_root *root)
495 return __btrfs_end_transaction(trans, root, 1, 1);
498 int btrfs_end_transaction_nolock(struct btrfs_trans_handle *trans,
499 struct btrfs_root *root)
501 return __btrfs_end_transaction(trans, root, 0, 0);
505 * when btree blocks are allocated, they have some corresponding bits set for
506 * them in one of two extent_io trees. This is used to make sure all of
507 * those extents are sent to disk but does not wait on them
509 int btrfs_write_marked_extents(struct btrfs_root *root,
510 struct extent_io_tree *dirty_pages, int mark)
516 struct inode *btree_inode = root->fs_info->btree_inode;
522 ret = find_first_extent_bit(dirty_pages, start, &start, &end,
526 while (start <= end) {
529 index = start >> PAGE_CACHE_SHIFT;
530 start = (u64)(index + 1) << PAGE_CACHE_SHIFT;
531 page = find_get_page(btree_inode->i_mapping, index);
535 btree_lock_page_hook(page);
536 if (!page->mapping) {
538 page_cache_release(page);
542 if (PageWriteback(page)) {
544 wait_on_page_writeback(page);
547 page_cache_release(page);
551 err = write_one_page(page, 0);
554 page_cache_release(page);
563 * when btree blocks are allocated, they have some corresponding bits set for
564 * them in one of two extent_io trees. This is used to make sure all of
565 * those extents are on disk for transaction or log commit. We wait
566 * on all the pages and clear them from the dirty pages state tree
568 int btrfs_wait_marked_extents(struct btrfs_root *root,
569 struct extent_io_tree *dirty_pages, int mark)
575 struct inode *btree_inode = root->fs_info->btree_inode;
581 ret = find_first_extent_bit(dirty_pages, start, &start, &end,
586 clear_extent_bits(dirty_pages, start, end, mark, GFP_NOFS);
587 while (start <= end) {
588 index = start >> PAGE_CACHE_SHIFT;
589 start = (u64)(index + 1) << PAGE_CACHE_SHIFT;
590 page = find_get_page(btree_inode->i_mapping, index);
593 if (PageDirty(page)) {
594 btree_lock_page_hook(page);
595 wait_on_page_writeback(page);
596 err = write_one_page(page, 0);
600 wait_on_page_writeback(page);
601 page_cache_release(page);
611 * when btree blocks are allocated, they have some corresponding bits set for
612 * them in one of two extent_io trees. This is used to make sure all of
613 * those extents are on disk for transaction or log commit
615 int btrfs_write_and_wait_marked_extents(struct btrfs_root *root,
616 struct extent_io_tree *dirty_pages, int mark)
621 ret = btrfs_write_marked_extents(root, dirty_pages, mark);
622 ret2 = btrfs_wait_marked_extents(root, dirty_pages, mark);
626 int btrfs_write_and_wait_transaction(struct btrfs_trans_handle *trans,
627 struct btrfs_root *root)
629 if (!trans || !trans->transaction) {
630 struct inode *btree_inode;
631 btree_inode = root->fs_info->btree_inode;
632 return filemap_write_and_wait(btree_inode->i_mapping);
634 return btrfs_write_and_wait_marked_extents(root,
635 &trans->transaction->dirty_pages,
640 * this is used to update the root pointer in the tree of tree roots.
642 * But, in the case of the extent allocation tree, updating the root
643 * pointer may allocate blocks which may change the root of the extent
646 * So, this loops and repeats and makes sure the cowonly root didn't
647 * change while the root pointer was being updated in the metadata.
649 static int update_cowonly_root(struct btrfs_trans_handle *trans,
650 struct btrfs_root *root)
655 struct btrfs_root *tree_root = root->fs_info->tree_root;
657 old_root_used = btrfs_root_used(&root->root_item);
658 btrfs_write_dirty_block_groups(trans, root);
661 old_root_bytenr = btrfs_root_bytenr(&root->root_item);
662 if (old_root_bytenr == root->node->start &&
663 old_root_used == btrfs_root_used(&root->root_item))
666 btrfs_set_root_node(&root->root_item, root->node);
667 ret = btrfs_update_root(trans, tree_root,
672 old_root_used = btrfs_root_used(&root->root_item);
673 ret = btrfs_write_dirty_block_groups(trans, root);
677 if (root != root->fs_info->extent_root)
678 switch_commit_root(root);
684 * update all the cowonly tree roots on disk
686 static noinline int commit_cowonly_roots(struct btrfs_trans_handle *trans,
687 struct btrfs_root *root)
689 struct btrfs_fs_info *fs_info = root->fs_info;
690 struct list_head *next;
691 struct extent_buffer *eb;
694 ret = btrfs_run_delayed_refs(trans, root, (unsigned long)-1);
697 eb = btrfs_lock_root_node(fs_info->tree_root);
698 btrfs_cow_block(trans, fs_info->tree_root, eb, NULL, 0, &eb);
699 btrfs_tree_unlock(eb);
700 free_extent_buffer(eb);
702 ret = btrfs_run_delayed_refs(trans, root, (unsigned long)-1);
705 while (!list_empty(&fs_info->dirty_cowonly_roots)) {
706 next = fs_info->dirty_cowonly_roots.next;
708 root = list_entry(next, struct btrfs_root, dirty_list);
710 update_cowonly_root(trans, root);
713 down_write(&fs_info->extent_commit_sem);
714 switch_commit_root(fs_info->extent_root);
715 up_write(&fs_info->extent_commit_sem);
721 * dead roots are old snapshots that need to be deleted. This allocates
722 * a dirty root struct and adds it into the list of dead roots that need to
725 int btrfs_add_dead_root(struct btrfs_root *root)
727 mutex_lock(&root->fs_info->trans_mutex);
728 list_add(&root->root_list, &root->fs_info->dead_roots);
729 mutex_unlock(&root->fs_info->trans_mutex);
734 * update all the cowonly tree roots on disk
736 static noinline int commit_fs_roots(struct btrfs_trans_handle *trans,
737 struct btrfs_root *root)
739 struct btrfs_root *gang[8];
740 struct btrfs_fs_info *fs_info = root->fs_info;
746 ret = radix_tree_gang_lookup_tag(&fs_info->fs_roots_radix,
749 BTRFS_ROOT_TRANS_TAG);
752 for (i = 0; i < ret; i++) {
754 radix_tree_tag_clear(&fs_info->fs_roots_radix,
755 (unsigned long)root->root_key.objectid,
756 BTRFS_ROOT_TRANS_TAG);
758 btrfs_free_log(trans, root);
759 btrfs_update_reloc_root(trans, root);
760 btrfs_orphan_commit_root(trans, root);
762 if (root->commit_root != root->node) {
763 switch_commit_root(root);
764 btrfs_set_root_node(&root->root_item,
768 err = btrfs_update_root(trans, fs_info->tree_root,
779 * defrag a given btree. If cacheonly == 1, this won't read from the disk,
780 * otherwise every leaf in the btree is read and defragged.
782 int btrfs_defrag_root(struct btrfs_root *root, int cacheonly)
784 struct btrfs_fs_info *info = root->fs_info;
785 struct btrfs_trans_handle *trans;
789 if (xchg(&root->defrag_running, 1))
793 trans = btrfs_start_transaction(root, 0);
795 return PTR_ERR(trans);
797 ret = btrfs_defrag_leaves(trans, root, cacheonly);
799 nr = trans->blocks_used;
800 btrfs_end_transaction(trans, root);
801 btrfs_btree_balance_dirty(info->tree_root, nr);
804 if (root->fs_info->closing || ret != -EAGAIN)
807 root->defrag_running = 0;
813 * when dropping snapshots, we generate a ton of delayed refs, and it makes
814 * sense not to join the transaction while it is trying to flush the current
815 * queue of delayed refs out.
817 * This is used by the drop snapshot code only
819 static noinline int wait_transaction_pre_flush(struct btrfs_fs_info *info)
823 mutex_lock(&info->trans_mutex);
824 while (info->running_transaction &&
825 info->running_transaction->delayed_refs.flushing) {
826 prepare_to_wait(&info->transaction_wait, &wait,
827 TASK_UNINTERRUPTIBLE);
828 mutex_unlock(&info->trans_mutex);
832 mutex_lock(&info->trans_mutex);
833 finish_wait(&info->transaction_wait, &wait);
835 mutex_unlock(&info->trans_mutex);
840 * Given a list of roots that need to be deleted, call btrfs_drop_snapshot on
843 int btrfs_drop_dead_root(struct btrfs_root *root)
845 struct btrfs_trans_handle *trans;
846 struct btrfs_root *tree_root = root->fs_info->tree_root;
852 * we don't want to jump in and create a bunch of
853 * delayed refs if the transaction is starting to close
855 wait_transaction_pre_flush(tree_root->fs_info);
856 trans = btrfs_start_transaction(tree_root, 1);
859 * we've joined a transaction, make sure it isn't
862 if (trans->transaction->delayed_refs.flushing) {
863 btrfs_end_transaction(trans, tree_root);
867 ret = btrfs_drop_snapshot(trans, root);
871 ret = btrfs_update_root(trans, tree_root,
877 nr = trans->blocks_used;
878 ret = btrfs_end_transaction(trans, tree_root);
881 btrfs_btree_balance_dirty(tree_root, nr);
886 ret = btrfs_del_root(trans, tree_root, &root->root_key);
889 nr = trans->blocks_used;
890 ret = btrfs_end_transaction(trans, tree_root);
893 free_extent_buffer(root->node);
894 free_extent_buffer(root->commit_root);
897 btrfs_btree_balance_dirty(tree_root, nr);
903 * new snapshots need to be created at a very specific time in the
904 * transaction commit. This does the actual creation
906 static noinline int create_pending_snapshot(struct btrfs_trans_handle *trans,
907 struct btrfs_fs_info *fs_info,
908 struct btrfs_pending_snapshot *pending)
910 struct btrfs_key key;
911 struct btrfs_root_item *new_root_item;
912 struct btrfs_root *tree_root = fs_info->tree_root;
913 struct btrfs_root *root = pending->root;
914 struct btrfs_root *parent_root;
915 struct inode *parent_inode;
916 struct dentry *parent;
917 struct dentry *dentry;
918 struct extent_buffer *tmp;
919 struct extent_buffer *old;
926 new_root_item = kmalloc(sizeof(*new_root_item), GFP_NOFS);
927 if (!new_root_item) {
928 pending->error = -ENOMEM;
932 ret = btrfs_find_free_objectid(trans, tree_root, 0, &objectid);
934 pending->error = ret;
938 btrfs_reloc_pre_snapshot(trans, pending, &to_reserve);
939 btrfs_orphan_pre_snapshot(trans, pending, &to_reserve);
941 if (to_reserve > 0) {
942 ret = btrfs_block_rsv_add(trans, root, &pending->block_rsv,
945 pending->error = ret;
950 key.objectid = objectid;
951 key.offset = (u64)-1;
952 key.type = BTRFS_ROOT_ITEM_KEY;
954 trans->block_rsv = &pending->block_rsv;
956 dentry = pending->dentry;
957 parent = dget_parent(dentry);
958 parent_inode = parent->d_inode;
959 parent_root = BTRFS_I(parent_inode)->root;
960 record_root_in_trans(trans, parent_root);
963 * insert the directory item
965 ret = btrfs_set_inode_index(parent_inode, &index);
967 ret = btrfs_insert_dir_item(trans, parent_root,
968 dentry->d_name.name, dentry->d_name.len,
969 parent_inode->i_ino, &key,
970 BTRFS_FT_DIR, index);
973 btrfs_i_size_write(parent_inode, parent_inode->i_size +
974 dentry->d_name.len * 2);
975 ret = btrfs_update_inode(trans, parent_root, parent_inode);
978 record_root_in_trans(trans, root);
979 btrfs_set_root_last_snapshot(&root->root_item, trans->transid);
980 memcpy(new_root_item, &root->root_item, sizeof(*new_root_item));
981 btrfs_check_and_init_root_item(new_root_item);
983 root_flags = btrfs_root_flags(new_root_item);
984 if (pending->readonly)
985 root_flags |= BTRFS_ROOT_SUBVOL_RDONLY;
987 root_flags &= ~BTRFS_ROOT_SUBVOL_RDONLY;
988 btrfs_set_root_flags(new_root_item, root_flags);
990 old = btrfs_lock_root_node(root);
991 btrfs_cow_block(trans, root, old, NULL, 0, &old);
992 btrfs_set_lock_blocking(old);
994 btrfs_copy_root(trans, root, old, &tmp, objectid);
995 btrfs_tree_unlock(old);
996 free_extent_buffer(old);
998 btrfs_set_root_node(new_root_item, tmp);
999 /* record when the snapshot was created in key.offset */
1000 key.offset = trans->transid;
1001 ret = btrfs_insert_root(trans, tree_root, &key, new_root_item);
1002 btrfs_tree_unlock(tmp);
1003 free_extent_buffer(tmp);
1007 * insert root back/forward references
1009 ret = btrfs_add_root_ref(trans, tree_root, objectid,
1010 parent_root->root_key.objectid,
1011 parent_inode->i_ino, index,
1012 dentry->d_name.name, dentry->d_name.len);
1016 key.offset = (u64)-1;
1017 pending->snap = btrfs_read_fs_root_no_name(root->fs_info, &key);
1018 BUG_ON(IS_ERR(pending->snap));
1020 btrfs_reloc_post_snapshot(trans, pending);
1021 btrfs_orphan_post_snapshot(trans, pending);
1023 kfree(new_root_item);
1024 btrfs_block_rsv_release(root, &pending->block_rsv, (u64)-1);
1029 * create all the snapshots we've scheduled for creation
1031 static noinline int create_pending_snapshots(struct btrfs_trans_handle *trans,
1032 struct btrfs_fs_info *fs_info)
1034 struct btrfs_pending_snapshot *pending;
1035 struct list_head *head = &trans->transaction->pending_snapshots;
1038 list_for_each_entry(pending, head, list) {
1039 ret = create_pending_snapshot(trans, fs_info, pending);
1045 static void update_super_roots(struct btrfs_root *root)
1047 struct btrfs_root_item *root_item;
1048 struct btrfs_super_block *super;
1050 super = &root->fs_info->super_copy;
1052 root_item = &root->fs_info->chunk_root->root_item;
1053 super->chunk_root = root_item->bytenr;
1054 super->chunk_root_generation = root_item->generation;
1055 super->chunk_root_level = root_item->level;
1057 root_item = &root->fs_info->tree_root->root_item;
1058 super->root = root_item->bytenr;
1059 super->generation = root_item->generation;
1060 super->root_level = root_item->level;
1061 if (super->cache_generation != 0 || btrfs_test_opt(root, SPACE_CACHE))
1062 super->cache_generation = root_item->generation;
1065 int btrfs_transaction_in_commit(struct btrfs_fs_info *info)
1068 spin_lock(&info->new_trans_lock);
1069 if (info->running_transaction)
1070 ret = info->running_transaction->in_commit;
1071 spin_unlock(&info->new_trans_lock);
1075 int btrfs_transaction_blocked(struct btrfs_fs_info *info)
1078 spin_lock(&info->new_trans_lock);
1079 if (info->running_transaction)
1080 ret = info->running_transaction->blocked;
1081 spin_unlock(&info->new_trans_lock);
1086 * wait for the current transaction commit to start and block subsequent
1089 static void wait_current_trans_commit_start(struct btrfs_root *root,
1090 struct btrfs_transaction *trans)
1094 if (trans->in_commit)
1098 prepare_to_wait(&root->fs_info->transaction_blocked_wait, &wait,
1099 TASK_UNINTERRUPTIBLE);
1100 if (trans->in_commit) {
1101 finish_wait(&root->fs_info->transaction_blocked_wait,
1105 mutex_unlock(&root->fs_info->trans_mutex);
1107 mutex_lock(&root->fs_info->trans_mutex);
1108 finish_wait(&root->fs_info->transaction_blocked_wait, &wait);
1113 * wait for the current transaction to start and then become unblocked.
1116 static void wait_current_trans_commit_start_and_unblock(struct btrfs_root *root,
1117 struct btrfs_transaction *trans)
1121 if (trans->commit_done || (trans->in_commit && !trans->blocked))
1125 prepare_to_wait(&root->fs_info->transaction_wait, &wait,
1126 TASK_UNINTERRUPTIBLE);
1127 if (trans->commit_done ||
1128 (trans->in_commit && !trans->blocked)) {
1129 finish_wait(&root->fs_info->transaction_wait,
1133 mutex_unlock(&root->fs_info->trans_mutex);
1135 mutex_lock(&root->fs_info->trans_mutex);
1136 finish_wait(&root->fs_info->transaction_wait,
1142 * commit transactions asynchronously. once btrfs_commit_transaction_async
1143 * returns, any subsequent transaction will not be allowed to join.
1145 struct btrfs_async_commit {
1146 struct btrfs_trans_handle *newtrans;
1147 struct btrfs_root *root;
1148 struct delayed_work work;
1151 static void do_async_commit(struct work_struct *work)
1153 struct btrfs_async_commit *ac =
1154 container_of(work, struct btrfs_async_commit, work.work);
1156 btrfs_commit_transaction(ac->newtrans, ac->root);
1160 int btrfs_commit_transaction_async(struct btrfs_trans_handle *trans,
1161 struct btrfs_root *root,
1162 int wait_for_unblock)
1164 struct btrfs_async_commit *ac;
1165 struct btrfs_transaction *cur_trans;
1167 ac = kmalloc(sizeof(*ac), GFP_NOFS);
1171 INIT_DELAYED_WORK(&ac->work, do_async_commit);
1173 ac->newtrans = btrfs_join_transaction(root, 0);
1174 if (IS_ERR(ac->newtrans)) {
1175 int err = PTR_ERR(ac->newtrans);
1180 /* take transaction reference */
1181 mutex_lock(&root->fs_info->trans_mutex);
1182 cur_trans = trans->transaction;
1183 atomic_inc(&cur_trans->use_count);
1184 mutex_unlock(&root->fs_info->trans_mutex);
1186 btrfs_end_transaction(trans, root);
1187 schedule_delayed_work(&ac->work, 0);
1189 /* wait for transaction to start and unblock */
1190 mutex_lock(&root->fs_info->trans_mutex);
1191 if (wait_for_unblock)
1192 wait_current_trans_commit_start_and_unblock(root, cur_trans);
1194 wait_current_trans_commit_start(root, cur_trans);
1195 put_transaction(cur_trans);
1196 mutex_unlock(&root->fs_info->trans_mutex);
1202 * btrfs_transaction state sequence:
1203 * in_commit = 0, blocked = 0 (initial)
1204 * in_commit = 1, blocked = 1
1208 int btrfs_commit_transaction(struct btrfs_trans_handle *trans,
1209 struct btrfs_root *root)
1211 unsigned long joined = 0;
1212 struct btrfs_transaction *cur_trans;
1213 struct btrfs_transaction *prev_trans = NULL;
1216 int should_grow = 0;
1217 unsigned long now = get_seconds();
1218 int flush_on_commit = btrfs_test_opt(root, FLUSHONCOMMIT);
1220 btrfs_run_ordered_operations(root, 0);
1222 /* make a pass through all the delayed refs we have so far
1223 * any runnings procs may add more while we are here
1225 ret = btrfs_run_delayed_refs(trans, root, 0);
1228 btrfs_trans_release_metadata(trans, root);
1230 cur_trans = trans->transaction;
1232 * set the flushing flag so procs in this transaction have to
1233 * start sending their work down.
1235 cur_trans->delayed_refs.flushing = 1;
1237 ret = btrfs_run_delayed_refs(trans, root, 0);
1240 mutex_lock(&root->fs_info->trans_mutex);
1241 if (cur_trans->in_commit) {
1242 atomic_inc(&cur_trans->use_count);
1243 mutex_unlock(&root->fs_info->trans_mutex);
1244 btrfs_end_transaction(trans, root);
1246 ret = wait_for_commit(root, cur_trans);
1249 mutex_lock(&root->fs_info->trans_mutex);
1250 put_transaction(cur_trans);
1251 mutex_unlock(&root->fs_info->trans_mutex);
1256 trans->transaction->in_commit = 1;
1257 trans->transaction->blocked = 1;
1258 wake_up(&root->fs_info->transaction_blocked_wait);
1260 if (cur_trans->list.prev != &root->fs_info->trans_list) {
1261 prev_trans = list_entry(cur_trans->list.prev,
1262 struct btrfs_transaction, list);
1263 if (!prev_trans->commit_done) {
1264 atomic_inc(&prev_trans->use_count);
1265 mutex_unlock(&root->fs_info->trans_mutex);
1267 wait_for_commit(root, prev_trans);
1269 mutex_lock(&root->fs_info->trans_mutex);
1270 put_transaction(prev_trans);
1274 if (now < cur_trans->start_time || now - cur_trans->start_time < 1)
1278 int snap_pending = 0;
1279 joined = cur_trans->num_joined;
1280 if (!list_empty(&trans->transaction->pending_snapshots))
1283 WARN_ON(cur_trans != trans->transaction);
1284 mutex_unlock(&root->fs_info->trans_mutex);
1286 if (flush_on_commit || snap_pending) {
1287 btrfs_start_delalloc_inodes(root, 1);
1288 ret = btrfs_wait_ordered_extents(root, 0, 1);
1293 * rename don't use btrfs_join_transaction, so, once we
1294 * set the transaction to blocked above, we aren't going
1295 * to get any new ordered operations. We can safely run
1296 * it here and no for sure that nothing new will be added
1299 btrfs_run_ordered_operations(root, 1);
1301 prepare_to_wait(&cur_trans->writer_wait, &wait,
1302 TASK_UNINTERRUPTIBLE);
1305 if (atomic_read(&cur_trans->num_writers) > 1)
1306 schedule_timeout(MAX_SCHEDULE_TIMEOUT);
1307 else if (should_grow)
1308 schedule_timeout(1);
1310 mutex_lock(&root->fs_info->trans_mutex);
1311 finish_wait(&cur_trans->writer_wait, &wait);
1312 } while (atomic_read(&cur_trans->num_writers) > 1 ||
1313 (should_grow && cur_trans->num_joined != joined));
1315 ret = create_pending_snapshots(trans, root->fs_info);
1318 ret = btrfs_run_delayed_refs(trans, root, (unsigned long)-1);
1321 WARN_ON(cur_trans != trans->transaction);
1323 /* btrfs_commit_tree_roots is responsible for getting the
1324 * various roots consistent with each other. Every pointer
1325 * in the tree of tree roots has to point to the most up to date
1326 * root for every subvolume and other tree. So, we have to keep
1327 * the tree logging code from jumping in and changing any
1330 * At this point in the commit, there can't be any tree-log
1331 * writers, but a little lower down we drop the trans mutex
1332 * and let new people in. By holding the tree_log_mutex
1333 * from now until after the super is written, we avoid races
1334 * with the tree-log code.
1336 mutex_lock(&root->fs_info->tree_log_mutex);
1338 ret = commit_fs_roots(trans, root);
1341 /* commit_fs_roots gets rid of all the tree log roots, it is now
1342 * safe to free the root of tree log roots
1344 btrfs_free_log_root_tree(trans, root->fs_info);
1346 ret = commit_cowonly_roots(trans, root);
1349 btrfs_prepare_extent_commit(trans, root);
1351 cur_trans = root->fs_info->running_transaction;
1352 spin_lock(&root->fs_info->new_trans_lock);
1353 root->fs_info->running_transaction = NULL;
1354 spin_unlock(&root->fs_info->new_trans_lock);
1356 btrfs_set_root_node(&root->fs_info->tree_root->root_item,
1357 root->fs_info->tree_root->node);
1358 switch_commit_root(root->fs_info->tree_root);
1360 btrfs_set_root_node(&root->fs_info->chunk_root->root_item,
1361 root->fs_info->chunk_root->node);
1362 switch_commit_root(root->fs_info->chunk_root);
1364 update_super_roots(root);
1366 if (!root->fs_info->log_root_recovering) {
1367 btrfs_set_super_log_root(&root->fs_info->super_copy, 0);
1368 btrfs_set_super_log_root_level(&root->fs_info->super_copy, 0);
1371 memcpy(&root->fs_info->super_for_commit, &root->fs_info->super_copy,
1372 sizeof(root->fs_info->super_copy));
1374 trans->transaction->blocked = 0;
1376 wake_up(&root->fs_info->transaction_wait);
1378 mutex_unlock(&root->fs_info->trans_mutex);
1379 ret = btrfs_write_and_wait_transaction(trans, root);
1381 write_ctree_super(trans, root, 0);
1384 * the super is written, we can safely allow the tree-loggers
1385 * to go about their business
1387 mutex_unlock(&root->fs_info->tree_log_mutex);
1389 btrfs_finish_extent_commit(trans, root);
1391 mutex_lock(&root->fs_info->trans_mutex);
1393 cur_trans->commit_done = 1;
1395 root->fs_info->last_trans_committed = cur_trans->transid;
1397 wake_up(&cur_trans->commit_wait);
1399 list_del_init(&cur_trans->list);
1400 put_transaction(cur_trans);
1401 put_transaction(cur_trans);
1403 trace_btrfs_transaction_commit(root);
1405 mutex_unlock(&root->fs_info->trans_mutex);
1407 if (current->journal_info == trans)
1408 current->journal_info = NULL;
1410 kmem_cache_free(btrfs_trans_handle_cachep, trans);
1412 if (current != root->fs_info->transaction_kthread)
1413 btrfs_run_delayed_iputs(root);
1419 * interface function to delete all the snapshots we have scheduled for deletion
1421 int btrfs_clean_old_snapshots(struct btrfs_root *root)
1424 struct btrfs_fs_info *fs_info = root->fs_info;
1426 mutex_lock(&fs_info->trans_mutex);
1427 list_splice_init(&fs_info->dead_roots, &list);
1428 mutex_unlock(&fs_info->trans_mutex);
1430 while (!list_empty(&list)) {
1431 root = list_entry(list.next, struct btrfs_root, root_list);
1432 list_del(&root->root_list);
1434 if (btrfs_header_backref_rev(root->node) <
1435 BTRFS_MIXED_BACKREF_REV)
1436 btrfs_drop_snapshot(root, NULL, 0);
1438 btrfs_drop_snapshot(root, NULL, 1);