Btrfs: cleaner make clean
[linux-2.6-block.git] / fs / btrfs / transaction.c
CommitLineData
6cbd5570
CM
1/*
2 * Copyright (C) 2007 Oracle. All rights reserved.
3 *
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.
7 *
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.
12 *
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.
17 */
18
79154b1b 19#include <linux/fs.h>
34088780 20#include <linux/sched.h>
79154b1b
CM
21#include "ctree.h"
22#include "disk-io.h"
23#include "transaction.h"
24
78fae27e 25static int total_trans = 0;
2c90e5d6
CM
26extern struct kmem_cache *btrfs_trans_handle_cachep;
27extern struct kmem_cache *btrfs_transaction_cachep;
28
08607c1b
CM
29static struct workqueue_struct *trans_wq;
30
0f7d52f4
CM
31#define BTRFS_ROOT_TRANS_TAG 0
32
79154b1b
CM
33static void put_transaction(struct btrfs_transaction *transaction)
34{
2c90e5d6 35 WARN_ON(transaction->use_count == 0);
79154b1b 36 transaction->use_count--;
78fae27e
CM
37 if (transaction->use_count == 0) {
38 WARN_ON(total_trans == 0);
39 total_trans--;
8fd17795 40 list_del_init(&transaction->list);
2c90e5d6
CM
41 memset(transaction, 0, sizeof(*transaction));
42 kmem_cache_free(btrfs_transaction_cachep, transaction);
78fae27e 43 }
79154b1b
CM
44}
45
46static int join_transaction(struct btrfs_root *root)
47{
48 struct btrfs_transaction *cur_trans;
49 cur_trans = root->fs_info->running_transaction;
50 if (!cur_trans) {
2c90e5d6
CM
51 cur_trans = kmem_cache_alloc(btrfs_transaction_cachep,
52 GFP_NOFS);
78fae27e 53 total_trans++;
79154b1b 54 BUG_ON(!cur_trans);
0f7d52f4 55 root->fs_info->generation++;
79154b1b
CM
56 root->fs_info->running_transaction = cur_trans;
57 cur_trans->num_writers = 0;
0f7d52f4 58 cur_trans->transid = root->fs_info->generation;
79154b1b
CM
59 init_waitqueue_head(&cur_trans->writer_wait);
60 init_waitqueue_head(&cur_trans->commit_wait);
61 cur_trans->in_commit = 0;
d5719762 62 cur_trans->use_count = 1;
79154b1b 63 cur_trans->commit_done = 0;
08607c1b 64 cur_trans->start_time = get_seconds();
8fd17795 65 list_add_tail(&cur_trans->list, &root->fs_info->trans_list);
7c4452b9 66 init_bit_radix(&cur_trans->dirty_pages);
79154b1b
CM
67 }
68 cur_trans->num_writers++;
69 return 0;
70}
71
72struct btrfs_trans_handle *btrfs_start_transaction(struct btrfs_root *root,
73 int num_blocks)
74{
2c90e5d6
CM
75 struct btrfs_trans_handle *h =
76 kmem_cache_alloc(btrfs_trans_handle_cachep, GFP_NOFS);
79154b1b 77 int ret;
0f7d52f4 78 u64 running_trans_id;
79154b1b
CM
79
80 mutex_lock(&root->fs_info->trans_mutex);
81 ret = join_transaction(root);
82 BUG_ON(ret);
0f7d52f4
CM
83 running_trans_id = root->fs_info->running_transaction->transid;
84
85 if (root != root->fs_info->tree_root && root->last_trans <
86 running_trans_id) {
ccd467d6
CM
87 WARN_ON(root == root->fs_info->extent_root);
88 WARN_ON(root->ref_cows != 1);
5eda7b5e
CM
89 if (root->root_item.refs != 0) {
90 radix_tree_tag_set(&root->fs_info->fs_roots_radix,
91 (unsigned long)root->root_key.objectid,
92 BTRFS_ROOT_TRANS_TAG);
93 root->commit_root = root->node;
94 get_bh(root->node);
95 } else {
96 WARN_ON(1);
97 }
0f7d52f4
CM
98 }
99 root->last_trans = running_trans_id;
100 h->transid = running_trans_id;
79154b1b
CM
101 h->transaction = root->fs_info->running_transaction;
102 h->blocks_reserved = num_blocks;
103 h->blocks_used = 0;
31f3c99b 104 h->block_group = NULL;
79154b1b
CM
105 root->fs_info->running_transaction->use_count++;
106 mutex_unlock(&root->fs_info->trans_mutex);
107 return h;
108}
109
110int btrfs_end_transaction(struct btrfs_trans_handle *trans,
111 struct btrfs_root *root)
112{
113 struct btrfs_transaction *cur_trans;
d6e4a428 114
79154b1b
CM
115 mutex_lock(&root->fs_info->trans_mutex);
116 cur_trans = root->fs_info->running_transaction;
ccd467d6 117 WARN_ON(cur_trans != trans->transaction);
d5719762 118 WARN_ON(cur_trans->num_writers < 1);
ccd467d6 119 cur_trans->num_writers--;
79154b1b
CM
120 if (waitqueue_active(&cur_trans->writer_wait))
121 wake_up(&cur_trans->writer_wait);
79154b1b
CM
122 put_transaction(cur_trans);
123 mutex_unlock(&root->fs_info->trans_mutex);
d6025579 124 memset(trans, 0, sizeof(*trans));
2c90e5d6 125 kmem_cache_free(btrfs_trans_handle_cachep, trans);
79154b1b
CM
126 return 0;
127}
128
129
130int btrfs_write_and_wait_transaction(struct btrfs_trans_handle *trans,
131 struct btrfs_root *root)
132{
7c4452b9
CM
133 unsigned long gang[16];
134 int ret;
135 int i;
136 int err;
137 int werr = 0;
138 struct page *page;
139 struct radix_tree_root *dirty_pages;
140 struct inode *btree_inode = root->fs_info->btree_inode;
141
142 if (!trans || !trans->transaction) {
143 return filemap_write_and_wait(btree_inode->i_mapping);
144 }
145 dirty_pages = &trans->transaction->dirty_pages;
146 while(1) {
e37c9e69
CM
147 ret = find_first_radix_bit(dirty_pages, gang,
148 0, ARRAY_SIZE(gang));
7c4452b9
CM
149 if (!ret)
150 break;
151 for (i = 0; i < ret; i++) {
152 /* FIXME EIO */
153 clear_radix_bit(dirty_pages, gang[i]);
154 page = find_lock_page(btree_inode->i_mapping,
155 gang[i]);
156 if (!page)
157 continue;
158 err = write_one_page(page, 0);
159 if (err)
160 werr = err;
161 page_cache_release(page);
162 }
163 }
164 err = filemap_fdatawait(btree_inode->i_mapping);
165 if (err)
166 werr = err;
167 return werr;
79154b1b
CM
168}
169
170int btrfs_commit_tree_roots(struct btrfs_trans_handle *trans,
171 struct btrfs_root *root)
172{
173 int ret;
174 u64 old_extent_block;
175 struct btrfs_fs_info *fs_info = root->fs_info;
176 struct btrfs_root *tree_root = fs_info->tree_root;
177 struct btrfs_root *extent_root = fs_info->extent_root;
79154b1b 178
9078a3e1 179 btrfs_write_dirty_block_groups(trans, extent_root);
79154b1b
CM
180 while(1) {
181 old_extent_block = btrfs_root_blocknr(&extent_root->root_item);
7eccb903 182 if (old_extent_block == bh_blocknr(extent_root->node))
79154b1b
CM
183 break;
184 btrfs_set_root_blocknr(&extent_root->root_item,
7eccb903 185 bh_blocknr(extent_root->node));
79154b1b
CM
186 ret = btrfs_update_root(trans, tree_root,
187 &extent_root->root_key,
188 &extent_root->root_item);
189 BUG_ON(ret);
9078a3e1 190 btrfs_write_dirty_block_groups(trans, extent_root);
79154b1b
CM
191 }
192 return 0;
193}
194
195static int wait_for_commit(struct btrfs_root *root,
196 struct btrfs_transaction *commit)
197{
198 DEFINE_WAIT(wait);
ccd467d6 199 mutex_lock(&root->fs_info->trans_mutex);
79154b1b
CM
200 while(!commit->commit_done) {
201 prepare_to_wait(&commit->commit_wait, &wait,
202 TASK_UNINTERRUPTIBLE);
203 if (commit->commit_done)
204 break;
205 mutex_unlock(&root->fs_info->trans_mutex);
206 schedule();
207 mutex_lock(&root->fs_info->trans_mutex);
208 }
ccd467d6 209 mutex_unlock(&root->fs_info->trans_mutex);
79154b1b
CM
210 finish_wait(&commit->commit_wait, &wait);
211 return 0;
212}
213
0f7d52f4
CM
214struct dirty_root {
215 struct list_head list;
216 struct btrfs_key snap_key;
217 struct buffer_head *commit_root;
218 struct btrfs_root *root;
5eda7b5e 219 int free_on_drop;
0f7d52f4
CM
220};
221
5eda7b5e
CM
222int btrfs_add_dead_root(struct btrfs_root *root, struct list_head *dead_list)
223{
224 struct dirty_root *dirty;
225
226 dirty = kmalloc(sizeof(*dirty), GFP_NOFS);
227 if (!dirty)
228 return -ENOMEM;
229 memcpy(&dirty->snap_key, &root->root_key, sizeof(root->root_key));
230 dirty->commit_root = root->node;
231 dirty->root = root;
232 dirty->free_on_drop = 1;
233 list_add(&dirty->list, dead_list);
234 return 0;
235}
236
35b7e476
CM
237static int add_dirty_roots(struct btrfs_trans_handle *trans,
238 struct radix_tree_root *radix,
239 struct list_head *list)
0f7d52f4
CM
240{
241 struct dirty_root *dirty;
242 struct btrfs_root *gang[8];
243 struct btrfs_root *root;
5eda7b5e 244 struct btrfs_root_item tmp_item;
0f7d52f4
CM
245 int i;
246 int ret;
54aa1f4d 247 int err = 0;
5eda7b5e 248 u32 refs;
54aa1f4d 249
0f7d52f4
CM
250 while(1) {
251 ret = radix_tree_gang_lookup_tag(radix, (void **)gang, 0,
252 ARRAY_SIZE(gang),
253 BTRFS_ROOT_TRANS_TAG);
254 if (ret == 0)
255 break;
256 for (i = 0; i < ret; i++) {
257 root = gang[i];
2619ba1f
CM
258 radix_tree_tag_clear(radix,
259 (unsigned long)root->root_key.objectid,
260 BTRFS_ROOT_TRANS_TAG);
0f7d52f4 261 if (root->commit_root == root->node) {
7eccb903 262 WARN_ON(bh_blocknr(root->node) !=
0f7d52f4
CM
263 btrfs_root_blocknr(&root->root_item));
264 brelse(root->commit_root);
265 root->commit_root = NULL;
266 continue;
267 }
268 dirty = kmalloc(sizeof(*dirty), GFP_NOFS);
269 BUG_ON(!dirty);
270 memcpy(&dirty->snap_key, &root->root_key,
271 sizeof(root->root_key));
272 dirty->commit_root = root->commit_root;
273 root->commit_root = NULL;
274 dirty->root = root;
5eda7b5e
CM
275 dirty->free_on_drop = 0;
276 memcpy(&tmp_item, &root->root_item, sizeof(tmp_item));
277
0f7d52f4
CM
278 root->root_key.offset = root->fs_info->generation;
279 btrfs_set_root_blocknr(&root->root_item,
7eccb903 280 bh_blocknr(root->node));
0f7d52f4
CM
281 err = btrfs_insert_root(trans, root->fs_info->tree_root,
282 &root->root_key,
283 &root->root_item);
54aa1f4d
CM
284 if (err)
285 break;
5eda7b5e
CM
286 refs = btrfs_root_refs(&tmp_item);
287 btrfs_set_root_refs(&tmp_item, refs - 1);
288 err = btrfs_update_root(trans, root->fs_info->tree_root,
289 &dirty->snap_key,
290 &tmp_item);
291
292 BUG_ON(err);
293 if (refs == 1)
294 list_add(&dirty->list, list);
295 else
296 kfree(dirty);
0f7d52f4
CM
297 }
298 }
54aa1f4d 299 return err;
0f7d52f4
CM
300}
301
35b7e476
CM
302static int drop_dirty_roots(struct btrfs_root *tree_root,
303 struct list_head *list)
0f7d52f4
CM
304{
305 struct dirty_root *dirty;
306 struct btrfs_trans_handle *trans;
54aa1f4d 307 int ret = 0;
0f7d52f4 308 while(!list_empty(list)) {
facda1e7 309 mutex_lock(&tree_root->fs_info->fs_mutex);
0f7d52f4
CM
310 dirty = list_entry(list->next, struct dirty_root, list);
311 list_del_init(&dirty->list);
5eda7b5e 312
0f7d52f4 313 trans = btrfs_start_transaction(tree_root, 1);
0f7d52f4
CM
314 ret = btrfs_drop_snapshot(trans, dirty->root,
315 dirty->commit_root);
316 BUG_ON(ret);
0f7d52f4 317 ret = btrfs_del_root(trans, tree_root, &dirty->snap_key);
54aa1f4d
CM
318 if (ret)
319 break;
0f7d52f4
CM
320 ret = btrfs_end_transaction(trans, tree_root);
321 BUG_ON(ret);
5eda7b5e
CM
322
323 if (dirty->free_on_drop)
324 kfree(dirty->root);
0f7d52f4 325 kfree(dirty);
facda1e7 326 mutex_unlock(&tree_root->fs_info->fs_mutex);
8c2383c3 327 btrfs_btree_balance_dirty(tree_root);
0f7d52f4 328 }
54aa1f4d 329 return ret;
0f7d52f4
CM
330}
331
79154b1b
CM
332int btrfs_commit_transaction(struct btrfs_trans_handle *trans,
333 struct btrfs_root *root)
334{
335 int ret = 0;
79154b1b 336 struct btrfs_transaction *cur_trans;
8fd17795 337 struct btrfs_transaction *prev_trans = NULL;
0f7d52f4 338 struct list_head dirty_fs_roots;
ccd467d6 339 struct radix_tree_root pinned_copy;
79154b1b
CM
340 DEFINE_WAIT(wait);
341
ccd467d6 342 init_bit_radix(&pinned_copy);
0f7d52f4 343 INIT_LIST_HEAD(&dirty_fs_roots);
d6e4a428 344
79154b1b
CM
345 mutex_lock(&root->fs_info->trans_mutex);
346 if (trans->transaction->in_commit) {
347 cur_trans = trans->transaction;
348 trans->transaction->use_count++;
ccd467d6 349 mutex_unlock(&root->fs_info->trans_mutex);
79154b1b 350 btrfs_end_transaction(trans, root);
ccd467d6
CM
351
352 mutex_unlock(&root->fs_info->fs_mutex);
79154b1b
CM
353 ret = wait_for_commit(root, cur_trans);
354 BUG_ON(ret);
355 put_transaction(cur_trans);
ccd467d6 356 mutex_lock(&root->fs_info->fs_mutex);
79154b1b
CM
357 return 0;
358 }
2c90e5d6 359 trans->transaction->in_commit = 1;
ccd467d6
CM
360 cur_trans = trans->transaction;
361 if (cur_trans->list.prev != &root->fs_info->trans_list) {
362 prev_trans = list_entry(cur_trans->list.prev,
363 struct btrfs_transaction, list);
364 if (!prev_trans->commit_done) {
365 prev_trans->use_count++;
366 mutex_unlock(&root->fs_info->fs_mutex);
367 mutex_unlock(&root->fs_info->trans_mutex);
368
369 wait_for_commit(root, prev_trans);
370 put_transaction(prev_trans);
371
372 mutex_lock(&root->fs_info->fs_mutex);
373 mutex_lock(&root->fs_info->trans_mutex);
374 }
375 }
79154b1b 376 while (trans->transaction->num_writers > 1) {
2c90e5d6 377 WARN_ON(cur_trans != trans->transaction);
79154b1b
CM
378 prepare_to_wait(&trans->transaction->writer_wait, &wait,
379 TASK_UNINTERRUPTIBLE);
380 if (trans->transaction->num_writers <= 1)
381 break;
ccd467d6 382 mutex_unlock(&root->fs_info->fs_mutex);
79154b1b
CM
383 mutex_unlock(&root->fs_info->trans_mutex);
384 schedule();
ccd467d6 385 mutex_lock(&root->fs_info->fs_mutex);
79154b1b 386 mutex_lock(&root->fs_info->trans_mutex);
2c90e5d6 387 finish_wait(&trans->transaction->writer_wait, &wait);
79154b1b
CM
388 }
389 finish_wait(&trans->transaction->writer_wait, &wait);
2c90e5d6 390 WARN_ON(cur_trans != trans->transaction);
54aa1f4d
CM
391 ret = add_dirty_roots(trans, &root->fs_info->fs_roots_radix,
392 &dirty_fs_roots);
393 BUG_ON(ret);
394
79154b1b
CM
395 ret = btrfs_commit_tree_roots(trans, root);
396 BUG_ON(ret);
54aa1f4d 397
78fae27e
CM
398 cur_trans = root->fs_info->running_transaction;
399 root->fs_info->running_transaction = NULL;
4b52dff6
CM
400 btrfs_set_super_generation(&root->fs_info->super_copy,
401 cur_trans->transid);
402 btrfs_set_super_root(&root->fs_info->super_copy,
403 bh_blocknr(root->fs_info->tree_root->node));
404 memcpy(root->fs_info->disk_super, &root->fs_info->super_copy,
405 sizeof(root->fs_info->super_copy));
ccd467d6
CM
406
407 btrfs_copy_pinned(root, &pinned_copy);
408
78fae27e 409 mutex_unlock(&root->fs_info->trans_mutex);
8fd17795 410 mutex_unlock(&root->fs_info->fs_mutex);
79154b1b
CM
411 ret = btrfs_write_and_wait_transaction(trans, root);
412 BUG_ON(ret);
79154b1b 413 write_ctree_super(trans, root);
8fd17795 414 mutex_lock(&root->fs_info->fs_mutex);
ccd467d6 415 btrfs_finish_extent_commit(trans, root, &pinned_copy);
78fae27e 416 mutex_lock(&root->fs_info->trans_mutex);
2c90e5d6
CM
417 cur_trans->commit_done = 1;
418 wake_up(&cur_trans->commit_wait);
78fae27e 419 put_transaction(cur_trans);
79154b1b 420 put_transaction(cur_trans);
facda1e7
CM
421 if (root->fs_info->closing)
422 list_splice_init(&root->fs_info->dead_roots, &dirty_fs_roots);
423 else
424 list_splice_init(&dirty_fs_roots, &root->fs_info->dead_roots);
78fae27e 425 mutex_unlock(&root->fs_info->trans_mutex);
2c90e5d6 426 kmem_cache_free(btrfs_trans_handle_cachep, trans);
79154b1b 427
facda1e7
CM
428 if (root->fs_info->closing) {
429 mutex_unlock(&root->fs_info->fs_mutex);
430 drop_dirty_roots(root->fs_info->tree_root, &dirty_fs_roots);
431 mutex_lock(&root->fs_info->fs_mutex);
432 }
79154b1b
CM
433 return ret;
434}
435
08607c1b
CM
436void btrfs_transaction_cleaner(struct work_struct *work)
437{
438 struct btrfs_fs_info *fs_info = container_of(work,
439 struct btrfs_fs_info,
440 trans_work.work);
441
442 struct btrfs_root *root = fs_info->tree_root;
443 struct btrfs_transaction *cur;
444 struct btrfs_trans_handle *trans;
facda1e7 445 struct list_head dirty_roots;
08607c1b
CM
446 unsigned long now;
447 unsigned long delay = HZ * 30;
448 int ret;
449
facda1e7 450 INIT_LIST_HEAD(&dirty_roots);
08607c1b
CM
451 mutex_lock(&root->fs_info->fs_mutex);
452 mutex_lock(&root->fs_info->trans_mutex);
453 cur = root->fs_info->running_transaction;
454 if (!cur) {
455 mutex_unlock(&root->fs_info->trans_mutex);
456 goto out;
457 }
458 now = get_seconds();
459 if (now < cur->start_time || now - cur->start_time < 30) {
460 mutex_unlock(&root->fs_info->trans_mutex);
461 delay = HZ * 5;
462 goto out;
463 }
464 mutex_unlock(&root->fs_info->trans_mutex);
08607c1b
CM
465 trans = btrfs_start_transaction(root, 1);
466 ret = btrfs_commit_transaction(trans, root);
467out:
468 mutex_unlock(&root->fs_info->fs_mutex);
ad693af6
CM
469
470 mutex_lock(&root->fs_info->trans_mutex);
471 list_splice_init(&root->fs_info->dead_roots, &dirty_roots);
472 mutex_unlock(&root->fs_info->trans_mutex);
473
474 if (!list_empty(&dirty_roots)) {
475 drop_dirty_roots(root, &dirty_roots);
476 }
08607c1b
CM
477 btrfs_transaction_queue_work(root, delay);
478}
479
480void btrfs_transaction_queue_work(struct btrfs_root *root, int delay)
481{
482 queue_delayed_work(trans_wq, &root->fs_info->trans_work, delay);
483}
484
485void btrfs_transaction_flush_work(struct btrfs_root *root)
486{
487 cancel_rearming_delayed_workqueue(trans_wq, &root->fs_info->trans_work);
488 flush_workqueue(trans_wq);
489}
490
491void __init btrfs_init_transaction_sys(void)
492{
493 trans_wq = create_workqueue("btrfs");
494}
495
496void __exit btrfs_exit_transaction_sys(void)
497{
498 destroy_workqueue(trans_wq);
499}
500