blob: 84c4e278ce2e051b00a0b76f3acc3f564c7e1ac6 [file] [log] [blame]
Chris Mason79154b12007-03-22 15:59:16 -04001#include <linux/module.h>
2#include <linux/fs.h>
3#include "ctree.h"
4#include "disk-io.h"
5#include "transaction.h"
6
Chris Mason78fae272007-03-25 11:35:08 -04007static int total_trans = 0;
Chris Mason79154b12007-03-22 15:59:16 -04008static void put_transaction(struct btrfs_transaction *transaction)
9{
10 transaction->use_count--;
Chris Mason78fae272007-03-25 11:35:08 -040011 if (transaction->use_count == 0) {
12 WARN_ON(total_trans == 0);
13 total_trans--;
Chris Mason79154b12007-03-22 15:59:16 -040014 kfree(transaction);
Chris Mason78fae272007-03-25 11:35:08 -040015 }
Chris Mason79154b12007-03-22 15:59:16 -040016}
17
18static int join_transaction(struct btrfs_root *root)
19{
20 struct btrfs_transaction *cur_trans;
21 cur_trans = root->fs_info->running_transaction;
22 if (!cur_trans) {
23 cur_trans = kmalloc(sizeof(*cur_trans), GFP_NOFS);
Chris Mason78fae272007-03-25 11:35:08 -040024 total_trans++;
Chris Mason79154b12007-03-22 15:59:16 -040025 BUG_ON(!cur_trans);
26 root->fs_info->running_transaction = cur_trans;
27 cur_trans->num_writers = 0;
28 cur_trans->transid = root->root_key.offset + 1;
29 init_waitqueue_head(&cur_trans->writer_wait);
30 init_waitqueue_head(&cur_trans->commit_wait);
31 cur_trans->in_commit = 0;
Chris Masond5719762007-03-23 10:01:08 -040032 cur_trans->use_count = 1;
Chris Mason79154b12007-03-22 15:59:16 -040033 cur_trans->commit_done = 0;
34 }
35 cur_trans->num_writers++;
36 return 0;
37}
38
39struct btrfs_trans_handle *btrfs_start_transaction(struct btrfs_root *root,
40 int num_blocks)
41{
42 struct btrfs_trans_handle *h = kmalloc(sizeof(*h), GFP_NOFS);
43 int ret;
44
45 mutex_lock(&root->fs_info->trans_mutex);
46 ret = join_transaction(root);
47 BUG_ON(ret);
48 h->transid = root->fs_info->running_transaction->transid;
49 h->transaction = root->fs_info->running_transaction;
50 h->blocks_reserved = num_blocks;
51 h->blocks_used = 0;
52 root->fs_info->running_transaction->use_count++;
53 mutex_unlock(&root->fs_info->trans_mutex);
54 return h;
55}
56
57int btrfs_end_transaction(struct btrfs_trans_handle *trans,
58 struct btrfs_root *root)
59{
60 struct btrfs_transaction *cur_trans;
61 mutex_lock(&root->fs_info->trans_mutex);
62 cur_trans = root->fs_info->running_transaction;
Chris Masond5719762007-03-23 10:01:08 -040063 WARN_ON(cur_trans->num_writers < 1);
Chris Mason79154b12007-03-22 15:59:16 -040064 if (waitqueue_active(&cur_trans->writer_wait))
65 wake_up(&cur_trans->writer_wait);
66 cur_trans->num_writers--;
67 put_transaction(cur_trans);
68 mutex_unlock(&root->fs_info->trans_mutex);
Chris Masond6025572007-03-30 14:27:56 -040069 memset(trans, 0, sizeof(*trans));
Chris Mason79154b12007-03-22 15:59:16 -040070 kfree(trans);
71 return 0;
72}
73
74
75int btrfs_write_and_wait_transaction(struct btrfs_trans_handle *trans,
76 struct btrfs_root *root)
77{
Chris Masond98237b2007-03-28 13:57:48 -040078 filemap_write_and_wait(root->fs_info->btree_inode->i_mapping);
Chris Mason79154b12007-03-22 15:59:16 -040079 return 0;
80}
81
82int btrfs_commit_tree_roots(struct btrfs_trans_handle *trans,
83 struct btrfs_root *root)
84{
85 int ret;
86 u64 old_extent_block;
87 struct btrfs_fs_info *fs_info = root->fs_info;
88 struct btrfs_root *tree_root = fs_info->tree_root;
89 struct btrfs_root *extent_root = fs_info->extent_root;
90 struct btrfs_root *inode_root = fs_info->inode_root;
91
92 btrfs_set_root_blocknr(&inode_root->root_item,
93 inode_root->node->b_blocknr);
94 ret = btrfs_update_root(trans, tree_root,
95 &inode_root->root_key,
96 &inode_root->root_item);
97 BUG_ON(ret);
98 while(1) {
99 old_extent_block = btrfs_root_blocknr(&extent_root->root_item);
100 if (old_extent_block == extent_root->node->b_blocknr)
101 break;
102 btrfs_set_root_blocknr(&extent_root->root_item,
103 extent_root->node->b_blocknr);
104 ret = btrfs_update_root(trans, tree_root,
105 &extent_root->root_key,
106 &extent_root->root_item);
107 BUG_ON(ret);
108 }
109 return 0;
110}
111
112static int wait_for_commit(struct btrfs_root *root,
113 struct btrfs_transaction *commit)
114{
115 DEFINE_WAIT(wait);
Chris Mason79154b12007-03-22 15:59:16 -0400116 while(!commit->commit_done) {
117 prepare_to_wait(&commit->commit_wait, &wait,
118 TASK_UNINTERRUPTIBLE);
119 if (commit->commit_done)
120 break;
121 mutex_unlock(&root->fs_info->trans_mutex);
122 schedule();
123 mutex_lock(&root->fs_info->trans_mutex);
124 }
125 finish_wait(&commit->commit_wait, &wait);
126 return 0;
127}
128
129int btrfs_commit_transaction(struct btrfs_trans_handle *trans,
130 struct btrfs_root *root)
131{
132 int ret = 0;
Chris Mason78fae272007-03-25 11:35:08 -0400133 struct buffer_head *snap;
Chris Mason79154b12007-03-22 15:59:16 -0400134 struct btrfs_key snap_key;
135 struct btrfs_transaction *cur_trans;
136 DEFINE_WAIT(wait);
137
138 mutex_lock(&root->fs_info->trans_mutex);
139 if (trans->transaction->in_commit) {
140 cur_trans = trans->transaction;
141 trans->transaction->use_count++;
142 btrfs_end_transaction(trans, root);
143 ret = wait_for_commit(root, cur_trans);
144 BUG_ON(ret);
145 put_transaction(cur_trans);
146 mutex_unlock(&root->fs_info->trans_mutex);
147 return 0;
148 }
149 while (trans->transaction->num_writers > 1) {
150 prepare_to_wait(&trans->transaction->writer_wait, &wait,
151 TASK_UNINTERRUPTIBLE);
152 if (trans->transaction->num_writers <= 1)
153 break;
154 mutex_unlock(&root->fs_info->trans_mutex);
155 schedule();
156 mutex_lock(&root->fs_info->trans_mutex);
157 }
158 finish_wait(&trans->transaction->writer_wait, &wait);
159
Chris Masond5719762007-03-23 10:01:08 -0400160 if (root->node != root->commit_root) {
161 memcpy(&snap_key, &root->root_key, sizeof(snap_key));
162 root->root_key.offset++;
163 }
164
Chris Mason79154b12007-03-22 15:59:16 -0400165
166 if (btrfs_root_blocknr(&root->root_item) != root->node->b_blocknr) {
167 btrfs_set_root_blocknr(&root->root_item, root->node->b_blocknr);
168 ret = btrfs_insert_root(trans, root->fs_info->tree_root,
169 &root->root_key, &root->root_item);
170 BUG_ON(ret);
171 }
172
173 ret = btrfs_commit_tree_roots(trans, root);
174 BUG_ON(ret);
175
Chris Mason78fae272007-03-25 11:35:08 -0400176 cur_trans = root->fs_info->running_transaction;
177 root->fs_info->running_transaction = NULL;
178 mutex_unlock(&root->fs_info->trans_mutex);
179
Chris Mason79154b12007-03-22 15:59:16 -0400180 ret = btrfs_write_and_wait_transaction(trans, root);
181 BUG_ON(ret);
182
183 write_ctree_super(trans, root);
Chris Mason78fae272007-03-25 11:35:08 -0400184 btrfs_finish_extent_commit(trans, root);
185 mutex_lock(&root->fs_info->trans_mutex);
Chris Mason79154b12007-03-22 15:59:16 -0400186 put_transaction(cur_trans);
Chris Mason78fae272007-03-25 11:35:08 -0400187 put_transaction(cur_trans);
188 mutex_unlock(&root->fs_info->trans_mutex);
Chris Mason79154b12007-03-22 15:59:16 -0400189 kfree(trans);
190
191 if (root->node != root->commit_root) {
192 trans = btrfs_start_transaction(root, 1);
Chris Mason78fae272007-03-25 11:35:08 -0400193 snap = root->commit_root;
Chris Mason79154b12007-03-22 15:59:16 -0400194 root->commit_root = root->node;
195 get_bh(root->node);
196 ret = btrfs_drop_snapshot(trans, root, snap);
197 BUG_ON(ret);
198
199 ret = btrfs_del_root(trans, root->fs_info->tree_root,
200 &snap_key);
Chris Mason22b0ebd2007-03-30 08:47:31 -0400201 BUG_ON(ret);
202 root->fs_info->generation = root->root_key.offset + 1;
203 ret = btrfs_end_transaction(trans, root);
204 BUG_ON(ret);
Chris Mason79154b12007-03-22 15:59:16 -0400205 }
206
207 return ret;
208}
209