Match the extent tree code to btrfs-progs for multi-device merging
[linux-2.6-block.git] / 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 noinline 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 noinline 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                 root->fs_info->last_alloc = 0;
61                 root->fs_info->last_data_alloc = 0;
62                 cur_trans->num_writers = 1;
63                 cur_trans->num_joined = 0;
64                 cur_trans->transid = root->fs_info->generation;
65                 init_waitqueue_head(&cur_trans->writer_wait);
66                 init_waitqueue_head(&cur_trans->commit_wait);
67                 cur_trans->in_commit = 0;
68                 cur_trans->use_count = 1;
69                 cur_trans->commit_done = 0;
70                 cur_trans->start_time = get_seconds();
71                 INIT_LIST_HEAD(&cur_trans->pending_snapshots);
72                 list_add_tail(&cur_trans->list, &root->fs_info->trans_list);
73                 btrfs_ordered_inode_tree_init(&cur_trans->ordered_inode_tree);
74                 extent_io_tree_init(&cur_trans->dirty_pages,
75                                      root->fs_info->btree_inode->i_mapping,
76                                      GFP_NOFS);
77         } else {
78                 cur_trans->num_writers++;
79                 cur_trans->num_joined++;
80         }
81
82         return 0;
83 }
84
85 static noinline int record_root_in_trans(struct btrfs_root *root)
86 {
87         u64 running_trans_id = root->fs_info->running_transaction->transid;
88         if (root->ref_cows && root->last_trans < running_trans_id) {
89                 WARN_ON(root == root->fs_info->extent_root);
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                         radix_tree_tag_set(&root->fs_info->fs_roots_radix,
95                                    (unsigned long)root->root_key.objectid,
96                                    BTRFS_ROOT_DEFRAG_TAG);
97                         root->commit_root = root->node;
98                         extent_buffer_get(root->node);
99                 } else {
100                         WARN_ON(1);
101                 }
102                 root->last_trans = running_trans_id;
103         }
104         return 0;
105 }
106
107 struct btrfs_trans_handle *btrfs_start_transaction(struct btrfs_root *root,
108                                                    int num_blocks)
109 {
110         struct btrfs_trans_handle *h =
111                 kmem_cache_alloc(btrfs_trans_handle_cachep, GFP_NOFS);
112         int ret;
113
114         mutex_lock(&root->fs_info->trans_mutex);
115         ret = join_transaction(root);
116         BUG_ON(ret);
117
118         record_root_in_trans(root);
119         h->transid = root->fs_info->running_transaction->transid;
120         h->transaction = root->fs_info->running_transaction;
121         h->blocks_reserved = num_blocks;
122         h->blocks_used = 0;
123         h->block_group = NULL;
124         h->alloc_exclude_nr = 0;
125         h->alloc_exclude_start = 0;
126         root->fs_info->running_transaction->use_count++;
127         mutex_unlock(&root->fs_info->trans_mutex);
128         return h;
129 }
130
131 int btrfs_end_transaction(struct btrfs_trans_handle *trans,
132                           struct btrfs_root *root)
133 {
134         struct btrfs_transaction *cur_trans;
135
136         mutex_lock(&root->fs_info->trans_mutex);
137         cur_trans = root->fs_info->running_transaction;
138         WARN_ON(cur_trans != trans->transaction);
139         WARN_ON(cur_trans->num_writers < 1);
140         cur_trans->num_writers--;
141         if (waitqueue_active(&cur_trans->writer_wait))
142                 wake_up(&cur_trans->writer_wait);
143         put_transaction(cur_trans);
144         mutex_unlock(&root->fs_info->trans_mutex);
145         memset(trans, 0, sizeof(*trans));
146         kmem_cache_free(btrfs_trans_handle_cachep, trans);
147         return 0;
148 }
149
150
151 int btrfs_write_and_wait_transaction(struct btrfs_trans_handle *trans,
152                                      struct btrfs_root *root)
153 {
154         int ret;
155         int err;
156         int werr = 0;
157         struct extent_io_tree *dirty_pages;
158         struct page *page;
159         struct inode *btree_inode = root->fs_info->btree_inode;
160         u64 start;
161         u64 end;
162         unsigned long index;
163
164         if (!trans || !trans->transaction) {
165                 return filemap_write_and_wait(btree_inode->i_mapping);
166         }
167         dirty_pages = &trans->transaction->dirty_pages;
168         while(1) {
169                 ret = find_first_extent_bit(dirty_pages, 0, &start, &end,
170                                             EXTENT_DIRTY);
171                 if (ret)
172                         break;
173                 clear_extent_dirty(dirty_pages, start, end, GFP_NOFS);
174                 while(start <= end) {
175                         index = start >> PAGE_CACHE_SHIFT;
176                         start = (u64)(index + 1) << PAGE_CACHE_SHIFT;
177                         page = find_lock_page(btree_inode->i_mapping, index);
178                         if (!page)
179                                 continue;
180                         if (PageWriteback(page)) {
181                                 if (PageDirty(page))
182                                         wait_on_page_writeback(page);
183                                 else {
184                                         unlock_page(page);
185                                         page_cache_release(page);
186                                         continue;
187                                 }
188                         }
189                         err = write_one_page(page, 0);
190                         if (err)
191                                 werr = err;
192                         page_cache_release(page);
193                 }
194         }
195         err = filemap_fdatawait(btree_inode->i_mapping);
196         if (err)
197                 werr = err;
198         return werr;
199 }
200
201 int btrfs_commit_tree_roots(struct btrfs_trans_handle *trans,
202                             struct btrfs_root *root)
203 {
204         int ret;
205         u64 old_extent_block;
206         struct btrfs_fs_info *fs_info = root->fs_info;
207         struct btrfs_root *tree_root = fs_info->tree_root;
208         struct btrfs_root *extent_root = fs_info->extent_root;
209
210         btrfs_write_dirty_block_groups(trans, extent_root);
211         while(1) {
212                 old_extent_block = btrfs_root_bytenr(&extent_root->root_item);
213                 if (old_extent_block == extent_root->node->start)
214                         break;
215                 btrfs_set_root_bytenr(&extent_root->root_item,
216                                       extent_root->node->start);
217                 btrfs_set_root_level(&extent_root->root_item,
218                                      btrfs_header_level(extent_root->node));
219                 ret = btrfs_update_root(trans, tree_root,
220                                         &extent_root->root_key,
221                                         &extent_root->root_item);
222                 BUG_ON(ret);
223                 btrfs_write_dirty_block_groups(trans, extent_root);
224         }
225         return 0;
226 }
227
228 static noinline int wait_for_commit(struct btrfs_root *root,
229                                     struct btrfs_transaction *commit)
230 {
231         DEFINE_WAIT(wait);
232         mutex_lock(&root->fs_info->trans_mutex);
233         while(!commit->commit_done) {
234                 prepare_to_wait(&commit->commit_wait, &wait,
235                                 TASK_UNINTERRUPTIBLE);
236                 if (commit->commit_done)
237                         break;
238                 mutex_unlock(&root->fs_info->trans_mutex);
239                 schedule();
240                 mutex_lock(&root->fs_info->trans_mutex);
241         }
242         mutex_unlock(&root->fs_info->trans_mutex);
243         finish_wait(&commit->commit_wait, &wait);
244         return 0;
245 }
246
247 struct dirty_root {
248         struct list_head list;
249         struct btrfs_root *root;
250         struct btrfs_root *latest_root;
251 };
252
253 int btrfs_add_dead_root(struct btrfs_root *root,
254                         struct btrfs_root *latest,
255                         struct list_head *dead_list)
256 {
257         struct dirty_root *dirty;
258
259         dirty = kmalloc(sizeof(*dirty), GFP_NOFS);
260         if (!dirty)
261                 return -ENOMEM;
262         dirty->root = root;
263         dirty->latest_root = latest;
264         list_add(&dirty->list, dead_list);
265         return 0;
266 }
267
268 static noinline int add_dirty_roots(struct btrfs_trans_handle *trans,
269                                     struct radix_tree_root *radix,
270                                     struct list_head *list)
271 {
272         struct dirty_root *dirty;
273         struct btrfs_root *gang[8];
274         struct btrfs_root *root;
275         int i;
276         int ret;
277         int err = 0;
278         u32 refs;
279
280         while(1) {
281                 ret = radix_tree_gang_lookup_tag(radix, (void **)gang, 0,
282                                                  ARRAY_SIZE(gang),
283                                                  BTRFS_ROOT_TRANS_TAG);
284                 if (ret == 0)
285                         break;
286                 for (i = 0; i < ret; i++) {
287                         root = gang[i];
288                         radix_tree_tag_clear(radix,
289                                      (unsigned long)root->root_key.objectid,
290                                      BTRFS_ROOT_TRANS_TAG);
291                         if (root->commit_root == root->node) {
292                                 WARN_ON(root->node->start !=
293                                         btrfs_root_bytenr(&root->root_item));
294                                 free_extent_buffer(root->commit_root);
295                                 root->commit_root = NULL;
296
297                                 /* make sure to update the root on disk
298                                  * so we get any updates to the block used
299                                  * counts
300                                  */
301                                 err = btrfs_update_root(trans,
302                                                 root->fs_info->tree_root,
303                                                 &root->root_key,
304                                                 &root->root_item);
305                                 continue;
306                         }
307                         dirty = kmalloc(sizeof(*dirty), GFP_NOFS);
308                         BUG_ON(!dirty);
309                         dirty->root = kmalloc(sizeof(*dirty->root), GFP_NOFS);
310                         BUG_ON(!dirty->root);
311
312                         memset(&root->root_item.drop_progress, 0,
313                                sizeof(struct btrfs_disk_key));
314                         root->root_item.drop_level = 0;
315
316                         memcpy(dirty->root, root, sizeof(*root));
317                         dirty->root->node = root->commit_root;
318                         dirty->latest_root = root;
319                         root->commit_root = NULL;
320
321                         root->root_key.offset = root->fs_info->generation;
322                         btrfs_set_root_bytenr(&root->root_item,
323                                               root->node->start);
324                         btrfs_set_root_level(&root->root_item,
325                                              btrfs_header_level(root->node));
326                         err = btrfs_insert_root(trans, root->fs_info->tree_root,
327                                                 &root->root_key,
328                                                 &root->root_item);
329                         if (err)
330                                 break;
331
332                         refs = btrfs_root_refs(&dirty->root->root_item);
333                         btrfs_set_root_refs(&dirty->root->root_item, refs - 1);
334                         err = btrfs_update_root(trans, root->fs_info->tree_root,
335                                                 &dirty->root->root_key,
336                                                 &dirty->root->root_item);
337
338                         BUG_ON(err);
339                         if (refs == 1) {
340                                 list_add(&dirty->list, list);
341                         } else {
342                                 WARN_ON(1);
343                                 kfree(dirty->root);
344                                 kfree(dirty);
345                         }
346                 }
347         }
348         return err;
349 }
350
351 int btrfs_defrag_root(struct btrfs_root *root, int cacheonly)
352 {
353         struct btrfs_fs_info *info = root->fs_info;
354         int ret;
355         struct btrfs_trans_handle *trans;
356         unsigned long nr;
357
358         if (root->defrag_running)
359                 return 0;
360         trans = btrfs_start_transaction(root, 1);
361         while (1) {
362                 root->defrag_running = 1;
363                 ret = btrfs_defrag_leaves(trans, root, cacheonly);
364                 nr = trans->blocks_used;
365                 btrfs_end_transaction(trans, root);
366                 mutex_unlock(&info->fs_mutex);
367                 btrfs_btree_balance_dirty(info->tree_root, nr);
368                 cond_resched();
369
370                 mutex_lock(&info->fs_mutex);
371                 trans = btrfs_start_transaction(root, 1);
372                 if (ret != -EAGAIN)
373                         break;
374         }
375         root->defrag_running = 0;
376         radix_tree_tag_clear(&info->fs_roots_radix,
377                      (unsigned long)root->root_key.objectid,
378                      BTRFS_ROOT_DEFRAG_TAG);
379         btrfs_end_transaction(trans, root);
380         return 0;
381 }
382
383 int btrfs_defrag_dirty_roots(struct btrfs_fs_info *info)
384 {
385         struct btrfs_root *gang[1];
386         struct btrfs_root *root;
387         int i;
388         int ret;
389         int err = 0;
390         u64 last = 0;
391
392         while(1) {
393                 ret = radix_tree_gang_lookup_tag(&info->fs_roots_radix,
394                                                  (void **)gang, last,
395                                                  ARRAY_SIZE(gang),
396                                                  BTRFS_ROOT_DEFRAG_TAG);
397                 if (ret == 0)
398                         break;
399                 for (i = 0; i < ret; i++) {
400                         root = gang[i];
401                         last = root->root_key.objectid + 1;
402                         btrfs_defrag_root(root, 1);
403                 }
404         }
405         btrfs_defrag_root(info->extent_root, 1);
406         return err;
407 }
408
409 static noinline int drop_dirty_roots(struct btrfs_root *tree_root,
410                                      struct list_head *list)
411 {
412         struct dirty_root *dirty;
413         struct btrfs_trans_handle *trans;
414         unsigned long nr;
415         u64 num_bytes;
416         u64 bytes_used;
417         int ret = 0;
418         int err;
419
420         while(!list_empty(list)) {
421                 struct btrfs_root *root;
422
423                 mutex_lock(&tree_root->fs_info->fs_mutex);
424                 dirty = list_entry(list->next, struct dirty_root, list);
425                 list_del_init(&dirty->list);
426
427                 num_bytes = btrfs_root_used(&dirty->root->root_item);
428                 root = dirty->latest_root;
429                 root->fs_info->throttles++;
430
431                 while(1) {
432                         trans = btrfs_start_transaction(tree_root, 1);
433                         ret = btrfs_drop_snapshot(trans, dirty->root);
434                         if (ret != -EAGAIN) {
435                                 break;
436                         }
437
438                         err = btrfs_update_root(trans,
439                                         tree_root,
440                                         &dirty->root->root_key,
441                                         &dirty->root->root_item);
442                         if (err)
443                                 ret = err;
444                         nr = trans->blocks_used;
445                         ret = btrfs_end_transaction(trans, tree_root);
446                         BUG_ON(ret);
447                         mutex_unlock(&tree_root->fs_info->fs_mutex);
448                         btrfs_btree_balance_dirty(tree_root, nr);
449                         cond_resched();
450                         mutex_lock(&tree_root->fs_info->fs_mutex);
451                 }
452                 BUG_ON(ret);
453                 root->fs_info->throttles--;
454
455                 num_bytes -= btrfs_root_used(&dirty->root->root_item);
456                 bytes_used = btrfs_root_used(&root->root_item);
457                 if (num_bytes) {
458                         record_root_in_trans(root);
459                         btrfs_set_root_used(&root->root_item,
460                                             bytes_used - num_bytes);
461                 }
462                 ret = btrfs_del_root(trans, tree_root, &dirty->root->root_key);
463                 if (ret) {
464                         BUG();
465                         break;
466                 }
467                 nr = trans->blocks_used;
468                 ret = btrfs_end_transaction(trans, tree_root);
469                 BUG_ON(ret);
470
471                 free_extent_buffer(dirty->root->node);
472                 kfree(dirty->root);
473                 kfree(dirty);
474                 mutex_unlock(&tree_root->fs_info->fs_mutex);
475
476                 btrfs_btree_balance_dirty(tree_root, nr);
477                 cond_resched();
478         }
479         return ret;
480 }
481
482 int btrfs_write_ordered_inodes(struct btrfs_trans_handle *trans,
483                                 struct btrfs_root *root)
484 {
485         struct btrfs_transaction *cur_trans = trans->transaction;
486         struct inode *inode;
487         u64 root_objectid = 0;
488         u64 objectid = 0;
489         int ret;
490
491         root->fs_info->throttles++;
492         while(1) {
493                 ret = btrfs_find_first_ordered_inode(
494                                 &cur_trans->ordered_inode_tree,
495                                 &root_objectid, &objectid, &inode);
496                 if (!ret)
497                         break;
498
499                 mutex_unlock(&root->fs_info->trans_mutex);
500                 mutex_unlock(&root->fs_info->fs_mutex);
501
502                 if (S_ISREG(inode->i_mode))
503                         filemap_fdatawrite(inode->i_mapping);
504                 iput(inode);
505
506                 mutex_lock(&root->fs_info->fs_mutex);
507                 mutex_lock(&root->fs_info->trans_mutex);
508         }
509         while(1) {
510                 root_objectid = 0;
511                 objectid = 0;
512                 ret = btrfs_find_del_first_ordered_inode(
513                                 &cur_trans->ordered_inode_tree,
514                                 &root_objectid, &objectid, &inode);
515                 if (!ret)
516                         break;
517                 mutex_unlock(&root->fs_info->trans_mutex);
518                 mutex_unlock(&root->fs_info->fs_mutex);
519
520                 if (S_ISREG(inode->i_mode))
521                         filemap_write_and_wait(inode->i_mapping);
522                 atomic_dec(&inode->i_count);
523                 iput(inode);
524
525                 mutex_lock(&root->fs_info->fs_mutex);
526                 mutex_lock(&root->fs_info->trans_mutex);
527         }
528         root->fs_info->throttles--;
529         return 0;
530 }
531
532 static noinline int create_pending_snapshot(struct btrfs_trans_handle *trans,
533                                    struct btrfs_fs_info *fs_info,
534                                    struct btrfs_pending_snapshot *pending)
535 {
536         struct btrfs_key key;
537         struct btrfs_root_item *new_root_item;
538         struct btrfs_root *tree_root = fs_info->tree_root;
539         struct btrfs_root *root = pending->root;
540         struct extent_buffer *tmp;
541         int ret;
542         u64 objectid;
543
544         new_root_item = kmalloc(sizeof(*new_root_item), GFP_NOFS);
545         if (!new_root_item) {
546                 ret = -ENOMEM;
547                 goto fail;
548         }
549         ret = btrfs_find_free_objectid(trans, tree_root, 0, &objectid);
550         if (ret)
551                 goto fail;
552
553         memcpy(new_root_item, &root->root_item, sizeof(*new_root_item));
554
555         key.objectid = objectid;
556         key.offset = 1;
557         btrfs_set_key_type(&key, BTRFS_ROOT_ITEM_KEY);
558
559         extent_buffer_get(root->node);
560         btrfs_cow_block(trans, root, root->node, NULL, 0, &tmp);
561         free_extent_buffer(tmp);
562
563         btrfs_copy_root(trans, root, root->node, &tmp, objectid);
564
565         btrfs_set_root_bytenr(new_root_item, tmp->start);
566         btrfs_set_root_level(new_root_item, btrfs_header_level(tmp));
567         ret = btrfs_insert_root(trans, root->fs_info->tree_root, &key,
568                                 new_root_item);
569         free_extent_buffer(tmp);
570         if (ret)
571                 goto fail;
572
573         /*
574          * insert the directory item
575          */
576         key.offset = (u64)-1;
577         ret = btrfs_insert_dir_item(trans, root->fs_info->tree_root,
578                                     pending->name, strlen(pending->name),
579                                     root->fs_info->sb->s_root->d_inode->i_ino,
580                                     &key, BTRFS_FT_DIR);
581
582         if (ret)
583                 goto fail;
584
585         ret = btrfs_insert_inode_ref(trans, root->fs_info->tree_root,
586                              pending->name, strlen(pending->name), objectid,
587                              root->fs_info->sb->s_root->d_inode->i_ino);
588 fail:
589         kfree(new_root_item);
590         return ret;
591 }
592
593 static noinline int create_pending_snapshots(struct btrfs_trans_handle *trans,
594                                              struct btrfs_fs_info *fs_info)
595 {
596         struct btrfs_pending_snapshot *pending;
597         struct list_head *head = &trans->transaction->pending_snapshots;
598         int ret;
599
600         while(!list_empty(head)) {
601                 pending = list_entry(head->next,
602                                      struct btrfs_pending_snapshot, list);
603                 ret = create_pending_snapshot(trans, fs_info, pending);
604                 BUG_ON(ret);
605                 list_del(&pending->list);
606                 kfree(pending->name);
607                 kfree(pending);
608         }
609         return 0;
610 }
611
612 int btrfs_commit_transaction(struct btrfs_trans_handle *trans,
613                              struct btrfs_root *root)
614 {
615         unsigned long joined = 0;
616         unsigned long timeout = 1;
617         struct btrfs_transaction *cur_trans;
618         struct btrfs_transaction *prev_trans = NULL;
619         struct list_head dirty_fs_roots;
620         struct extent_io_tree *pinned_copy;
621         DEFINE_WAIT(wait);
622         int ret;
623
624         INIT_LIST_HEAD(&dirty_fs_roots);
625
626         mutex_lock(&root->fs_info->trans_mutex);
627         if (trans->transaction->in_commit) {
628                 cur_trans = trans->transaction;
629                 trans->transaction->use_count++;
630                 mutex_unlock(&root->fs_info->trans_mutex);
631                 btrfs_end_transaction(trans, root);
632
633                 mutex_unlock(&root->fs_info->fs_mutex);
634                 ret = wait_for_commit(root, cur_trans);
635                 BUG_ON(ret);
636
637                 mutex_lock(&root->fs_info->trans_mutex);
638                 put_transaction(cur_trans);
639                 mutex_unlock(&root->fs_info->trans_mutex);
640
641                 mutex_lock(&root->fs_info->fs_mutex);
642                 return 0;
643         }
644
645         pinned_copy = kmalloc(sizeof(*pinned_copy), GFP_NOFS);
646         if (!pinned_copy)
647                 return -ENOMEM;
648
649         extent_io_tree_init(pinned_copy,
650                              root->fs_info->btree_inode->i_mapping, GFP_NOFS);
651
652         trans->transaction->in_commit = 1;
653         cur_trans = trans->transaction;
654         if (cur_trans->list.prev != &root->fs_info->trans_list) {
655                 prev_trans = list_entry(cur_trans->list.prev,
656                                         struct btrfs_transaction, list);
657                 if (!prev_trans->commit_done) {
658                         prev_trans->use_count++;
659                         mutex_unlock(&root->fs_info->fs_mutex);
660                         mutex_unlock(&root->fs_info->trans_mutex);
661
662                         wait_for_commit(root, prev_trans);
663
664                         mutex_lock(&root->fs_info->fs_mutex);
665                         mutex_lock(&root->fs_info->trans_mutex);
666                         put_transaction(prev_trans);
667                 }
668         }
669
670         do {
671                 joined = cur_trans->num_joined;
672                 WARN_ON(cur_trans != trans->transaction);
673                 prepare_to_wait(&cur_trans->writer_wait, &wait,
674                                 TASK_UNINTERRUPTIBLE);
675
676                 if (cur_trans->num_writers > 1)
677                         timeout = MAX_SCHEDULE_TIMEOUT;
678                 else
679                         timeout = 1;
680
681                 mutex_unlock(&root->fs_info->fs_mutex);
682                 mutex_unlock(&root->fs_info->trans_mutex);
683
684                 schedule_timeout(timeout);
685
686                 mutex_lock(&root->fs_info->fs_mutex);
687                 mutex_lock(&root->fs_info->trans_mutex);
688                 finish_wait(&cur_trans->writer_wait, &wait);
689                 ret = btrfs_write_ordered_inodes(trans, root);
690
691         } while (cur_trans->num_writers > 1 ||
692                  (cur_trans->num_joined != joined));
693
694         ret = create_pending_snapshots(trans, root->fs_info);
695         BUG_ON(ret);
696
697         WARN_ON(cur_trans != trans->transaction);
698
699         ret = add_dirty_roots(trans, &root->fs_info->fs_roots_radix,
700                               &dirty_fs_roots);
701         BUG_ON(ret);
702
703         ret = btrfs_commit_tree_roots(trans, root);
704         BUG_ON(ret);
705
706         cur_trans = root->fs_info->running_transaction;
707         spin_lock(&root->fs_info->new_trans_lock);
708         root->fs_info->running_transaction = NULL;
709         spin_unlock(&root->fs_info->new_trans_lock);
710         btrfs_set_super_generation(&root->fs_info->super_copy,
711                                    cur_trans->transid);
712         btrfs_set_super_root(&root->fs_info->super_copy,
713                              root->fs_info->tree_root->node->start);
714         btrfs_set_super_root_level(&root->fs_info->super_copy,
715                            btrfs_header_level(root->fs_info->tree_root->node));
716
717         write_extent_buffer(root->fs_info->sb_buffer,
718                             &root->fs_info->super_copy, 0,
719                             sizeof(root->fs_info->super_copy));
720
721         btrfs_copy_pinned(root, pinned_copy);
722
723         mutex_unlock(&root->fs_info->trans_mutex);
724         mutex_unlock(&root->fs_info->fs_mutex);
725         ret = btrfs_write_and_wait_transaction(trans, root);
726         BUG_ON(ret);
727         write_ctree_super(trans, root);
728
729         mutex_lock(&root->fs_info->fs_mutex);
730         btrfs_finish_extent_commit(trans, root, pinned_copy);
731         mutex_lock(&root->fs_info->trans_mutex);
732
733         kfree(pinned_copy);
734
735         cur_trans->commit_done = 1;
736         root->fs_info->last_trans_committed = cur_trans->transid;
737         wake_up(&cur_trans->commit_wait);
738         put_transaction(cur_trans);
739         put_transaction(cur_trans);
740
741         if (root->fs_info->closing)
742                 list_splice_init(&root->fs_info->dead_roots, &dirty_fs_roots);
743         else
744                 list_splice_init(&dirty_fs_roots, &root->fs_info->dead_roots);
745
746         mutex_unlock(&root->fs_info->trans_mutex);
747         kmem_cache_free(btrfs_trans_handle_cachep, trans);
748
749         if (root->fs_info->closing) {
750                 mutex_unlock(&root->fs_info->fs_mutex);
751                 drop_dirty_roots(root->fs_info->tree_root, &dirty_fs_roots);
752                 mutex_lock(&root->fs_info->fs_mutex);
753         }
754         return ret;
755 }
756
757 int btrfs_clean_old_snapshots(struct btrfs_root *root)
758 {
759         struct list_head dirty_roots;
760         INIT_LIST_HEAD(&dirty_roots);
761
762         mutex_lock(&root->fs_info->trans_mutex);
763         list_splice_init(&root->fs_info->dead_roots, &dirty_roots);
764         mutex_unlock(&root->fs_info->trans_mutex);
765
766         if (!list_empty(&dirty_roots)) {
767                 drop_dirty_roots(root, &dirty_roots);
768         }
769         return 0;
770 }
771 #if LINUX_VERSION_CODE <= KERNEL_VERSION(2,6,18)
772 void btrfs_transaction_cleaner(void *p)
773 #else
774 void btrfs_transaction_cleaner(struct work_struct *work)
775 #endif
776 {
777 #if LINUX_VERSION_CODE <= KERNEL_VERSION(2,6,18)
778         struct btrfs_fs_info *fs_info = p;
779 #else
780         struct btrfs_fs_info *fs_info = container_of(work,
781                                                      struct btrfs_fs_info,
782                                                      trans_work.work);
783
784 #endif
785         struct btrfs_root *root = fs_info->tree_root;
786         struct btrfs_transaction *cur;
787         struct btrfs_trans_handle *trans;
788         unsigned long now;
789         unsigned long delay = HZ * 30;
790         int ret;
791
792         mutex_lock(&root->fs_info->fs_mutex);
793         mutex_lock(&root->fs_info->trans_mutex);
794         cur = root->fs_info->running_transaction;
795         if (!cur) {
796                 mutex_unlock(&root->fs_info->trans_mutex);
797                 goto out;
798         }
799         now = get_seconds();
800         if (now < cur->start_time || now - cur->start_time < 30) {
801                 mutex_unlock(&root->fs_info->trans_mutex);
802                 delay = HZ * 5;
803                 goto out;
804         }
805         mutex_unlock(&root->fs_info->trans_mutex);
806         btrfs_defrag_dirty_roots(root->fs_info);
807         trans = btrfs_start_transaction(root, 1);
808         ret = btrfs_commit_transaction(trans, root);
809 out:
810         mutex_unlock(&root->fs_info->fs_mutex);
811         btrfs_clean_old_snapshots(root);
812         btrfs_transaction_queue_work(root, delay);
813 }
814
815 void btrfs_transaction_queue_work(struct btrfs_root *root, int delay)
816 {
817         queue_delayed_work(trans_wq, &root->fs_info->trans_work, delay);
818 }
819
820 void btrfs_transaction_flush_work(struct btrfs_root *root)
821 {
822         cancel_rearming_delayed_workqueue(trans_wq, &root->fs_info->trans_work);
823         flush_workqueue(trans_wq);
824 }
825
826 void __init btrfs_init_transaction_sys(void)
827 {
828         trans_wq = create_workqueue("btrfs");
829 }
830
831 void btrfs_exit_transaction_sys(void)
832 {
833         destroy_workqueue(trans_wq);
834 }
835