blob: bf7eef67ba0b4ecf231baddf9fca55fdfeccec4c [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 Mason08607c12007-06-08 15:33:54 -040011static struct workqueue_struct *trans_wq;
12
Chris Mason0f7d52f2007-04-09 10:42:37 -040013#define BTRFS_ROOT_TRANS_TAG 0
14
Chris Mason2c90e5d2007-04-02 10:50:19 -040015#define TRANS_MAGIC 0xE1E10E
Chris Mason79154b12007-03-22 15:59:16 -040016static void put_transaction(struct btrfs_transaction *transaction)
17{
Chris Mason2c90e5d2007-04-02 10:50:19 -040018 WARN_ON(transaction->use_count == 0);
Chris Mason79154b12007-03-22 15:59:16 -040019 transaction->use_count--;
Chris Mason2c90e5d2007-04-02 10:50:19 -040020 WARN_ON(transaction->magic != TRANS_MAGIC);
Chris Mason78fae272007-03-25 11:35:08 -040021 if (transaction->use_count == 0) {
22 WARN_ON(total_trans == 0);
23 total_trans--;
Chris Mason8fd17792007-04-19 21:01:03 -040024 list_del_init(&transaction->list);
Chris Mason2c90e5d2007-04-02 10:50:19 -040025 memset(transaction, 0, sizeof(*transaction));
26 kmem_cache_free(btrfs_transaction_cachep, transaction);
Chris Mason78fae272007-03-25 11:35:08 -040027 }
Chris Mason79154b12007-03-22 15:59:16 -040028}
29
30static int join_transaction(struct btrfs_root *root)
31{
32 struct btrfs_transaction *cur_trans;
33 cur_trans = root->fs_info->running_transaction;
34 if (!cur_trans) {
Chris Mason2c90e5d2007-04-02 10:50:19 -040035 cur_trans = kmem_cache_alloc(btrfs_transaction_cachep,
36 GFP_NOFS);
Chris Mason78fae272007-03-25 11:35:08 -040037 total_trans++;
Chris Mason79154b12007-03-22 15:59:16 -040038 BUG_ON(!cur_trans);
Chris Mason0f7d52f2007-04-09 10:42:37 -040039 root->fs_info->generation++;
Chris Mason79154b12007-03-22 15:59:16 -040040 root->fs_info->running_transaction = cur_trans;
41 cur_trans->num_writers = 0;
Chris Mason0f7d52f2007-04-09 10:42:37 -040042 cur_trans->transid = root->fs_info->generation;
Chris Mason79154b12007-03-22 15:59:16 -040043 init_waitqueue_head(&cur_trans->writer_wait);
44 init_waitqueue_head(&cur_trans->commit_wait);
Chris Mason2c90e5d2007-04-02 10:50:19 -040045 cur_trans->magic = TRANS_MAGIC;
Chris Mason79154b12007-03-22 15:59:16 -040046 cur_trans->in_commit = 0;
Chris Masond5719762007-03-23 10:01:08 -040047 cur_trans->use_count = 1;
Chris Mason79154b12007-03-22 15:59:16 -040048 cur_trans->commit_done = 0;
Chris Mason08607c12007-06-08 15:33:54 -040049 cur_trans->start_time = get_seconds();
Chris Mason8fd17792007-04-19 21:01:03 -040050 list_add_tail(&cur_trans->list, &root->fs_info->trans_list);
Chris Mason7c4452b2007-04-28 09:29:35 -040051 init_bit_radix(&cur_trans->dirty_pages);
Chris Mason79154b12007-03-22 15:59:16 -040052 }
53 cur_trans->num_writers++;
54 return 0;
55}
56
57struct btrfs_trans_handle *btrfs_start_transaction(struct btrfs_root *root,
58 int num_blocks)
59{
Chris Mason2c90e5d2007-04-02 10:50:19 -040060 struct btrfs_trans_handle *h =
61 kmem_cache_alloc(btrfs_trans_handle_cachep, GFP_NOFS);
Chris Mason79154b12007-03-22 15:59:16 -040062 int ret;
Chris Mason0f7d52f2007-04-09 10:42:37 -040063 u64 running_trans_id;
Chris Mason79154b12007-03-22 15:59:16 -040064
65 mutex_lock(&root->fs_info->trans_mutex);
66 ret = join_transaction(root);
67 BUG_ON(ret);
Chris Mason0f7d52f2007-04-09 10:42:37 -040068 running_trans_id = root->fs_info->running_transaction->transid;
69
70 if (root != root->fs_info->tree_root && root->last_trans <
71 running_trans_id) {
72 radix_tree_tag_set(&root->fs_info->fs_roots_radix,
Chris Mason2619ba12007-04-10 16:58:11 -040073 (unsigned long)root->root_key.objectid,
74 BTRFS_ROOT_TRANS_TAG);
Chris Mason0f7d52f2007-04-09 10:42:37 -040075 root->commit_root = root->node;
76 get_bh(root->node);
77 }
78 root->last_trans = running_trans_id;
79 h->transid = running_trans_id;
Chris Mason79154b12007-03-22 15:59:16 -040080 h->transaction = root->fs_info->running_transaction;
81 h->blocks_reserved = num_blocks;
82 h->blocks_used = 0;
Chris Mason31f3c992007-04-30 15:25:45 -040083 h->block_group = NULL;
Chris Mason79154b12007-03-22 15:59:16 -040084 root->fs_info->running_transaction->use_count++;
85 mutex_unlock(&root->fs_info->trans_mutex);
Chris Mason2c90e5d2007-04-02 10:50:19 -040086 h->magic = h->magic2 = TRANS_MAGIC;
Chris Mason79154b12007-03-22 15:59:16 -040087 return h;
88}
89
90int btrfs_end_transaction(struct btrfs_trans_handle *trans,
91 struct btrfs_root *root)
92{
93 struct btrfs_transaction *cur_trans;
Chris Masond6e4a422007-04-06 15:37:36 -040094
Chris Mason2c90e5d2007-04-02 10:50:19 -040095 WARN_ON(trans->magic != TRANS_MAGIC);
96 WARN_ON(trans->magic2 != TRANS_MAGIC);
Chris Mason79154b12007-03-22 15:59:16 -040097 mutex_lock(&root->fs_info->trans_mutex);
98 cur_trans = root->fs_info->running_transaction;
Chris Masond5719762007-03-23 10:01:08 -040099 WARN_ON(cur_trans->num_writers < 1);
Chris Mason79154b12007-03-22 15:59:16 -0400100 if (waitqueue_active(&cur_trans->writer_wait))
101 wake_up(&cur_trans->writer_wait);
102 cur_trans->num_writers--;
103 put_transaction(cur_trans);
104 mutex_unlock(&root->fs_info->trans_mutex);
Chris Masond6025572007-03-30 14:27:56 -0400105 memset(trans, 0, sizeof(*trans));
Chris Mason2c90e5d2007-04-02 10:50:19 -0400106 kmem_cache_free(btrfs_trans_handle_cachep, trans);
Chris Mason79154b12007-03-22 15:59:16 -0400107 return 0;
108}
109
110
111int btrfs_write_and_wait_transaction(struct btrfs_trans_handle *trans,
112 struct btrfs_root *root)
113{
Chris Mason7c4452b2007-04-28 09:29:35 -0400114 unsigned long gang[16];
115 int ret;
116 int i;
117 int err;
118 int werr = 0;
119 struct page *page;
120 struct radix_tree_root *dirty_pages;
121 struct inode *btree_inode = root->fs_info->btree_inode;
122
123 if (!trans || !trans->transaction) {
124 return filemap_write_and_wait(btree_inode->i_mapping);
125 }
126 dirty_pages = &trans->transaction->dirty_pages;
127 while(1) {
Chris Masone37c9e62007-05-09 20:13:14 -0400128 ret = find_first_radix_bit(dirty_pages, gang,
129 0, ARRAY_SIZE(gang));
Chris Mason7c4452b2007-04-28 09:29:35 -0400130 if (!ret)
131 break;
132 for (i = 0; i < ret; i++) {
133 /* FIXME EIO */
134 clear_radix_bit(dirty_pages, gang[i]);
135 page = find_lock_page(btree_inode->i_mapping,
136 gang[i]);
137 if (!page)
138 continue;
139 err = write_one_page(page, 0);
140 if (err)
141 werr = err;
142 page_cache_release(page);
143 }
144 }
145 err = filemap_fdatawait(btree_inode->i_mapping);
146 if (err)
147 werr = err;
148 return werr;
Chris Mason79154b12007-03-22 15:59:16 -0400149}
150
151int btrfs_commit_tree_roots(struct btrfs_trans_handle *trans,
152 struct btrfs_root *root)
153{
154 int ret;
155 u64 old_extent_block;
156 struct btrfs_fs_info *fs_info = root->fs_info;
157 struct btrfs_root *tree_root = fs_info->tree_root;
158 struct btrfs_root *extent_root = fs_info->extent_root;
Chris Mason8352d8a2007-04-12 10:43:05 -0400159 struct btrfs_root *dev_root = fs_info->dev_root;
Chris Mason79154b12007-03-22 15:59:16 -0400160
Chris Mason8352d8a2007-04-12 10:43:05 -0400161 if (btrfs_super_device_root(fs_info->disk_super) !=
162 bh_blocknr(dev_root->node)) {
163 btrfs_set_super_device_root(fs_info->disk_super,
164 bh_blocknr(dev_root->node));
165 }
Chris Mason9078a3e2007-04-26 16:46:15 -0400166 btrfs_write_dirty_block_groups(trans, extent_root);
Chris Mason79154b12007-03-22 15:59:16 -0400167 while(1) {
168 old_extent_block = btrfs_root_blocknr(&extent_root->root_item);
Chris Mason7eccb902007-04-11 15:53:25 -0400169 if (old_extent_block == bh_blocknr(extent_root->node))
Chris Mason79154b12007-03-22 15:59:16 -0400170 break;
171 btrfs_set_root_blocknr(&extent_root->root_item,
Chris Mason7eccb902007-04-11 15:53:25 -0400172 bh_blocknr(extent_root->node));
Chris Mason79154b12007-03-22 15:59:16 -0400173 ret = btrfs_update_root(trans, tree_root,
174 &extent_root->root_key,
175 &extent_root->root_item);
176 BUG_ON(ret);
Chris Mason9078a3e2007-04-26 16:46:15 -0400177 btrfs_write_dirty_block_groups(trans, extent_root);
Chris Mason79154b12007-03-22 15:59:16 -0400178 }
179 return 0;
180}
181
182static int wait_for_commit(struct btrfs_root *root,
183 struct btrfs_transaction *commit)
184{
185 DEFINE_WAIT(wait);
Chris Mason79154b12007-03-22 15:59:16 -0400186 while(!commit->commit_done) {
187 prepare_to_wait(&commit->commit_wait, &wait,
188 TASK_UNINTERRUPTIBLE);
189 if (commit->commit_done)
190 break;
191 mutex_unlock(&root->fs_info->trans_mutex);
192 schedule();
193 mutex_lock(&root->fs_info->trans_mutex);
194 }
195 finish_wait(&commit->commit_wait, &wait);
196 return 0;
197}
198
Chris Mason0f7d52f2007-04-09 10:42:37 -0400199struct dirty_root {
200 struct list_head list;
201 struct btrfs_key snap_key;
202 struct buffer_head *commit_root;
203 struct btrfs_root *root;
204};
205
Chris Mason35b7e472007-05-02 15:53:43 -0400206static int add_dirty_roots(struct btrfs_trans_handle *trans,
207 struct radix_tree_root *radix,
208 struct list_head *list)
Chris Mason0f7d52f2007-04-09 10:42:37 -0400209{
210 struct dirty_root *dirty;
211 struct btrfs_root *gang[8];
212 struct btrfs_root *root;
213 int i;
214 int ret;
215 int err;
Chris Mason0f7d52f2007-04-09 10:42:37 -0400216 while(1) {
217 ret = radix_tree_gang_lookup_tag(radix, (void **)gang, 0,
218 ARRAY_SIZE(gang),
219 BTRFS_ROOT_TRANS_TAG);
220 if (ret == 0)
221 break;
222 for (i = 0; i < ret; i++) {
223 root = gang[i];
Chris Mason2619ba12007-04-10 16:58:11 -0400224 radix_tree_tag_clear(radix,
225 (unsigned long)root->root_key.objectid,
226 BTRFS_ROOT_TRANS_TAG);
Chris Mason0f7d52f2007-04-09 10:42:37 -0400227 if (root->commit_root == root->node) {
Chris Mason7eccb902007-04-11 15:53:25 -0400228 WARN_ON(bh_blocknr(root->node) !=
Chris Mason0f7d52f2007-04-09 10:42:37 -0400229 btrfs_root_blocknr(&root->root_item));
230 brelse(root->commit_root);
231 root->commit_root = NULL;
232 continue;
233 }
234 dirty = kmalloc(sizeof(*dirty), GFP_NOFS);
235 BUG_ON(!dirty);
236 memcpy(&dirty->snap_key, &root->root_key,
237 sizeof(root->root_key));
238 dirty->commit_root = root->commit_root;
239 root->commit_root = NULL;
240 dirty->root = root;
Chris Mason0f7d52f2007-04-09 10:42:37 -0400241 root->root_key.offset = root->fs_info->generation;
242 btrfs_set_root_blocknr(&root->root_item,
Chris Mason7eccb902007-04-11 15:53:25 -0400243 bh_blocknr(root->node));
Chris Mason0f7d52f2007-04-09 10:42:37 -0400244 err = btrfs_insert_root(trans, root->fs_info->tree_root,
245 &root->root_key,
246 &root->root_item);
247 BUG_ON(err);
248 list_add(&dirty->list, list);
249 }
250 }
Chris Mason0f7d52f2007-04-09 10:42:37 -0400251 return 0;
252}
253
Chris Mason35b7e472007-05-02 15:53:43 -0400254static int drop_dirty_roots(struct btrfs_root *tree_root,
255 struct list_head *list)
Chris Mason0f7d52f2007-04-09 10:42:37 -0400256{
257 struct dirty_root *dirty;
258 struct btrfs_trans_handle *trans;
259 int ret;
260
261 while(!list_empty(list)) {
262 dirty = list_entry(list->next, struct dirty_root, list);
263 list_del_init(&dirty->list);
264 trans = btrfs_start_transaction(tree_root, 1);
Chris Mason0f7d52f2007-04-09 10:42:37 -0400265 ret = btrfs_drop_snapshot(trans, dirty->root,
266 dirty->commit_root);
267 BUG_ON(ret);
268
Chris Mason0f7d52f2007-04-09 10:42:37 -0400269 ret = btrfs_del_root(trans, tree_root, &dirty->snap_key);
270 BUG_ON(ret);
271 ret = btrfs_end_transaction(trans, tree_root);
272 BUG_ON(ret);
273 kfree(dirty);
274 }
275 return 0;
276}
277
Chris Mason79154b12007-03-22 15:59:16 -0400278int btrfs_commit_transaction(struct btrfs_trans_handle *trans,
279 struct btrfs_root *root)
280{
281 int ret = 0;
Chris Mason79154b12007-03-22 15:59:16 -0400282 struct btrfs_transaction *cur_trans;
Chris Mason8fd17792007-04-19 21:01:03 -0400283 struct btrfs_transaction *prev_trans = NULL;
Chris Mason0f7d52f2007-04-09 10:42:37 -0400284 struct list_head dirty_fs_roots;
Chris Mason79154b12007-03-22 15:59:16 -0400285 DEFINE_WAIT(wait);
286
Chris Mason0f7d52f2007-04-09 10:42:37 -0400287 INIT_LIST_HEAD(&dirty_fs_roots);
Chris Masond6e4a422007-04-06 15:37:36 -0400288
Chris Mason79154b12007-03-22 15:59:16 -0400289 mutex_lock(&root->fs_info->trans_mutex);
290 if (trans->transaction->in_commit) {
291 cur_trans = trans->transaction;
292 trans->transaction->use_count++;
293 btrfs_end_transaction(trans, root);
294 ret = wait_for_commit(root, cur_trans);
295 BUG_ON(ret);
296 put_transaction(cur_trans);
297 mutex_unlock(&root->fs_info->trans_mutex);
298 return 0;
299 }
Chris Mason2c90e5d2007-04-02 10:50:19 -0400300 cur_trans = trans->transaction;
301 trans->transaction->in_commit = 1;
Chris Mason79154b12007-03-22 15:59:16 -0400302 while (trans->transaction->num_writers > 1) {
Chris Mason2c90e5d2007-04-02 10:50:19 -0400303 WARN_ON(cur_trans != trans->transaction);
Chris Mason79154b12007-03-22 15:59:16 -0400304 prepare_to_wait(&trans->transaction->writer_wait, &wait,
305 TASK_UNINTERRUPTIBLE);
306 if (trans->transaction->num_writers <= 1)
307 break;
308 mutex_unlock(&root->fs_info->trans_mutex);
309 schedule();
310 mutex_lock(&root->fs_info->trans_mutex);
Chris Mason2c90e5d2007-04-02 10:50:19 -0400311 finish_wait(&trans->transaction->writer_wait, &wait);
Chris Mason79154b12007-03-22 15:59:16 -0400312 }
313 finish_wait(&trans->transaction->writer_wait, &wait);
Chris Mason2c90e5d2007-04-02 10:50:19 -0400314 WARN_ON(cur_trans != trans->transaction);
Chris Mason0f7d52f2007-04-09 10:42:37 -0400315 add_dirty_roots(trans, &root->fs_info->fs_roots_radix, &dirty_fs_roots);
Chris Mason79154b12007-03-22 15:59:16 -0400316 ret = btrfs_commit_tree_roots(trans, root);
317 BUG_ON(ret);
Chris Mason78fae272007-03-25 11:35:08 -0400318 cur_trans = root->fs_info->running_transaction;
319 root->fs_info->running_transaction = NULL;
Chris Mason8fd17792007-04-19 21:01:03 -0400320 if (cur_trans->list.prev != &root->fs_info->trans_list) {
321 prev_trans = list_entry(cur_trans->list.prev,
322 struct btrfs_transaction, list);
323 if (prev_trans->commit_done)
324 prev_trans = NULL;
325 else
326 prev_trans->use_count++;
327 }
Chris Mason78fae272007-03-25 11:35:08 -0400328 mutex_unlock(&root->fs_info->trans_mutex);
Chris Mason8fd17792007-04-19 21:01:03 -0400329 mutex_unlock(&root->fs_info->fs_mutex);
Chris Mason79154b12007-03-22 15:59:16 -0400330 ret = btrfs_write_and_wait_transaction(trans, root);
Chris Mason8fd17792007-04-19 21:01:03 -0400331 if (prev_trans) {
332 mutex_lock(&root->fs_info->trans_mutex);
333 wait_for_commit(root, prev_trans);
334 put_transaction(prev_trans);
335 mutex_unlock(&root->fs_info->trans_mutex);
336 }
337 btrfs_set_super_generation(root->fs_info->disk_super,
338 cur_trans->transid);
Chris Mason79154b12007-03-22 15:59:16 -0400339 BUG_ON(ret);
Chris Mason79154b12007-03-22 15:59:16 -0400340 write_ctree_super(trans, root);
Chris Mason8fd17792007-04-19 21:01:03 -0400341
342 mutex_lock(&root->fs_info->fs_mutex);
Chris Mason78fae272007-03-25 11:35:08 -0400343 btrfs_finish_extent_commit(trans, root);
344 mutex_lock(&root->fs_info->trans_mutex);
Chris Mason2c90e5d2007-04-02 10:50:19 -0400345 cur_trans->commit_done = 1;
346 wake_up(&cur_trans->commit_wait);
Chris Mason79154b12007-03-22 15:59:16 -0400347 put_transaction(cur_trans);
Chris Mason78fae272007-03-25 11:35:08 -0400348 put_transaction(cur_trans);
349 mutex_unlock(&root->fs_info->trans_mutex);
Chris Mason2c90e5d2007-04-02 10:50:19 -0400350 kmem_cache_free(btrfs_trans_handle_cachep, trans);
Chris Mason79154b12007-03-22 15:59:16 -0400351
Chris Mason0f7d52f2007-04-09 10:42:37 -0400352 drop_dirty_roots(root->fs_info->tree_root, &dirty_fs_roots);
Chris Mason79154b12007-03-22 15:59:16 -0400353 return ret;
354}
355
Chris Mason08607c12007-06-08 15:33:54 -0400356void btrfs_transaction_cleaner(struct work_struct *work)
357{
358 struct btrfs_fs_info *fs_info = container_of(work,
359 struct btrfs_fs_info,
360 trans_work.work);
361
362 struct btrfs_root *root = fs_info->tree_root;
363 struct btrfs_transaction *cur;
364 struct btrfs_trans_handle *trans;
365 unsigned long now;
366 unsigned long delay = HZ * 30;
367 int ret;
368
369printk("btrfs transaction cleaner\n");
370 mutex_lock(&root->fs_info->fs_mutex);
371 mutex_lock(&root->fs_info->trans_mutex);
372 cur = root->fs_info->running_transaction;
373 if (!cur) {
374 mutex_unlock(&root->fs_info->trans_mutex);
375 goto out;
376 }
377 now = get_seconds();
378 if (now < cur->start_time || now - cur->start_time < 30) {
379 mutex_unlock(&root->fs_info->trans_mutex);
380 delay = HZ * 5;
381 goto out;
382 }
383 mutex_unlock(&root->fs_info->trans_mutex);
384printk("forcing commit\n");
385 trans = btrfs_start_transaction(root, 1);
386 ret = btrfs_commit_transaction(trans, root);
387out:
388 mutex_unlock(&root->fs_info->fs_mutex);
389 btrfs_transaction_queue_work(root, delay);
390}
391
392void btrfs_transaction_queue_work(struct btrfs_root *root, int delay)
393{
394 queue_delayed_work(trans_wq, &root->fs_info->trans_work, delay);
395}
396
397void btrfs_transaction_flush_work(struct btrfs_root *root)
398{
399 cancel_rearming_delayed_workqueue(trans_wq, &root->fs_info->trans_work);
400 flush_workqueue(trans_wq);
401}
402
403void __init btrfs_init_transaction_sys(void)
404{
405 trans_wq = create_workqueue("btrfs");
406}
407
408void __exit btrfs_exit_transaction_sys(void)
409{
410 destroy_workqueue(trans_wq);
411}
412