blob: 20d84bd0322387a3a99f007f7f04cc2331cd6aa1 [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 Mason2c90e5d2007-04-02 10:50:19 -04008extern struct kmem_cache *btrfs_trans_handle_cachep;
9extern struct kmem_cache *btrfs_transaction_cachep;
10
11#define TRANS_MAGIC 0xE1E10E
Chris Mason79154b12007-03-22 15:59:16 -040012static void put_transaction(struct btrfs_transaction *transaction)
13{
Chris Mason2c90e5d2007-04-02 10:50:19 -040014 WARN_ON(transaction->use_count == 0);
Chris Mason79154b12007-03-22 15:59:16 -040015 transaction->use_count--;
Chris Mason2c90e5d2007-04-02 10:50:19 -040016 WARN_ON(transaction->magic != TRANS_MAGIC);
Chris Mason78fae272007-03-25 11:35:08 -040017 if (transaction->use_count == 0) {
18 WARN_ON(total_trans == 0);
19 total_trans--;
Chris Mason2c90e5d2007-04-02 10:50:19 -040020 memset(transaction, 0, sizeof(*transaction));
21 kmem_cache_free(btrfs_transaction_cachep, transaction);
Chris Mason78fae272007-03-25 11:35:08 -040022 }
Chris Mason79154b12007-03-22 15:59:16 -040023}
24
25static int join_transaction(struct btrfs_root *root)
26{
27 struct btrfs_transaction *cur_trans;
28 cur_trans = root->fs_info->running_transaction;
29 if (!cur_trans) {
Chris Mason2c90e5d2007-04-02 10:50:19 -040030 cur_trans = kmem_cache_alloc(btrfs_transaction_cachep,
31 GFP_NOFS);
Chris Mason78fae272007-03-25 11:35:08 -040032 total_trans++;
Chris Mason79154b12007-03-22 15:59:16 -040033 BUG_ON(!cur_trans);
34 root->fs_info->running_transaction = cur_trans;
35 cur_trans->num_writers = 0;
36 cur_trans->transid = root->root_key.offset + 1;
37 init_waitqueue_head(&cur_trans->writer_wait);
38 init_waitqueue_head(&cur_trans->commit_wait);
Chris Mason2c90e5d2007-04-02 10:50:19 -040039 cur_trans->magic = TRANS_MAGIC;
Chris Mason79154b12007-03-22 15:59:16 -040040 cur_trans->in_commit = 0;
Chris Masond5719762007-03-23 10:01:08 -040041 cur_trans->use_count = 1;
Chris Mason79154b12007-03-22 15:59:16 -040042 cur_trans->commit_done = 0;
43 }
44 cur_trans->num_writers++;
45 return 0;
46}
47
48struct btrfs_trans_handle *btrfs_start_transaction(struct btrfs_root *root,
49 int num_blocks)
50{
Chris Mason2c90e5d2007-04-02 10:50:19 -040051 struct btrfs_trans_handle *h =
52 kmem_cache_alloc(btrfs_trans_handle_cachep, GFP_NOFS);
Chris Mason79154b12007-03-22 15:59:16 -040053 int ret;
54
Chris Masond6e4a422007-04-06 15:37:36 -040055 /* FIXME, use the right root */
56 root = root->fs_info->fs_root;
Chris Mason79154b12007-03-22 15:59:16 -040057 mutex_lock(&root->fs_info->trans_mutex);
58 ret = join_transaction(root);
59 BUG_ON(ret);
60 h->transid = root->fs_info->running_transaction->transid;
61 h->transaction = root->fs_info->running_transaction;
62 h->blocks_reserved = num_blocks;
63 h->blocks_used = 0;
64 root->fs_info->running_transaction->use_count++;
65 mutex_unlock(&root->fs_info->trans_mutex);
Chris Mason2c90e5d2007-04-02 10:50:19 -040066 h->magic = h->magic2 = TRANS_MAGIC;
Chris Mason79154b12007-03-22 15:59:16 -040067 return h;
68}
69
70int btrfs_end_transaction(struct btrfs_trans_handle *trans,
71 struct btrfs_root *root)
72{
73 struct btrfs_transaction *cur_trans;
Chris Masond6e4a422007-04-06 15:37:36 -040074
75 /* FIXME, use the right root */
76 root = root->fs_info->fs_root;
77
Chris Mason2c90e5d2007-04-02 10:50:19 -040078 WARN_ON(trans->magic != TRANS_MAGIC);
79 WARN_ON(trans->magic2 != TRANS_MAGIC);
Chris Mason79154b12007-03-22 15:59:16 -040080 mutex_lock(&root->fs_info->trans_mutex);
81 cur_trans = root->fs_info->running_transaction;
Chris Masond5719762007-03-23 10:01:08 -040082 WARN_ON(cur_trans->num_writers < 1);
Chris Mason79154b12007-03-22 15:59:16 -040083 if (waitqueue_active(&cur_trans->writer_wait))
84 wake_up(&cur_trans->writer_wait);
85 cur_trans->num_writers--;
86 put_transaction(cur_trans);
87 mutex_unlock(&root->fs_info->trans_mutex);
Chris Masond6025572007-03-30 14:27:56 -040088 memset(trans, 0, sizeof(*trans));
Chris Mason2c90e5d2007-04-02 10:50:19 -040089 kmem_cache_free(btrfs_trans_handle_cachep, trans);
Chris Mason79154b12007-03-22 15:59:16 -040090 return 0;
91}
92
93
94int btrfs_write_and_wait_transaction(struct btrfs_trans_handle *trans,
95 struct btrfs_root *root)
96{
Chris Mason7cfcc172007-04-02 14:53:59 -040097 filemap_write_and_wait(root->fs_info->btree_inode->i_mapping);
Chris Mason79154b12007-03-22 15:59:16 -040098 return 0;
99}
100
101int btrfs_commit_tree_roots(struct btrfs_trans_handle *trans,
102 struct btrfs_root *root)
103{
104 int ret;
105 u64 old_extent_block;
106 struct btrfs_fs_info *fs_info = root->fs_info;
107 struct btrfs_root *tree_root = fs_info->tree_root;
108 struct btrfs_root *extent_root = fs_info->extent_root;
109 struct btrfs_root *inode_root = fs_info->inode_root;
110
111 btrfs_set_root_blocknr(&inode_root->root_item,
112 inode_root->node->b_blocknr);
113 ret = btrfs_update_root(trans, tree_root,
114 &inode_root->root_key,
115 &inode_root->root_item);
116 BUG_ON(ret);
117 while(1) {
118 old_extent_block = btrfs_root_blocknr(&extent_root->root_item);
119 if (old_extent_block == extent_root->node->b_blocknr)
120 break;
121 btrfs_set_root_blocknr(&extent_root->root_item,
122 extent_root->node->b_blocknr);
123 ret = btrfs_update_root(trans, tree_root,
124 &extent_root->root_key,
125 &extent_root->root_item);
126 BUG_ON(ret);
127 }
128 return 0;
129}
130
131static int wait_for_commit(struct btrfs_root *root,
132 struct btrfs_transaction *commit)
133{
134 DEFINE_WAIT(wait);
Chris Mason79154b12007-03-22 15:59:16 -0400135 while(!commit->commit_done) {
136 prepare_to_wait(&commit->commit_wait, &wait,
137 TASK_UNINTERRUPTIBLE);
138 if (commit->commit_done)
139 break;
140 mutex_unlock(&root->fs_info->trans_mutex);
141 schedule();
142 mutex_lock(&root->fs_info->trans_mutex);
143 }
144 finish_wait(&commit->commit_wait, &wait);
145 return 0;
146}
147
148int btrfs_commit_transaction(struct btrfs_trans_handle *trans,
149 struct btrfs_root *root)
150{
151 int ret = 0;
Chris Mason78fae272007-03-25 11:35:08 -0400152 struct buffer_head *snap;
Chris Mason79154b12007-03-22 15:59:16 -0400153 struct btrfs_key snap_key;
154 struct btrfs_transaction *cur_trans;
155 DEFINE_WAIT(wait);
156
Chris Masond6e4a422007-04-06 15:37:36 -0400157 /* FIXME, use the right root */
158 root = root->fs_info->fs_root;
159
Chris Mason79154b12007-03-22 15:59:16 -0400160 mutex_lock(&root->fs_info->trans_mutex);
161 if (trans->transaction->in_commit) {
Chris Mason2c90e5d2007-04-02 10:50:19 -0400162printk("already in commit!, waiting\n");
Chris Mason79154b12007-03-22 15:59:16 -0400163 cur_trans = trans->transaction;
164 trans->transaction->use_count++;
165 btrfs_end_transaction(trans, root);
166 ret = wait_for_commit(root, cur_trans);
167 BUG_ON(ret);
168 put_transaction(cur_trans);
169 mutex_unlock(&root->fs_info->trans_mutex);
170 return 0;
171 }
Chris Mason2c90e5d2007-04-02 10:50:19 -0400172 cur_trans = trans->transaction;
173 trans->transaction->in_commit = 1;
Chris Mason79154b12007-03-22 15:59:16 -0400174 while (trans->transaction->num_writers > 1) {
Chris Mason2c90e5d2007-04-02 10:50:19 -0400175 WARN_ON(cur_trans != trans->transaction);
Chris Mason79154b12007-03-22 15:59:16 -0400176 prepare_to_wait(&trans->transaction->writer_wait, &wait,
177 TASK_UNINTERRUPTIBLE);
178 if (trans->transaction->num_writers <= 1)
179 break;
180 mutex_unlock(&root->fs_info->trans_mutex);
181 schedule();
182 mutex_lock(&root->fs_info->trans_mutex);
Chris Mason2c90e5d2007-04-02 10:50:19 -0400183 finish_wait(&trans->transaction->writer_wait, &wait);
Chris Mason79154b12007-03-22 15:59:16 -0400184 }
185 finish_wait(&trans->transaction->writer_wait, &wait);
Chris Mason2c90e5d2007-04-02 10:50:19 -0400186 WARN_ON(cur_trans != trans->transaction);
Chris Masond5719762007-03-23 10:01:08 -0400187 if (root->node != root->commit_root) {
188 memcpy(&snap_key, &root->root_key, sizeof(snap_key));
189 root->root_key.offset++;
190 }
191
Chris Mason79154b12007-03-22 15:59:16 -0400192 if (btrfs_root_blocknr(&root->root_item) != root->node->b_blocknr) {
193 btrfs_set_root_blocknr(&root->root_item, root->node->b_blocknr);
194 ret = btrfs_insert_root(trans, root->fs_info->tree_root,
195 &root->root_key, &root->root_item);
196 BUG_ON(ret);
197 }
198
199 ret = btrfs_commit_tree_roots(trans, root);
200 BUG_ON(ret);
Chris Mason78fae272007-03-25 11:35:08 -0400201 cur_trans = root->fs_info->running_transaction;
202 root->fs_info->running_transaction = NULL;
203 mutex_unlock(&root->fs_info->trans_mutex);
Chris Mason79154b12007-03-22 15:59:16 -0400204 ret = btrfs_write_and_wait_transaction(trans, root);
205 BUG_ON(ret);
206
207 write_ctree_super(trans, root);
Chris Mason78fae272007-03-25 11:35:08 -0400208 btrfs_finish_extent_commit(trans, root);
209 mutex_lock(&root->fs_info->trans_mutex);
Chris Mason2c90e5d2007-04-02 10:50:19 -0400210 cur_trans->commit_done = 1;
211 wake_up(&cur_trans->commit_wait);
Chris Mason79154b12007-03-22 15:59:16 -0400212 put_transaction(cur_trans);
Chris Mason78fae272007-03-25 11:35:08 -0400213 put_transaction(cur_trans);
214 mutex_unlock(&root->fs_info->trans_mutex);
Chris Mason2c90e5d2007-04-02 10:50:19 -0400215 kmem_cache_free(btrfs_trans_handle_cachep, trans);
Chris Mason79154b12007-03-22 15:59:16 -0400216 if (root->node != root->commit_root) {
217 trans = btrfs_start_transaction(root, 1);
Chris Mason78fae272007-03-25 11:35:08 -0400218 snap = root->commit_root;
Chris Mason79154b12007-03-22 15:59:16 -0400219 root->commit_root = root->node;
220 get_bh(root->node);
221 ret = btrfs_drop_snapshot(trans, root, snap);
222 BUG_ON(ret);
223
224 ret = btrfs_del_root(trans, root->fs_info->tree_root,
225 &snap_key);
Chris Mason22b0ebd2007-03-30 08:47:31 -0400226 BUG_ON(ret);
227 root->fs_info->generation = root->root_key.offset + 1;
228 ret = btrfs_end_transaction(trans, root);
229 BUG_ON(ret);
Chris Mason79154b12007-03-22 15:59:16 -0400230 }
Chris Mason79154b12007-03-22 15:59:16 -0400231 return ret;
232}
233