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 = 1;
59 cur_trans->num_joined = 0;
60 cur_trans->transid = root->fs_info->generation;
61 init_waitqueue_head(&cur_trans->writer_wait);
62 init_waitqueue_head(&cur_trans->commit_wait);
63 cur_trans->in_commit = 0;
64 cur_trans->use_count = 1;
65 cur_trans->commit_done = 0;
66 cur_trans->start_time = get_seconds();
67 list_add_tail(&cur_trans->list, &root->fs_info->trans_list);
68 init_bit_radix(&cur_trans->dirty_pages);
70 cur_trans->num_writers++;
71 cur_trans->num_joined++;
77 static int record_root_in_trans(struct btrfs_root *root)
79 u64 running_trans_id = root->fs_info->running_transaction->transid;
80 if (root->ref_cows && root->last_trans < running_trans_id) {
81 WARN_ON(root == root->fs_info->extent_root);
82 if (root->root_item.refs != 0) {
83 radix_tree_tag_set(&root->fs_info->fs_roots_radix,
84 (unsigned long)root->root_key.objectid,
85 BTRFS_ROOT_TRANS_TAG);
86 radix_tree_tag_set(&root->fs_info->fs_roots_radix,
87 (unsigned long)root->root_key.objectid,
88 BTRFS_ROOT_DEFRAG_TAG);
89 root->commit_root = root->node;
94 root->last_trans = running_trans_id;
99 struct btrfs_trans_handle *btrfs_start_transaction(struct btrfs_root *root,
102 struct btrfs_trans_handle *h =
103 kmem_cache_alloc(btrfs_trans_handle_cachep, GFP_NOFS);
106 mutex_lock(&root->fs_info->trans_mutex);
107 ret = join_transaction(root);
110 record_root_in_trans(root);
111 h->transid = root->fs_info->running_transaction->transid;
112 h->transaction = root->fs_info->running_transaction;
113 h->blocks_reserved = num_blocks;
115 h->block_group = NULL;
116 h->alloc_exclude_nr = 0;
117 h->alloc_exclude_start = 0;
118 root->fs_info->running_transaction->use_count++;
119 mutex_unlock(&root->fs_info->trans_mutex);
123 int btrfs_end_transaction(struct btrfs_trans_handle *trans,
124 struct btrfs_root *root)
126 struct btrfs_transaction *cur_trans;
128 mutex_lock(&root->fs_info->trans_mutex);
129 cur_trans = root->fs_info->running_transaction;
130 WARN_ON(cur_trans != trans->transaction);
131 WARN_ON(cur_trans->num_writers < 1);
132 cur_trans->num_writers--;
133 if (waitqueue_active(&cur_trans->writer_wait))
134 wake_up(&cur_trans->writer_wait);
135 put_transaction(cur_trans);
136 mutex_unlock(&root->fs_info->trans_mutex);
137 memset(trans, 0, sizeof(*trans));
138 kmem_cache_free(btrfs_trans_handle_cachep, trans);
143 int btrfs_write_and_wait_transaction(struct btrfs_trans_handle *trans,
144 struct btrfs_root *root)
146 unsigned long gang[16];
152 struct radix_tree_root *dirty_pages;
153 struct inode *btree_inode = root->fs_info->btree_inode;
155 if (!trans || !trans->transaction) {
156 return filemap_write_and_wait(btree_inode->i_mapping);
158 dirty_pages = &trans->transaction->dirty_pages;
160 ret = find_first_radix_bit(dirty_pages, gang,
161 0, ARRAY_SIZE(gang));
164 for (i = 0; i < ret; i++) {
166 clear_radix_bit(dirty_pages, gang[i]);
167 page = find_lock_page(btree_inode->i_mapping,
171 if (PageWriteback(page)) {
173 wait_on_page_writeback(page);
176 page_cache_release(page);
180 err = write_one_page(page, 0);
183 page_cache_release(page);
186 err = filemap_fdatawait(btree_inode->i_mapping);
192 int btrfs_commit_tree_roots(struct btrfs_trans_handle *trans,
193 struct btrfs_root *root)
196 u64 old_extent_block;
197 struct btrfs_fs_info *fs_info = root->fs_info;
198 struct btrfs_root *tree_root = fs_info->tree_root;
199 struct btrfs_root *extent_root = fs_info->extent_root;
201 btrfs_write_dirty_block_groups(trans, extent_root);
203 old_extent_block = btrfs_root_blocknr(&extent_root->root_item);
204 if (old_extent_block == bh_blocknr(extent_root->node))
206 btrfs_set_root_blocknr(&extent_root->root_item,
207 bh_blocknr(extent_root->node));
208 ret = btrfs_update_root(trans, tree_root,
209 &extent_root->root_key,
210 &extent_root->root_item);
212 btrfs_write_dirty_block_groups(trans, extent_root);
217 static int wait_for_commit(struct btrfs_root *root,
218 struct btrfs_transaction *commit)
221 mutex_lock(&root->fs_info->trans_mutex);
222 while(!commit->commit_done) {
223 prepare_to_wait(&commit->commit_wait, &wait,
224 TASK_UNINTERRUPTIBLE);
225 if (commit->commit_done)
227 mutex_unlock(&root->fs_info->trans_mutex);
229 mutex_lock(&root->fs_info->trans_mutex);
231 mutex_unlock(&root->fs_info->trans_mutex);
232 finish_wait(&commit->commit_wait, &wait);
237 struct list_head list;
238 struct btrfs_root *root;
241 int btrfs_add_dead_root(struct btrfs_root *root, struct list_head *dead_list)
243 struct dirty_root *dirty;
245 dirty = kmalloc(sizeof(*dirty), GFP_NOFS);
249 list_add(&dirty->list, dead_list);
253 static int add_dirty_roots(struct btrfs_trans_handle *trans,
254 struct radix_tree_root *radix,
255 struct list_head *list)
257 struct dirty_root *dirty;
258 struct btrfs_root *gang[8];
259 struct btrfs_root *root;
266 ret = radix_tree_gang_lookup_tag(radix, (void **)gang, 0,
268 BTRFS_ROOT_TRANS_TAG);
271 for (i = 0; i < ret; i++) {
273 radix_tree_tag_clear(radix,
274 (unsigned long)root->root_key.objectid,
275 BTRFS_ROOT_TRANS_TAG);
276 if (root->commit_root == root->node) {
277 WARN_ON(bh_blocknr(root->node) !=
278 btrfs_root_blocknr(&root->root_item));
279 brelse(root->commit_root);
280 root->commit_root = NULL;
283 dirty = kmalloc(sizeof(*dirty), GFP_NOFS);
285 dirty->root = kmalloc(sizeof(*dirty->root), GFP_NOFS);
286 BUG_ON(!dirty->root);
288 memset(&root->root_item.drop_progress, 0,
289 sizeof(struct btrfs_disk_key));
290 root->root_item.drop_level = 0;
292 memcpy(dirty->root, root, sizeof(*root));
293 dirty->root->node = root->commit_root;
294 root->commit_root = NULL;
296 root->root_key.offset = root->fs_info->generation;
297 btrfs_set_root_blocknr(&root->root_item,
298 bh_blocknr(root->node));
299 err = btrfs_insert_root(trans, root->fs_info->tree_root,
305 refs = btrfs_root_refs(&dirty->root->root_item);
306 btrfs_set_root_refs(&dirty->root->root_item, refs - 1);
307 err = btrfs_update_root(trans, root->fs_info->tree_root,
308 &dirty->root->root_key,
309 &dirty->root->root_item);
313 list_add(&dirty->list, list);
324 int btrfs_defrag_root(struct btrfs_root *root, int cacheonly)
326 struct btrfs_fs_info *info = root->fs_info;
328 struct btrfs_trans_handle *trans;
330 if (root->defrag_running)
333 trans = btrfs_start_transaction(root, 1);
335 root->defrag_running = 1;
336 ret = btrfs_defrag_leaves(trans, root, cacheonly);
337 btrfs_end_transaction(trans, root);
338 mutex_unlock(&info->fs_mutex);
340 btrfs_btree_balance_dirty(root);
343 mutex_lock(&info->fs_mutex);
344 trans = btrfs_start_transaction(root, 1);
348 root->defrag_running = 0;
349 radix_tree_tag_clear(&info->fs_roots_radix,
350 (unsigned long)root->root_key.objectid,
351 BTRFS_ROOT_DEFRAG_TAG);
352 btrfs_end_transaction(trans, root);
356 int btrfs_defrag_dirty_roots(struct btrfs_fs_info *info)
358 struct btrfs_root *gang[1];
359 struct btrfs_root *root;
366 ret = radix_tree_gang_lookup_tag(&info->fs_roots_radix,
369 BTRFS_ROOT_DEFRAG_TAG);
372 for (i = 0; i < ret; i++) {
374 last = root->root_key.objectid + 1;
375 btrfs_defrag_root(root, 1);
378 btrfs_defrag_root(info->extent_root, 1);
382 static int drop_dirty_roots(struct btrfs_root *tree_root,
383 struct list_head *list)
385 struct dirty_root *dirty;
386 struct btrfs_trans_handle *trans;
390 while(!list_empty(list)) {
391 mutex_lock(&tree_root->fs_info->fs_mutex);
392 dirty = list_entry(list->next, struct dirty_root, list);
393 list_del_init(&dirty->list);
396 trans = btrfs_start_transaction(tree_root, 1);
397 ret = btrfs_drop_snapshot(trans, dirty->root);
398 if (ret != -EAGAIN) {
401 err = btrfs_update_root(trans,
403 &dirty->root->root_key,
404 &dirty->root->root_item);
407 ret = btrfs_end_transaction(trans, tree_root);
409 mutex_unlock(&tree_root->fs_info->fs_mutex);
411 btrfs_btree_balance_dirty(tree_root);
414 mutex_lock(&tree_root->fs_info->fs_mutex);
417 ret = btrfs_del_root(trans, tree_root, &dirty->root->root_key);
420 ret = btrfs_end_transaction(trans, tree_root);
425 mutex_unlock(&tree_root->fs_info->fs_mutex);
426 btrfs_btree_balance_dirty(tree_root);
432 int btrfs_commit_transaction(struct btrfs_trans_handle *trans,
433 struct btrfs_root *root)
435 unsigned long joined = 0;
436 unsigned long timeout = 1;
437 struct btrfs_transaction *cur_trans;
438 struct btrfs_transaction *prev_trans = NULL;
439 struct list_head dirty_fs_roots;
440 struct radix_tree_root pinned_copy;
444 init_bit_radix(&pinned_copy);
445 INIT_LIST_HEAD(&dirty_fs_roots);
447 mutex_lock(&root->fs_info->trans_mutex);
448 if (trans->transaction->in_commit) {
449 cur_trans = trans->transaction;
450 trans->transaction->use_count++;
451 mutex_unlock(&root->fs_info->trans_mutex);
452 btrfs_end_transaction(trans, root);
454 mutex_unlock(&root->fs_info->fs_mutex);
455 ret = wait_for_commit(root, cur_trans);
458 mutex_lock(&root->fs_info->trans_mutex);
459 put_transaction(cur_trans);
460 mutex_unlock(&root->fs_info->trans_mutex);
462 mutex_lock(&root->fs_info->fs_mutex);
465 trans->transaction->in_commit = 1;
466 cur_trans = trans->transaction;
467 if (cur_trans->list.prev != &root->fs_info->trans_list) {
468 prev_trans = list_entry(cur_trans->list.prev,
469 struct btrfs_transaction, list);
470 if (!prev_trans->commit_done) {
471 prev_trans->use_count++;
472 mutex_unlock(&root->fs_info->fs_mutex);
473 mutex_unlock(&root->fs_info->trans_mutex);
475 wait_for_commit(root, prev_trans);
477 mutex_lock(&root->fs_info->fs_mutex);
478 mutex_lock(&root->fs_info->trans_mutex);
479 put_transaction(prev_trans);
484 joined = cur_trans->num_joined;
485 WARN_ON(cur_trans != trans->transaction);
486 prepare_to_wait(&cur_trans->writer_wait, &wait,
487 TASK_UNINTERRUPTIBLE);
489 if (cur_trans->num_writers > 1)
490 timeout = MAX_SCHEDULE_TIMEOUT;
494 mutex_unlock(&root->fs_info->fs_mutex);
495 mutex_unlock(&root->fs_info->trans_mutex);
497 schedule_timeout(timeout);
499 mutex_lock(&root->fs_info->fs_mutex);
500 mutex_lock(&root->fs_info->trans_mutex);
501 finish_wait(&cur_trans->writer_wait, &wait);
502 } while (cur_trans->num_writers > 1 ||
503 (cur_trans->num_joined != joined));
505 WARN_ON(cur_trans != trans->transaction);
506 ret = add_dirty_roots(trans, &root->fs_info->fs_roots_radix,
510 ret = btrfs_commit_tree_roots(trans, root);
513 cur_trans = root->fs_info->running_transaction;
514 root->fs_info->running_transaction = NULL;
515 btrfs_set_super_generation(&root->fs_info->super_copy,
517 btrfs_set_super_root(&root->fs_info->super_copy,
518 bh_blocknr(root->fs_info->tree_root->node));
519 memcpy(root->fs_info->disk_super, &root->fs_info->super_copy,
520 sizeof(root->fs_info->super_copy));
522 btrfs_copy_pinned(root, &pinned_copy);
524 mutex_unlock(&root->fs_info->trans_mutex);
525 mutex_unlock(&root->fs_info->fs_mutex);
526 ret = btrfs_write_and_wait_transaction(trans, root);
528 write_ctree_super(trans, root);
529 mutex_lock(&root->fs_info->fs_mutex);
530 btrfs_finish_extent_commit(trans, root, &pinned_copy);
531 mutex_lock(&root->fs_info->trans_mutex);
532 cur_trans->commit_done = 1;
533 root->fs_info->last_trans_committed = cur_trans->transid;
534 wake_up(&cur_trans->commit_wait);
535 put_transaction(cur_trans);
536 put_transaction(cur_trans);
537 if (root->fs_info->closing)
538 list_splice_init(&root->fs_info->dead_roots, &dirty_fs_roots);
540 list_splice_init(&dirty_fs_roots, &root->fs_info->dead_roots);
541 mutex_unlock(&root->fs_info->trans_mutex);
542 kmem_cache_free(btrfs_trans_handle_cachep, trans);
544 if (root->fs_info->closing) {
545 mutex_unlock(&root->fs_info->fs_mutex);
546 drop_dirty_roots(root->fs_info->tree_root, &dirty_fs_roots);
547 mutex_lock(&root->fs_info->fs_mutex);
552 int btrfs_clean_old_snapshots(struct btrfs_root *root)
554 struct list_head dirty_roots;
555 INIT_LIST_HEAD(&dirty_roots);
557 mutex_lock(&root->fs_info->trans_mutex);
558 list_splice_init(&root->fs_info->dead_roots, &dirty_roots);
559 mutex_unlock(&root->fs_info->trans_mutex);
561 if (!list_empty(&dirty_roots)) {
562 drop_dirty_roots(root, &dirty_roots);
566 void btrfs_transaction_cleaner(struct work_struct *work)
568 struct btrfs_fs_info *fs_info = container_of(work,
569 struct btrfs_fs_info,
572 struct btrfs_root *root = fs_info->tree_root;
573 struct btrfs_transaction *cur;
574 struct btrfs_trans_handle *trans;
576 unsigned long delay = HZ * 30;
579 mutex_lock(&root->fs_info->fs_mutex);
580 mutex_lock(&root->fs_info->trans_mutex);
581 cur = root->fs_info->running_transaction;
583 mutex_unlock(&root->fs_info->trans_mutex);
587 if (now < cur->start_time || now - cur->start_time < 30) {
588 mutex_unlock(&root->fs_info->trans_mutex);
592 mutex_unlock(&root->fs_info->trans_mutex);
593 btrfs_defrag_dirty_roots(root->fs_info);
594 trans = btrfs_start_transaction(root, 1);
595 ret = btrfs_commit_transaction(trans, root);
597 mutex_unlock(&root->fs_info->fs_mutex);
598 btrfs_clean_old_snapshots(root);
599 btrfs_transaction_queue_work(root, delay);
602 void btrfs_transaction_queue_work(struct btrfs_root *root, int delay)
604 queue_delayed_work(trans_wq, &root->fs_info->trans_work, delay);
607 void btrfs_transaction_flush_work(struct btrfs_root *root)
609 cancel_rearming_delayed_workqueue(trans_wq, &root->fs_info->trans_work);
610 flush_workqueue(trans_wq);
613 void __init btrfs_init_transaction_sys(void)
615 trans_wq = create_workqueue("btrfs");
618 void __exit btrfs_exit_transaction_sys(void)
620 destroy_workqueue(trans_wq);