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
33 static void put_transaction(struct btrfs_transaction *transaction)
35 WARN_ON(transaction->use_count == 0);
36 transaction->use_count--;
37 if (transaction->use_count == 0) {
38 WARN_ON(total_trans == 0);
40 list_del_init(&transaction->list);
41 memset(transaction, 0, sizeof(*transaction));
42 kmem_cache_free(btrfs_transaction_cachep, transaction);
46 static int join_transaction(struct btrfs_root *root)
48 struct btrfs_transaction *cur_trans;
49 cur_trans = root->fs_info->running_transaction;
51 cur_trans = kmem_cache_alloc(btrfs_transaction_cachep,
55 root->fs_info->generation++;
56 root->fs_info->running_transaction = cur_trans;
57 cur_trans->num_writers = 0;
58 cur_trans->transid = root->fs_info->generation;
59 init_waitqueue_head(&cur_trans->writer_wait);
60 init_waitqueue_head(&cur_trans->commit_wait);
61 cur_trans->in_commit = 0;
62 cur_trans->use_count = 1;
63 cur_trans->commit_done = 0;
64 cur_trans->start_time = get_seconds();
65 list_add_tail(&cur_trans->list, &root->fs_info->trans_list);
66 init_bit_radix(&cur_trans->dirty_pages);
68 cur_trans->num_writers++;
72 struct btrfs_trans_handle *btrfs_start_transaction(struct btrfs_root *root,
75 struct btrfs_trans_handle *h =
76 kmem_cache_alloc(btrfs_trans_handle_cachep, GFP_NOFS);
80 mutex_lock(&root->fs_info->trans_mutex);
81 ret = join_transaction(root);
83 running_trans_id = root->fs_info->running_transaction->transid;
85 if (root != root->fs_info->tree_root && root->last_trans <
87 WARN_ON(root == root->fs_info->extent_root);
88 WARN_ON(root->ref_cows != 1);
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;
99 root->last_trans = running_trans_id;
100 h->transid = running_trans_id;
101 h->transaction = root->fs_info->running_transaction;
102 h->blocks_reserved = num_blocks;
104 h->block_group = NULL;
105 root->fs_info->running_transaction->use_count++;
106 mutex_unlock(&root->fs_info->trans_mutex);
110 int btrfs_end_transaction(struct btrfs_trans_handle *trans,
111 struct btrfs_root *root)
113 struct btrfs_transaction *cur_trans;
115 mutex_lock(&root->fs_info->trans_mutex);
116 cur_trans = root->fs_info->running_transaction;
117 WARN_ON(cur_trans != trans->transaction);
118 WARN_ON(cur_trans->num_writers < 1);
119 cur_trans->num_writers--;
120 if (waitqueue_active(&cur_trans->writer_wait))
121 wake_up(&cur_trans->writer_wait);
122 put_transaction(cur_trans);
123 mutex_unlock(&root->fs_info->trans_mutex);
124 memset(trans, 0, sizeof(*trans));
125 kmem_cache_free(btrfs_trans_handle_cachep, trans);
130 int btrfs_write_and_wait_transaction(struct btrfs_trans_handle *trans,
131 struct btrfs_root *root)
133 unsigned long gang[16];
139 struct radix_tree_root *dirty_pages;
140 struct inode *btree_inode = root->fs_info->btree_inode;
142 if (!trans || !trans->transaction) {
143 return filemap_write_and_wait(btree_inode->i_mapping);
145 dirty_pages = &trans->transaction->dirty_pages;
147 ret = find_first_radix_bit(dirty_pages, gang,
148 0, ARRAY_SIZE(gang));
151 for (i = 0; i < ret; i++) {
153 clear_radix_bit(dirty_pages, gang[i]);
154 page = find_lock_page(btree_inode->i_mapping,
158 err = write_one_page(page, 0);
161 page_cache_release(page);
164 err = filemap_fdatawait(btree_inode->i_mapping);
170 int btrfs_commit_tree_roots(struct btrfs_trans_handle *trans,
171 struct btrfs_root *root)
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;
179 btrfs_write_dirty_block_groups(trans, extent_root);
181 old_extent_block = btrfs_root_blocknr(&extent_root->root_item);
182 if (old_extent_block == bh_blocknr(extent_root->node))
184 btrfs_set_root_blocknr(&extent_root->root_item,
185 bh_blocknr(extent_root->node));
186 ret = btrfs_update_root(trans, tree_root,
187 &extent_root->root_key,
188 &extent_root->root_item);
190 btrfs_write_dirty_block_groups(trans, extent_root);
195 static int wait_for_commit(struct btrfs_root *root,
196 struct btrfs_transaction *commit)
199 mutex_lock(&root->fs_info->trans_mutex);
200 while(!commit->commit_done) {
201 prepare_to_wait(&commit->commit_wait, &wait,
202 TASK_UNINTERRUPTIBLE);
203 if (commit->commit_done)
205 mutex_unlock(&root->fs_info->trans_mutex);
207 mutex_lock(&root->fs_info->trans_mutex);
209 mutex_unlock(&root->fs_info->trans_mutex);
210 finish_wait(&commit->commit_wait, &wait);
215 struct list_head list;
216 struct btrfs_root *root;
219 int btrfs_add_dead_root(struct btrfs_root *root, struct list_head *dead_list)
221 struct dirty_root *dirty;
223 dirty = kmalloc(sizeof(*dirty), GFP_NOFS);
227 list_add(&dirty->list, dead_list);
231 static int add_dirty_roots(struct btrfs_trans_handle *trans,
232 struct radix_tree_root *radix,
233 struct list_head *list)
235 struct dirty_root *dirty;
236 struct btrfs_root *gang[8];
237 struct btrfs_root *root;
244 ret = radix_tree_gang_lookup_tag(radix, (void **)gang, 0,
246 BTRFS_ROOT_TRANS_TAG);
249 for (i = 0; i < ret; i++) {
251 radix_tree_tag_clear(radix,
252 (unsigned long)root->root_key.objectid,
253 BTRFS_ROOT_TRANS_TAG);
254 if (root->commit_root == root->node) {
255 WARN_ON(bh_blocknr(root->node) !=
256 btrfs_root_blocknr(&root->root_item));
257 brelse(root->commit_root);
258 root->commit_root = NULL;
261 dirty = kmalloc(sizeof(*dirty), GFP_NOFS);
263 dirty->root = kmalloc(sizeof(*dirty->root), GFP_NOFS);
264 BUG_ON(!dirty->root);
266 memset(&root->root_item.drop_progress, 0,
267 sizeof(struct btrfs_disk_key));
268 root->root_item.drop_level = 0;
270 memcpy(dirty->root, root, sizeof(*root));
271 dirty->root->node = root->commit_root;
272 root->commit_root = NULL;
274 root->root_key.offset = root->fs_info->generation;
275 btrfs_set_root_blocknr(&root->root_item,
276 bh_blocknr(root->node));
277 err = btrfs_insert_root(trans, root->fs_info->tree_root,
283 refs = btrfs_root_refs(&dirty->root->root_item);
284 btrfs_set_root_refs(&dirty->root->root_item, refs - 1);
285 err = btrfs_update_root(trans, root->fs_info->tree_root,
286 &dirty->root->root_key,
287 &dirty->root->root_item);
291 list_add(&dirty->list, list);
302 static int drop_dirty_roots(struct btrfs_root *tree_root,
303 struct list_head *list)
305 struct dirty_root *dirty;
306 struct btrfs_trans_handle *trans;
310 while(!list_empty(list)) {
311 mutex_lock(&tree_root->fs_info->fs_mutex);
312 dirty = list_entry(list->next, struct dirty_root, list);
313 list_del_init(&dirty->list);
316 trans = btrfs_start_transaction(tree_root, 1);
317 ret = btrfs_drop_snapshot(trans, dirty->root);
318 if (ret != -EAGAIN) {
321 err = btrfs_update_root(trans,
323 &dirty->root->root_key,
324 &dirty->root->root_item);
327 ret = btrfs_end_transaction(trans, tree_root);
331 ret = btrfs_del_root(trans, tree_root, &dirty->root->root_key);
334 ret = btrfs_end_transaction(trans, tree_root);
339 mutex_unlock(&tree_root->fs_info->fs_mutex);
340 btrfs_btree_balance_dirty(tree_root);
345 int btrfs_commit_transaction(struct btrfs_trans_handle *trans,
346 struct btrfs_root *root)
349 struct btrfs_transaction *cur_trans;
350 struct btrfs_transaction *prev_trans = NULL;
351 struct list_head dirty_fs_roots;
352 struct radix_tree_root pinned_copy;
355 init_bit_radix(&pinned_copy);
356 INIT_LIST_HEAD(&dirty_fs_roots);
358 mutex_lock(&root->fs_info->trans_mutex);
359 if (trans->transaction->in_commit) {
360 cur_trans = trans->transaction;
361 trans->transaction->use_count++;
362 mutex_unlock(&root->fs_info->trans_mutex);
363 btrfs_end_transaction(trans, root);
365 mutex_unlock(&root->fs_info->fs_mutex);
366 ret = wait_for_commit(root, cur_trans);
368 put_transaction(cur_trans);
369 mutex_lock(&root->fs_info->fs_mutex);
372 trans->transaction->in_commit = 1;
373 cur_trans = trans->transaction;
374 if (cur_trans->list.prev != &root->fs_info->trans_list) {
375 prev_trans = list_entry(cur_trans->list.prev,
376 struct btrfs_transaction, list);
377 if (!prev_trans->commit_done) {
378 prev_trans->use_count++;
379 mutex_unlock(&root->fs_info->fs_mutex);
380 mutex_unlock(&root->fs_info->trans_mutex);
382 wait_for_commit(root, prev_trans);
383 put_transaction(prev_trans);
385 mutex_lock(&root->fs_info->fs_mutex);
386 mutex_lock(&root->fs_info->trans_mutex);
389 while (trans->transaction->num_writers > 1) {
390 WARN_ON(cur_trans != trans->transaction);
391 prepare_to_wait(&trans->transaction->writer_wait, &wait,
392 TASK_UNINTERRUPTIBLE);
393 if (trans->transaction->num_writers <= 1)
395 mutex_unlock(&root->fs_info->fs_mutex);
396 mutex_unlock(&root->fs_info->trans_mutex);
398 mutex_lock(&root->fs_info->fs_mutex);
399 mutex_lock(&root->fs_info->trans_mutex);
400 finish_wait(&trans->transaction->writer_wait, &wait);
402 finish_wait(&trans->transaction->writer_wait, &wait);
403 WARN_ON(cur_trans != trans->transaction);
404 ret = add_dirty_roots(trans, &root->fs_info->fs_roots_radix,
408 ret = btrfs_commit_tree_roots(trans, root);
411 cur_trans = root->fs_info->running_transaction;
412 root->fs_info->running_transaction = NULL;
413 btrfs_set_super_generation(&root->fs_info->super_copy,
415 btrfs_set_super_root(&root->fs_info->super_copy,
416 bh_blocknr(root->fs_info->tree_root->node));
417 memcpy(root->fs_info->disk_super, &root->fs_info->super_copy,
418 sizeof(root->fs_info->super_copy));
420 btrfs_copy_pinned(root, &pinned_copy);
422 mutex_unlock(&root->fs_info->trans_mutex);
423 mutex_unlock(&root->fs_info->fs_mutex);
424 ret = btrfs_write_and_wait_transaction(trans, root);
426 write_ctree_super(trans, root);
427 mutex_lock(&root->fs_info->fs_mutex);
428 btrfs_finish_extent_commit(trans, root, &pinned_copy);
429 mutex_lock(&root->fs_info->trans_mutex);
430 cur_trans->commit_done = 1;
431 wake_up(&cur_trans->commit_wait);
432 put_transaction(cur_trans);
433 put_transaction(cur_trans);
434 if (root->fs_info->closing)
435 list_splice_init(&root->fs_info->dead_roots, &dirty_fs_roots);
437 list_splice_init(&dirty_fs_roots, &root->fs_info->dead_roots);
438 mutex_unlock(&root->fs_info->trans_mutex);
439 kmem_cache_free(btrfs_trans_handle_cachep, trans);
441 if (root->fs_info->closing) {
442 mutex_unlock(&root->fs_info->fs_mutex);
443 drop_dirty_roots(root->fs_info->tree_root, &dirty_fs_roots);
444 mutex_lock(&root->fs_info->fs_mutex);
449 void btrfs_transaction_cleaner(struct work_struct *work)
451 struct btrfs_fs_info *fs_info = container_of(work,
452 struct btrfs_fs_info,
455 struct btrfs_root *root = fs_info->tree_root;
456 struct btrfs_transaction *cur;
457 struct btrfs_trans_handle *trans;
458 struct list_head dirty_roots;
460 unsigned long delay = HZ * 30;
463 INIT_LIST_HEAD(&dirty_roots);
464 mutex_lock(&root->fs_info->fs_mutex);
465 mutex_lock(&root->fs_info->trans_mutex);
466 cur = root->fs_info->running_transaction;
468 mutex_unlock(&root->fs_info->trans_mutex);
472 if (now < cur->start_time || now - cur->start_time < 30) {
473 mutex_unlock(&root->fs_info->trans_mutex);
477 mutex_unlock(&root->fs_info->trans_mutex);
478 trans = btrfs_start_transaction(root, 1);
479 ret = btrfs_commit_transaction(trans, root);
481 mutex_unlock(&root->fs_info->fs_mutex);
483 mutex_lock(&root->fs_info->trans_mutex);
484 list_splice_init(&root->fs_info->dead_roots, &dirty_roots);
485 mutex_unlock(&root->fs_info->trans_mutex);
487 if (!list_empty(&dirty_roots)) {
488 drop_dirty_roots(root, &dirty_roots);
490 btrfs_transaction_queue_work(root, delay);
493 void btrfs_transaction_queue_work(struct btrfs_root *root, int delay)
495 queue_delayed_work(trans_wq, &root->fs_info->trans_work, delay);
498 void btrfs_transaction_flush_work(struct btrfs_root *root)
500 cancel_rearming_delayed_workqueue(trans_wq, &root->fs_info->trans_work);
501 flush_workqueue(trans_wq);
504 void __init btrfs_init_transaction_sys(void)
506 trans_wq = create_workqueue("btrfs");
509 void __exit btrfs_exit_transaction_sys(void)
511 destroy_workqueue(trans_wq);