blob: 8740752f3845efea1029b9b9688047cf98182df2 [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
Chris Mason0f7d52f2007-04-09 10:42:37 -040011#define BTRFS_ROOT_TRANS_TAG 0
12
Chris Mason2c90e5d2007-04-02 10:50:19 -040013#define TRANS_MAGIC 0xE1E10E
Chris Mason79154b12007-03-22 15:59:16 -040014static void put_transaction(struct btrfs_transaction *transaction)
15{
Chris Mason2c90e5d2007-04-02 10:50:19 -040016 WARN_ON(transaction->use_count == 0);
Chris Mason79154b12007-03-22 15:59:16 -040017 transaction->use_count--;
Chris Mason2c90e5d2007-04-02 10:50:19 -040018 WARN_ON(transaction->magic != TRANS_MAGIC);
Chris Mason78fae272007-03-25 11:35:08 -040019 if (transaction->use_count == 0) {
20 WARN_ON(total_trans == 0);
21 total_trans--;
Chris Mason2c90e5d2007-04-02 10:50:19 -040022 memset(transaction, 0, sizeof(*transaction));
23 kmem_cache_free(btrfs_transaction_cachep, transaction);
Chris Mason78fae272007-03-25 11:35:08 -040024 }
Chris Mason79154b12007-03-22 15:59:16 -040025}
26
27static int join_transaction(struct btrfs_root *root)
28{
29 struct btrfs_transaction *cur_trans;
30 cur_trans = root->fs_info->running_transaction;
31 if (!cur_trans) {
Chris Mason2c90e5d2007-04-02 10:50:19 -040032 cur_trans = kmem_cache_alloc(btrfs_transaction_cachep,
33 GFP_NOFS);
Chris Mason78fae272007-03-25 11:35:08 -040034 total_trans++;
Chris Mason79154b12007-03-22 15:59:16 -040035 BUG_ON(!cur_trans);
Chris Mason0f7d52f2007-04-09 10:42:37 -040036 root->fs_info->generation++;
Chris Mason79154b12007-03-22 15:59:16 -040037 root->fs_info->running_transaction = cur_trans;
38 cur_trans->num_writers = 0;
Chris Mason0f7d52f2007-04-09 10:42:37 -040039 cur_trans->transid = root->fs_info->generation;
Chris Mason79154b12007-03-22 15:59:16 -040040 init_waitqueue_head(&cur_trans->writer_wait);
41 init_waitqueue_head(&cur_trans->commit_wait);
Chris Mason2c90e5d2007-04-02 10:50:19 -040042 cur_trans->magic = TRANS_MAGIC;
Chris Mason79154b12007-03-22 15:59:16 -040043 cur_trans->in_commit = 0;
Chris Masond5719762007-03-23 10:01:08 -040044 cur_trans->use_count = 1;
Chris Mason79154b12007-03-22 15:59:16 -040045 cur_trans->commit_done = 0;
46 }
47 cur_trans->num_writers++;
48 return 0;
49}
50
51struct btrfs_trans_handle *btrfs_start_transaction(struct btrfs_root *root,
52 int num_blocks)
53{
Chris Mason2c90e5d2007-04-02 10:50:19 -040054 struct btrfs_trans_handle *h =
55 kmem_cache_alloc(btrfs_trans_handle_cachep, GFP_NOFS);
Chris Mason79154b12007-03-22 15:59:16 -040056 int ret;
Chris Mason0f7d52f2007-04-09 10:42:37 -040057 u64 running_trans_id;
Chris Mason79154b12007-03-22 15:59:16 -040058
59 mutex_lock(&root->fs_info->trans_mutex);
60 ret = join_transaction(root);
61 BUG_ON(ret);
Chris Mason0f7d52f2007-04-09 10:42:37 -040062 running_trans_id = root->fs_info->running_transaction->transid;
63
64 if (root != root->fs_info->tree_root && root->last_trans <
65 running_trans_id) {
66 radix_tree_tag_set(&root->fs_info->fs_roots_radix,
Chris Mason2619ba12007-04-10 16:58:11 -040067 (unsigned long)root->root_key.objectid,
68 BTRFS_ROOT_TRANS_TAG);
Chris Mason0f7d52f2007-04-09 10:42:37 -040069 root->commit_root = root->node;
70 get_bh(root->node);
71 }
72 root->last_trans = running_trans_id;
73 h->transid = running_trans_id;
Chris Mason79154b12007-03-22 15:59:16 -040074 h->transaction = root->fs_info->running_transaction;
75 h->blocks_reserved = num_blocks;
76 h->blocks_used = 0;
77 root->fs_info->running_transaction->use_count++;
78 mutex_unlock(&root->fs_info->trans_mutex);
Chris Mason2c90e5d2007-04-02 10:50:19 -040079 h->magic = h->magic2 = TRANS_MAGIC;
Chris Mason79154b12007-03-22 15:59:16 -040080 return h;
81}
82
83int btrfs_end_transaction(struct btrfs_trans_handle *trans,
84 struct btrfs_root *root)
85{
86 struct btrfs_transaction *cur_trans;
Chris Masond6e4a422007-04-06 15:37:36 -040087
Chris Mason2c90e5d2007-04-02 10:50:19 -040088 WARN_ON(trans->magic != TRANS_MAGIC);
89 WARN_ON(trans->magic2 != TRANS_MAGIC);
Chris Mason79154b12007-03-22 15:59:16 -040090 mutex_lock(&root->fs_info->trans_mutex);
91 cur_trans = root->fs_info->running_transaction;
Chris Masond5719762007-03-23 10:01:08 -040092 WARN_ON(cur_trans->num_writers < 1);
Chris Mason79154b12007-03-22 15:59:16 -040093 if (waitqueue_active(&cur_trans->writer_wait))
94 wake_up(&cur_trans->writer_wait);
95 cur_trans->num_writers--;
96 put_transaction(cur_trans);
97 mutex_unlock(&root->fs_info->trans_mutex);
Chris Masond6025572007-03-30 14:27:56 -040098 memset(trans, 0, sizeof(*trans));
Chris Mason2c90e5d2007-04-02 10:50:19 -040099 kmem_cache_free(btrfs_trans_handle_cachep, trans);
Chris Mason79154b12007-03-22 15:59:16 -0400100 return 0;
101}
102
103
104int btrfs_write_and_wait_transaction(struct btrfs_trans_handle *trans,
105 struct btrfs_root *root)
106{
Chris Mason7cfcc172007-04-02 14:53:59 -0400107 filemap_write_and_wait(root->fs_info->btree_inode->i_mapping);
Chris Mason79154b12007-03-22 15:59:16 -0400108 return 0;
109}
110
111int btrfs_commit_tree_roots(struct btrfs_trans_handle *trans,
112 struct btrfs_root *root)
113{
114 int ret;
115 u64 old_extent_block;
116 struct btrfs_fs_info *fs_info = root->fs_info;
117 struct btrfs_root *tree_root = fs_info->tree_root;
118 struct btrfs_root *extent_root = fs_info->extent_root;
Chris Mason8352d8a2007-04-12 10:43:05 -0400119 struct btrfs_root *dev_root = fs_info->dev_root;
Chris Mason79154b12007-03-22 15:59:16 -0400120
Chris Mason8352d8a2007-04-12 10:43:05 -0400121 if (btrfs_super_device_root(fs_info->disk_super) !=
122 bh_blocknr(dev_root->node)) {
123 btrfs_set_super_device_root(fs_info->disk_super,
124 bh_blocknr(dev_root->node));
125 }
Chris Mason79154b12007-03-22 15:59:16 -0400126 while(1) {
127 old_extent_block = btrfs_root_blocknr(&extent_root->root_item);
Chris Mason7eccb902007-04-11 15:53:25 -0400128 if (old_extent_block == bh_blocknr(extent_root->node))
Chris Mason79154b12007-03-22 15:59:16 -0400129 break;
130 btrfs_set_root_blocknr(&extent_root->root_item,
Chris Mason7eccb902007-04-11 15:53:25 -0400131 bh_blocknr(extent_root->node));
Chris Mason79154b12007-03-22 15:59:16 -0400132 ret = btrfs_update_root(trans, tree_root,
133 &extent_root->root_key,
134 &extent_root->root_item);
135 BUG_ON(ret);
136 }
137 return 0;
138}
139
140static int wait_for_commit(struct btrfs_root *root,
141 struct btrfs_transaction *commit)
142{
143 DEFINE_WAIT(wait);
Chris Mason79154b12007-03-22 15:59:16 -0400144 while(!commit->commit_done) {
145 prepare_to_wait(&commit->commit_wait, &wait,
146 TASK_UNINTERRUPTIBLE);
147 if (commit->commit_done)
148 break;
149 mutex_unlock(&root->fs_info->trans_mutex);
150 schedule();
151 mutex_lock(&root->fs_info->trans_mutex);
152 }
153 finish_wait(&commit->commit_wait, &wait);
154 return 0;
155}
156
Chris Mason0f7d52f2007-04-09 10:42:37 -0400157struct dirty_root {
158 struct list_head list;
159 struct btrfs_key snap_key;
160 struct buffer_head *commit_root;
161 struct btrfs_root *root;
162};
163
164int add_dirty_roots(struct btrfs_trans_handle *trans,
165 struct radix_tree_root *radix, struct list_head *list)
166{
167 struct dirty_root *dirty;
168 struct btrfs_root *gang[8];
169 struct btrfs_root *root;
170 int i;
171 int ret;
172 int err;
Chris Mason0f7d52f2007-04-09 10:42:37 -0400173 while(1) {
174 ret = radix_tree_gang_lookup_tag(radix, (void **)gang, 0,
175 ARRAY_SIZE(gang),
176 BTRFS_ROOT_TRANS_TAG);
177 if (ret == 0)
178 break;
179 for (i = 0; i < ret; i++) {
180 root = gang[i];
Chris Mason2619ba12007-04-10 16:58:11 -0400181 radix_tree_tag_clear(radix,
182 (unsigned long)root->root_key.objectid,
183 BTRFS_ROOT_TRANS_TAG);
Chris Mason0f7d52f2007-04-09 10:42:37 -0400184 if (root->commit_root == root->node) {
Chris Mason7eccb902007-04-11 15:53:25 -0400185 WARN_ON(bh_blocknr(root->node) !=
Chris Mason0f7d52f2007-04-09 10:42:37 -0400186 btrfs_root_blocknr(&root->root_item));
187 brelse(root->commit_root);
188 root->commit_root = NULL;
189 continue;
190 }
191 dirty = kmalloc(sizeof(*dirty), GFP_NOFS);
192 BUG_ON(!dirty);
193 memcpy(&dirty->snap_key, &root->root_key,
194 sizeof(root->root_key));
195 dirty->commit_root = root->commit_root;
196 root->commit_root = NULL;
197 dirty->root = root;
Chris Mason0f7d52f2007-04-09 10:42:37 -0400198 root->root_key.offset = root->fs_info->generation;
199 btrfs_set_root_blocknr(&root->root_item,
Chris Mason7eccb902007-04-11 15:53:25 -0400200 bh_blocknr(root->node));
Chris Mason0f7d52f2007-04-09 10:42:37 -0400201 err = btrfs_insert_root(trans, root->fs_info->tree_root,
202 &root->root_key,
203 &root->root_item);
204 BUG_ON(err);
205 list_add(&dirty->list, list);
206 }
207 }
Chris Mason0f7d52f2007-04-09 10:42:37 -0400208 return 0;
209}
210
211int drop_dirty_roots(struct btrfs_root *tree_root, struct list_head *list)
212{
213 struct dirty_root *dirty;
214 struct btrfs_trans_handle *trans;
215 int ret;
216
217 while(!list_empty(list)) {
218 dirty = list_entry(list->next, struct dirty_root, list);
219 list_del_init(&dirty->list);
220 trans = btrfs_start_transaction(tree_root, 1);
Chris Mason0f7d52f2007-04-09 10:42:37 -0400221 ret = btrfs_drop_snapshot(trans, dirty->root,
222 dirty->commit_root);
223 BUG_ON(ret);
224
Chris Mason0f7d52f2007-04-09 10:42:37 -0400225 ret = btrfs_del_root(trans, tree_root, &dirty->snap_key);
226 BUG_ON(ret);
227 ret = btrfs_end_transaction(trans, tree_root);
228 BUG_ON(ret);
229 kfree(dirty);
230 }
231 return 0;
232}
233
Chris Mason79154b12007-03-22 15:59:16 -0400234int btrfs_commit_transaction(struct btrfs_trans_handle *trans,
235 struct btrfs_root *root)
236{
237 int ret = 0;
Chris Mason79154b12007-03-22 15:59:16 -0400238 struct btrfs_transaction *cur_trans;
Chris Mason0f7d52f2007-04-09 10:42:37 -0400239 struct list_head dirty_fs_roots;
Chris Mason79154b12007-03-22 15:59:16 -0400240 DEFINE_WAIT(wait);
241
Chris Mason0f7d52f2007-04-09 10:42:37 -0400242 INIT_LIST_HEAD(&dirty_fs_roots);
Chris Masond6e4a422007-04-06 15:37:36 -0400243
Chris Mason79154b12007-03-22 15:59:16 -0400244 mutex_lock(&root->fs_info->trans_mutex);
245 if (trans->transaction->in_commit) {
246 cur_trans = trans->transaction;
247 trans->transaction->use_count++;
248 btrfs_end_transaction(trans, root);
249 ret = wait_for_commit(root, cur_trans);
250 BUG_ON(ret);
251 put_transaction(cur_trans);
252 mutex_unlock(&root->fs_info->trans_mutex);
253 return 0;
254 }
Chris Mason2c90e5d2007-04-02 10:50:19 -0400255 cur_trans = trans->transaction;
256 trans->transaction->in_commit = 1;
Chris Mason79154b12007-03-22 15:59:16 -0400257 while (trans->transaction->num_writers > 1) {
Chris Mason2c90e5d2007-04-02 10:50:19 -0400258 WARN_ON(cur_trans != trans->transaction);
Chris Mason79154b12007-03-22 15:59:16 -0400259 prepare_to_wait(&trans->transaction->writer_wait, &wait,
260 TASK_UNINTERRUPTIBLE);
261 if (trans->transaction->num_writers <= 1)
262 break;
263 mutex_unlock(&root->fs_info->trans_mutex);
264 schedule();
265 mutex_lock(&root->fs_info->trans_mutex);
Chris Mason2c90e5d2007-04-02 10:50:19 -0400266 finish_wait(&trans->transaction->writer_wait, &wait);
Chris Mason79154b12007-03-22 15:59:16 -0400267 }
268 finish_wait(&trans->transaction->writer_wait, &wait);
Chris Mason2c90e5d2007-04-02 10:50:19 -0400269 WARN_ON(cur_trans != trans->transaction);
Chris Mason0f7d52f2007-04-09 10:42:37 -0400270 add_dirty_roots(trans, &root->fs_info->fs_roots_radix, &dirty_fs_roots);
Chris Mason79154b12007-03-22 15:59:16 -0400271 ret = btrfs_commit_tree_roots(trans, root);
272 BUG_ON(ret);
Chris Mason78fae272007-03-25 11:35:08 -0400273 cur_trans = root->fs_info->running_transaction;
274 root->fs_info->running_transaction = NULL;
Chris Mason0f7d52f2007-04-09 10:42:37 -0400275 btrfs_set_super_generation(root->fs_info->disk_super,
276 root->fs_info->generation + 1);
Chris Mason78fae272007-03-25 11:35:08 -0400277 mutex_unlock(&root->fs_info->trans_mutex);
Chris Mason79154b12007-03-22 15:59:16 -0400278 ret = btrfs_write_and_wait_transaction(trans, root);
279 BUG_ON(ret);
280
281 write_ctree_super(trans, root);
Chris Mason78fae272007-03-25 11:35:08 -0400282 btrfs_finish_extent_commit(trans, root);
283 mutex_lock(&root->fs_info->trans_mutex);
Chris Mason2c90e5d2007-04-02 10:50:19 -0400284 cur_trans->commit_done = 1;
285 wake_up(&cur_trans->commit_wait);
Chris Mason79154b12007-03-22 15:59:16 -0400286 put_transaction(cur_trans);
Chris Mason78fae272007-03-25 11:35:08 -0400287 put_transaction(cur_trans);
288 mutex_unlock(&root->fs_info->trans_mutex);
Chris Mason2c90e5d2007-04-02 10:50:19 -0400289 kmem_cache_free(btrfs_trans_handle_cachep, trans);
Chris Mason79154b12007-03-22 15:59:16 -0400290
Chris Mason0f7d52f2007-04-09 10:42:37 -0400291 drop_dirty_roots(root->fs_info->tree_root, &dirty_fs_roots);
Chris Mason79154b12007-03-22 15:59:16 -0400292 return ret;
293}
294