Btrfs: Add data=ordered support
[safe/jmp/linux-2.6] / fs / btrfs / transaction.c
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
19 #include <linux/fs.h>
20 #include <linux/sched.h>
21 #include <linux/writeback.h>
22 #include <linux/pagemap.h>
23 #include "ctree.h"
24 #include "disk-io.h"
25 #include "transaction.h"
26
27 static int total_trans = 0;
28 extern struct kmem_cache *btrfs_trans_handle_cachep;
29 extern struct kmem_cache *btrfs_transaction_cachep;
30
31 static struct workqueue_struct *trans_wq;
32
33 #define BTRFS_ROOT_TRANS_TAG 0
34 #define BTRFS_ROOT_DEFRAG_TAG 1
35
36 static void put_transaction(struct btrfs_transaction *transaction)
37 {
38         WARN_ON(transaction->use_count == 0);
39         transaction->use_count--;
40         if (transaction->use_count == 0) {
41                 WARN_ON(total_trans == 0);
42                 total_trans--;
43                 list_del_init(&transaction->list);
44                 memset(transaction, 0, sizeof(*transaction));
45                 kmem_cache_free(btrfs_transaction_cachep, transaction);
46         }
47 }
48
49 static 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) {
54                 cur_trans = kmem_cache_alloc(btrfs_transaction_cachep,
55                                              GFP_NOFS);
56                 total_trans++;
57                 BUG_ON(!cur_trans);
58                 root->fs_info->generation++;
59                 root->fs_info->running_transaction = cur_trans;
60                 cur_trans->num_writers = 1;
61                 cur_trans->num_joined = 0;
62                 cur_trans->transid = root->fs_info->generation;
63                 init_waitqueue_head(&cur_trans->writer_wait);
64                 init_waitqueue_head(&cur_trans->commit_wait);
65                 cur_trans->in_commit = 0;
66                 cur_trans->use_count = 1;
67                 cur_trans->commit_done = 0;
68                 cur_trans->start_time = get_seconds();
69                 list_add_tail(&cur_trans->list, &root->fs_info->trans_list);
70                 btrfs_ordered_inode_tree_init(&cur_trans->ordered_inode_tree);
71                 extent_map_tree_init(&cur_trans->dirty_pages,
72                                      root->fs_info->btree_inode->i_mapping,
73                                      GFP_NOFS);
74         } else {
75                 cur_trans->num_writers++;
76                 cur_trans->num_joined++;
77         }
78
79         return 0;
80 }
81
82 static 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;
95                         extent_buffer_get(root->node);
96                 } else {
97                         WARN_ON(1);
98                 }
99                 root->last_trans = running_trans_id;
100         }
101         return 0;
102 }
103
104 struct btrfs_trans_handle *btrfs_start_transaction(struct btrfs_root *root,
105                                                    int num_blocks)
106 {
107         struct btrfs_trans_handle *h =
108                 kmem_cache_alloc(btrfs_trans_handle_cachep, GFP_NOFS);
109         int ret;
110
111         mutex_lock(&root->fs_info->trans_mutex);
112         ret = join_transaction(root);
113         BUG_ON(ret);
114
115         record_root_in_trans(root);
116         h->transid = root->fs_info->running_transaction->transid;
117         h->transaction = root->fs_info->running_transaction;
118         h->blocks_reserved = num_blocks;
119         h->blocks_used = 0;
120         h->block_group = NULL;
121         h->alloc_exclude_nr = 0;
122         h->alloc_exclude_start = 0;
123         root->fs_info->running_transaction->use_count++;
124         mutex_unlock(&root->fs_info->trans_mutex);
125         return h;
126 }
127
128 int btrfs_end_transaction(struct btrfs_trans_handle *trans,
129                           struct btrfs_root *root)
130 {
131         struct btrfs_transaction *cur_trans;
132
133         mutex_lock(&root->fs_info->trans_mutex);
134         cur_trans = root->fs_info->running_transaction;
135         WARN_ON(cur_trans != trans->transaction);
136         WARN_ON(cur_trans->num_writers < 1);
137         cur_trans->num_writers--;
138         if (waitqueue_active(&cur_trans->writer_wait))
139                 wake_up(&cur_trans->writer_wait);
140         put_transaction(cur_trans);
141         mutex_unlock(&root->fs_info->trans_mutex);
142         memset(trans, 0, sizeof(*trans));
143         kmem_cache_free(btrfs_trans_handle_cachep, trans);
144         return 0;
145 }
146
147
148 int btrfs_write_and_wait_transaction(struct btrfs_trans_handle *trans,
149                                      struct btrfs_root *root)
150 {
151         int ret;
152         int err;
153         int werr = 0;
154         struct extent_map_tree *dirty_pages;
155         struct page *page;
156         struct inode *btree_inode = root->fs_info->btree_inode;
157         u64 start;
158         u64 end;
159         unsigned long index;
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) {
166                 ret = find_first_extent_bit(dirty_pages, 0, &start, &end,
167                                             EXTENT_DIRTY);
168                 if (ret)
169                         break;
170                 clear_extent_dirty(dirty_pages, start, end, GFP_NOFS);
171                 while(start <= end) {
172                         index = start >> PAGE_CACHE_SHIFT;
173                         start = (u64)(index + 1) << PAGE_CACHE_SHIFT;
174                         page = find_lock_page(btree_inode->i_mapping, index);
175                         if (!page)
176                                 continue;
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                         }
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;
196 }
197
198 int 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;
206
207         btrfs_write_dirty_block_groups(trans, extent_root);
208         while(1) {
209                 old_extent_block = btrfs_root_bytenr(&extent_root->root_item);
210                 if (old_extent_block == extent_root->node->start)
211                         break;
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));
216                 ret = btrfs_update_root(trans, tree_root,
217                                         &extent_root->root_key,
218                                         &extent_root->root_item);
219                 BUG_ON(ret);
220                 btrfs_write_dirty_block_groups(trans, extent_root);
221         }
222         return 0;
223 }
224
225 static int wait_for_commit(struct btrfs_root *root,
226                            struct btrfs_transaction *commit)
227 {
228         DEFINE_WAIT(wait);
229         mutex_lock(&root->fs_info->trans_mutex);
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         }
239         mutex_unlock(&root->fs_info->trans_mutex);
240         finish_wait(&commit->commit_wait, &wait);
241         return 0;
242 }
243
244 struct dirty_root {
245         struct list_head list;
246         struct btrfs_root *root;
247         struct btrfs_root *latest_root;
248 };
249
250 int btrfs_add_dead_root(struct btrfs_root *root,
251                         struct btrfs_root *latest,
252                         struct list_head *dead_list)
253 {
254         struct dirty_root *dirty;
255
256         dirty = kmalloc(sizeof(*dirty), GFP_NOFS);
257         if (!dirty)
258                 return -ENOMEM;
259         dirty->root = root;
260         dirty->latest_root = latest;
261         list_add(&dirty->list, dead_list);
262         return 0;
263 }
264
265 static int add_dirty_roots(struct btrfs_trans_handle *trans,
266                            struct radix_tree_root *radix,
267                            struct list_head *list)
268 {
269         struct dirty_root *dirty;
270         struct btrfs_root *gang[8];
271         struct btrfs_root *root;
272         int i;
273         int ret;
274         int err = 0;
275         u32 refs;
276
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];
285                         radix_tree_tag_clear(radix,
286                                      (unsigned long)root->root_key.objectid,
287                                      BTRFS_ROOT_TRANS_TAG);
288                         if (root->commit_root == root->node) {
289                                 WARN_ON(root->node->start !=
290                                         btrfs_root_bytenr(&root->root_item));
291                                 free_extent_buffer(root->commit_root);
292                                 root->commit_root = NULL;
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);
302                                 continue;
303                         }
304                         dirty = kmalloc(sizeof(*dirty), GFP_NOFS);
305                         BUG_ON(!dirty);
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;
315                         dirty->latest_root = root;
316                         root->commit_root = NULL;
317
318                         root->root_key.offset = root->fs_info->generation;
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));
323                         err = btrfs_insert_root(trans, root->fs_info->tree_root,
324                                                 &root->root_key,
325                                                 &root->root_item);
326                         if (err)
327                                 break;
328
329                         refs = btrfs_root_refs(&dirty->root->root_item);
330                         btrfs_set_root_refs(&dirty->root->root_item, refs - 1);
331                         err = btrfs_update_root(trans, root->fs_info->tree_root,
332                                                 &dirty->root->root_key,
333                                                 &dirty->root->root_item);
334
335                         BUG_ON(err);
336                         if (refs == 1) {
337                                 list_add(&dirty->list, list);
338                         } else {
339                                 WARN_ON(1);
340                                 kfree(dirty->root);
341                                 kfree(dirty);
342                         }
343                 }
344         }
345         return err;
346 }
347
348 int 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;
353         unsigned long nr;
354
355         if (root->defrag_running)
356                 return 0;
357         trans = btrfs_start_transaction(root, 1);
358         while (1) {
359                 root->defrag_running = 1;
360                 ret = btrfs_defrag_leaves(trans, root, cacheonly);
361                 nr = trans->blocks_used;
362                 btrfs_end_transaction(trans, root);
363                 mutex_unlock(&info->fs_mutex);
364                 btrfs_btree_balance_dirty(info->tree_root, nr);
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
380 int btrfs_defrag_dirty_roots(struct btrfs_fs_info *info)
381 {
382         struct btrfs_root *gang[1];
383         struct btrfs_root *root;
384         int i;
385         int ret;
386         int err = 0;
387         u64 last = 0;
388
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;
399                         btrfs_defrag_root(root, 1);
400                 }
401         }
402         btrfs_defrag_root(info->extent_root, 1);
403         return err;
404 }
405
406 static int drop_dirty_roots(struct btrfs_root *tree_root,
407                             struct list_head *list)
408 {
409         struct dirty_root *dirty;
410         struct btrfs_trans_handle *trans;
411         unsigned long nr;
412         u64 num_bytes;
413         u64 bytes_used;
414         int ret = 0;
415         int err;
416
417         while(!list_empty(list)) {
418                 struct btrfs_root *root;
419
420                 mutex_lock(&tree_root->fs_info->fs_mutex);
421                 dirty = list_entry(list->next, struct dirty_root, list);
422                 list_del_init(&dirty->list);
423
424                 num_bytes = btrfs_root_used(&dirty->root->root_item);
425                 root = dirty->latest_root;
426
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                         }
433
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;
440                         nr = trans->blocks_used;
441                         ret = btrfs_end_transaction(trans, tree_root);
442                         BUG_ON(ret);
443                         mutex_unlock(&tree_root->fs_info->fs_mutex);
444                         btrfs_btree_balance_dirty(tree_root, nr);
445                         cond_resched();
446                         mutex_lock(&tree_root->fs_info->fs_mutex);
447                 }
448                 BUG_ON(ret);
449
450                 num_bytes -= btrfs_root_used(&dirty->root->root_item);
451                 bytes_used = btrfs_root_used(&root->root_item);
452                 if (num_bytes) {
453                         record_root_in_trans(root);
454                         btrfs_set_root_used(&root->root_item,
455                                             bytes_used - num_bytes);
456                 }
457                 ret = btrfs_del_root(trans, tree_root, &dirty->root->root_key);
458                 if (ret) {
459                         BUG();
460                         break;
461                 }
462                 nr = trans->blocks_used;
463                 ret = btrfs_end_transaction(trans, tree_root);
464                 BUG_ON(ret);
465
466                 free_extent_buffer(dirty->root->node);
467                 kfree(dirty->root);
468                 kfree(dirty);
469                 mutex_unlock(&tree_root->fs_info->fs_mutex);
470
471                 btrfs_btree_balance_dirty(tree_root, nr);
472                 cond_resched();
473         }
474         return ret;
475 }
476
477 int 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
487 printk("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         }
527 printk("done write ordered trans %Lu\n", transid);
528         return 0;
529 }
530
531 int btrfs_commit_transaction(struct btrfs_trans_handle *trans,
532                              struct btrfs_root *root)
533 {
534         unsigned long joined = 0;
535         unsigned long timeout = 1;
536         struct btrfs_transaction *cur_trans;
537         struct btrfs_transaction *prev_trans = NULL;
538         struct list_head dirty_fs_roots;
539         struct extent_map_tree *pinned_copy;
540         DEFINE_WAIT(wait);
541         int ret;
542
543         INIT_LIST_HEAD(&dirty_fs_roots);
544
545         mutex_lock(&root->fs_info->trans_mutex);
546         if (trans->transaction->in_commit) {
547                 cur_trans = trans->transaction;
548                 trans->transaction->use_count++;
549                 mutex_unlock(&root->fs_info->trans_mutex);
550                 btrfs_end_transaction(trans, root);
551
552                 mutex_unlock(&root->fs_info->fs_mutex);
553                 ret = wait_for_commit(root, cur_trans);
554                 BUG_ON(ret);
555
556                 mutex_lock(&root->fs_info->trans_mutex);
557                 put_transaction(cur_trans);
558                 mutex_unlock(&root->fs_info->trans_mutex);
559
560                 mutex_lock(&root->fs_info->fs_mutex);
561                 return 0;
562         }
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
571         trans->transaction->in_commit = 1;
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);
582
583                         mutex_lock(&root->fs_info->fs_mutex);
584                         mutex_lock(&root->fs_info->trans_mutex);
585                         put_transaction(prev_trans);
586                 }
587         }
588
589         do {
590                 joined = cur_trans->num_joined;
591                 WARN_ON(cur_trans != trans->transaction);
592                 prepare_to_wait(&cur_trans->writer_wait, &wait,
593                                 TASK_UNINTERRUPTIBLE);
594
595                 if (cur_trans->num_writers > 1)
596                         timeout = MAX_SCHEDULE_TIMEOUT;
597                 else
598                         timeout = 1;
599
600                 mutex_unlock(&root->fs_info->fs_mutex);
601                 mutex_unlock(&root->fs_info->trans_mutex);
602
603                 schedule_timeout(timeout);
604
605                 mutex_lock(&root->fs_info->fs_mutex);
606                 mutex_lock(&root->fs_info->trans_mutex);
607                 finish_wait(&cur_trans->writer_wait, &wait);
608                 ret = btrfs_write_ordered_inodes(trans, root);
609
610         } while (cur_trans->num_writers > 1 ||
611                  (cur_trans->num_joined != joined));
612
613         WARN_ON(cur_trans != trans->transaction);
614
615         ret = add_dirty_roots(trans, &root->fs_info->fs_roots_radix,
616                               &dirty_fs_roots);
617         BUG_ON(ret);
618
619         ret = btrfs_commit_tree_roots(trans, root);
620         BUG_ON(ret);
621
622         cur_trans = root->fs_info->running_transaction;
623         root->fs_info->running_transaction = NULL;
624         btrfs_set_super_generation(&root->fs_info->super_copy,
625                                    cur_trans->transid);
626         btrfs_set_super_root(&root->fs_info->super_copy,
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));
630
631         write_extent_buffer(root->fs_info->sb_buffer,
632                             &root->fs_info->super_copy, 0,
633                             sizeof(root->fs_info->super_copy));
634
635         btrfs_copy_pinned(root, pinned_copy);
636
637         mutex_unlock(&root->fs_info->trans_mutex);
638         mutex_unlock(&root->fs_info->fs_mutex);
639         ret = btrfs_write_and_wait_transaction(trans, root);
640         BUG_ON(ret);
641         write_ctree_super(trans, root);
642
643         mutex_lock(&root->fs_info->fs_mutex);
644         btrfs_finish_extent_commit(trans, root, pinned_copy);
645         mutex_lock(&root->fs_info->trans_mutex);
646
647         kfree(pinned_copy);
648
649         cur_trans->commit_done = 1;
650         root->fs_info->last_trans_committed = cur_trans->transid;
651         wake_up(&cur_trans->commit_wait);
652         put_transaction(cur_trans);
653         put_transaction(cur_trans);
654
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);
659
660         mutex_unlock(&root->fs_info->trans_mutex);
661         kmem_cache_free(btrfs_trans_handle_cachep, trans);
662
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         }
668         return ret;
669 }
670
671 int 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 }
685 #if LINUX_VERSION_CODE <= KERNEL_VERSION(2,6,18)
686 void btrfs_transaction_cleaner(void *p)
687 #else
688 void btrfs_transaction_cleaner(struct work_struct *work)
689 #endif
690 {
691 #if LINUX_VERSION_CODE <= KERNEL_VERSION(2,6,18)
692         struct btrfs_fs_info *fs_info = p;
693 #else
694         struct btrfs_fs_info *fs_info = container_of(work,
695                                                      struct btrfs_fs_info,
696                                                      trans_work.work);
697
698 #endif
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
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);
720         btrfs_defrag_dirty_roots(root->fs_info);
721         trans = btrfs_start_transaction(root, 1);
722         ret = btrfs_commit_transaction(trans, root);
723 out:
724         mutex_unlock(&root->fs_info->fs_mutex);
725         btrfs_clean_old_snapshots(root);
726         btrfs_transaction_queue_work(root, delay);
727 }
728
729 void 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
734 void 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
740 void __init btrfs_init_transaction_sys(void)
741 {
742         trans_wq = create_workqueue("btrfs");
743 }
744
745 void btrfs_exit_transaction_sys(void)
746 {
747         destroy_workqueue(trans_wq);
748 }
749