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,
85 spin_lock(&root->fs_info->new_trans_lock);
86 root->fs_info->running_transaction = cur_trans;
87 spin_unlock(&root->fs_info->new_trans_lock);
89 atomic_inc(&cur_trans->num_writers);
90 cur_trans->num_joined++;
97 * this does all the record keeping required to make sure that a reference
98 * counted root is properly recorded in a given transaction. This is required
99 * to make sure the old root from before we joined the transaction is deleted
100 * when the transaction commits
102 static noinline int record_root_in_trans(struct btrfs_trans_handle *trans,
103 struct btrfs_root *root)
105 if (root->ref_cows && root->last_trans < trans->transid) {
106 WARN_ON(root == root->fs_info->extent_root);
107 WARN_ON(root->commit_root != root->node);
109 radix_tree_tag_set(&root->fs_info->fs_roots_radix,
110 (unsigned long)root->root_key.objectid,
111 BTRFS_ROOT_TRANS_TAG);
112 root->last_trans = trans->transid;
113 btrfs_init_reloc_root(trans, root);
118 int btrfs_record_root_in_trans(struct btrfs_trans_handle *trans,
119 struct btrfs_root *root)
124 mutex_lock(&root->fs_info->trans_mutex);
125 if (root->last_trans == trans->transid) {
126 mutex_unlock(&root->fs_info->trans_mutex);
130 record_root_in_trans(trans, root);
131 mutex_unlock(&root->fs_info->trans_mutex);
135 /* wait for commit against the current transaction to become unblocked
136 * when this is done, it is safe to start a new transaction, but the current
137 * transaction might not be fully on disk.
139 static void wait_current_trans(struct btrfs_root *root)
141 struct btrfs_transaction *cur_trans;
143 cur_trans = root->fs_info->running_transaction;
144 if (cur_trans && cur_trans->blocked) {
146 atomic_inc(&cur_trans->use_count);
148 prepare_to_wait(&root->fs_info->transaction_wait, &wait,
149 TASK_UNINTERRUPTIBLE);
150 if (!cur_trans->blocked)
152 mutex_unlock(&root->fs_info->trans_mutex);
154 mutex_lock(&root->fs_info->trans_mutex);
156 finish_wait(&root->fs_info->transaction_wait, &wait);
157 put_transaction(cur_trans);
161 enum btrfs_trans_type {
168 static int may_wait_transaction(struct btrfs_root *root, int type)
170 if (!root->fs_info->log_root_recovering &&
171 ((type == TRANS_START && !root->fs_info->open_ioctl_trans) ||
172 type == TRANS_USERSPACE))
177 static struct btrfs_trans_handle *start_transaction(struct btrfs_root *root,
178 u64 num_items, int type)
180 struct btrfs_trans_handle *h;
181 struct btrfs_transaction *cur_trans;
185 if (root->fs_info->fs_state & BTRFS_SUPER_FLAG_ERROR)
186 return ERR_PTR(-EROFS);
188 if (current->journal_info) {
189 WARN_ON(type != TRANS_JOIN && type != TRANS_JOIN_NOLOCK);
190 h = current->journal_info;
192 h->orig_rsv = h->block_rsv;
197 h = kmem_cache_alloc(btrfs_trans_handle_cachep, GFP_NOFS);
199 return ERR_PTR(-ENOMEM);
201 if (type != TRANS_JOIN_NOLOCK)
202 mutex_lock(&root->fs_info->trans_mutex);
203 if (may_wait_transaction(root, type))
204 wait_current_trans(root);
206 ret = join_transaction(root);
208 kmem_cache_free(btrfs_trans_handle_cachep, h);
209 if (type != TRANS_JOIN_NOLOCK)
210 mutex_unlock(&root->fs_info->trans_mutex);
214 cur_trans = root->fs_info->running_transaction;
215 atomic_inc(&cur_trans->use_count);
216 if (type != TRANS_JOIN_NOLOCK)
217 mutex_unlock(&root->fs_info->trans_mutex);
219 h->transid = cur_trans->transid;
220 h->transaction = cur_trans;
223 h->bytes_reserved = 0;
224 h->delayed_ref_updates = 0;
230 if (cur_trans->blocked && may_wait_transaction(root, type)) {
231 btrfs_commit_transaction(h, root);
236 ret = btrfs_trans_reserve_metadata(h, root, num_items);
237 if (ret == -EAGAIN && !retries) {
239 btrfs_commit_transaction(h, root);
241 } else if (ret == -EAGAIN) {
243 * We have already retried and got EAGAIN, so really we
244 * don't have space, so set ret to -ENOSPC.
250 btrfs_end_transaction(h, root);
256 if (type != TRANS_JOIN_NOLOCK)
257 mutex_lock(&root->fs_info->trans_mutex);
258 record_root_in_trans(h, root);
259 if (type != TRANS_JOIN_NOLOCK)
260 mutex_unlock(&root->fs_info->trans_mutex);
262 if (!current->journal_info && type != TRANS_USERSPACE)
263 current->journal_info = h;
267 struct btrfs_trans_handle *btrfs_start_transaction(struct btrfs_root *root,
270 return start_transaction(root, num_items, TRANS_START);
272 struct btrfs_trans_handle *btrfs_join_transaction(struct btrfs_root *root)
274 return start_transaction(root, 0, TRANS_JOIN);
277 struct btrfs_trans_handle *btrfs_join_transaction_nolock(struct btrfs_root *root)
279 return start_transaction(root, 0, TRANS_JOIN_NOLOCK);
282 struct btrfs_trans_handle *btrfs_start_ioctl_transaction(struct btrfs_root *root)
284 return start_transaction(root, 0, TRANS_USERSPACE);
287 /* wait for a transaction commit to be fully complete */
288 static noinline int wait_for_commit(struct btrfs_root *root,
289 struct btrfs_transaction *commit)
292 mutex_lock(&root->fs_info->trans_mutex);
293 while (!commit->commit_done) {
294 prepare_to_wait(&commit->commit_wait, &wait,
295 TASK_UNINTERRUPTIBLE);
296 if (commit->commit_done)
298 mutex_unlock(&root->fs_info->trans_mutex);
300 mutex_lock(&root->fs_info->trans_mutex);
302 mutex_unlock(&root->fs_info->trans_mutex);
303 finish_wait(&commit->commit_wait, &wait);
307 int btrfs_wait_for_commit(struct btrfs_root *root, u64 transid)
309 struct btrfs_transaction *cur_trans = NULL, *t;
312 mutex_lock(&root->fs_info->trans_mutex);
316 if (transid <= root->fs_info->last_trans_committed)
319 /* find specified transaction */
320 list_for_each_entry(t, &root->fs_info->trans_list, list) {
321 if (t->transid == transid) {
325 if (t->transid > transid)
330 goto out_unlock; /* bad transid */
332 /* find newest transaction that is committing | committed */
333 list_for_each_entry_reverse(t, &root->fs_info->trans_list,
343 goto out_unlock; /* nothing committing|committed */
346 atomic_inc(&cur_trans->use_count);
347 mutex_unlock(&root->fs_info->trans_mutex);
349 wait_for_commit(root, cur_trans);
351 mutex_lock(&root->fs_info->trans_mutex);
352 put_transaction(cur_trans);
355 mutex_unlock(&root->fs_info->trans_mutex);
361 * rate limit against the drop_snapshot code. This helps to slow down new
362 * operations if the drop_snapshot code isn't able to keep up.
364 static void throttle_on_drops(struct btrfs_root *root)
366 struct btrfs_fs_info *info = root->fs_info;
367 int harder_count = 0;
370 if (atomic_read(&info->throttles)) {
373 thr = atomic_read(&info->throttle_gen);
376 prepare_to_wait(&info->transaction_throttle,
377 &wait, TASK_UNINTERRUPTIBLE);
378 if (!atomic_read(&info->throttles)) {
379 finish_wait(&info->transaction_throttle, &wait);
383 finish_wait(&info->transaction_throttle, &wait);
384 } while (thr == atomic_read(&info->throttle_gen));
387 if (root->fs_info->total_ref_cache_size > 1 * 1024 * 1024 &&
391 if (root->fs_info->total_ref_cache_size > 5 * 1024 * 1024 &&
395 if (root->fs_info->total_ref_cache_size > 10 * 1024 * 1024 &&
402 void btrfs_throttle(struct btrfs_root *root)
404 mutex_lock(&root->fs_info->trans_mutex);
405 if (!root->fs_info->open_ioctl_trans)
406 wait_current_trans(root);
407 mutex_unlock(&root->fs_info->trans_mutex);
410 static int should_end_transaction(struct btrfs_trans_handle *trans,
411 struct btrfs_root *root)
414 ret = btrfs_block_rsv_check(trans, root,
415 &root->fs_info->global_block_rsv, 0, 5);
419 int btrfs_should_end_transaction(struct btrfs_trans_handle *trans,
420 struct btrfs_root *root)
422 struct btrfs_transaction *cur_trans = trans->transaction;
425 if (cur_trans->blocked || cur_trans->delayed_refs.flushing)
428 updates = trans->delayed_ref_updates;
429 trans->delayed_ref_updates = 0;
431 btrfs_run_delayed_refs(trans, root, updates);
433 return should_end_transaction(trans, root);
436 static int __btrfs_end_transaction(struct btrfs_trans_handle *trans,
437 struct btrfs_root *root, int throttle, int lock)
439 struct btrfs_transaction *cur_trans = trans->transaction;
440 struct btrfs_fs_info *info = root->fs_info;
443 if (--trans->use_count) {
444 trans->block_rsv = trans->orig_rsv;
449 unsigned long cur = trans->delayed_ref_updates;
450 trans->delayed_ref_updates = 0;
452 trans->transaction->delayed_refs.num_heads_ready > 64) {
453 trans->delayed_ref_updates = 0;
456 * do a full flush if the transaction is trying
459 if (trans->transaction->delayed_refs.flushing)
461 btrfs_run_delayed_refs(trans, root, cur);
468 btrfs_trans_release_metadata(trans, root);
470 if (lock && !root->fs_info->open_ioctl_trans &&
471 should_end_transaction(trans, root))
472 trans->transaction->blocked = 1;
474 if (lock && cur_trans->blocked && !cur_trans->in_commit) {
476 return btrfs_commit_transaction(trans, root);
478 wake_up_process(info->transaction_kthread);
481 WARN_ON(cur_trans != info->running_transaction);
482 WARN_ON(atomic_read(&cur_trans->num_writers) < 1);
483 atomic_dec(&cur_trans->num_writers);
486 if (waitqueue_active(&cur_trans->writer_wait))
487 wake_up(&cur_trans->writer_wait);
488 put_transaction(cur_trans);
490 if (current->journal_info == trans)
491 current->journal_info = NULL;
492 memset(trans, 0, sizeof(*trans));
493 kmem_cache_free(btrfs_trans_handle_cachep, trans);
496 btrfs_run_delayed_iputs(root);
501 int btrfs_end_transaction(struct btrfs_trans_handle *trans,
502 struct btrfs_root *root)
504 return __btrfs_end_transaction(trans, root, 0, 1);
507 int btrfs_end_transaction_throttle(struct btrfs_trans_handle *trans,
508 struct btrfs_root *root)
510 return __btrfs_end_transaction(trans, root, 1, 1);
513 int btrfs_end_transaction_nolock(struct btrfs_trans_handle *trans,
514 struct btrfs_root *root)
516 return __btrfs_end_transaction(trans, root, 0, 0);
520 * when btree blocks are allocated, they have some corresponding bits set for
521 * them in one of two extent_io trees. This is used to make sure all of
522 * those extents are sent to disk but does not wait on them
524 int btrfs_write_marked_extents(struct btrfs_root *root,
525 struct extent_io_tree *dirty_pages, int mark)
531 struct inode *btree_inode = root->fs_info->btree_inode;
537 ret = find_first_extent_bit(dirty_pages, start, &start, &end,
541 while (start <= end) {
544 index = start >> PAGE_CACHE_SHIFT;
545 start = (u64)(index + 1) << PAGE_CACHE_SHIFT;
546 page = find_get_page(btree_inode->i_mapping, index);
550 btree_lock_page_hook(page);
551 if (!page->mapping) {
553 page_cache_release(page);
557 if (PageWriteback(page)) {
559 wait_on_page_writeback(page);
562 page_cache_release(page);
566 err = write_one_page(page, 0);
569 page_cache_release(page);
578 * when btree blocks are allocated, they have some corresponding bits set for
579 * them in one of two extent_io trees. This is used to make sure all of
580 * those extents are on disk for transaction or log commit. We wait
581 * on all the pages and clear them from the dirty pages state tree
583 int btrfs_wait_marked_extents(struct btrfs_root *root,
584 struct extent_io_tree *dirty_pages, int mark)
590 struct inode *btree_inode = root->fs_info->btree_inode;
596 ret = find_first_extent_bit(dirty_pages, start, &start, &end,
601 clear_extent_bits(dirty_pages, start, end, mark, GFP_NOFS);
602 while (start <= end) {
603 index = start >> PAGE_CACHE_SHIFT;
604 start = (u64)(index + 1) << PAGE_CACHE_SHIFT;
605 page = find_get_page(btree_inode->i_mapping, index);
608 if (PageDirty(page)) {
609 btree_lock_page_hook(page);
610 wait_on_page_writeback(page);
611 err = write_one_page(page, 0);
615 wait_on_page_writeback(page);
616 page_cache_release(page);
626 * when btree blocks are allocated, they have some corresponding bits set for
627 * them in one of two extent_io trees. This is used to make sure all of
628 * those extents are on disk for transaction or log commit
630 int btrfs_write_and_wait_marked_extents(struct btrfs_root *root,
631 struct extent_io_tree *dirty_pages, int mark)
636 ret = btrfs_write_marked_extents(root, dirty_pages, mark);
637 ret2 = btrfs_wait_marked_extents(root, dirty_pages, mark);
641 int btrfs_write_and_wait_transaction(struct btrfs_trans_handle *trans,
642 struct btrfs_root *root)
644 if (!trans || !trans->transaction) {
645 struct inode *btree_inode;
646 btree_inode = root->fs_info->btree_inode;
647 return filemap_write_and_wait(btree_inode->i_mapping);
649 return btrfs_write_and_wait_marked_extents(root,
650 &trans->transaction->dirty_pages,
655 * this is used to update the root pointer in the tree of tree roots.
657 * But, in the case of the extent allocation tree, updating the root
658 * pointer may allocate blocks which may change the root of the extent
661 * So, this loops and repeats and makes sure the cowonly root didn't
662 * change while the root pointer was being updated in the metadata.
664 static int update_cowonly_root(struct btrfs_trans_handle *trans,
665 struct btrfs_root *root)
670 struct btrfs_root *tree_root = root->fs_info->tree_root;
672 old_root_used = btrfs_root_used(&root->root_item);
673 btrfs_write_dirty_block_groups(trans, root);
676 old_root_bytenr = btrfs_root_bytenr(&root->root_item);
677 if (old_root_bytenr == root->node->start &&
678 old_root_used == btrfs_root_used(&root->root_item))
681 btrfs_set_root_node(&root->root_item, root->node);
682 ret = btrfs_update_root(trans, tree_root,
687 old_root_used = btrfs_root_used(&root->root_item);
688 ret = btrfs_write_dirty_block_groups(trans, root);
692 if (root != root->fs_info->extent_root)
693 switch_commit_root(root);
699 * update all the cowonly tree roots on disk
701 static noinline int commit_cowonly_roots(struct btrfs_trans_handle *trans,
702 struct btrfs_root *root)
704 struct btrfs_fs_info *fs_info = root->fs_info;
705 struct list_head *next;
706 struct extent_buffer *eb;
709 ret = btrfs_run_delayed_refs(trans, root, (unsigned long)-1);
712 eb = btrfs_lock_root_node(fs_info->tree_root);
713 btrfs_cow_block(trans, fs_info->tree_root, eb, NULL, 0, &eb);
714 btrfs_tree_unlock(eb);
715 free_extent_buffer(eb);
717 ret = btrfs_run_delayed_refs(trans, root, (unsigned long)-1);
720 while (!list_empty(&fs_info->dirty_cowonly_roots)) {
721 next = fs_info->dirty_cowonly_roots.next;
723 root = list_entry(next, struct btrfs_root, dirty_list);
725 update_cowonly_root(trans, root);
728 down_write(&fs_info->extent_commit_sem);
729 switch_commit_root(fs_info->extent_root);
730 up_write(&fs_info->extent_commit_sem);
736 * dead roots are old snapshots that need to be deleted. This allocates
737 * a dirty root struct and adds it into the list of dead roots that need to
740 int btrfs_add_dead_root(struct btrfs_root *root)
742 mutex_lock(&root->fs_info->trans_mutex);
743 list_add(&root->root_list, &root->fs_info->dead_roots);
744 mutex_unlock(&root->fs_info->trans_mutex);
749 * update all the cowonly tree roots on disk
751 static noinline int commit_fs_roots(struct btrfs_trans_handle *trans,
752 struct btrfs_root *root)
754 struct btrfs_root *gang[8];
755 struct btrfs_fs_info *fs_info = root->fs_info;
761 ret = radix_tree_gang_lookup_tag(&fs_info->fs_roots_radix,
764 BTRFS_ROOT_TRANS_TAG);
767 for (i = 0; i < ret; i++) {
769 radix_tree_tag_clear(&fs_info->fs_roots_radix,
770 (unsigned long)root->root_key.objectid,
771 BTRFS_ROOT_TRANS_TAG);
773 btrfs_free_log(trans, root);
774 btrfs_update_reloc_root(trans, root);
775 btrfs_orphan_commit_root(trans, root);
777 if (root->commit_root != root->node) {
778 switch_commit_root(root);
779 btrfs_set_root_node(&root->root_item,
783 err = btrfs_update_root(trans, fs_info->tree_root,
794 * defrag a given btree. If cacheonly == 1, this won't read from the disk,
795 * otherwise every leaf in the btree is read and defragged.
797 int btrfs_defrag_root(struct btrfs_root *root, int cacheonly)
799 struct btrfs_fs_info *info = root->fs_info;
800 struct btrfs_trans_handle *trans;
804 if (xchg(&root->defrag_running, 1))
808 trans = btrfs_start_transaction(root, 0);
810 return PTR_ERR(trans);
812 ret = btrfs_defrag_leaves(trans, root, cacheonly);
814 nr = trans->blocks_used;
815 btrfs_end_transaction(trans, root);
816 btrfs_btree_balance_dirty(info->tree_root, nr);
819 if (root->fs_info->closing || ret != -EAGAIN)
822 root->defrag_running = 0;
828 * when dropping snapshots, we generate a ton of delayed refs, and it makes
829 * sense not to join the transaction while it is trying to flush the current
830 * queue of delayed refs out.
832 * This is used by the drop snapshot code only
834 static noinline int wait_transaction_pre_flush(struct btrfs_fs_info *info)
838 mutex_lock(&info->trans_mutex);
839 while (info->running_transaction &&
840 info->running_transaction->delayed_refs.flushing) {
841 prepare_to_wait(&info->transaction_wait, &wait,
842 TASK_UNINTERRUPTIBLE);
843 mutex_unlock(&info->trans_mutex);
847 mutex_lock(&info->trans_mutex);
848 finish_wait(&info->transaction_wait, &wait);
850 mutex_unlock(&info->trans_mutex);
855 * Given a list of roots that need to be deleted, call btrfs_drop_snapshot on
858 int btrfs_drop_dead_root(struct btrfs_root *root)
860 struct btrfs_trans_handle *trans;
861 struct btrfs_root *tree_root = root->fs_info->tree_root;
867 * we don't want to jump in and create a bunch of
868 * delayed refs if the transaction is starting to close
870 wait_transaction_pre_flush(tree_root->fs_info);
871 trans = btrfs_start_transaction(tree_root, 1);
874 * we've joined a transaction, make sure it isn't
877 if (trans->transaction->delayed_refs.flushing) {
878 btrfs_end_transaction(trans, tree_root);
882 ret = btrfs_drop_snapshot(trans, root);
886 ret = btrfs_update_root(trans, tree_root,
892 nr = trans->blocks_used;
893 ret = btrfs_end_transaction(trans, tree_root);
896 btrfs_btree_balance_dirty(tree_root, nr);
901 ret = btrfs_del_root(trans, tree_root, &root->root_key);
904 nr = trans->blocks_used;
905 ret = btrfs_end_transaction(trans, tree_root);
908 free_extent_buffer(root->node);
909 free_extent_buffer(root->commit_root);
912 btrfs_btree_balance_dirty(tree_root, nr);
918 * new snapshots need to be created at a very specific time in the
919 * transaction commit. This does the actual creation
921 static noinline int create_pending_snapshot(struct btrfs_trans_handle *trans,
922 struct btrfs_fs_info *fs_info,
923 struct btrfs_pending_snapshot *pending)
925 struct btrfs_key key;
926 struct btrfs_root_item *new_root_item;
927 struct btrfs_root *tree_root = fs_info->tree_root;
928 struct btrfs_root *root = pending->root;
929 struct btrfs_root *parent_root;
930 struct inode *parent_inode;
931 struct dentry *parent;
932 struct dentry *dentry;
933 struct extent_buffer *tmp;
934 struct extent_buffer *old;
941 new_root_item = kmalloc(sizeof(*new_root_item), GFP_NOFS);
942 if (!new_root_item) {
943 pending->error = -ENOMEM;
947 ret = btrfs_find_free_objectid(trans, tree_root, 0, &objectid);
949 pending->error = ret;
953 btrfs_reloc_pre_snapshot(trans, pending, &to_reserve);
954 btrfs_orphan_pre_snapshot(trans, pending, &to_reserve);
956 if (to_reserve > 0) {
957 ret = btrfs_block_rsv_add(trans, root, &pending->block_rsv,
960 pending->error = ret;
965 key.objectid = objectid;
966 key.offset = (u64)-1;
967 key.type = BTRFS_ROOT_ITEM_KEY;
969 trans->block_rsv = &pending->block_rsv;
971 dentry = pending->dentry;
972 parent = dget_parent(dentry);
973 parent_inode = parent->d_inode;
974 parent_root = BTRFS_I(parent_inode)->root;
975 record_root_in_trans(trans, parent_root);
978 * insert the directory item
980 ret = btrfs_set_inode_index(parent_inode, &index);
982 ret = btrfs_insert_dir_item(trans, parent_root,
983 dentry->d_name.name, dentry->d_name.len,
984 parent_inode->i_ino, &key,
985 BTRFS_FT_DIR, index);
988 btrfs_i_size_write(parent_inode, parent_inode->i_size +
989 dentry->d_name.len * 2);
990 ret = btrfs_update_inode(trans, parent_root, parent_inode);
993 record_root_in_trans(trans, root);
994 btrfs_set_root_last_snapshot(&root->root_item, trans->transid);
995 memcpy(new_root_item, &root->root_item, sizeof(*new_root_item));
996 btrfs_check_and_init_root_item(new_root_item);
998 root_flags = btrfs_root_flags(new_root_item);
999 if (pending->readonly)
1000 root_flags |= BTRFS_ROOT_SUBVOL_RDONLY;
1002 root_flags &= ~BTRFS_ROOT_SUBVOL_RDONLY;
1003 btrfs_set_root_flags(new_root_item, root_flags);
1005 old = btrfs_lock_root_node(root);
1006 btrfs_cow_block(trans, root, old, NULL, 0, &old);
1007 btrfs_set_lock_blocking(old);
1009 btrfs_copy_root(trans, root, old, &tmp, objectid);
1010 btrfs_tree_unlock(old);
1011 free_extent_buffer(old);
1013 btrfs_set_root_node(new_root_item, tmp);
1014 /* record when the snapshot was created in key.offset */
1015 key.offset = trans->transid;
1016 ret = btrfs_insert_root(trans, tree_root, &key, new_root_item);
1017 btrfs_tree_unlock(tmp);
1018 free_extent_buffer(tmp);
1022 * insert root back/forward references
1024 ret = btrfs_add_root_ref(trans, tree_root, objectid,
1025 parent_root->root_key.objectid,
1026 parent_inode->i_ino, index,
1027 dentry->d_name.name, dentry->d_name.len);
1031 key.offset = (u64)-1;
1032 pending->snap = btrfs_read_fs_root_no_name(root->fs_info, &key);
1033 BUG_ON(IS_ERR(pending->snap));
1035 btrfs_reloc_post_snapshot(trans, pending);
1036 btrfs_orphan_post_snapshot(trans, pending);
1038 kfree(new_root_item);
1039 btrfs_block_rsv_release(root, &pending->block_rsv, (u64)-1);
1044 * create all the snapshots we've scheduled for creation
1046 static noinline int create_pending_snapshots(struct btrfs_trans_handle *trans,
1047 struct btrfs_fs_info *fs_info)
1049 struct btrfs_pending_snapshot *pending;
1050 struct list_head *head = &trans->transaction->pending_snapshots;
1053 list_for_each_entry(pending, head, list) {
1054 ret = create_pending_snapshot(trans, fs_info, pending);
1060 static void update_super_roots(struct btrfs_root *root)
1062 struct btrfs_root_item *root_item;
1063 struct btrfs_super_block *super;
1065 super = &root->fs_info->super_copy;
1067 root_item = &root->fs_info->chunk_root->root_item;
1068 super->chunk_root = root_item->bytenr;
1069 super->chunk_root_generation = root_item->generation;
1070 super->chunk_root_level = root_item->level;
1072 root_item = &root->fs_info->tree_root->root_item;
1073 super->root = root_item->bytenr;
1074 super->generation = root_item->generation;
1075 super->root_level = root_item->level;
1076 if (super->cache_generation != 0 || btrfs_test_opt(root, SPACE_CACHE))
1077 super->cache_generation = root_item->generation;
1080 int btrfs_transaction_in_commit(struct btrfs_fs_info *info)
1083 spin_lock(&info->new_trans_lock);
1084 if (info->running_transaction)
1085 ret = info->running_transaction->in_commit;
1086 spin_unlock(&info->new_trans_lock);
1090 int btrfs_transaction_blocked(struct btrfs_fs_info *info)
1093 spin_lock(&info->new_trans_lock);
1094 if (info->running_transaction)
1095 ret = info->running_transaction->blocked;
1096 spin_unlock(&info->new_trans_lock);
1101 * wait for the current transaction commit to start and block subsequent
1104 static void wait_current_trans_commit_start(struct btrfs_root *root,
1105 struct btrfs_transaction *trans)
1109 if (trans->in_commit)
1113 prepare_to_wait(&root->fs_info->transaction_blocked_wait, &wait,
1114 TASK_UNINTERRUPTIBLE);
1115 if (trans->in_commit) {
1116 finish_wait(&root->fs_info->transaction_blocked_wait,
1120 mutex_unlock(&root->fs_info->trans_mutex);
1122 mutex_lock(&root->fs_info->trans_mutex);
1123 finish_wait(&root->fs_info->transaction_blocked_wait, &wait);
1128 * wait for the current transaction to start and then become unblocked.
1131 static void wait_current_trans_commit_start_and_unblock(struct btrfs_root *root,
1132 struct btrfs_transaction *trans)
1136 if (trans->commit_done || (trans->in_commit && !trans->blocked))
1140 prepare_to_wait(&root->fs_info->transaction_wait, &wait,
1141 TASK_UNINTERRUPTIBLE);
1142 if (trans->commit_done ||
1143 (trans->in_commit && !trans->blocked)) {
1144 finish_wait(&root->fs_info->transaction_wait,
1148 mutex_unlock(&root->fs_info->trans_mutex);
1150 mutex_lock(&root->fs_info->trans_mutex);
1151 finish_wait(&root->fs_info->transaction_wait,
1157 * commit transactions asynchronously. once btrfs_commit_transaction_async
1158 * returns, any subsequent transaction will not be allowed to join.
1160 struct btrfs_async_commit {
1161 struct btrfs_trans_handle *newtrans;
1162 struct btrfs_root *root;
1163 struct delayed_work work;
1166 static void do_async_commit(struct work_struct *work)
1168 struct btrfs_async_commit *ac =
1169 container_of(work, struct btrfs_async_commit, work.work);
1171 btrfs_commit_transaction(ac->newtrans, ac->root);
1175 int btrfs_commit_transaction_async(struct btrfs_trans_handle *trans,
1176 struct btrfs_root *root,
1177 int wait_for_unblock)
1179 struct btrfs_async_commit *ac;
1180 struct btrfs_transaction *cur_trans;
1182 ac = kmalloc(sizeof(*ac), GFP_NOFS);
1186 INIT_DELAYED_WORK(&ac->work, do_async_commit);
1188 ac->newtrans = btrfs_join_transaction(root);
1189 if (IS_ERR(ac->newtrans)) {
1190 int err = PTR_ERR(ac->newtrans);
1195 /* take transaction reference */
1196 mutex_lock(&root->fs_info->trans_mutex);
1197 cur_trans = trans->transaction;
1198 atomic_inc(&cur_trans->use_count);
1199 mutex_unlock(&root->fs_info->trans_mutex);
1201 btrfs_end_transaction(trans, root);
1202 schedule_delayed_work(&ac->work, 0);
1204 /* wait for transaction to start and unblock */
1205 mutex_lock(&root->fs_info->trans_mutex);
1206 if (wait_for_unblock)
1207 wait_current_trans_commit_start_and_unblock(root, cur_trans);
1209 wait_current_trans_commit_start(root, cur_trans);
1210 put_transaction(cur_trans);
1211 mutex_unlock(&root->fs_info->trans_mutex);
1217 * btrfs_transaction state sequence:
1218 * in_commit = 0, blocked = 0 (initial)
1219 * in_commit = 1, blocked = 1
1223 int btrfs_commit_transaction(struct btrfs_trans_handle *trans,
1224 struct btrfs_root *root)
1226 unsigned long joined = 0;
1227 struct btrfs_transaction *cur_trans;
1228 struct btrfs_transaction *prev_trans = NULL;
1231 int should_grow = 0;
1232 unsigned long now = get_seconds();
1233 int flush_on_commit = btrfs_test_opt(root, FLUSHONCOMMIT);
1235 btrfs_run_ordered_operations(root, 0);
1237 /* make a pass through all the delayed refs we have so far
1238 * any runnings procs may add more while we are here
1240 ret = btrfs_run_delayed_refs(trans, root, 0);
1243 btrfs_trans_release_metadata(trans, root);
1245 cur_trans = trans->transaction;
1247 * set the flushing flag so procs in this transaction have to
1248 * start sending their work down.
1250 cur_trans->delayed_refs.flushing = 1;
1252 ret = btrfs_run_delayed_refs(trans, root, 0);
1255 mutex_lock(&root->fs_info->trans_mutex);
1256 if (cur_trans->in_commit) {
1257 atomic_inc(&cur_trans->use_count);
1258 mutex_unlock(&root->fs_info->trans_mutex);
1259 btrfs_end_transaction(trans, root);
1261 ret = wait_for_commit(root, cur_trans);
1264 mutex_lock(&root->fs_info->trans_mutex);
1265 put_transaction(cur_trans);
1266 mutex_unlock(&root->fs_info->trans_mutex);
1271 trans->transaction->in_commit = 1;
1272 trans->transaction->blocked = 1;
1273 wake_up(&root->fs_info->transaction_blocked_wait);
1275 if (cur_trans->list.prev != &root->fs_info->trans_list) {
1276 prev_trans = list_entry(cur_trans->list.prev,
1277 struct btrfs_transaction, list);
1278 if (!prev_trans->commit_done) {
1279 atomic_inc(&prev_trans->use_count);
1280 mutex_unlock(&root->fs_info->trans_mutex);
1282 wait_for_commit(root, prev_trans);
1284 mutex_lock(&root->fs_info->trans_mutex);
1285 put_transaction(prev_trans);
1289 if (now < cur_trans->start_time || now - cur_trans->start_time < 1)
1293 int snap_pending = 0;
1294 joined = cur_trans->num_joined;
1295 if (!list_empty(&trans->transaction->pending_snapshots))
1298 WARN_ON(cur_trans != trans->transaction);
1299 mutex_unlock(&root->fs_info->trans_mutex);
1301 if (flush_on_commit || snap_pending) {
1302 btrfs_start_delalloc_inodes(root, 1);
1303 ret = btrfs_wait_ordered_extents(root, 0, 1);
1308 * rename don't use btrfs_join_transaction, so, once we
1309 * set the transaction to blocked above, we aren't going
1310 * to get any new ordered operations. We can safely run
1311 * it here and no for sure that nothing new will be added
1314 btrfs_run_ordered_operations(root, 1);
1316 prepare_to_wait(&cur_trans->writer_wait, &wait,
1317 TASK_UNINTERRUPTIBLE);
1320 if (atomic_read(&cur_trans->num_writers) > 1)
1321 schedule_timeout(MAX_SCHEDULE_TIMEOUT);
1322 else if (should_grow)
1323 schedule_timeout(1);
1325 mutex_lock(&root->fs_info->trans_mutex);
1326 finish_wait(&cur_trans->writer_wait, &wait);
1327 } while (atomic_read(&cur_trans->num_writers) > 1 ||
1328 (should_grow && cur_trans->num_joined != joined));
1330 ret = create_pending_snapshots(trans, root->fs_info);
1333 ret = btrfs_run_delayed_refs(trans, root, (unsigned long)-1);
1336 WARN_ON(cur_trans != trans->transaction);
1338 /* btrfs_commit_tree_roots is responsible for getting the
1339 * various roots consistent with each other. Every pointer
1340 * in the tree of tree roots has to point to the most up to date
1341 * root for every subvolume and other tree. So, we have to keep
1342 * the tree logging code from jumping in and changing any
1345 * At this point in the commit, there can't be any tree-log
1346 * writers, but a little lower down we drop the trans mutex
1347 * and let new people in. By holding the tree_log_mutex
1348 * from now until after the super is written, we avoid races
1349 * with the tree-log code.
1351 mutex_lock(&root->fs_info->tree_log_mutex);
1353 ret = commit_fs_roots(trans, root);
1356 /* commit_fs_roots gets rid of all the tree log roots, it is now
1357 * safe to free the root of tree log roots
1359 btrfs_free_log_root_tree(trans, root->fs_info);
1361 ret = commit_cowonly_roots(trans, root);
1364 btrfs_prepare_extent_commit(trans, root);
1366 cur_trans = root->fs_info->running_transaction;
1367 spin_lock(&root->fs_info->new_trans_lock);
1368 root->fs_info->running_transaction = NULL;
1369 spin_unlock(&root->fs_info->new_trans_lock);
1371 btrfs_set_root_node(&root->fs_info->tree_root->root_item,
1372 root->fs_info->tree_root->node);
1373 switch_commit_root(root->fs_info->tree_root);
1375 btrfs_set_root_node(&root->fs_info->chunk_root->root_item,
1376 root->fs_info->chunk_root->node);
1377 switch_commit_root(root->fs_info->chunk_root);
1379 update_super_roots(root);
1381 if (!root->fs_info->log_root_recovering) {
1382 btrfs_set_super_log_root(&root->fs_info->super_copy, 0);
1383 btrfs_set_super_log_root_level(&root->fs_info->super_copy, 0);
1386 memcpy(&root->fs_info->super_for_commit, &root->fs_info->super_copy,
1387 sizeof(root->fs_info->super_copy));
1389 trans->transaction->blocked = 0;
1391 wake_up(&root->fs_info->transaction_wait);
1393 mutex_unlock(&root->fs_info->trans_mutex);
1394 ret = btrfs_write_and_wait_transaction(trans, root);
1396 write_ctree_super(trans, root, 0);
1399 * the super is written, we can safely allow the tree-loggers
1400 * to go about their business
1402 mutex_unlock(&root->fs_info->tree_log_mutex);
1404 btrfs_finish_extent_commit(trans, root);
1406 mutex_lock(&root->fs_info->trans_mutex);
1408 cur_trans->commit_done = 1;
1410 root->fs_info->last_trans_committed = cur_trans->transid;
1412 wake_up(&cur_trans->commit_wait);
1414 list_del_init(&cur_trans->list);
1415 put_transaction(cur_trans);
1416 put_transaction(cur_trans);
1418 trace_btrfs_transaction_commit(root);
1420 mutex_unlock(&root->fs_info->trans_mutex);
1422 if (current->journal_info == trans)
1423 current->journal_info = NULL;
1425 kmem_cache_free(btrfs_trans_handle_cachep, trans);
1427 if (current != root->fs_info->transaction_kthread)
1428 btrfs_run_delayed_iputs(root);
1434 * interface function to delete all the snapshots we have scheduled for deletion
1436 int btrfs_clean_old_snapshots(struct btrfs_root *root)
1439 struct btrfs_fs_info *fs_info = root->fs_info;
1441 mutex_lock(&fs_info->trans_mutex);
1442 list_splice_init(&fs_info->dead_roots, &list);
1443 mutex_unlock(&fs_info->trans_mutex);
1445 while (!list_empty(&list)) {
1446 root = list_entry(list.next, struct btrfs_root, root_list);
1447 list_del(&root->root_list);
1449 if (btrfs_header_backref_rev(root->node) <
1450 BTRFS_MIXED_BACKREF_REV)
1451 btrfs_drop_snapshot(root, NULL, 0);
1453 btrfs_drop_snapshot(root, NULL, 1);