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.
19 #include <linux/module.h>
21 #include <linux/sched.h>
24 #include "transaction.h"
26 static int total_trans = 0;
27 extern struct kmem_cache *btrfs_trans_handle_cachep;
28 extern struct kmem_cache *btrfs_transaction_cachep;
30 static struct workqueue_struct *trans_wq;
32 #define BTRFS_ROOT_TRANS_TAG 0
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 struct btrfs_trans_handle *btrfs_start_transaction(struct btrfs_root *root,
76 struct btrfs_trans_handle *h =
77 kmem_cache_alloc(btrfs_trans_handle_cachep, GFP_NOFS);
81 mutex_lock(&root->fs_info->trans_mutex);
82 ret = join_transaction(root);
84 running_trans_id = root->fs_info->running_transaction->transid;
86 if (root != root->fs_info->tree_root && root->last_trans <
88 WARN_ON(root == root->fs_info->extent_root);
89 WARN_ON(root->ref_cows != 1);
90 if (root->root_item.refs != 0) {
91 radix_tree_tag_set(&root->fs_info->fs_roots_radix,
92 (unsigned long)root->root_key.objectid,
93 BTRFS_ROOT_TRANS_TAG);
94 root->commit_root = root->node;
100 root->last_trans = running_trans_id;
101 h->transid = running_trans_id;
102 h->transaction = root->fs_info->running_transaction;
103 h->blocks_reserved = num_blocks;
105 h->block_group = NULL;
106 root->fs_info->running_transaction->use_count++;
107 mutex_unlock(&root->fs_info->trans_mutex);
111 int btrfs_end_transaction(struct btrfs_trans_handle *trans,
112 struct btrfs_root *root)
114 struct btrfs_transaction *cur_trans;
116 mutex_lock(&root->fs_info->trans_mutex);
117 cur_trans = root->fs_info->running_transaction;
118 WARN_ON(cur_trans != trans->transaction);
119 WARN_ON(cur_trans->num_writers < 1);
120 cur_trans->num_writers--;
121 if (waitqueue_active(&cur_trans->writer_wait))
122 wake_up(&cur_trans->writer_wait);
123 put_transaction(cur_trans);
124 mutex_unlock(&root->fs_info->trans_mutex);
125 memset(trans, 0, sizeof(*trans));
126 kmem_cache_free(btrfs_trans_handle_cachep, trans);
131 int btrfs_write_and_wait_transaction(struct btrfs_trans_handle *trans,
132 struct btrfs_root *root)
134 unsigned long gang[16];
140 struct radix_tree_root *dirty_pages;
141 struct inode *btree_inode = root->fs_info->btree_inode;
143 if (!trans || !trans->transaction) {
144 return filemap_write_and_wait(btree_inode->i_mapping);
146 dirty_pages = &trans->transaction->dirty_pages;
148 ret = find_first_radix_bit(dirty_pages, gang,
149 0, ARRAY_SIZE(gang));
152 for (i = 0; i < ret; i++) {
154 clear_radix_bit(dirty_pages, gang[i]);
155 page = find_lock_page(btree_inode->i_mapping,
159 err = write_one_page(page, 0);
162 page_cache_release(page);
165 err = filemap_fdatawait(btree_inode->i_mapping);
171 int btrfs_commit_tree_roots(struct btrfs_trans_handle *trans,
172 struct btrfs_root *root)
175 u64 old_extent_block;
176 struct btrfs_fs_info *fs_info = root->fs_info;
177 struct btrfs_root *tree_root = fs_info->tree_root;
178 struct btrfs_root *extent_root = fs_info->extent_root;
180 btrfs_write_dirty_block_groups(trans, extent_root);
182 old_extent_block = btrfs_root_blocknr(&extent_root->root_item);
183 if (old_extent_block == bh_blocknr(extent_root->node))
185 btrfs_set_root_blocknr(&extent_root->root_item,
186 bh_blocknr(extent_root->node));
187 ret = btrfs_update_root(trans, tree_root,
188 &extent_root->root_key,
189 &extent_root->root_item);
191 btrfs_write_dirty_block_groups(trans, extent_root);
196 static int wait_for_commit(struct btrfs_root *root,
197 struct btrfs_transaction *commit)
200 mutex_lock(&root->fs_info->trans_mutex);
201 while(!commit->commit_done) {
202 prepare_to_wait(&commit->commit_wait, &wait,
203 TASK_UNINTERRUPTIBLE);
204 if (commit->commit_done)
206 mutex_unlock(&root->fs_info->trans_mutex);
208 mutex_lock(&root->fs_info->trans_mutex);
210 mutex_unlock(&root->fs_info->trans_mutex);
211 finish_wait(&commit->commit_wait, &wait);
216 struct list_head list;
217 struct btrfs_key snap_key;
218 struct buffer_head *commit_root;
219 struct btrfs_root *root;
223 int btrfs_add_dead_root(struct btrfs_root *root, struct list_head *dead_list)
225 struct dirty_root *dirty;
227 dirty = kmalloc(sizeof(*dirty), GFP_NOFS);
230 memcpy(&dirty->snap_key, &root->root_key, sizeof(root->root_key));
231 dirty->commit_root = root->node;
233 dirty->free_on_drop = 1;
234 list_add(&dirty->list, dead_list);
238 static int add_dirty_roots(struct btrfs_trans_handle *trans,
239 struct radix_tree_root *radix,
240 struct list_head *list)
242 struct dirty_root *dirty;
243 struct btrfs_root *gang[8];
244 struct btrfs_root *root;
245 struct btrfs_root_item tmp_item;
252 ret = radix_tree_gang_lookup_tag(radix, (void **)gang, 0,
254 BTRFS_ROOT_TRANS_TAG);
257 for (i = 0; i < ret; i++) {
259 radix_tree_tag_clear(radix,
260 (unsigned long)root->root_key.objectid,
261 BTRFS_ROOT_TRANS_TAG);
262 if (root->commit_root == root->node) {
263 WARN_ON(bh_blocknr(root->node) !=
264 btrfs_root_blocknr(&root->root_item));
265 brelse(root->commit_root);
266 root->commit_root = NULL;
269 dirty = kmalloc(sizeof(*dirty), GFP_NOFS);
271 memcpy(&dirty->snap_key, &root->root_key,
272 sizeof(root->root_key));
273 dirty->commit_root = root->commit_root;
274 root->commit_root = NULL;
276 dirty->free_on_drop = 0;
277 memcpy(&tmp_item, &root->root_item, sizeof(tmp_item));
279 root->root_key.offset = root->fs_info->generation;
280 btrfs_set_root_blocknr(&root->root_item,
281 bh_blocknr(root->node));
282 err = btrfs_insert_root(trans, root->fs_info->tree_root,
287 refs = btrfs_root_refs(&tmp_item);
288 btrfs_set_root_refs(&tmp_item, refs - 1);
289 err = btrfs_update_root(trans, root->fs_info->tree_root,
295 list_add(&dirty->list, list);
303 static int drop_dirty_roots(struct btrfs_root *tree_root,
304 struct list_head *list)
306 struct dirty_root *dirty;
307 struct btrfs_trans_handle *trans;
309 while(!list_empty(list)) {
310 mutex_lock(&tree_root->fs_info->fs_mutex);
311 dirty = list_entry(list->next, struct dirty_root, list);
312 list_del_init(&dirty->list);
314 trans = btrfs_start_transaction(tree_root, 1);
315 ret = btrfs_drop_snapshot(trans, dirty->root,
318 ret = btrfs_del_root(trans, tree_root, &dirty->snap_key);
321 ret = btrfs_end_transaction(trans, tree_root);
324 if (dirty->free_on_drop)
327 mutex_unlock(&tree_root->fs_info->fs_mutex);
328 btrfs_btree_balance_dirty(tree_root);
333 int btrfs_commit_transaction(struct btrfs_trans_handle *trans,
334 struct btrfs_root *root)
337 struct btrfs_transaction *cur_trans;
338 struct btrfs_transaction *prev_trans = NULL;
339 struct list_head dirty_fs_roots;
340 struct radix_tree_root pinned_copy;
343 init_bit_radix(&pinned_copy);
344 INIT_LIST_HEAD(&dirty_fs_roots);
346 mutex_lock(&root->fs_info->trans_mutex);
347 if (trans->transaction->in_commit) {
348 cur_trans = trans->transaction;
349 trans->transaction->use_count++;
350 mutex_unlock(&root->fs_info->trans_mutex);
351 btrfs_end_transaction(trans, root);
353 mutex_unlock(&root->fs_info->fs_mutex);
354 ret = wait_for_commit(root, cur_trans);
356 put_transaction(cur_trans);
357 mutex_lock(&root->fs_info->fs_mutex);
360 trans->transaction->in_commit = 1;
361 cur_trans = trans->transaction;
362 if (cur_trans->list.prev != &root->fs_info->trans_list) {
363 prev_trans = list_entry(cur_trans->list.prev,
364 struct btrfs_transaction, list);
365 if (!prev_trans->commit_done) {
366 prev_trans->use_count++;
367 mutex_unlock(&root->fs_info->fs_mutex);
368 mutex_unlock(&root->fs_info->trans_mutex);
370 wait_for_commit(root, prev_trans);
371 put_transaction(prev_trans);
373 mutex_lock(&root->fs_info->fs_mutex);
374 mutex_lock(&root->fs_info->trans_mutex);
377 while (trans->transaction->num_writers > 1) {
378 WARN_ON(cur_trans != trans->transaction);
379 prepare_to_wait(&trans->transaction->writer_wait, &wait,
380 TASK_UNINTERRUPTIBLE);
381 if (trans->transaction->num_writers <= 1)
383 mutex_unlock(&root->fs_info->fs_mutex);
384 mutex_unlock(&root->fs_info->trans_mutex);
386 mutex_lock(&root->fs_info->fs_mutex);
387 mutex_lock(&root->fs_info->trans_mutex);
388 finish_wait(&trans->transaction->writer_wait, &wait);
390 finish_wait(&trans->transaction->writer_wait, &wait);
391 WARN_ON(cur_trans != trans->transaction);
392 ret = add_dirty_roots(trans, &root->fs_info->fs_roots_radix,
396 ret = btrfs_commit_tree_roots(trans, root);
399 cur_trans = root->fs_info->running_transaction;
400 root->fs_info->running_transaction = NULL;
401 btrfs_set_super_generation(&root->fs_info->super_copy,
403 btrfs_set_super_root(&root->fs_info->super_copy,
404 bh_blocknr(root->fs_info->tree_root->node));
405 memcpy(root->fs_info->disk_super, &root->fs_info->super_copy,
406 sizeof(root->fs_info->super_copy));
408 btrfs_copy_pinned(root, &pinned_copy);
410 mutex_unlock(&root->fs_info->trans_mutex);
411 mutex_unlock(&root->fs_info->fs_mutex);
412 ret = btrfs_write_and_wait_transaction(trans, root);
414 write_ctree_super(trans, root);
415 mutex_lock(&root->fs_info->fs_mutex);
416 btrfs_finish_extent_commit(trans, root, &pinned_copy);
417 mutex_lock(&root->fs_info->trans_mutex);
418 cur_trans->commit_done = 1;
419 wake_up(&cur_trans->commit_wait);
420 put_transaction(cur_trans);
421 put_transaction(cur_trans);
422 if (root->fs_info->closing)
423 list_splice_init(&root->fs_info->dead_roots, &dirty_fs_roots);
425 list_splice_init(&dirty_fs_roots, &root->fs_info->dead_roots);
426 mutex_unlock(&root->fs_info->trans_mutex);
427 kmem_cache_free(btrfs_trans_handle_cachep, trans);
429 if (root->fs_info->closing) {
430 mutex_unlock(&root->fs_info->fs_mutex);
431 drop_dirty_roots(root->fs_info->tree_root, &dirty_fs_roots);
432 mutex_lock(&root->fs_info->fs_mutex);
437 void btrfs_transaction_cleaner(struct work_struct *work)
439 struct btrfs_fs_info *fs_info = container_of(work,
440 struct btrfs_fs_info,
443 struct btrfs_root *root = fs_info->tree_root;
444 struct btrfs_transaction *cur;
445 struct btrfs_trans_handle *trans;
446 struct list_head dirty_roots;
448 unsigned long delay = HZ * 30;
451 INIT_LIST_HEAD(&dirty_roots);
452 mutex_lock(&root->fs_info->fs_mutex);
453 mutex_lock(&root->fs_info->trans_mutex);
454 cur = root->fs_info->running_transaction;
456 mutex_unlock(&root->fs_info->trans_mutex);
460 if (now < cur->start_time || now - cur->start_time < 30) {
461 mutex_unlock(&root->fs_info->trans_mutex);
465 mutex_unlock(&root->fs_info->trans_mutex);
466 trans = btrfs_start_transaction(root, 1);
467 ret = btrfs_commit_transaction(trans, root);
469 mutex_unlock(&root->fs_info->fs_mutex);
471 mutex_lock(&root->fs_info->trans_mutex);
472 list_splice_init(&root->fs_info->dead_roots, &dirty_roots);
473 mutex_unlock(&root->fs_info->trans_mutex);
475 if (!list_empty(&dirty_roots)) {
476 drop_dirty_roots(root, &dirty_roots);
478 btrfs_transaction_queue_work(root, delay);
481 void btrfs_transaction_queue_work(struct btrfs_root *root, int delay)
483 queue_delayed_work(trans_wq, &root->fs_info->trans_work, delay);
486 void btrfs_transaction_flush_work(struct btrfs_root *root)
488 cancel_rearming_delayed_workqueue(trans_wq, &root->fs_info->trans_work);
489 flush_workqueue(trans_wq);
492 void __init btrfs_init_transaction_sys(void)
494 trans_wq = create_workqueue("btrfs");
497 void __exit btrfs_exit_transaction_sys(void)
499 destroy_workqueue(trans_wq);