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>
21 #include <linux/writeback.h>
22 #include <linux/pagemap.h>
25 #include "transaction.h"
27 static int total_trans = 0;
28 extern struct kmem_cache *btrfs_trans_handle_cachep;
29 extern struct kmem_cache *btrfs_transaction_cachep;
31 static struct workqueue_struct *trans_wq;
33 #define BTRFS_ROOT_TRANS_TAG 0
34 #define BTRFS_ROOT_DEFRAG_TAG 1
36 static void put_transaction(struct btrfs_transaction *transaction)
38 WARN_ON(transaction->use_count == 0);
39 transaction->use_count--;
40 if (transaction->use_count == 0) {
41 WARN_ON(total_trans == 0);
43 list_del_init(&transaction->list);
44 memset(transaction, 0, sizeof(*transaction));
45 kmem_cache_free(btrfs_transaction_cachep, transaction);
49 static int join_transaction(struct btrfs_root *root)
51 struct btrfs_transaction *cur_trans;
52 cur_trans = root->fs_info->running_transaction;
54 cur_trans = kmem_cache_alloc(btrfs_transaction_cachep,
58 root->fs_info->generation++;
59 root->fs_info->running_transaction = cur_trans;
60 cur_trans->num_writers = 1;
61 cur_trans->num_joined = 0;
62 cur_trans->transid = root->fs_info->generation;
63 init_waitqueue_head(&cur_trans->writer_wait);
64 init_waitqueue_head(&cur_trans->commit_wait);
65 cur_trans->in_commit = 0;
66 cur_trans->use_count = 1;
67 cur_trans->commit_done = 0;
68 cur_trans->start_time = get_seconds();
69 list_add_tail(&cur_trans->list, &root->fs_info->trans_list);
70 btrfs_ordered_inode_tree_init(&cur_trans->ordered_inode_tree);
71 extent_map_tree_init(&cur_trans->dirty_pages,
72 root->fs_info->btree_inode->i_mapping,
75 cur_trans->num_writers++;
76 cur_trans->num_joined++;
82 static int record_root_in_trans(struct btrfs_root *root)
84 u64 running_trans_id = root->fs_info->running_transaction->transid;
85 if (root->ref_cows && root->last_trans < running_trans_id) {
86 WARN_ON(root == root->fs_info->extent_root);
87 if (root->root_item.refs != 0) {
88 radix_tree_tag_set(&root->fs_info->fs_roots_radix,
89 (unsigned long)root->root_key.objectid,
90 BTRFS_ROOT_TRANS_TAG);
91 radix_tree_tag_set(&root->fs_info->fs_roots_radix,
92 (unsigned long)root->root_key.objectid,
93 BTRFS_ROOT_DEFRAG_TAG);
94 root->commit_root = root->node;
95 extent_buffer_get(root->node);
99 root->last_trans = running_trans_id;
104 struct btrfs_trans_handle *btrfs_start_transaction(struct btrfs_root *root,
107 struct btrfs_trans_handle *h =
108 kmem_cache_alloc(btrfs_trans_handle_cachep, GFP_NOFS);
111 mutex_lock(&root->fs_info->trans_mutex);
112 ret = join_transaction(root);
115 record_root_in_trans(root);
116 h->transid = root->fs_info->running_transaction->transid;
117 h->transaction = root->fs_info->running_transaction;
118 h->blocks_reserved = num_blocks;
120 h->block_group = NULL;
121 h->alloc_exclude_nr = 0;
122 h->alloc_exclude_start = 0;
123 root->fs_info->running_transaction->use_count++;
124 mutex_unlock(&root->fs_info->trans_mutex);
128 int btrfs_end_transaction(struct btrfs_trans_handle *trans,
129 struct btrfs_root *root)
131 struct btrfs_transaction *cur_trans;
133 mutex_lock(&root->fs_info->trans_mutex);
134 cur_trans = root->fs_info->running_transaction;
135 WARN_ON(cur_trans != trans->transaction);
136 WARN_ON(cur_trans->num_writers < 1);
137 cur_trans->num_writers--;
138 if (waitqueue_active(&cur_trans->writer_wait))
139 wake_up(&cur_trans->writer_wait);
140 put_transaction(cur_trans);
141 mutex_unlock(&root->fs_info->trans_mutex);
142 memset(trans, 0, sizeof(*trans));
143 kmem_cache_free(btrfs_trans_handle_cachep, trans);
148 int btrfs_write_and_wait_transaction(struct btrfs_trans_handle *trans,
149 struct btrfs_root *root)
154 struct extent_map_tree *dirty_pages;
156 struct inode *btree_inode = root->fs_info->btree_inode;
161 if (!trans || !trans->transaction) {
162 return filemap_write_and_wait(btree_inode->i_mapping);
164 dirty_pages = &trans->transaction->dirty_pages;
166 ret = find_first_extent_bit(dirty_pages, 0, &start, &end,
170 clear_extent_dirty(dirty_pages, start, end, GFP_NOFS);
171 while(start <= end) {
172 index = start >> PAGE_CACHE_SHIFT;
173 start = (u64)(index + 1) << PAGE_CACHE_SHIFT;
174 page = find_lock_page(btree_inode->i_mapping, index);
177 if (PageWriteback(page)) {
179 wait_on_page_writeback(page);
182 page_cache_release(page);
186 err = write_one_page(page, 0);
189 page_cache_release(page);
192 err = filemap_fdatawait(btree_inode->i_mapping);
198 int btrfs_commit_tree_roots(struct btrfs_trans_handle *trans,
199 struct btrfs_root *root)
202 u64 old_extent_block;
203 struct btrfs_fs_info *fs_info = root->fs_info;
204 struct btrfs_root *tree_root = fs_info->tree_root;
205 struct btrfs_root *extent_root = fs_info->extent_root;
207 btrfs_write_dirty_block_groups(trans, extent_root);
209 old_extent_block = btrfs_root_bytenr(&extent_root->root_item);
210 if (old_extent_block == extent_root->node->start)
212 btrfs_set_root_bytenr(&extent_root->root_item,
213 extent_root->node->start);
214 btrfs_set_root_level(&extent_root->root_item,
215 btrfs_header_level(extent_root->node));
216 ret = btrfs_update_root(trans, tree_root,
217 &extent_root->root_key,
218 &extent_root->root_item);
220 btrfs_write_dirty_block_groups(trans, extent_root);
225 static int wait_for_commit(struct btrfs_root *root,
226 struct btrfs_transaction *commit)
229 mutex_lock(&root->fs_info->trans_mutex);
230 while(!commit->commit_done) {
231 prepare_to_wait(&commit->commit_wait, &wait,
232 TASK_UNINTERRUPTIBLE);
233 if (commit->commit_done)
235 mutex_unlock(&root->fs_info->trans_mutex);
237 mutex_lock(&root->fs_info->trans_mutex);
239 mutex_unlock(&root->fs_info->trans_mutex);
240 finish_wait(&commit->commit_wait, &wait);
245 struct list_head list;
246 struct btrfs_root *root;
247 struct btrfs_root *latest_root;
250 int btrfs_add_dead_root(struct btrfs_root *root,
251 struct btrfs_root *latest,
252 struct list_head *dead_list)
254 struct dirty_root *dirty;
256 dirty = kmalloc(sizeof(*dirty), GFP_NOFS);
260 dirty->latest_root = latest;
261 list_add(&dirty->list, dead_list);
265 static int add_dirty_roots(struct btrfs_trans_handle *trans,
266 struct radix_tree_root *radix,
267 struct list_head *list)
269 struct dirty_root *dirty;
270 struct btrfs_root *gang[8];
271 struct btrfs_root *root;
278 ret = radix_tree_gang_lookup_tag(radix, (void **)gang, 0,
280 BTRFS_ROOT_TRANS_TAG);
283 for (i = 0; i < ret; i++) {
285 radix_tree_tag_clear(radix,
286 (unsigned long)root->root_key.objectid,
287 BTRFS_ROOT_TRANS_TAG);
288 if (root->commit_root == root->node) {
289 WARN_ON(root->node->start !=
290 btrfs_root_bytenr(&root->root_item));
291 free_extent_buffer(root->commit_root);
292 root->commit_root = NULL;
294 /* make sure to update the root on disk
295 * so we get any updates to the block used
298 err = btrfs_update_root(trans,
299 root->fs_info->tree_root,
304 dirty = kmalloc(sizeof(*dirty), GFP_NOFS);
306 dirty->root = kmalloc(sizeof(*dirty->root), GFP_NOFS);
307 BUG_ON(!dirty->root);
309 memset(&root->root_item.drop_progress, 0,
310 sizeof(struct btrfs_disk_key));
311 root->root_item.drop_level = 0;
313 memcpy(dirty->root, root, sizeof(*root));
314 dirty->root->node = root->commit_root;
315 dirty->latest_root = root;
316 root->commit_root = NULL;
318 root->root_key.offset = root->fs_info->generation;
319 btrfs_set_root_bytenr(&root->root_item,
321 btrfs_set_root_level(&root->root_item,
322 btrfs_header_level(root->node));
323 err = btrfs_insert_root(trans, root->fs_info->tree_root,
329 refs = btrfs_root_refs(&dirty->root->root_item);
330 btrfs_set_root_refs(&dirty->root->root_item, refs - 1);
331 err = btrfs_update_root(trans, root->fs_info->tree_root,
332 &dirty->root->root_key,
333 &dirty->root->root_item);
337 list_add(&dirty->list, list);
348 int btrfs_defrag_root(struct btrfs_root *root, int cacheonly)
350 struct btrfs_fs_info *info = root->fs_info;
352 struct btrfs_trans_handle *trans;
355 if (root->defrag_running)
357 trans = btrfs_start_transaction(root, 1);
359 root->defrag_running = 1;
360 ret = btrfs_defrag_leaves(trans, root, cacheonly);
361 nr = trans->blocks_used;
362 btrfs_end_transaction(trans, root);
363 mutex_unlock(&info->fs_mutex);
364 btrfs_btree_balance_dirty(info->tree_root, nr);
367 mutex_lock(&info->fs_mutex);
368 trans = btrfs_start_transaction(root, 1);
372 root->defrag_running = 0;
373 radix_tree_tag_clear(&info->fs_roots_radix,
374 (unsigned long)root->root_key.objectid,
375 BTRFS_ROOT_DEFRAG_TAG);
376 btrfs_end_transaction(trans, root);
380 int btrfs_defrag_dirty_roots(struct btrfs_fs_info *info)
382 struct btrfs_root *gang[1];
383 struct btrfs_root *root;
390 ret = radix_tree_gang_lookup_tag(&info->fs_roots_radix,
393 BTRFS_ROOT_DEFRAG_TAG);
396 for (i = 0; i < ret; i++) {
398 last = root->root_key.objectid + 1;
399 btrfs_defrag_root(root, 1);
402 btrfs_defrag_root(info->extent_root, 1);
406 static int drop_dirty_roots(struct btrfs_root *tree_root,
407 struct list_head *list)
409 struct dirty_root *dirty;
410 struct btrfs_trans_handle *trans;
417 while(!list_empty(list)) {
418 struct btrfs_root *root;
420 mutex_lock(&tree_root->fs_info->fs_mutex);
421 dirty = list_entry(list->next, struct dirty_root, list);
422 list_del_init(&dirty->list);
424 num_bytes = btrfs_root_used(&dirty->root->root_item);
425 root = dirty->latest_root;
428 trans = btrfs_start_transaction(tree_root, 1);
429 ret = btrfs_drop_snapshot(trans, dirty->root);
430 if (ret != -EAGAIN) {
434 err = btrfs_update_root(trans,
436 &dirty->root->root_key,
437 &dirty->root->root_item);
440 nr = trans->blocks_used;
441 ret = btrfs_end_transaction(trans, tree_root);
443 mutex_unlock(&tree_root->fs_info->fs_mutex);
444 btrfs_btree_balance_dirty(tree_root, nr);
446 mutex_lock(&tree_root->fs_info->fs_mutex);
450 num_bytes -= btrfs_root_used(&dirty->root->root_item);
451 bytes_used = btrfs_root_used(&root->root_item);
453 record_root_in_trans(root);
454 btrfs_set_root_used(&root->root_item,
455 bytes_used - num_bytes);
457 ret = btrfs_del_root(trans, tree_root, &dirty->root->root_key);
462 nr = trans->blocks_used;
463 ret = btrfs_end_transaction(trans, tree_root);
466 free_extent_buffer(dirty->root->node);
469 mutex_unlock(&tree_root->fs_info->fs_mutex);
471 btrfs_btree_balance_dirty(tree_root, nr);
477 int btrfs_write_ordered_inodes(struct btrfs_trans_handle *trans,
478 struct btrfs_root *root)
480 struct btrfs_transaction *cur_trans = trans->transaction;
482 u64 root_objectid = 0;
484 u64 transid = trans->transid;
487 printk("write ordered trans %Lu\n", transid);
489 ret = btrfs_find_first_ordered_inode(
490 &cur_trans->ordered_inode_tree,
491 &root_objectid, &objectid);
495 mutex_unlock(&root->fs_info->trans_mutex);
496 mutex_unlock(&root->fs_info->fs_mutex);
497 inode = btrfs_ilookup(root->fs_info->sb, objectid,
500 if (S_ISREG(inode->i_mode))
501 filemap_fdatawrite(inode->i_mapping);
504 mutex_lock(&root->fs_info->fs_mutex);
505 mutex_lock(&root->fs_info->trans_mutex);
510 ret = btrfs_find_del_first_ordered_inode(
511 &cur_trans->ordered_inode_tree,
512 &root_objectid, &objectid);
515 mutex_unlock(&root->fs_info->trans_mutex);
516 mutex_unlock(&root->fs_info->fs_mutex);
517 inode = btrfs_ilookup(root->fs_info->sb, objectid,
520 if (S_ISREG(inode->i_mode))
521 filemap_write_and_wait(inode->i_mapping);
524 mutex_lock(&root->fs_info->fs_mutex);
525 mutex_lock(&root->fs_info->trans_mutex);
527 printk("done write ordered trans %Lu\n", transid);
531 int btrfs_commit_transaction(struct btrfs_trans_handle *trans,
532 struct btrfs_root *root)
534 unsigned long joined = 0;
535 unsigned long timeout = 1;
536 struct btrfs_transaction *cur_trans;
537 struct btrfs_transaction *prev_trans = NULL;
538 struct list_head dirty_fs_roots;
539 struct extent_map_tree *pinned_copy;
543 INIT_LIST_HEAD(&dirty_fs_roots);
545 mutex_lock(&root->fs_info->trans_mutex);
546 if (trans->transaction->in_commit) {
547 cur_trans = trans->transaction;
548 trans->transaction->use_count++;
549 mutex_unlock(&root->fs_info->trans_mutex);
550 btrfs_end_transaction(trans, root);
552 mutex_unlock(&root->fs_info->fs_mutex);
553 ret = wait_for_commit(root, cur_trans);
556 mutex_lock(&root->fs_info->trans_mutex);
557 put_transaction(cur_trans);
558 mutex_unlock(&root->fs_info->trans_mutex);
560 mutex_lock(&root->fs_info->fs_mutex);
564 pinned_copy = kmalloc(sizeof(*pinned_copy), GFP_NOFS);
568 extent_map_tree_init(pinned_copy,
569 root->fs_info->btree_inode->i_mapping, GFP_NOFS);
571 trans->transaction->in_commit = 1;
572 cur_trans = trans->transaction;
573 if (cur_trans->list.prev != &root->fs_info->trans_list) {
574 prev_trans = list_entry(cur_trans->list.prev,
575 struct btrfs_transaction, list);
576 if (!prev_trans->commit_done) {
577 prev_trans->use_count++;
578 mutex_unlock(&root->fs_info->fs_mutex);
579 mutex_unlock(&root->fs_info->trans_mutex);
581 wait_for_commit(root, prev_trans);
583 mutex_lock(&root->fs_info->fs_mutex);
584 mutex_lock(&root->fs_info->trans_mutex);
585 put_transaction(prev_trans);
590 joined = cur_trans->num_joined;
591 WARN_ON(cur_trans != trans->transaction);
592 prepare_to_wait(&cur_trans->writer_wait, &wait,
593 TASK_UNINTERRUPTIBLE);
595 if (cur_trans->num_writers > 1)
596 timeout = MAX_SCHEDULE_TIMEOUT;
600 mutex_unlock(&root->fs_info->fs_mutex);
601 mutex_unlock(&root->fs_info->trans_mutex);
603 schedule_timeout(timeout);
605 mutex_lock(&root->fs_info->fs_mutex);
606 mutex_lock(&root->fs_info->trans_mutex);
607 finish_wait(&cur_trans->writer_wait, &wait);
608 ret = btrfs_write_ordered_inodes(trans, root);
610 } while (cur_trans->num_writers > 1 ||
611 (cur_trans->num_joined != joined));
613 WARN_ON(cur_trans != trans->transaction);
615 ret = add_dirty_roots(trans, &root->fs_info->fs_roots_radix,
619 ret = btrfs_commit_tree_roots(trans, root);
622 cur_trans = root->fs_info->running_transaction;
623 root->fs_info->running_transaction = NULL;
624 btrfs_set_super_generation(&root->fs_info->super_copy,
626 btrfs_set_super_root(&root->fs_info->super_copy,
627 root->fs_info->tree_root->node->start);
628 btrfs_set_super_root_level(&root->fs_info->super_copy,
629 btrfs_header_level(root->fs_info->tree_root->node));
631 write_extent_buffer(root->fs_info->sb_buffer,
632 &root->fs_info->super_copy, 0,
633 sizeof(root->fs_info->super_copy));
635 btrfs_copy_pinned(root, pinned_copy);
637 mutex_unlock(&root->fs_info->trans_mutex);
638 mutex_unlock(&root->fs_info->fs_mutex);
639 ret = btrfs_write_and_wait_transaction(trans, root);
641 write_ctree_super(trans, root);
643 mutex_lock(&root->fs_info->fs_mutex);
644 btrfs_finish_extent_commit(trans, root, pinned_copy);
645 mutex_lock(&root->fs_info->trans_mutex);
649 cur_trans->commit_done = 1;
650 root->fs_info->last_trans_committed = cur_trans->transid;
651 wake_up(&cur_trans->commit_wait);
652 put_transaction(cur_trans);
653 put_transaction(cur_trans);
655 if (root->fs_info->closing)
656 list_splice_init(&root->fs_info->dead_roots, &dirty_fs_roots);
658 list_splice_init(&dirty_fs_roots, &root->fs_info->dead_roots);
660 mutex_unlock(&root->fs_info->trans_mutex);
661 kmem_cache_free(btrfs_trans_handle_cachep, trans);
663 if (root->fs_info->closing) {
664 mutex_unlock(&root->fs_info->fs_mutex);
665 drop_dirty_roots(root->fs_info->tree_root, &dirty_fs_roots);
666 mutex_lock(&root->fs_info->fs_mutex);
671 int btrfs_clean_old_snapshots(struct btrfs_root *root)
673 struct list_head dirty_roots;
674 INIT_LIST_HEAD(&dirty_roots);
676 mutex_lock(&root->fs_info->trans_mutex);
677 list_splice_init(&root->fs_info->dead_roots, &dirty_roots);
678 mutex_unlock(&root->fs_info->trans_mutex);
680 if (!list_empty(&dirty_roots)) {
681 drop_dirty_roots(root, &dirty_roots);
685 #if LINUX_VERSION_CODE <= KERNEL_VERSION(2,6,18)
686 void btrfs_transaction_cleaner(void *p)
688 void btrfs_transaction_cleaner(struct work_struct *work)
691 #if LINUX_VERSION_CODE <= KERNEL_VERSION(2,6,18)
692 struct btrfs_fs_info *fs_info = p;
694 struct btrfs_fs_info *fs_info = container_of(work,
695 struct btrfs_fs_info,
699 struct btrfs_root *root = fs_info->tree_root;
700 struct btrfs_transaction *cur;
701 struct btrfs_trans_handle *trans;
703 unsigned long delay = HZ * 30;
706 mutex_lock(&root->fs_info->fs_mutex);
707 mutex_lock(&root->fs_info->trans_mutex);
708 cur = root->fs_info->running_transaction;
710 mutex_unlock(&root->fs_info->trans_mutex);
714 if (now < cur->start_time || now - cur->start_time < 30) {
715 mutex_unlock(&root->fs_info->trans_mutex);
719 mutex_unlock(&root->fs_info->trans_mutex);
720 btrfs_defrag_dirty_roots(root->fs_info);
721 trans = btrfs_start_transaction(root, 1);
722 ret = btrfs_commit_transaction(trans, root);
724 mutex_unlock(&root->fs_info->fs_mutex);
725 btrfs_clean_old_snapshots(root);
726 btrfs_transaction_queue_work(root, delay);
729 void btrfs_transaction_queue_work(struct btrfs_root *root, int delay)
731 queue_delayed_work(trans_wq, &root->fs_info->trans_work, delay);
734 void btrfs_transaction_flush_work(struct btrfs_root *root)
736 cancel_rearming_delayed_workqueue(trans_wq, &root->fs_info->trans_work);
737 flush_workqueue(trans_wq);
740 void __init btrfs_init_transaction_sys(void)
742 trans_wq = create_workqueue("btrfs");
745 void btrfs_exit_transaction_sys(void)
747 destroy_workqueue(trans_wq);