blob: 30914521cf0189b5e72a6b819e1d042d6a7969af [file] [log] [blame]
Chris Mason6cbd5572007-06-12 09:07:21 -04001/*
2 * Copyright (C) 2007 Oracle. All rights reserved.
3 *
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.
7 *
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.
12 *
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.
17 */
18
Chris Mason79154b12007-03-22 15:59:16 -040019#include <linux/module.h>
20#include <linux/fs.h>
21#include "ctree.h"
22#include "disk-io.h"
23#include "transaction.h"
24
Chris Mason78fae272007-03-25 11:35:08 -040025static int total_trans = 0;
Chris Mason2c90e5d2007-04-02 10:50:19 -040026extern struct kmem_cache *btrfs_trans_handle_cachep;
27extern struct kmem_cache *btrfs_transaction_cachep;
28
Chris Mason08607c12007-06-08 15:33:54 -040029static struct workqueue_struct *trans_wq;
30
Chris Mason0f7d52f2007-04-09 10:42:37 -040031#define BTRFS_ROOT_TRANS_TAG 0
32
Chris Mason79154b12007-03-22 15:59:16 -040033static void put_transaction(struct btrfs_transaction *transaction)
34{
Chris Mason2c90e5d2007-04-02 10:50:19 -040035 WARN_ON(transaction->use_count == 0);
Chris Mason79154b12007-03-22 15:59:16 -040036 transaction->use_count--;
Chris Mason78fae272007-03-25 11:35:08 -040037 if (transaction->use_count == 0) {
38 WARN_ON(total_trans == 0);
39 total_trans--;
Chris Mason8fd17792007-04-19 21:01:03 -040040 list_del_init(&transaction->list);
Chris Mason2c90e5d2007-04-02 10:50:19 -040041 memset(transaction, 0, sizeof(*transaction));
42 kmem_cache_free(btrfs_transaction_cachep, transaction);
Chris Mason78fae272007-03-25 11:35:08 -040043 }
Chris Mason79154b12007-03-22 15:59:16 -040044}
45
46static int join_transaction(struct btrfs_root *root)
47{
48 struct btrfs_transaction *cur_trans;
49 cur_trans = root->fs_info->running_transaction;
50 if (!cur_trans) {
Chris Mason2c90e5d2007-04-02 10:50:19 -040051 cur_trans = kmem_cache_alloc(btrfs_transaction_cachep,
52 GFP_NOFS);
Chris Mason78fae272007-03-25 11:35:08 -040053 total_trans++;
Chris Mason79154b12007-03-22 15:59:16 -040054 BUG_ON(!cur_trans);
Chris Mason0f7d52f2007-04-09 10:42:37 -040055 root->fs_info->generation++;
Chris Mason79154b12007-03-22 15:59:16 -040056 root->fs_info->running_transaction = cur_trans;
57 cur_trans->num_writers = 0;
Chris Mason0f7d52f2007-04-09 10:42:37 -040058 cur_trans->transid = root->fs_info->generation;
Chris Mason79154b12007-03-22 15:59:16 -040059 init_waitqueue_head(&cur_trans->writer_wait);
60 init_waitqueue_head(&cur_trans->commit_wait);
61 cur_trans->in_commit = 0;
Chris Masond5719762007-03-23 10:01:08 -040062 cur_trans->use_count = 1;
Chris Mason79154b12007-03-22 15:59:16 -040063 cur_trans->commit_done = 0;
Chris Mason08607c12007-06-08 15:33:54 -040064 cur_trans->start_time = get_seconds();
Chris Mason8fd17792007-04-19 21:01:03 -040065 list_add_tail(&cur_trans->list, &root->fs_info->trans_list);
Chris Mason7c4452b2007-04-28 09:29:35 -040066 init_bit_radix(&cur_trans->dirty_pages);
Chris Mason79154b12007-03-22 15:59:16 -040067 }
68 cur_trans->num_writers++;
69 return 0;
70}
71
72struct btrfs_trans_handle *btrfs_start_transaction(struct btrfs_root *root,
73 int num_blocks)
74{
Chris Mason2c90e5d2007-04-02 10:50:19 -040075 struct btrfs_trans_handle *h =
76 kmem_cache_alloc(btrfs_trans_handle_cachep, GFP_NOFS);
Chris Mason79154b12007-03-22 15:59:16 -040077 int ret;
Chris Mason0f7d52f2007-04-09 10:42:37 -040078 u64 running_trans_id;
Chris Mason79154b12007-03-22 15:59:16 -040079
80 mutex_lock(&root->fs_info->trans_mutex);
81 ret = join_transaction(root);
82 BUG_ON(ret);
Chris Mason0f7d52f2007-04-09 10:42:37 -040083 running_trans_id = root->fs_info->running_transaction->transid;
84
85 if (root != root->fs_info->tree_root && root->last_trans <
86 running_trans_id) {
87 radix_tree_tag_set(&root->fs_info->fs_roots_radix,
Chris Mason2619ba12007-04-10 16:58:11 -040088 (unsigned long)root->root_key.objectid,
89 BTRFS_ROOT_TRANS_TAG);
Chris Mason0f7d52f2007-04-09 10:42:37 -040090 root->commit_root = root->node;
91 get_bh(root->node);
92 }
93 root->last_trans = running_trans_id;
94 h->transid = running_trans_id;
Chris Mason79154b12007-03-22 15:59:16 -040095 h->transaction = root->fs_info->running_transaction;
96 h->blocks_reserved = num_blocks;
97 h->blocks_used = 0;
Chris Mason31f3c992007-04-30 15:25:45 -040098 h->block_group = NULL;
Chris Mason79154b12007-03-22 15:59:16 -040099 root->fs_info->running_transaction->use_count++;
100 mutex_unlock(&root->fs_info->trans_mutex);
101 return h;
102}
103
104int btrfs_end_transaction(struct btrfs_trans_handle *trans,
105 struct btrfs_root *root)
106{
107 struct btrfs_transaction *cur_trans;
Chris Masond6e4a422007-04-06 15:37:36 -0400108
Chris Mason79154b12007-03-22 15:59:16 -0400109 mutex_lock(&root->fs_info->trans_mutex);
110 cur_trans = root->fs_info->running_transaction;
Chris Masond5719762007-03-23 10:01:08 -0400111 WARN_ON(cur_trans->num_writers < 1);
Chris Mason79154b12007-03-22 15:59:16 -0400112 if (waitqueue_active(&cur_trans->writer_wait))
113 wake_up(&cur_trans->writer_wait);
114 cur_trans->num_writers--;
115 put_transaction(cur_trans);
116 mutex_unlock(&root->fs_info->trans_mutex);
Chris Masond6025572007-03-30 14:27:56 -0400117 memset(trans, 0, sizeof(*trans));
Chris Mason2c90e5d2007-04-02 10:50:19 -0400118 kmem_cache_free(btrfs_trans_handle_cachep, trans);
Chris Mason79154b12007-03-22 15:59:16 -0400119 return 0;
120}
121
122
123int btrfs_write_and_wait_transaction(struct btrfs_trans_handle *trans,
124 struct btrfs_root *root)
125{
Chris Mason7c4452b2007-04-28 09:29:35 -0400126 unsigned long gang[16];
127 int ret;
128 int i;
129 int err;
130 int werr = 0;
131 struct page *page;
132 struct radix_tree_root *dirty_pages;
133 struct inode *btree_inode = root->fs_info->btree_inode;
134
135 if (!trans || !trans->transaction) {
136 return filemap_write_and_wait(btree_inode->i_mapping);
137 }
138 dirty_pages = &trans->transaction->dirty_pages;
139 while(1) {
Chris Masone37c9e62007-05-09 20:13:14 -0400140 ret = find_first_radix_bit(dirty_pages, gang,
141 0, ARRAY_SIZE(gang));
Chris Mason7c4452b2007-04-28 09:29:35 -0400142 if (!ret)
143 break;
144 for (i = 0; i < ret; i++) {
145 /* FIXME EIO */
146 clear_radix_bit(dirty_pages, gang[i]);
147 page = find_lock_page(btree_inode->i_mapping,
148 gang[i]);
149 if (!page)
150 continue;
151 err = write_one_page(page, 0);
152 if (err)
153 werr = err;
154 page_cache_release(page);
155 }
156 }
157 err = filemap_fdatawait(btree_inode->i_mapping);
158 if (err)
159 werr = err;
160 return werr;
Chris Mason79154b12007-03-22 15:59:16 -0400161}
162
163int btrfs_commit_tree_roots(struct btrfs_trans_handle *trans,
164 struct btrfs_root *root)
165{
166 int ret;
167 u64 old_extent_block;
168 struct btrfs_fs_info *fs_info = root->fs_info;
169 struct btrfs_root *tree_root = fs_info->tree_root;
170 struct btrfs_root *extent_root = fs_info->extent_root;
Chris Mason79154b12007-03-22 15:59:16 -0400171
Chris Mason9078a3e2007-04-26 16:46:15 -0400172 btrfs_write_dirty_block_groups(trans, extent_root);
Chris Mason79154b12007-03-22 15:59:16 -0400173 while(1) {
174 old_extent_block = btrfs_root_blocknr(&extent_root->root_item);
Chris Mason7eccb902007-04-11 15:53:25 -0400175 if (old_extent_block == bh_blocknr(extent_root->node))
Chris Mason79154b12007-03-22 15:59:16 -0400176 break;
177 btrfs_set_root_blocknr(&extent_root->root_item,
Chris Mason7eccb902007-04-11 15:53:25 -0400178 bh_blocknr(extent_root->node));
Chris Mason79154b12007-03-22 15:59:16 -0400179 ret = btrfs_update_root(trans, tree_root,
180 &extent_root->root_key,
181 &extent_root->root_item);
182 BUG_ON(ret);
Chris Mason9078a3e2007-04-26 16:46:15 -0400183 btrfs_write_dirty_block_groups(trans, extent_root);
Chris Mason79154b12007-03-22 15:59:16 -0400184 }
185 return 0;
186}
187
188static int wait_for_commit(struct btrfs_root *root,
189 struct btrfs_transaction *commit)
190{
191 DEFINE_WAIT(wait);
Chris Mason79154b12007-03-22 15:59:16 -0400192 while(!commit->commit_done) {
193 prepare_to_wait(&commit->commit_wait, &wait,
194 TASK_UNINTERRUPTIBLE);
195 if (commit->commit_done)
196 break;
197 mutex_unlock(&root->fs_info->trans_mutex);
198 schedule();
199 mutex_lock(&root->fs_info->trans_mutex);
200 }
201 finish_wait(&commit->commit_wait, &wait);
202 return 0;
203}
204
Chris Mason0f7d52f2007-04-09 10:42:37 -0400205struct dirty_root {
206 struct list_head list;
207 struct btrfs_key snap_key;
208 struct buffer_head *commit_root;
209 struct btrfs_root *root;
210};
211
Chris Mason35b7e472007-05-02 15:53:43 -0400212static int add_dirty_roots(struct btrfs_trans_handle *trans,
213 struct radix_tree_root *radix,
214 struct list_head *list)
Chris Mason0f7d52f2007-04-09 10:42:37 -0400215{
216 struct dirty_root *dirty;
217 struct btrfs_root *gang[8];
218 struct btrfs_root *root;
219 int i;
220 int ret;
221 int err;
Chris Mason0f7d52f2007-04-09 10:42:37 -0400222 while(1) {
223 ret = radix_tree_gang_lookup_tag(radix, (void **)gang, 0,
224 ARRAY_SIZE(gang),
225 BTRFS_ROOT_TRANS_TAG);
226 if (ret == 0)
227 break;
228 for (i = 0; i < ret; i++) {
229 root = gang[i];
Chris Mason2619ba12007-04-10 16:58:11 -0400230 radix_tree_tag_clear(radix,
231 (unsigned long)root->root_key.objectid,
232 BTRFS_ROOT_TRANS_TAG);
Chris Mason0f7d52f2007-04-09 10:42:37 -0400233 if (root->commit_root == root->node) {
Chris Mason7eccb902007-04-11 15:53:25 -0400234 WARN_ON(bh_blocknr(root->node) !=
Chris Mason0f7d52f2007-04-09 10:42:37 -0400235 btrfs_root_blocknr(&root->root_item));
236 brelse(root->commit_root);
237 root->commit_root = NULL;
238 continue;
239 }
240 dirty = kmalloc(sizeof(*dirty), GFP_NOFS);
241 BUG_ON(!dirty);
242 memcpy(&dirty->snap_key, &root->root_key,
243 sizeof(root->root_key));
244 dirty->commit_root = root->commit_root;
245 root->commit_root = NULL;
246 dirty->root = root;
Chris Mason0f7d52f2007-04-09 10:42:37 -0400247 root->root_key.offset = root->fs_info->generation;
248 btrfs_set_root_blocknr(&root->root_item,
Chris Mason7eccb902007-04-11 15:53:25 -0400249 bh_blocknr(root->node));
Chris Mason0f7d52f2007-04-09 10:42:37 -0400250 err = btrfs_insert_root(trans, root->fs_info->tree_root,
251 &root->root_key,
252 &root->root_item);
253 BUG_ON(err);
254 list_add(&dirty->list, list);
255 }
256 }
Chris Mason0f7d52f2007-04-09 10:42:37 -0400257 return 0;
258}
259
Chris Mason35b7e472007-05-02 15:53:43 -0400260static int drop_dirty_roots(struct btrfs_root *tree_root,
261 struct list_head *list)
Chris Mason0f7d52f2007-04-09 10:42:37 -0400262{
263 struct dirty_root *dirty;
264 struct btrfs_trans_handle *trans;
265 int ret;
Chris Mason0f7d52f2007-04-09 10:42:37 -0400266 while(!list_empty(list)) {
Chris Masonfacda1e2007-06-08 18:11:48 -0400267 mutex_lock(&tree_root->fs_info->fs_mutex);
Chris Mason0f7d52f2007-04-09 10:42:37 -0400268 dirty = list_entry(list->next, struct dirty_root, list);
269 list_del_init(&dirty->list);
270 trans = btrfs_start_transaction(tree_root, 1);
Chris Mason0f7d52f2007-04-09 10:42:37 -0400271 ret = btrfs_drop_snapshot(trans, dirty->root,
272 dirty->commit_root);
273 BUG_ON(ret);
274
Chris Mason0f7d52f2007-04-09 10:42:37 -0400275 ret = btrfs_del_root(trans, tree_root, &dirty->snap_key);
276 BUG_ON(ret);
277 ret = btrfs_end_transaction(trans, tree_root);
278 BUG_ON(ret);
279 kfree(dirty);
Chris Masonfacda1e2007-06-08 18:11:48 -0400280 mutex_unlock(&tree_root->fs_info->fs_mutex);
Chris Mason0f7d52f2007-04-09 10:42:37 -0400281 }
282 return 0;
283}
284
Chris Mason79154b12007-03-22 15:59:16 -0400285int btrfs_commit_transaction(struct btrfs_trans_handle *trans,
286 struct btrfs_root *root)
287{
288 int ret = 0;
Chris Mason79154b12007-03-22 15:59:16 -0400289 struct btrfs_transaction *cur_trans;
Chris Mason8fd17792007-04-19 21:01:03 -0400290 struct btrfs_transaction *prev_trans = NULL;
Chris Mason0f7d52f2007-04-09 10:42:37 -0400291 struct list_head dirty_fs_roots;
Chris Mason79154b12007-03-22 15:59:16 -0400292 DEFINE_WAIT(wait);
293
Chris Mason0f7d52f2007-04-09 10:42:37 -0400294 INIT_LIST_HEAD(&dirty_fs_roots);
Chris Masond6e4a422007-04-06 15:37:36 -0400295
Chris Mason79154b12007-03-22 15:59:16 -0400296 mutex_lock(&root->fs_info->trans_mutex);
297 if (trans->transaction->in_commit) {
298 cur_trans = trans->transaction;
299 trans->transaction->use_count++;
300 btrfs_end_transaction(trans, root);
301 ret = wait_for_commit(root, cur_trans);
302 BUG_ON(ret);
303 put_transaction(cur_trans);
304 mutex_unlock(&root->fs_info->trans_mutex);
305 return 0;
306 }
Chris Mason2c90e5d2007-04-02 10:50:19 -0400307 cur_trans = trans->transaction;
308 trans->transaction->in_commit = 1;
Chris Mason79154b12007-03-22 15:59:16 -0400309 while (trans->transaction->num_writers > 1) {
Chris Mason2c90e5d2007-04-02 10:50:19 -0400310 WARN_ON(cur_trans != trans->transaction);
Chris Mason79154b12007-03-22 15:59:16 -0400311 prepare_to_wait(&trans->transaction->writer_wait, &wait,
312 TASK_UNINTERRUPTIBLE);
313 if (trans->transaction->num_writers <= 1)
314 break;
315 mutex_unlock(&root->fs_info->trans_mutex);
316 schedule();
317 mutex_lock(&root->fs_info->trans_mutex);
Chris Mason2c90e5d2007-04-02 10:50:19 -0400318 finish_wait(&trans->transaction->writer_wait, &wait);
Chris Mason79154b12007-03-22 15:59:16 -0400319 }
320 finish_wait(&trans->transaction->writer_wait, &wait);
Chris Mason2c90e5d2007-04-02 10:50:19 -0400321 WARN_ON(cur_trans != trans->transaction);
Chris Mason0f7d52f2007-04-09 10:42:37 -0400322 add_dirty_roots(trans, &root->fs_info->fs_roots_radix, &dirty_fs_roots);
Chris Mason79154b12007-03-22 15:59:16 -0400323 ret = btrfs_commit_tree_roots(trans, root);
324 BUG_ON(ret);
Chris Mason78fae272007-03-25 11:35:08 -0400325 cur_trans = root->fs_info->running_transaction;
326 root->fs_info->running_transaction = NULL;
Chris Mason8fd17792007-04-19 21:01:03 -0400327 if (cur_trans->list.prev != &root->fs_info->trans_list) {
328 prev_trans = list_entry(cur_trans->list.prev,
329 struct btrfs_transaction, list);
330 if (prev_trans->commit_done)
331 prev_trans = NULL;
332 else
333 prev_trans->use_count++;
334 }
Chris Mason78fae272007-03-25 11:35:08 -0400335 mutex_unlock(&root->fs_info->trans_mutex);
Chris Mason8fd17792007-04-19 21:01:03 -0400336 mutex_unlock(&root->fs_info->fs_mutex);
Chris Mason79154b12007-03-22 15:59:16 -0400337 ret = btrfs_write_and_wait_transaction(trans, root);
Chris Mason8fd17792007-04-19 21:01:03 -0400338 if (prev_trans) {
339 mutex_lock(&root->fs_info->trans_mutex);
340 wait_for_commit(root, prev_trans);
341 put_transaction(prev_trans);
342 mutex_unlock(&root->fs_info->trans_mutex);
343 }
344 btrfs_set_super_generation(root->fs_info->disk_super,
345 cur_trans->transid);
Chris Mason79154b12007-03-22 15:59:16 -0400346 BUG_ON(ret);
Chris Mason79154b12007-03-22 15:59:16 -0400347 write_ctree_super(trans, root);
Chris Mason8fd17792007-04-19 21:01:03 -0400348
349 mutex_lock(&root->fs_info->fs_mutex);
Chris Mason78fae272007-03-25 11:35:08 -0400350 btrfs_finish_extent_commit(trans, root);
351 mutex_lock(&root->fs_info->trans_mutex);
Chris Mason2c90e5d2007-04-02 10:50:19 -0400352 cur_trans->commit_done = 1;
353 wake_up(&cur_trans->commit_wait);
Chris Mason79154b12007-03-22 15:59:16 -0400354 put_transaction(cur_trans);
Chris Mason78fae272007-03-25 11:35:08 -0400355 put_transaction(cur_trans);
Chris Masonfacda1e2007-06-08 18:11:48 -0400356 if (root->fs_info->closing)
357 list_splice_init(&root->fs_info->dead_roots, &dirty_fs_roots);
358 else
359 list_splice_init(&dirty_fs_roots, &root->fs_info->dead_roots);
Chris Mason78fae272007-03-25 11:35:08 -0400360 mutex_unlock(&root->fs_info->trans_mutex);
Chris Mason2c90e5d2007-04-02 10:50:19 -0400361 kmem_cache_free(btrfs_trans_handle_cachep, trans);
Chris Mason79154b12007-03-22 15:59:16 -0400362
Chris Masonfacda1e2007-06-08 18:11:48 -0400363 if (root->fs_info->closing) {
364 mutex_unlock(&root->fs_info->fs_mutex);
365 drop_dirty_roots(root->fs_info->tree_root, &dirty_fs_roots);
366 mutex_lock(&root->fs_info->fs_mutex);
367 }
Chris Mason79154b12007-03-22 15:59:16 -0400368 return ret;
369}
370
Chris Mason08607c12007-06-08 15:33:54 -0400371void btrfs_transaction_cleaner(struct work_struct *work)
372{
373 struct btrfs_fs_info *fs_info = container_of(work,
374 struct btrfs_fs_info,
375 trans_work.work);
376
377 struct btrfs_root *root = fs_info->tree_root;
378 struct btrfs_transaction *cur;
379 struct btrfs_trans_handle *trans;
Chris Masonfacda1e2007-06-08 18:11:48 -0400380 struct list_head dirty_roots;
Chris Mason08607c12007-06-08 15:33:54 -0400381 unsigned long now;
382 unsigned long delay = HZ * 30;
383 int ret;
384
Chris Masonfacda1e2007-06-08 18:11:48 -0400385 INIT_LIST_HEAD(&dirty_roots);
Chris Mason08607c12007-06-08 15:33:54 -0400386 mutex_lock(&root->fs_info->fs_mutex);
387 mutex_lock(&root->fs_info->trans_mutex);
388 cur = root->fs_info->running_transaction;
389 if (!cur) {
390 mutex_unlock(&root->fs_info->trans_mutex);
391 goto out;
392 }
393 now = get_seconds();
394 if (now < cur->start_time || now - cur->start_time < 30) {
395 mutex_unlock(&root->fs_info->trans_mutex);
396 delay = HZ * 5;
397 goto out;
398 }
399 mutex_unlock(&root->fs_info->trans_mutex);
Chris Mason08607c12007-06-08 15:33:54 -0400400 trans = btrfs_start_transaction(root, 1);
401 ret = btrfs_commit_transaction(trans, root);
402out:
403 mutex_unlock(&root->fs_info->fs_mutex);
Chris Masonad693af2007-06-09 08:19:57 -0400404
405 mutex_lock(&root->fs_info->trans_mutex);
406 list_splice_init(&root->fs_info->dead_roots, &dirty_roots);
407 mutex_unlock(&root->fs_info->trans_mutex);
408
409 if (!list_empty(&dirty_roots)) {
410 drop_dirty_roots(root, &dirty_roots);
411 }
Chris Mason08607c12007-06-08 15:33:54 -0400412 btrfs_transaction_queue_work(root, delay);
413}
414
415void btrfs_transaction_queue_work(struct btrfs_root *root, int delay)
416{
417 queue_delayed_work(trans_wq, &root->fs_info->trans_work, delay);
418}
419
420void btrfs_transaction_flush_work(struct btrfs_root *root)
421{
422 cancel_rearming_delayed_workqueue(trans_wq, &root->fs_info->trans_work);
423 flush_workqueue(trans_wq);
424}
425
426void __init btrfs_init_transaction_sys(void)
427{
428 trans_wq = create_workqueue("btrfs");
429}
430
431void __exit btrfs_exit_transaction_sys(void)
432{
433 destroy_workqueue(trans_wq);
434}
435