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 h->alloc_exclude_nr = 0;
113 h->alloc_exclude_start = 0;
114 root->fs_info->running_transaction->use_count++;
115 mutex_unlock(&root->fs_info->trans_mutex);
119 int btrfs_end_transaction(struct btrfs_trans_handle *trans,
120 struct btrfs_root *root)
122 struct btrfs_transaction *cur_trans;
124 mutex_lock(&root->fs_info->trans_mutex);
125 cur_trans = root->fs_info->running_transaction;
126 WARN_ON(cur_trans != trans->transaction);
127 WARN_ON(cur_trans->num_writers < 1);
128 cur_trans->num_writers--;
129 if (waitqueue_active(&cur_trans->writer_wait))
130 wake_up(&cur_trans->writer_wait);
131 put_transaction(cur_trans);
132 mutex_unlock(&root->fs_info->trans_mutex);
133 memset(trans, 0, sizeof(*trans));
134 kmem_cache_free(btrfs_trans_handle_cachep, trans);
139 int btrfs_write_and_wait_transaction(struct btrfs_trans_handle *trans,
140 struct btrfs_root *root)
142 unsigned long gang[16];
148 struct radix_tree_root *dirty_pages;
149 struct inode *btree_inode = root->fs_info->btree_inode;
151 if (!trans || !trans->transaction) {
152 return filemap_write_and_wait(btree_inode->i_mapping);
154 dirty_pages = &trans->transaction->dirty_pages;
156 ret = find_first_radix_bit(dirty_pages, gang,
157 0, ARRAY_SIZE(gang));
160 for (i = 0; i < ret; i++) {
162 clear_radix_bit(dirty_pages, gang[i]);
163 page = find_lock_page(btree_inode->i_mapping,
167 if (PageWriteback(page)) {
169 wait_on_page_writeback(page);
172 page_cache_release(page);
176 err = write_one_page(page, 0);
179 page_cache_release(page);
182 err = filemap_fdatawait(btree_inode->i_mapping);
188 int btrfs_commit_tree_roots(struct btrfs_trans_handle *trans,
189 struct btrfs_root *root)
192 u64 old_extent_block;
193 struct btrfs_fs_info *fs_info = root->fs_info;
194 struct btrfs_root *tree_root = fs_info->tree_root;
195 struct btrfs_root *extent_root = fs_info->extent_root;
197 btrfs_write_dirty_block_groups(trans, extent_root);
199 old_extent_block = btrfs_root_blocknr(&extent_root->root_item);
200 if (old_extent_block == bh_blocknr(extent_root->node))
202 btrfs_set_root_blocknr(&extent_root->root_item,
203 bh_blocknr(extent_root->node));
204 ret = btrfs_update_root(trans, tree_root,
205 &extent_root->root_key,
206 &extent_root->root_item);
208 btrfs_write_dirty_block_groups(trans, extent_root);
213 static int wait_for_commit(struct btrfs_root *root,
214 struct btrfs_transaction *commit)
217 mutex_lock(&root->fs_info->trans_mutex);
218 while(!commit->commit_done) {
219 prepare_to_wait(&commit->commit_wait, &wait,
220 TASK_UNINTERRUPTIBLE);
221 if (commit->commit_done)
223 mutex_unlock(&root->fs_info->trans_mutex);
225 mutex_lock(&root->fs_info->trans_mutex);
227 mutex_unlock(&root->fs_info->trans_mutex);
228 finish_wait(&commit->commit_wait, &wait);
233 struct list_head list;
234 struct btrfs_root *root;
237 int btrfs_add_dead_root(struct btrfs_root *root, struct list_head *dead_list)
239 struct dirty_root *dirty;
241 dirty = kmalloc(sizeof(*dirty), GFP_NOFS);
245 list_add(&dirty->list, dead_list);
249 static int add_dirty_roots(struct btrfs_trans_handle *trans,
250 struct radix_tree_root *radix,
251 struct list_head *list)
253 struct dirty_root *dirty;
254 struct btrfs_root *gang[8];
255 struct btrfs_root *root;
262 ret = radix_tree_gang_lookup_tag(radix, (void **)gang, 0,
264 BTRFS_ROOT_TRANS_TAG);
267 for (i = 0; i < ret; i++) {
269 radix_tree_tag_clear(radix,
270 (unsigned long)root->root_key.objectid,
271 BTRFS_ROOT_TRANS_TAG);
272 if (root->commit_root == root->node) {
273 WARN_ON(bh_blocknr(root->node) !=
274 btrfs_root_blocknr(&root->root_item));
275 brelse(root->commit_root);
276 root->commit_root = NULL;
279 dirty = kmalloc(sizeof(*dirty), GFP_NOFS);
281 dirty->root = kmalloc(sizeof(*dirty->root), GFP_NOFS);
282 BUG_ON(!dirty->root);
284 memset(&root->root_item.drop_progress, 0,
285 sizeof(struct btrfs_disk_key));
286 root->root_item.drop_level = 0;
288 memcpy(dirty->root, root, sizeof(*root));
289 dirty->root->node = root->commit_root;
290 root->commit_root = NULL;
292 root->root_key.offset = root->fs_info->generation;
293 btrfs_set_root_blocknr(&root->root_item,
294 bh_blocknr(root->node));
295 err = btrfs_insert_root(trans, root->fs_info->tree_root,
301 refs = btrfs_root_refs(&dirty->root->root_item);
302 btrfs_set_root_refs(&dirty->root->root_item, refs - 1);
303 err = btrfs_update_root(trans, root->fs_info->tree_root,
304 &dirty->root->root_key,
305 &dirty->root->root_item);
309 list_add(&dirty->list, list);
320 int btrfs_defrag_dirty_roots(struct btrfs_fs_info *info)
322 struct btrfs_root *gang[1];
323 struct btrfs_root *root;
324 struct btrfs_root *tree_root = info->tree_root;
325 struct btrfs_trans_handle *trans;
331 trans = btrfs_start_transaction(tree_root, 1);
333 ret = radix_tree_gang_lookup_tag(&info->fs_roots_radix,
336 BTRFS_ROOT_DEFRAG_TAG);
339 for (i = 0; i < ret; i++) {
341 last = root->root_key.objectid + 1;
342 radix_tree_tag_clear(&info->fs_roots_radix,
343 (unsigned long)root->root_key.objectid,
344 BTRFS_ROOT_DEFRAG_TAG);
345 if (root->defrag_running)
349 mutex_lock(&root->fs_info->trans_mutex);
350 record_root_in_trans(root);
351 mutex_unlock(&root->fs_info->trans_mutex);
353 root->defrag_running = 1;
354 err = btrfs_defrag_leaves(trans, root, 1);
355 btrfs_end_transaction(trans, tree_root);
356 mutex_unlock(&info->fs_mutex);
358 btrfs_btree_balance_dirty(root);
361 mutex_lock(&info->fs_mutex);
362 trans = btrfs_start_transaction(tree_root, 1);
366 root->defrag_running = 0;
369 btrfs_end_transaction(trans, tree_root);
373 static int drop_dirty_roots(struct btrfs_root *tree_root,
374 struct list_head *list)
376 struct dirty_root *dirty;
377 struct btrfs_trans_handle *trans;
381 while(!list_empty(list)) {
382 mutex_lock(&tree_root->fs_info->fs_mutex);
383 dirty = list_entry(list->next, struct dirty_root, list);
384 list_del_init(&dirty->list);
387 trans = btrfs_start_transaction(tree_root, 1);
388 ret = btrfs_drop_snapshot(trans, dirty->root);
389 if (ret != -EAGAIN) {
392 err = btrfs_update_root(trans,
394 &dirty->root->root_key,
395 &dirty->root->root_item);
398 ret = btrfs_end_transaction(trans, tree_root);
400 mutex_unlock(&tree_root->fs_info->fs_mutex);
402 btrfs_btree_balance_dirty(tree_root);
405 mutex_lock(&tree_root->fs_info->fs_mutex);
408 ret = btrfs_del_root(trans, tree_root, &dirty->root->root_key);
411 ret = btrfs_end_transaction(trans, tree_root);
416 mutex_unlock(&tree_root->fs_info->fs_mutex);
417 btrfs_btree_balance_dirty(tree_root);
423 int btrfs_commit_transaction(struct btrfs_trans_handle *trans,
424 struct btrfs_root *root)
427 struct btrfs_transaction *cur_trans;
428 struct btrfs_transaction *prev_trans = NULL;
429 struct list_head dirty_fs_roots;
430 struct radix_tree_root pinned_copy;
433 init_bit_radix(&pinned_copy);
434 INIT_LIST_HEAD(&dirty_fs_roots);
436 mutex_lock(&root->fs_info->trans_mutex);
437 if (trans->transaction->in_commit) {
438 cur_trans = trans->transaction;
439 trans->transaction->use_count++;
440 mutex_unlock(&root->fs_info->trans_mutex);
441 btrfs_end_transaction(trans, root);
443 mutex_unlock(&root->fs_info->fs_mutex);
444 ret = wait_for_commit(root, cur_trans);
446 put_transaction(cur_trans);
447 mutex_lock(&root->fs_info->fs_mutex);
450 trans->transaction->in_commit = 1;
451 cur_trans = trans->transaction;
452 if (cur_trans->list.prev != &root->fs_info->trans_list) {
453 prev_trans = list_entry(cur_trans->list.prev,
454 struct btrfs_transaction, list);
455 if (!prev_trans->commit_done) {
456 prev_trans->use_count++;
457 mutex_unlock(&root->fs_info->fs_mutex);
458 mutex_unlock(&root->fs_info->trans_mutex);
460 wait_for_commit(root, prev_trans);
461 put_transaction(prev_trans);
463 mutex_lock(&root->fs_info->fs_mutex);
464 mutex_lock(&root->fs_info->trans_mutex);
467 while (trans->transaction->num_writers > 1) {
468 WARN_ON(cur_trans != trans->transaction);
469 prepare_to_wait(&trans->transaction->writer_wait, &wait,
470 TASK_UNINTERRUPTIBLE);
471 if (trans->transaction->num_writers <= 1)
473 mutex_unlock(&root->fs_info->fs_mutex);
474 mutex_unlock(&root->fs_info->trans_mutex);
476 mutex_lock(&root->fs_info->fs_mutex);
477 mutex_lock(&root->fs_info->trans_mutex);
478 finish_wait(&trans->transaction->writer_wait, &wait);
480 finish_wait(&trans->transaction->writer_wait, &wait);
481 WARN_ON(cur_trans != trans->transaction);
482 ret = add_dirty_roots(trans, &root->fs_info->fs_roots_radix,
486 ret = btrfs_commit_tree_roots(trans, root);
489 cur_trans = root->fs_info->running_transaction;
490 root->fs_info->running_transaction = NULL;
491 btrfs_set_super_generation(&root->fs_info->super_copy,
493 btrfs_set_super_root(&root->fs_info->super_copy,
494 bh_blocknr(root->fs_info->tree_root->node));
495 memcpy(root->fs_info->disk_super, &root->fs_info->super_copy,
496 sizeof(root->fs_info->super_copy));
498 btrfs_copy_pinned(root, &pinned_copy);
500 mutex_unlock(&root->fs_info->trans_mutex);
501 mutex_unlock(&root->fs_info->fs_mutex);
502 ret = btrfs_write_and_wait_transaction(trans, root);
504 write_ctree_super(trans, root);
505 mutex_lock(&root->fs_info->fs_mutex);
506 btrfs_finish_extent_commit(trans, root, &pinned_copy);
507 mutex_lock(&root->fs_info->trans_mutex);
508 cur_trans->commit_done = 1;
509 wake_up(&cur_trans->commit_wait);
510 put_transaction(cur_trans);
511 put_transaction(cur_trans);
512 if (root->fs_info->closing)
513 list_splice_init(&root->fs_info->dead_roots, &dirty_fs_roots);
515 list_splice_init(&dirty_fs_roots, &root->fs_info->dead_roots);
516 mutex_unlock(&root->fs_info->trans_mutex);
517 kmem_cache_free(btrfs_trans_handle_cachep, trans);
519 if (root->fs_info->closing) {
520 mutex_unlock(&root->fs_info->fs_mutex);
521 drop_dirty_roots(root->fs_info->tree_root, &dirty_fs_roots);
522 mutex_lock(&root->fs_info->fs_mutex);
527 void btrfs_transaction_cleaner(struct work_struct *work)
529 struct btrfs_fs_info *fs_info = container_of(work,
530 struct btrfs_fs_info,
533 struct btrfs_root *root = fs_info->tree_root;
534 struct btrfs_transaction *cur;
535 struct btrfs_trans_handle *trans;
536 struct list_head dirty_roots;
538 unsigned long delay = HZ * 30;
541 INIT_LIST_HEAD(&dirty_roots);
542 mutex_lock(&root->fs_info->fs_mutex);
543 mutex_lock(&root->fs_info->trans_mutex);
544 cur = root->fs_info->running_transaction;
546 mutex_unlock(&root->fs_info->trans_mutex);
550 if (now < cur->start_time || now - cur->start_time < 30) {
551 mutex_unlock(&root->fs_info->trans_mutex);
555 mutex_unlock(&root->fs_info->trans_mutex);
556 btrfs_defrag_dirty_roots(root->fs_info);
557 trans = btrfs_start_transaction(root, 1);
558 ret = btrfs_commit_transaction(trans, root);
560 mutex_unlock(&root->fs_info->fs_mutex);
562 mutex_lock(&root->fs_info->trans_mutex);
563 list_splice_init(&root->fs_info->dead_roots, &dirty_roots);
564 mutex_unlock(&root->fs_info->trans_mutex);
566 if (!list_empty(&dirty_roots)) {
567 drop_dirty_roots(root, &dirty_roots);
569 btrfs_transaction_queue_work(root, delay);
572 void btrfs_transaction_queue_work(struct btrfs_root *root, int delay)
574 queue_delayed_work(trans_wq, &root->fs_info->trans_work, delay);
577 void btrfs_transaction_flush_work(struct btrfs_root *root)
579 cancel_rearming_delayed_workqueue(trans_wq, &root->fs_info->trans_work);
580 flush_workqueue(trans_wq);
583 void __init btrfs_init_transaction_sys(void)
585 trans_wq = create_workqueue("btrfs");
588 void __exit btrfs_exit_transaction_sys(void)
590 destroy_workqueue(trans_wq);