Btrfs: Add data=ordered support
[linux-2.6-block.git] / fs / btrfs / transaction.c
CommitLineData
6cbd5570
CM
1/*
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
79154b1b 19#include <linux/fs.h>
34088780 20#include <linux/sched.h>
d3c2fdcf 21#include <linux/writeback.h>
5f39d397 22#include <linux/pagemap.h>
79154b1b
CM
23#include "ctree.h"
24#include "disk-io.h"
25#include "transaction.h"
26
78fae27e 27static int total_trans = 0;
2c90e5d6
CM
28extern struct kmem_cache *btrfs_trans_handle_cachep;
29extern struct kmem_cache *btrfs_transaction_cachep;
30
08607c1b
CM
31static struct workqueue_struct *trans_wq;
32
0f7d52f4 33#define BTRFS_ROOT_TRANS_TAG 0
6702ed49 34#define BTRFS_ROOT_DEFRAG_TAG 1
0f7d52f4 35
79154b1b
CM
36static void put_transaction(struct btrfs_transaction *transaction)
37{
2c90e5d6 38 WARN_ON(transaction->use_count == 0);
79154b1b 39 transaction->use_count--;
78fae27e
CM
40 if (transaction->use_count == 0) {
41 WARN_ON(total_trans == 0);
42 total_trans--;
8fd17795 43 list_del_init(&transaction->list);
2c90e5d6
CM
44 memset(transaction, 0, sizeof(*transaction));
45 kmem_cache_free(btrfs_transaction_cachep, transaction);
78fae27e 46 }
79154b1b
CM
47}
48
49static int join_transaction(struct btrfs_root *root)
50{
51 struct btrfs_transaction *cur_trans;
52 cur_trans = root->fs_info->running_transaction;
53 if (!cur_trans) {
2c90e5d6
CM
54 cur_trans = kmem_cache_alloc(btrfs_transaction_cachep,
55 GFP_NOFS);
78fae27e 56 total_trans++;
79154b1b 57 BUG_ON(!cur_trans);
0f7d52f4 58 root->fs_info->generation++;
79154b1b 59 root->fs_info->running_transaction = cur_trans;
15ee9bc7
JB
60 cur_trans->num_writers = 1;
61 cur_trans->num_joined = 0;
0f7d52f4 62 cur_trans->transid = root->fs_info->generation;
79154b1b
CM
63 init_waitqueue_head(&cur_trans->writer_wait);
64 init_waitqueue_head(&cur_trans->commit_wait);
65 cur_trans->in_commit = 0;
d5719762 66 cur_trans->use_count = 1;
79154b1b 67 cur_trans->commit_done = 0;
08607c1b 68 cur_trans->start_time = get_seconds();
8fd17795 69 list_add_tail(&cur_trans->list, &root->fs_info->trans_list);
dc17ff8f 70 btrfs_ordered_inode_tree_init(&cur_trans->ordered_inode_tree);
5f39d397
CM
71 extent_map_tree_init(&cur_trans->dirty_pages,
72 root->fs_info->btree_inode->i_mapping,
73 GFP_NOFS);
15ee9bc7
JB
74 } else {
75 cur_trans->num_writers++;
76 cur_trans->num_joined++;
79154b1b 77 }
15ee9bc7 78
79154b1b
CM
79 return 0;
80}
81
6702ed49
CM
82static int record_root_in_trans(struct btrfs_root *root)
83{
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;
5f39d397 95 extent_buffer_get(root->node);
6702ed49
CM
96 } else {
97 WARN_ON(1);
98 }
99 root->last_trans = running_trans_id;
100 }
101 return 0;
102}
103
79154b1b
CM
104struct btrfs_trans_handle *btrfs_start_transaction(struct btrfs_root *root,
105 int num_blocks)
106{
2c90e5d6
CM
107 struct btrfs_trans_handle *h =
108 kmem_cache_alloc(btrfs_trans_handle_cachep, GFP_NOFS);
79154b1b
CM
109 int ret;
110
111 mutex_lock(&root->fs_info->trans_mutex);
112 ret = join_transaction(root);
113 BUG_ON(ret);
0f7d52f4 114
6702ed49
CM
115 record_root_in_trans(root);
116 h->transid = root->fs_info->running_transaction->transid;
79154b1b
CM
117 h->transaction = root->fs_info->running_transaction;
118 h->blocks_reserved = num_blocks;
119 h->blocks_used = 0;
31f3c99b 120 h->block_group = NULL;
26b8003f
CM
121 h->alloc_exclude_nr = 0;
122 h->alloc_exclude_start = 0;
79154b1b
CM
123 root->fs_info->running_transaction->use_count++;
124 mutex_unlock(&root->fs_info->trans_mutex);
125 return h;
126}
127
128int btrfs_end_transaction(struct btrfs_trans_handle *trans,
129 struct btrfs_root *root)
130{
131 struct btrfs_transaction *cur_trans;
d6e4a428 132
79154b1b
CM
133 mutex_lock(&root->fs_info->trans_mutex);
134 cur_trans = root->fs_info->running_transaction;
ccd467d6 135 WARN_ON(cur_trans != trans->transaction);
d5719762 136 WARN_ON(cur_trans->num_writers < 1);
ccd467d6 137 cur_trans->num_writers--;
79154b1b
CM
138 if (waitqueue_active(&cur_trans->writer_wait))
139 wake_up(&cur_trans->writer_wait);
79154b1b
CM
140 put_transaction(cur_trans);
141 mutex_unlock(&root->fs_info->trans_mutex);
d6025579 142 memset(trans, 0, sizeof(*trans));
2c90e5d6 143 kmem_cache_free(btrfs_trans_handle_cachep, trans);
79154b1b
CM
144 return 0;
145}
146
147
148int btrfs_write_and_wait_transaction(struct btrfs_trans_handle *trans,
149 struct btrfs_root *root)
150{
7c4452b9 151 int ret;
7c4452b9
CM
152 int err;
153 int werr = 0;
5f39d397 154 struct extent_map_tree *dirty_pages;
7c4452b9 155 struct page *page;
7c4452b9 156 struct inode *btree_inode = root->fs_info->btree_inode;
5f39d397
CM
157 u64 start;
158 u64 end;
159 unsigned long index;
7c4452b9
CM
160
161 if (!trans || !trans->transaction) {
162 return filemap_write_and_wait(btree_inode->i_mapping);
163 }
164 dirty_pages = &trans->transaction->dirty_pages;
165 while(1) {
5f39d397
CM
166 ret = find_first_extent_bit(dirty_pages, 0, &start, &end,
167 EXTENT_DIRTY);
168 if (ret)
7c4452b9 169 break;
5f39d397
CM
170 clear_extent_dirty(dirty_pages, start, end, GFP_NOFS);
171 while(start <= end) {
172 index = start >> PAGE_CACHE_SHIFT;
35ebb934 173 start = (u64)(index + 1) << PAGE_CACHE_SHIFT;
5f39d397 174 page = find_lock_page(btree_inode->i_mapping, index);
7c4452b9
CM
175 if (!page)
176 continue;
6702ed49
CM
177 if (PageWriteback(page)) {
178 if (PageDirty(page))
179 wait_on_page_writeback(page);
180 else {
181 unlock_page(page);
182 page_cache_release(page);
183 continue;
184 }
185 }
7c4452b9
CM
186 err = write_one_page(page, 0);
187 if (err)
188 werr = err;
189 page_cache_release(page);
190 }
191 }
192 err = filemap_fdatawait(btree_inode->i_mapping);
193 if (err)
194 werr = err;
195 return werr;
79154b1b
CM
196}
197
198int btrfs_commit_tree_roots(struct btrfs_trans_handle *trans,
199 struct btrfs_root *root)
200{
201 int ret;
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;
79154b1b 206
9078a3e1 207 btrfs_write_dirty_block_groups(trans, extent_root);
79154b1b 208 while(1) {
db94535d
CM
209 old_extent_block = btrfs_root_bytenr(&extent_root->root_item);
210 if (old_extent_block == extent_root->node->start)
79154b1b 211 break;
db94535d
CM
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));
79154b1b
CM
216 ret = btrfs_update_root(trans, tree_root,
217 &extent_root->root_key,
218 &extent_root->root_item);
219 BUG_ON(ret);
9078a3e1 220 btrfs_write_dirty_block_groups(trans, extent_root);
79154b1b
CM
221 }
222 return 0;
223}
224
225static int wait_for_commit(struct btrfs_root *root,
226 struct btrfs_transaction *commit)
227{
228 DEFINE_WAIT(wait);
ccd467d6 229 mutex_lock(&root->fs_info->trans_mutex);
79154b1b
CM
230 while(!commit->commit_done) {
231 prepare_to_wait(&commit->commit_wait, &wait,
232 TASK_UNINTERRUPTIBLE);
233 if (commit->commit_done)
234 break;
235 mutex_unlock(&root->fs_info->trans_mutex);
236 schedule();
237 mutex_lock(&root->fs_info->trans_mutex);
238 }
ccd467d6 239 mutex_unlock(&root->fs_info->trans_mutex);
79154b1b
CM
240 finish_wait(&commit->commit_wait, &wait);
241 return 0;
242}
243
0f7d52f4
CM
244struct dirty_root {
245 struct list_head list;
0f7d52f4 246 struct btrfs_root *root;
58176a96 247 struct btrfs_root *latest_root;
0f7d52f4
CM
248};
249
5ce14bbc
CM
250int btrfs_add_dead_root(struct btrfs_root *root,
251 struct btrfs_root *latest,
252 struct list_head *dead_list)
5eda7b5e
CM
253{
254 struct dirty_root *dirty;
255
256 dirty = kmalloc(sizeof(*dirty), GFP_NOFS);
257 if (!dirty)
258 return -ENOMEM;
5eda7b5e 259 dirty->root = root;
5ce14bbc 260 dirty->latest_root = latest;
5eda7b5e
CM
261 list_add(&dirty->list, dead_list);
262 return 0;
263}
264
35b7e476
CM
265static int add_dirty_roots(struct btrfs_trans_handle *trans,
266 struct radix_tree_root *radix,
267 struct list_head *list)
0f7d52f4
CM
268{
269 struct dirty_root *dirty;
270 struct btrfs_root *gang[8];
271 struct btrfs_root *root;
272 int i;
273 int ret;
54aa1f4d 274 int err = 0;
5eda7b5e 275 u32 refs;
54aa1f4d 276
0f7d52f4
CM
277 while(1) {
278 ret = radix_tree_gang_lookup_tag(radix, (void **)gang, 0,
279 ARRAY_SIZE(gang),
280 BTRFS_ROOT_TRANS_TAG);
281 if (ret == 0)
282 break;
283 for (i = 0; i < ret; i++) {
284 root = gang[i];
2619ba1f
CM
285 radix_tree_tag_clear(radix,
286 (unsigned long)root->root_key.objectid,
287 BTRFS_ROOT_TRANS_TAG);
0f7d52f4 288 if (root->commit_root == root->node) {
db94535d
CM
289 WARN_ON(root->node->start !=
290 btrfs_root_bytenr(&root->root_item));
5f39d397 291 free_extent_buffer(root->commit_root);
0f7d52f4 292 root->commit_root = NULL;
58176a96
JB
293
294 /* make sure to update the root on disk
295 * so we get any updates to the block used
296 * counts
297 */
298 err = btrfs_update_root(trans,
299 root->fs_info->tree_root,
300 &root->root_key,
301 &root->root_item);
0f7d52f4
CM
302 continue;
303 }
304 dirty = kmalloc(sizeof(*dirty), GFP_NOFS);
305 BUG_ON(!dirty);
9f3a7427
CM
306 dirty->root = kmalloc(sizeof(*dirty->root), GFP_NOFS);
307 BUG_ON(!dirty->root);
308
309 memset(&root->root_item.drop_progress, 0,
310 sizeof(struct btrfs_disk_key));
311 root->root_item.drop_level = 0;
312
313 memcpy(dirty->root, root, sizeof(*root));
314 dirty->root->node = root->commit_root;
58176a96 315 dirty->latest_root = root;
0f7d52f4 316 root->commit_root = NULL;
5eda7b5e 317
0f7d52f4 318 root->root_key.offset = root->fs_info->generation;
db94535d
CM
319 btrfs_set_root_bytenr(&root->root_item,
320 root->node->start);
321 btrfs_set_root_level(&root->root_item,
322 btrfs_header_level(root->node));
0f7d52f4
CM
323 err = btrfs_insert_root(trans, root->fs_info->tree_root,
324 &root->root_key,
325 &root->root_item);
54aa1f4d
CM
326 if (err)
327 break;
9f3a7427
CM
328
329 refs = btrfs_root_refs(&dirty->root->root_item);
330 btrfs_set_root_refs(&dirty->root->root_item, refs - 1);
5eda7b5e 331 err = btrfs_update_root(trans, root->fs_info->tree_root,
9f3a7427
CM
332 &dirty->root->root_key,
333 &dirty->root->root_item);
5eda7b5e
CM
334
335 BUG_ON(err);
9f3a7427 336 if (refs == 1) {
5eda7b5e 337 list_add(&dirty->list, list);
9f3a7427
CM
338 } else {
339 WARN_ON(1);
340 kfree(dirty->root);
5eda7b5e 341 kfree(dirty);
9f3a7427 342 }
0f7d52f4
CM
343 }
344 }
54aa1f4d 345 return err;
0f7d52f4
CM
346}
347
e9d0b13b
CM
348int btrfs_defrag_root(struct btrfs_root *root, int cacheonly)
349{
350 struct btrfs_fs_info *info = root->fs_info;
351 int ret;
352 struct btrfs_trans_handle *trans;
d3c2fdcf 353 unsigned long nr;
e9d0b13b
CM
354
355 if (root->defrag_running)
356 return 0;
e9d0b13b 357 trans = btrfs_start_transaction(root, 1);
6b80053d 358 while (1) {
e9d0b13b
CM
359 root->defrag_running = 1;
360 ret = btrfs_defrag_leaves(trans, root, cacheonly);
d3c2fdcf 361 nr = trans->blocks_used;
e9d0b13b
CM
362 btrfs_end_transaction(trans, root);
363 mutex_unlock(&info->fs_mutex);
d3c2fdcf 364 btrfs_btree_balance_dirty(info->tree_root, nr);
e9d0b13b
CM
365 cond_resched();
366
367 mutex_lock(&info->fs_mutex);
368 trans = btrfs_start_transaction(root, 1);
369 if (ret != -EAGAIN)
370 break;
371 }
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);
377 return 0;
378}
379
6702ed49
CM
380int btrfs_defrag_dirty_roots(struct btrfs_fs_info *info)
381{
382 struct btrfs_root *gang[1];
383 struct btrfs_root *root;
6702ed49
CM
384 int i;
385 int ret;
386 int err = 0;
387 u64 last = 0;
388
6702ed49
CM
389 while(1) {
390 ret = radix_tree_gang_lookup_tag(&info->fs_roots_radix,
391 (void **)gang, last,
392 ARRAY_SIZE(gang),
393 BTRFS_ROOT_DEFRAG_TAG);
394 if (ret == 0)
395 break;
396 for (i = 0; i < ret; i++) {
397 root = gang[i];
398 last = root->root_key.objectid + 1;
f510cfec 399 btrfs_defrag_root(root, 1);
6702ed49
CM
400 }
401 }
6b80053d 402 btrfs_defrag_root(info->extent_root, 1);
6702ed49
CM
403 return err;
404}
405
35b7e476
CM
406static int drop_dirty_roots(struct btrfs_root *tree_root,
407 struct list_head *list)
0f7d52f4
CM
408{
409 struct dirty_root *dirty;
410 struct btrfs_trans_handle *trans;
d3c2fdcf 411 unsigned long nr;
db94535d
CM
412 u64 num_bytes;
413 u64 bytes_used;
54aa1f4d 414 int ret = 0;
9f3a7427
CM
415 int err;
416
0f7d52f4 417 while(!list_empty(list)) {
58176a96
JB
418 struct btrfs_root *root;
419
facda1e7 420 mutex_lock(&tree_root->fs_info->fs_mutex);
0f7d52f4
CM
421 dirty = list_entry(list->next, struct dirty_root, list);
422 list_del_init(&dirty->list);
5eda7b5e 423
db94535d 424 num_bytes = btrfs_root_used(&dirty->root->root_item);
58176a96
JB
425 root = dirty->latest_root;
426
9f3a7427
CM
427 while(1) {
428 trans = btrfs_start_transaction(tree_root, 1);
429 ret = btrfs_drop_snapshot(trans, dirty->root);
430 if (ret != -EAGAIN) {
431 break;
432 }
58176a96 433
9f3a7427
CM
434 err = btrfs_update_root(trans,
435 tree_root,
436 &dirty->root->root_key,
437 &dirty->root->root_item);
438 if (err)
439 ret = err;
d3c2fdcf 440 nr = trans->blocks_used;
9f3a7427
CM
441 ret = btrfs_end_transaction(trans, tree_root);
442 BUG_ON(ret);
f4468e94 443 mutex_unlock(&tree_root->fs_info->fs_mutex);
d3c2fdcf 444 btrfs_btree_balance_dirty(tree_root, nr);
4dc11904 445 cond_resched();
f4468e94 446 mutex_lock(&tree_root->fs_info->fs_mutex);
9f3a7427 447 }
0f7d52f4 448 BUG_ON(ret);
58176a96 449
db94535d
CM
450 num_bytes -= btrfs_root_used(&dirty->root->root_item);
451 bytes_used = btrfs_root_used(&root->root_item);
452 if (num_bytes) {
58176a96 453 record_root_in_trans(root);
5f39d397 454 btrfs_set_root_used(&root->root_item,
db94535d 455 bytes_used - num_bytes);
58176a96 456 }
9f3a7427 457 ret = btrfs_del_root(trans, tree_root, &dirty->root->root_key);
58176a96
JB
458 if (ret) {
459 BUG();
54aa1f4d 460 break;
58176a96 461 }
d3c2fdcf 462 nr = trans->blocks_used;
0f7d52f4
CM
463 ret = btrfs_end_transaction(trans, tree_root);
464 BUG_ON(ret);
5eda7b5e 465
f510cfec 466 free_extent_buffer(dirty->root->node);
9f3a7427 467 kfree(dirty->root);
0f7d52f4 468 kfree(dirty);
facda1e7 469 mutex_unlock(&tree_root->fs_info->fs_mutex);
d3c2fdcf
CM
470
471 btrfs_btree_balance_dirty(tree_root, nr);
4dc11904 472 cond_resched();
0f7d52f4 473 }
54aa1f4d 474 return ret;
0f7d52f4
CM
475}
476
dc17ff8f
CM
477int btrfs_write_ordered_inodes(struct btrfs_trans_handle *trans,
478 struct btrfs_root *root)
479{
480 struct btrfs_transaction *cur_trans = trans->transaction;
481 struct inode *inode;
482 u64 root_objectid = 0;
483 u64 objectid = 0;
484 u64 transid = trans->transid;
485 int ret;
486
487printk("write ordered trans %Lu\n", transid);
488 while(1) {
489 ret = btrfs_find_first_ordered_inode(
490 &cur_trans->ordered_inode_tree,
491 &root_objectid, &objectid);
492 if (!ret)
493 break;
494
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,
498 root_objectid);
499 if (inode) {
500 if (S_ISREG(inode->i_mode))
501 filemap_fdatawrite(inode->i_mapping);
502 iput(inode);
503 }
504 mutex_lock(&root->fs_info->fs_mutex);
505 mutex_lock(&root->fs_info->trans_mutex);
506 }
507 while(1) {
508 root_objectid = 0;
509 objectid = 0;
510 ret = btrfs_find_del_first_ordered_inode(
511 &cur_trans->ordered_inode_tree,
512 &root_objectid, &objectid);
513 if (!ret)
514 break;
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,
518 root_objectid);
519 if (inode) {
520 if (S_ISREG(inode->i_mode))
521 filemap_write_and_wait(inode->i_mapping);
522 iput(inode);
523 }
524 mutex_lock(&root->fs_info->fs_mutex);
525 mutex_lock(&root->fs_info->trans_mutex);
526 }
527printk("done write ordered trans %Lu\n", transid);
528 return 0;
529}
530
79154b1b
CM
531int btrfs_commit_transaction(struct btrfs_trans_handle *trans,
532 struct btrfs_root *root)
533{
15ee9bc7
JB
534 unsigned long joined = 0;
535 unsigned long timeout = 1;
79154b1b 536 struct btrfs_transaction *cur_trans;
8fd17795 537 struct btrfs_transaction *prev_trans = NULL;
0f7d52f4 538 struct list_head dirty_fs_roots;
4313b399 539 struct extent_map_tree *pinned_copy;
79154b1b 540 DEFINE_WAIT(wait);
15ee9bc7 541 int ret;
79154b1b 542
0f7d52f4 543 INIT_LIST_HEAD(&dirty_fs_roots);
d6e4a428 544
79154b1b
CM
545 mutex_lock(&root->fs_info->trans_mutex);
546 if (trans->transaction->in_commit) {
547 cur_trans = trans->transaction;
548 trans->transaction->use_count++;
ccd467d6 549 mutex_unlock(&root->fs_info->trans_mutex);
79154b1b 550 btrfs_end_transaction(trans, root);
ccd467d6
CM
551
552 mutex_unlock(&root->fs_info->fs_mutex);
79154b1b
CM
553 ret = wait_for_commit(root, cur_trans);
554 BUG_ON(ret);
15ee9bc7
JB
555
556 mutex_lock(&root->fs_info->trans_mutex);
79154b1b 557 put_transaction(cur_trans);
15ee9bc7
JB
558 mutex_unlock(&root->fs_info->trans_mutex);
559
ccd467d6 560 mutex_lock(&root->fs_info->fs_mutex);
79154b1b
CM
561 return 0;
562 }
4313b399
CM
563
564 pinned_copy = kmalloc(sizeof(*pinned_copy), GFP_NOFS);
565 if (!pinned_copy)
566 return -ENOMEM;
567
568 extent_map_tree_init(pinned_copy,
569 root->fs_info->btree_inode->i_mapping, GFP_NOFS);
570
2c90e5d6 571 trans->transaction->in_commit = 1;
ccd467d6
CM
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);
580
581 wait_for_commit(root, prev_trans);
ccd467d6
CM
582
583 mutex_lock(&root->fs_info->fs_mutex);
584 mutex_lock(&root->fs_info->trans_mutex);
15ee9bc7 585 put_transaction(prev_trans);
ccd467d6
CM
586 }
587 }
15ee9bc7
JB
588
589 do {
590 joined = cur_trans->num_joined;
2c90e5d6 591 WARN_ON(cur_trans != trans->transaction);
15ee9bc7 592 prepare_to_wait(&cur_trans->writer_wait, &wait,
79154b1b 593 TASK_UNINTERRUPTIBLE);
15ee9bc7
JB
594
595 if (cur_trans->num_writers > 1)
596 timeout = MAX_SCHEDULE_TIMEOUT;
597 else
598 timeout = 1;
599
ccd467d6 600 mutex_unlock(&root->fs_info->fs_mutex);
79154b1b 601 mutex_unlock(&root->fs_info->trans_mutex);
15ee9bc7
JB
602
603 schedule_timeout(timeout);
604
ccd467d6 605 mutex_lock(&root->fs_info->fs_mutex);
79154b1b 606 mutex_lock(&root->fs_info->trans_mutex);
15ee9bc7 607 finish_wait(&cur_trans->writer_wait, &wait);
dc17ff8f
CM
608 ret = btrfs_write_ordered_inodes(trans, root);
609
15ee9bc7
JB
610 } while (cur_trans->num_writers > 1 ||
611 (cur_trans->num_joined != joined));
612
2c90e5d6 613 WARN_ON(cur_trans != trans->transaction);
dc17ff8f 614
54aa1f4d
CM
615 ret = add_dirty_roots(trans, &root->fs_info->fs_roots_radix,
616 &dirty_fs_roots);
617 BUG_ON(ret);
618
79154b1b
CM
619 ret = btrfs_commit_tree_roots(trans, root);
620 BUG_ON(ret);
54aa1f4d 621
78fae27e
CM
622 cur_trans = root->fs_info->running_transaction;
623 root->fs_info->running_transaction = NULL;
4b52dff6
CM
624 btrfs_set_super_generation(&root->fs_info->super_copy,
625 cur_trans->transid);
626 btrfs_set_super_root(&root->fs_info->super_copy,
db94535d
CM
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));
5f39d397
CM
630
631 write_extent_buffer(root->fs_info->sb_buffer,
632 &root->fs_info->super_copy, 0,
633 sizeof(root->fs_info->super_copy));
ccd467d6 634
4313b399 635 btrfs_copy_pinned(root, pinned_copy);
ccd467d6 636
78fae27e 637 mutex_unlock(&root->fs_info->trans_mutex);
8fd17795 638 mutex_unlock(&root->fs_info->fs_mutex);
79154b1b
CM
639 ret = btrfs_write_and_wait_transaction(trans, root);
640 BUG_ON(ret);
79154b1b 641 write_ctree_super(trans, root);
4313b399 642
8fd17795 643 mutex_lock(&root->fs_info->fs_mutex);
4313b399 644 btrfs_finish_extent_commit(trans, root, pinned_copy);
78fae27e 645 mutex_lock(&root->fs_info->trans_mutex);
4313b399
CM
646
647 kfree(pinned_copy);
648
2c90e5d6 649 cur_trans->commit_done = 1;
15ee9bc7 650 root->fs_info->last_trans_committed = cur_trans->transid;
2c90e5d6 651 wake_up(&cur_trans->commit_wait);
78fae27e 652 put_transaction(cur_trans);
79154b1b 653 put_transaction(cur_trans);
58176a96 654
facda1e7
CM
655 if (root->fs_info->closing)
656 list_splice_init(&root->fs_info->dead_roots, &dirty_fs_roots);
657 else
658 list_splice_init(&dirty_fs_roots, &root->fs_info->dead_roots);
58176a96 659
78fae27e 660 mutex_unlock(&root->fs_info->trans_mutex);
2c90e5d6 661 kmem_cache_free(btrfs_trans_handle_cachep, trans);
79154b1b 662
facda1e7
CM
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);
667 }
79154b1b
CM
668 return ret;
669}
670
e9d0b13b
CM
671int btrfs_clean_old_snapshots(struct btrfs_root *root)
672{
673 struct list_head dirty_roots;
674 INIT_LIST_HEAD(&dirty_roots);
675
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);
679
680 if (!list_empty(&dirty_roots)) {
681 drop_dirty_roots(root, &dirty_roots);
682 }
683 return 0;
684}
6da6abae
CM
685#if LINUX_VERSION_CODE <= KERNEL_VERSION(2,6,18)
686void btrfs_transaction_cleaner(void *p)
687#else
08607c1b 688void btrfs_transaction_cleaner(struct work_struct *work)
6da6abae 689#endif
08607c1b 690{
6da6abae
CM
691#if LINUX_VERSION_CODE <= KERNEL_VERSION(2,6,18)
692 struct btrfs_fs_info *fs_info = p;
693#else
08607c1b
CM
694 struct btrfs_fs_info *fs_info = container_of(work,
695 struct btrfs_fs_info,
696 trans_work.work);
697
6da6abae 698#endif
08607c1b
CM
699 struct btrfs_root *root = fs_info->tree_root;
700 struct btrfs_transaction *cur;
701 struct btrfs_trans_handle *trans;
702 unsigned long now;
703 unsigned long delay = HZ * 30;
704 int ret;
705
08607c1b
CM
706 mutex_lock(&root->fs_info->fs_mutex);
707 mutex_lock(&root->fs_info->trans_mutex);
708 cur = root->fs_info->running_transaction;
709 if (!cur) {
710 mutex_unlock(&root->fs_info->trans_mutex);
711 goto out;
712 }
713 now = get_seconds();
714 if (now < cur->start_time || now - cur->start_time < 30) {
715 mutex_unlock(&root->fs_info->trans_mutex);
716 delay = HZ * 5;
717 goto out;
718 }
719 mutex_unlock(&root->fs_info->trans_mutex);
6702ed49 720 btrfs_defrag_dirty_roots(root->fs_info);
08607c1b
CM
721 trans = btrfs_start_transaction(root, 1);
722 ret = btrfs_commit_transaction(trans, root);
723out:
724 mutex_unlock(&root->fs_info->fs_mutex);
e9d0b13b 725 btrfs_clean_old_snapshots(root);
08607c1b
CM
726 btrfs_transaction_queue_work(root, delay);
727}
728
729void btrfs_transaction_queue_work(struct btrfs_root *root, int delay)
730{
731 queue_delayed_work(trans_wq, &root->fs_info->trans_work, delay);
732}
733
734void btrfs_transaction_flush_work(struct btrfs_root *root)
735{
736 cancel_rearming_delayed_workqueue(trans_wq, &root->fs_info->trans_work);
737 flush_workqueue(trans_wq);
738}
739
740void __init btrfs_init_transaction_sys(void)
741{
742 trans_wq = create_workqueue("btrfs");
743}
744
17636e03 745void btrfs_exit_transaction_sys(void)
08607c1b
CM
746{
747 destroy_workqueue(trans_wq);
748}
749