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 radix_tree_tag_set(&root->fs_info->fs_roots_radix,
89 (unsigned long)root->root_key.objectid,
90 BTRFS_ROOT_TRANS_TAG);
91 root->commit_root = root->node;
94 root->last_trans = running_trans_id;
95 h->transid = running_trans_id;
96 h->transaction = root->fs_info->running_transaction;
97 h->blocks_reserved = num_blocks;
99 h->block_group = NULL;
100 root->fs_info->running_transaction->use_count++;
101 mutex_unlock(&root->fs_info->trans_mutex);
105 int btrfs_end_transaction(struct btrfs_trans_handle *trans,
106 struct btrfs_root *root)
108 struct btrfs_transaction *cur_trans;
110 mutex_lock(&root->fs_info->trans_mutex);
111 cur_trans = root->fs_info->running_transaction;
112 WARN_ON(cur_trans->num_writers < 1);
113 if (waitqueue_active(&cur_trans->writer_wait))
114 wake_up(&cur_trans->writer_wait);
115 cur_trans->num_writers--;
116 put_transaction(cur_trans);
117 mutex_unlock(&root->fs_info->trans_mutex);
118 memset(trans, 0, sizeof(*trans));
119 kmem_cache_free(btrfs_trans_handle_cachep, trans);
124 int btrfs_write_and_wait_transaction(struct btrfs_trans_handle *trans,
125 struct btrfs_root *root)
127 unsigned long gang[16];
133 struct radix_tree_root *dirty_pages;
134 struct inode *btree_inode = root->fs_info->btree_inode;
136 if (!trans || !trans->transaction) {
137 return filemap_write_and_wait(btree_inode->i_mapping);
139 dirty_pages = &trans->transaction->dirty_pages;
141 ret = find_first_radix_bit(dirty_pages, gang,
142 0, ARRAY_SIZE(gang));
145 for (i = 0; i < ret; i++) {
147 clear_radix_bit(dirty_pages, gang[i]);
148 page = find_lock_page(btree_inode->i_mapping,
152 err = write_one_page(page, 0);
155 page_cache_release(page);
158 err = filemap_fdatawait(btree_inode->i_mapping);
164 int btrfs_commit_tree_roots(struct btrfs_trans_handle *trans,
165 struct btrfs_root *root)
168 u64 old_extent_block;
169 struct btrfs_fs_info *fs_info = root->fs_info;
170 struct btrfs_root *tree_root = fs_info->tree_root;
171 struct btrfs_root *extent_root = fs_info->extent_root;
173 btrfs_write_dirty_block_groups(trans, extent_root);
175 old_extent_block = btrfs_root_blocknr(&extent_root->root_item);
176 if (old_extent_block == bh_blocknr(extent_root->node))
178 btrfs_set_root_blocknr(&extent_root->root_item,
179 bh_blocknr(extent_root->node));
180 ret = btrfs_update_root(trans, tree_root,
181 &extent_root->root_key,
182 &extent_root->root_item);
184 btrfs_write_dirty_block_groups(trans, extent_root);
189 static int wait_for_commit(struct btrfs_root *root,
190 struct btrfs_transaction *commit)
193 while(!commit->commit_done) {
194 prepare_to_wait(&commit->commit_wait, &wait,
195 TASK_UNINTERRUPTIBLE);
196 if (commit->commit_done)
198 mutex_unlock(&root->fs_info->trans_mutex);
200 mutex_lock(&root->fs_info->trans_mutex);
202 finish_wait(&commit->commit_wait, &wait);
207 struct list_head list;
208 struct btrfs_key snap_key;
209 struct buffer_head *commit_root;
210 struct btrfs_root *root;
213 static int add_dirty_roots(struct btrfs_trans_handle *trans,
214 struct radix_tree_root *radix,
215 struct list_head *list)
217 struct dirty_root *dirty;
218 struct btrfs_root *gang[8];
219 struct btrfs_root *root;
224 ret = radix_tree_gang_lookup_tag(radix, (void **)gang, 0,
226 BTRFS_ROOT_TRANS_TAG);
229 for (i = 0; i < ret; i++) {
231 radix_tree_tag_clear(radix,
232 (unsigned long)root->root_key.objectid,
233 BTRFS_ROOT_TRANS_TAG);
234 if (root->commit_root == root->node) {
235 WARN_ON(bh_blocknr(root->node) !=
236 btrfs_root_blocknr(&root->root_item));
237 brelse(root->commit_root);
238 root->commit_root = NULL;
241 dirty = kmalloc(sizeof(*dirty), GFP_NOFS);
243 memcpy(&dirty->snap_key, &root->root_key,
244 sizeof(root->root_key));
245 dirty->commit_root = root->commit_root;
246 root->commit_root = NULL;
248 root->root_key.offset = root->fs_info->generation;
249 btrfs_set_root_blocknr(&root->root_item,
250 bh_blocknr(root->node));
251 err = btrfs_insert_root(trans, root->fs_info->tree_root,
255 list_add(&dirty->list, list);
261 static int drop_dirty_roots(struct btrfs_root *tree_root,
262 struct list_head *list)
264 struct dirty_root *dirty;
265 struct btrfs_trans_handle *trans;
267 while(!list_empty(list)) {
268 mutex_lock(&tree_root->fs_info->fs_mutex);
269 dirty = list_entry(list->next, struct dirty_root, list);
270 list_del_init(&dirty->list);
271 trans = btrfs_start_transaction(tree_root, 1);
272 ret = btrfs_drop_snapshot(trans, dirty->root,
276 ret = btrfs_del_root(trans, tree_root, &dirty->snap_key);
278 ret = btrfs_end_transaction(trans, tree_root);
281 mutex_unlock(&tree_root->fs_info->fs_mutex);
286 int btrfs_commit_transaction(struct btrfs_trans_handle *trans,
287 struct btrfs_root *root)
290 struct btrfs_transaction *cur_trans;
291 struct btrfs_transaction *prev_trans = NULL;
292 struct list_head dirty_fs_roots;
295 INIT_LIST_HEAD(&dirty_fs_roots);
297 mutex_lock(&root->fs_info->trans_mutex);
298 if (trans->transaction->in_commit) {
299 cur_trans = trans->transaction;
300 trans->transaction->use_count++;
301 btrfs_end_transaction(trans, root);
302 ret = wait_for_commit(root, cur_trans);
304 put_transaction(cur_trans);
305 mutex_unlock(&root->fs_info->trans_mutex);
308 cur_trans = trans->transaction;
309 trans->transaction->in_commit = 1;
310 while (trans->transaction->num_writers > 1) {
311 WARN_ON(cur_trans != trans->transaction);
312 prepare_to_wait(&trans->transaction->writer_wait, &wait,
313 TASK_UNINTERRUPTIBLE);
314 if (trans->transaction->num_writers <= 1)
316 mutex_unlock(&root->fs_info->trans_mutex);
318 mutex_lock(&root->fs_info->trans_mutex);
319 finish_wait(&trans->transaction->writer_wait, &wait);
321 finish_wait(&trans->transaction->writer_wait, &wait);
322 WARN_ON(cur_trans != trans->transaction);
323 add_dirty_roots(trans, &root->fs_info->fs_roots_radix, &dirty_fs_roots);
324 ret = btrfs_commit_tree_roots(trans, root);
326 cur_trans = root->fs_info->running_transaction;
327 root->fs_info->running_transaction = NULL;
328 if (cur_trans->list.prev != &root->fs_info->trans_list) {
329 prev_trans = list_entry(cur_trans->list.prev,
330 struct btrfs_transaction, list);
331 if (prev_trans->commit_done)
334 prev_trans->use_count++;
336 mutex_unlock(&root->fs_info->trans_mutex);
337 mutex_unlock(&root->fs_info->fs_mutex);
338 ret = btrfs_write_and_wait_transaction(trans, root);
340 mutex_lock(&root->fs_info->trans_mutex);
341 wait_for_commit(root, prev_trans);
342 put_transaction(prev_trans);
343 mutex_unlock(&root->fs_info->trans_mutex);
345 btrfs_set_super_generation(root->fs_info->disk_super,
348 write_ctree_super(trans, root);
350 mutex_lock(&root->fs_info->fs_mutex);
351 btrfs_finish_extent_commit(trans, root);
352 mutex_lock(&root->fs_info->trans_mutex);
353 cur_trans->commit_done = 1;
354 wake_up(&cur_trans->commit_wait);
355 put_transaction(cur_trans);
356 put_transaction(cur_trans);
357 if (root->fs_info->closing)
358 list_splice_init(&root->fs_info->dead_roots, &dirty_fs_roots);
360 list_splice_init(&dirty_fs_roots, &root->fs_info->dead_roots);
361 mutex_unlock(&root->fs_info->trans_mutex);
362 kmem_cache_free(btrfs_trans_handle_cachep, trans);
364 if (root->fs_info->closing) {
365 mutex_unlock(&root->fs_info->fs_mutex);
366 drop_dirty_roots(root->fs_info->tree_root, &dirty_fs_roots);
367 mutex_lock(&root->fs_info->fs_mutex);
372 void btrfs_transaction_cleaner(struct work_struct *work)
374 struct btrfs_fs_info *fs_info = container_of(work,
375 struct btrfs_fs_info,
378 struct btrfs_root *root = fs_info->tree_root;
379 struct btrfs_transaction *cur;
380 struct btrfs_trans_handle *trans;
381 struct list_head dirty_roots;
383 unsigned long delay = HZ * 30;
386 INIT_LIST_HEAD(&dirty_roots);
387 mutex_lock(&root->fs_info->fs_mutex);
388 mutex_lock(&root->fs_info->trans_mutex);
389 cur = root->fs_info->running_transaction;
391 mutex_unlock(&root->fs_info->trans_mutex);
395 if (now < cur->start_time || now - cur->start_time < 30) {
396 mutex_unlock(&root->fs_info->trans_mutex);
400 mutex_unlock(&root->fs_info->trans_mutex);
401 trans = btrfs_start_transaction(root, 1);
402 ret = btrfs_commit_transaction(trans, root);
404 mutex_unlock(&root->fs_info->fs_mutex);
406 mutex_lock(&root->fs_info->trans_mutex);
407 list_splice_init(&root->fs_info->dead_roots, &dirty_roots);
408 mutex_unlock(&root->fs_info->trans_mutex);
410 if (!list_empty(&dirty_roots)) {
411 drop_dirty_roots(root, &dirty_roots);
413 btrfs_transaction_queue_work(root, delay);
416 void btrfs_transaction_queue_work(struct btrfs_root *root, int delay)
418 queue_delayed_work(trans_wq, &root->fs_info->trans_work, delay);
421 void btrfs_transaction_flush_work(struct btrfs_root *root)
423 cancel_rearming_delayed_workqueue(trans_wq, &root->fs_info->trans_work);
424 flush_workqueue(trans_wq);
427 void __init btrfs_init_transaction_sys(void)
429 trans_wq = create_workqueue("btrfs");
432 void __exit btrfs_exit_transaction_sys(void)
434 destroy_workqueue(trans_wq);