2 * Copyright (C) 2007 Oracle. All rights reserved.
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.
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.
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.
20 #include <linux/sched.h>
23 #include "transaction.h"
25 static int total_trans = 0;
26 extern struct kmem_cache *btrfs_trans_handle_cachep;
27 extern struct kmem_cache *btrfs_transaction_cachep;
29 static struct workqueue_struct *trans_wq;
31 #define BTRFS_ROOT_TRANS_TAG 0
32 #define BTRFS_ROOT_DEFRAG_TAG 1
34 static void put_transaction(struct btrfs_transaction *transaction)
36 WARN_ON(transaction->use_count == 0);
37 transaction->use_count--;
38 if (transaction->use_count == 0) {
39 WARN_ON(total_trans == 0);
41 list_del_init(&transaction->list);
42 memset(transaction, 0, sizeof(*transaction));
43 kmem_cache_free(btrfs_transaction_cachep, transaction);
47 static int join_transaction(struct btrfs_root *root)
49 struct btrfs_transaction *cur_trans;
50 cur_trans = root->fs_info->running_transaction;
52 cur_trans = kmem_cache_alloc(btrfs_transaction_cachep,
56 root->fs_info->generation++;
57 root->fs_info->running_transaction = cur_trans;
58 cur_trans->num_writers = 0;
59 cur_trans->transid = root->fs_info->generation;
60 init_waitqueue_head(&cur_trans->writer_wait);
61 init_waitqueue_head(&cur_trans->commit_wait);
62 cur_trans->in_commit = 0;
63 cur_trans->use_count = 1;
64 cur_trans->commit_done = 0;
65 cur_trans->start_time = get_seconds();
66 list_add_tail(&cur_trans->list, &root->fs_info->trans_list);
67 init_bit_radix(&cur_trans->dirty_pages);
69 cur_trans->num_writers++;
73 static int record_root_in_trans(struct btrfs_root *root)
75 u64 running_trans_id = root->fs_info->running_transaction->transid;
76 if (root->ref_cows && root->last_trans < running_trans_id) {
77 WARN_ON(root == root->fs_info->extent_root);
78 if (root->root_item.refs != 0) {
79 radix_tree_tag_set(&root->fs_info->fs_roots_radix,
80 (unsigned long)root->root_key.objectid,
81 BTRFS_ROOT_TRANS_TAG);
82 radix_tree_tag_set(&root->fs_info->fs_roots_radix,
83 (unsigned long)root->root_key.objectid,
84 BTRFS_ROOT_DEFRAG_TAG);
85 root->commit_root = root->node;
90 root->last_trans = running_trans_id;
95 struct btrfs_trans_handle *btrfs_start_transaction(struct btrfs_root *root,
98 struct btrfs_trans_handle *h =
99 kmem_cache_alloc(btrfs_trans_handle_cachep, GFP_NOFS);
102 mutex_lock(&root->fs_info->trans_mutex);
103 ret = join_transaction(root);
106 record_root_in_trans(root);
107 h->transid = root->fs_info->running_transaction->transid;
108 h->transaction = root->fs_info->running_transaction;
109 h->blocks_reserved = num_blocks;
111 h->block_group = NULL;
112 root->fs_info->running_transaction->use_count++;
113 mutex_unlock(&root->fs_info->trans_mutex);
117 int btrfs_end_transaction(struct btrfs_trans_handle *trans,
118 struct btrfs_root *root)
120 struct btrfs_transaction *cur_trans;
122 mutex_lock(&root->fs_info->trans_mutex);
123 cur_trans = root->fs_info->running_transaction;
124 WARN_ON(cur_trans != trans->transaction);
125 WARN_ON(cur_trans->num_writers < 1);
126 cur_trans->num_writers--;
127 if (waitqueue_active(&cur_trans->writer_wait))
128 wake_up(&cur_trans->writer_wait);
129 put_transaction(cur_trans);
130 mutex_unlock(&root->fs_info->trans_mutex);
131 memset(trans, 0, sizeof(*trans));
132 kmem_cache_free(btrfs_trans_handle_cachep, trans);
137 int btrfs_write_and_wait_transaction(struct btrfs_trans_handle *trans,
138 struct btrfs_root *root)
140 unsigned long gang[16];
146 struct radix_tree_root *dirty_pages;
147 struct inode *btree_inode = root->fs_info->btree_inode;
149 if (!trans || !trans->transaction) {
150 return filemap_write_and_wait(btree_inode->i_mapping);
152 dirty_pages = &trans->transaction->dirty_pages;
154 ret = find_first_radix_bit(dirty_pages, gang,
155 0, ARRAY_SIZE(gang));
158 for (i = 0; i < ret; i++) {
160 clear_radix_bit(dirty_pages, gang[i]);
161 page = find_lock_page(btree_inode->i_mapping,
165 if (PageWriteback(page)) {
167 wait_on_page_writeback(page);
170 page_cache_release(page);
174 err = write_one_page(page, 0);
177 page_cache_release(page);
180 err = filemap_fdatawait(btree_inode->i_mapping);
186 int btrfs_commit_tree_roots(struct btrfs_trans_handle *trans,
187 struct btrfs_root *root)
190 u64 old_extent_block;
191 struct btrfs_fs_info *fs_info = root->fs_info;
192 struct btrfs_root *tree_root = fs_info->tree_root;
193 struct btrfs_root *extent_root = fs_info->extent_root;
195 btrfs_write_dirty_block_groups(trans, extent_root);
197 old_extent_block = btrfs_root_blocknr(&extent_root->root_item);
198 if (old_extent_block == bh_blocknr(extent_root->node))
200 btrfs_set_root_blocknr(&extent_root->root_item,
201 bh_blocknr(extent_root->node));
202 ret = btrfs_update_root(trans, tree_root,
203 &extent_root->root_key,
204 &extent_root->root_item);
206 btrfs_write_dirty_block_groups(trans, extent_root);
211 static int wait_for_commit(struct btrfs_root *root,
212 struct btrfs_transaction *commit)
215 mutex_lock(&root->fs_info->trans_mutex);
216 while(!commit->commit_done) {
217 prepare_to_wait(&commit->commit_wait, &wait,
218 TASK_UNINTERRUPTIBLE);
219 if (commit->commit_done)
221 mutex_unlock(&root->fs_info->trans_mutex);
223 mutex_lock(&root->fs_info->trans_mutex);
225 mutex_unlock(&root->fs_info->trans_mutex);
226 finish_wait(&commit->commit_wait, &wait);
231 struct list_head list;
232 struct btrfs_root *root;
235 int btrfs_add_dead_root(struct btrfs_root *root, struct list_head *dead_list)
237 struct dirty_root *dirty;
239 dirty = kmalloc(sizeof(*dirty), GFP_NOFS);
243 list_add(&dirty->list, dead_list);
247 static int add_dirty_roots(struct btrfs_trans_handle *trans,
248 struct radix_tree_root *radix,
249 struct list_head *list)
251 struct dirty_root *dirty;
252 struct btrfs_root *gang[8];
253 struct btrfs_root *root;
260 ret = radix_tree_gang_lookup_tag(radix, (void **)gang, 0,
262 BTRFS_ROOT_TRANS_TAG);
265 for (i = 0; i < ret; i++) {
267 radix_tree_tag_clear(radix,
268 (unsigned long)root->root_key.objectid,
269 BTRFS_ROOT_TRANS_TAG);
270 if (root->commit_root == root->node) {
271 WARN_ON(bh_blocknr(root->node) !=
272 btrfs_root_blocknr(&root->root_item));
273 brelse(root->commit_root);
274 root->commit_root = NULL;
277 dirty = kmalloc(sizeof(*dirty), GFP_NOFS);
279 dirty->root = kmalloc(sizeof(*dirty->root), GFP_NOFS);
280 BUG_ON(!dirty->root);
282 memset(&root->root_item.drop_progress, 0,
283 sizeof(struct btrfs_disk_key));
284 root->root_item.drop_level = 0;
286 memcpy(dirty->root, root, sizeof(*root));
287 dirty->root->node = root->commit_root;
288 root->commit_root = NULL;
290 root->root_key.offset = root->fs_info->generation;
291 btrfs_set_root_blocknr(&root->root_item,
292 bh_blocknr(root->node));
293 err = btrfs_insert_root(trans, root->fs_info->tree_root,
299 refs = btrfs_root_refs(&dirty->root->root_item);
300 btrfs_set_root_refs(&dirty->root->root_item, refs - 1);
301 err = btrfs_update_root(trans, root->fs_info->tree_root,
302 &dirty->root->root_key,
303 &dirty->root->root_item);
307 list_add(&dirty->list, list);
318 int btrfs_defrag_dirty_roots(struct btrfs_fs_info *info)
320 struct btrfs_root *gang[1];
321 struct btrfs_root *root;
322 struct btrfs_root *tree_root = info->tree_root;
323 struct btrfs_trans_handle *trans;
329 trans = btrfs_start_transaction(tree_root, 1);
331 ret = radix_tree_gang_lookup_tag(&info->fs_roots_radix,
334 BTRFS_ROOT_DEFRAG_TAG);
337 for (i = 0; i < ret; i++) {
339 last = root->root_key.objectid + 1;
340 radix_tree_tag_clear(&info->fs_roots_radix,
341 (unsigned long)root->root_key.objectid,
342 BTRFS_ROOT_DEFRAG_TAG);
343 if (root->defrag_running)
347 mutex_lock(&root->fs_info->trans_mutex);
348 record_root_in_trans(root);
349 mutex_unlock(&root->fs_info->trans_mutex);
351 root->defrag_running = 1;
352 err = btrfs_defrag_leaves(trans, root, 1);
353 btrfs_end_transaction(trans, tree_root);
354 mutex_unlock(&info->fs_mutex);
356 btrfs_btree_balance_dirty(root);
359 mutex_lock(&info->fs_mutex);
360 trans = btrfs_start_transaction(tree_root, 1);
364 root->defrag_running = 0;
367 btrfs_end_transaction(trans, tree_root);
371 static int drop_dirty_roots(struct btrfs_root *tree_root,
372 struct list_head *list)
374 struct dirty_root *dirty;
375 struct btrfs_trans_handle *trans;
379 while(!list_empty(list)) {
380 mutex_lock(&tree_root->fs_info->fs_mutex);
381 dirty = list_entry(list->next, struct dirty_root, list);
382 list_del_init(&dirty->list);
385 trans = btrfs_start_transaction(tree_root, 1);
386 ret = btrfs_drop_snapshot(trans, dirty->root);
387 if (ret != -EAGAIN) {
390 err = btrfs_update_root(trans,
392 &dirty->root->root_key,
393 &dirty->root->root_item);
396 ret = btrfs_end_transaction(trans, tree_root);
398 mutex_unlock(&tree_root->fs_info->fs_mutex);
400 btrfs_btree_balance_dirty(tree_root);
403 mutex_lock(&tree_root->fs_info->fs_mutex);
406 ret = btrfs_del_root(trans, tree_root, &dirty->root->root_key);
409 ret = btrfs_end_transaction(trans, tree_root);
414 mutex_unlock(&tree_root->fs_info->fs_mutex);
415 btrfs_btree_balance_dirty(tree_root);
421 int btrfs_commit_transaction(struct btrfs_trans_handle *trans,
422 struct btrfs_root *root)
425 struct btrfs_transaction *cur_trans;
426 struct btrfs_transaction *prev_trans = NULL;
427 struct list_head dirty_fs_roots;
428 struct radix_tree_root pinned_copy;
431 init_bit_radix(&pinned_copy);
432 INIT_LIST_HEAD(&dirty_fs_roots);
434 mutex_lock(&root->fs_info->trans_mutex);
435 if (trans->transaction->in_commit) {
436 cur_trans = trans->transaction;
437 trans->transaction->use_count++;
438 mutex_unlock(&root->fs_info->trans_mutex);
439 btrfs_end_transaction(trans, root);
441 mutex_unlock(&root->fs_info->fs_mutex);
442 ret = wait_for_commit(root, cur_trans);
444 put_transaction(cur_trans);
445 mutex_lock(&root->fs_info->fs_mutex);
448 trans->transaction->in_commit = 1;
449 cur_trans = trans->transaction;
450 if (cur_trans->list.prev != &root->fs_info->trans_list) {
451 prev_trans = list_entry(cur_trans->list.prev,
452 struct btrfs_transaction, list);
453 if (!prev_trans->commit_done) {
454 prev_trans->use_count++;
455 mutex_unlock(&root->fs_info->fs_mutex);
456 mutex_unlock(&root->fs_info->trans_mutex);
458 wait_for_commit(root, prev_trans);
459 put_transaction(prev_trans);
461 mutex_lock(&root->fs_info->fs_mutex);
462 mutex_lock(&root->fs_info->trans_mutex);
465 while (trans->transaction->num_writers > 1) {
466 WARN_ON(cur_trans != trans->transaction);
467 prepare_to_wait(&trans->transaction->writer_wait, &wait,
468 TASK_UNINTERRUPTIBLE);
469 if (trans->transaction->num_writers <= 1)
471 mutex_unlock(&root->fs_info->fs_mutex);
472 mutex_unlock(&root->fs_info->trans_mutex);
474 mutex_lock(&root->fs_info->fs_mutex);
475 mutex_lock(&root->fs_info->trans_mutex);
476 finish_wait(&trans->transaction->writer_wait, &wait);
478 finish_wait(&trans->transaction->writer_wait, &wait);
479 WARN_ON(cur_trans != trans->transaction);
480 ret = add_dirty_roots(trans, &root->fs_info->fs_roots_radix,
484 ret = btrfs_commit_tree_roots(trans, root);
487 cur_trans = root->fs_info->running_transaction;
488 root->fs_info->running_transaction = NULL;
489 btrfs_set_super_generation(&root->fs_info->super_copy,
491 btrfs_set_super_root(&root->fs_info->super_copy,
492 bh_blocknr(root->fs_info->tree_root->node));
493 memcpy(root->fs_info->disk_super, &root->fs_info->super_copy,
494 sizeof(root->fs_info->super_copy));
496 btrfs_copy_pinned(root, &pinned_copy);
498 mutex_unlock(&root->fs_info->trans_mutex);
499 mutex_unlock(&root->fs_info->fs_mutex);
500 ret = btrfs_write_and_wait_transaction(trans, root);
502 write_ctree_super(trans, root);
503 mutex_lock(&root->fs_info->fs_mutex);
504 btrfs_finish_extent_commit(trans, root, &pinned_copy);
505 mutex_lock(&root->fs_info->trans_mutex);
506 cur_trans->commit_done = 1;
507 wake_up(&cur_trans->commit_wait);
508 put_transaction(cur_trans);
509 put_transaction(cur_trans);
510 if (root->fs_info->closing)
511 list_splice_init(&root->fs_info->dead_roots, &dirty_fs_roots);
513 list_splice_init(&dirty_fs_roots, &root->fs_info->dead_roots);
514 mutex_unlock(&root->fs_info->trans_mutex);
515 kmem_cache_free(btrfs_trans_handle_cachep, trans);
517 if (root->fs_info->closing) {
518 mutex_unlock(&root->fs_info->fs_mutex);
519 drop_dirty_roots(root->fs_info->tree_root, &dirty_fs_roots);
520 mutex_lock(&root->fs_info->fs_mutex);
525 void btrfs_transaction_cleaner(struct work_struct *work)
527 struct btrfs_fs_info *fs_info = container_of(work,
528 struct btrfs_fs_info,
531 struct btrfs_root *root = fs_info->tree_root;
532 struct btrfs_transaction *cur;
533 struct btrfs_trans_handle *trans;
534 struct list_head dirty_roots;
536 unsigned long delay = HZ * 30;
539 INIT_LIST_HEAD(&dirty_roots);
540 mutex_lock(&root->fs_info->fs_mutex);
541 mutex_lock(&root->fs_info->trans_mutex);
542 cur = root->fs_info->running_transaction;
544 mutex_unlock(&root->fs_info->trans_mutex);
548 if (now < cur->start_time || now - cur->start_time < 30) {
549 mutex_unlock(&root->fs_info->trans_mutex);
553 mutex_unlock(&root->fs_info->trans_mutex);
554 btrfs_defrag_dirty_roots(root->fs_info);
555 trans = btrfs_start_transaction(root, 1);
556 ret = btrfs_commit_transaction(trans, root);
558 mutex_unlock(&root->fs_info->fs_mutex);
560 mutex_lock(&root->fs_info->trans_mutex);
561 list_splice_init(&root->fs_info->dead_roots, &dirty_roots);
562 mutex_unlock(&root->fs_info->trans_mutex);
564 if (!list_empty(&dirty_roots)) {
565 drop_dirty_roots(root, &dirty_roots);
567 btrfs_transaction_queue_work(root, delay);
570 void btrfs_transaction_queue_work(struct btrfs_root *root, int delay)
572 queue_delayed_work(trans_wq, &root->fs_info->trans_work, delay);
575 void btrfs_transaction_flush_work(struct btrfs_root *root)
577 cancel_rearming_delayed_workqueue(trans_wq, &root->fs_info->trans_work);
578 flush_workqueue(trans_wq);
581 void __init btrfs_init_transaction_sys(void)
583 trans_wq = create_workqueue("btrfs");
586 void __exit btrfs_exit_transaction_sys(void)
588 destroy_workqueue(trans_wq);