btrfs: drop unused argument from extent_io_tree_init
[pandora-kernel.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/slab.h>
21 #include <linux/sched.h>
22 #include <linux/writeback.h>
23 #include <linux/pagemap.h>
24 #include <linux/blkdev.h>
25 #include "ctree.h"
26 #include "disk-io.h"
27 #include "transaction.h"
28 #include "locking.h"
29 #include "tree-log.h"
30
31 #define BTRFS_ROOT_TRANS_TAG 0
32
33 static noinline void put_transaction(struct btrfs_transaction *transaction)
34 {
35         WARN_ON(atomic_read(&transaction->use_count) == 0);
36         if (atomic_dec_and_test(&transaction->use_count)) {
37                 memset(transaction, 0, sizeof(*transaction));
38                 kmem_cache_free(btrfs_transaction_cachep, transaction);
39         }
40 }
41
42 static noinline void switch_commit_root(struct btrfs_root *root)
43 {
44         free_extent_buffer(root->commit_root);
45         root->commit_root = btrfs_root_node(root);
46 }
47
48 /*
49  * either allocate a new transaction or hop into the existing one
50  */
51 static noinline int join_transaction(struct btrfs_root *root)
52 {
53         struct btrfs_transaction *cur_trans;
54         cur_trans = root->fs_info->running_transaction;
55         if (!cur_trans) {
56                 cur_trans = kmem_cache_alloc(btrfs_transaction_cachep,
57                                              GFP_NOFS);
58                 if (!cur_trans)
59                         return -ENOMEM;
60                 root->fs_info->generation++;
61                 atomic_set(&cur_trans->num_writers, 1);
62                 cur_trans->num_joined = 0;
63                 cur_trans->transid = root->fs_info->generation;
64                 init_waitqueue_head(&cur_trans->writer_wait);
65                 init_waitqueue_head(&cur_trans->commit_wait);
66                 cur_trans->in_commit = 0;
67                 cur_trans->blocked = 0;
68                 atomic_set(&cur_trans->use_count, 1);
69                 cur_trans->commit_done = 0;
70                 cur_trans->start_time = get_seconds();
71
72                 cur_trans->delayed_refs.root = RB_ROOT;
73                 cur_trans->delayed_refs.num_entries = 0;
74                 cur_trans->delayed_refs.num_heads_ready = 0;
75                 cur_trans->delayed_refs.num_heads = 0;
76                 cur_trans->delayed_refs.flushing = 0;
77                 cur_trans->delayed_refs.run_delayed_start = 0;
78                 spin_lock_init(&cur_trans->delayed_refs.lock);
79
80                 INIT_LIST_HEAD(&cur_trans->pending_snapshots);
81                 list_add_tail(&cur_trans->list, &root->fs_info->trans_list);
82                 extent_io_tree_init(&cur_trans->dirty_pages,
83                                      root->fs_info->btree_inode->i_mapping);
84                 spin_lock(&root->fs_info->new_trans_lock);
85                 root->fs_info->running_transaction = cur_trans;
86                 spin_unlock(&root->fs_info->new_trans_lock);
87         } else {
88                 atomic_inc(&cur_trans->num_writers);
89                 cur_trans->num_joined++;
90         }
91
92         return 0;
93 }
94
95 /*
96  * this does all the record keeping required to make sure that a reference
97  * counted root is properly recorded in a given transaction.  This is required
98  * to make sure the old root from before we joined the transaction is deleted
99  * when the transaction commits
100  */
101 static noinline int record_root_in_trans(struct btrfs_trans_handle *trans,
102                                          struct btrfs_root *root)
103 {
104         if (root->ref_cows && root->last_trans < trans->transid) {
105                 WARN_ON(root == root->fs_info->extent_root);
106                 WARN_ON(root->commit_root != root->node);
107
108                 radix_tree_tag_set(&root->fs_info->fs_roots_radix,
109                            (unsigned long)root->root_key.objectid,
110                            BTRFS_ROOT_TRANS_TAG);
111                 root->last_trans = trans->transid;
112                 btrfs_init_reloc_root(trans, root);
113         }
114         return 0;
115 }
116
117 int btrfs_record_root_in_trans(struct btrfs_trans_handle *trans,
118                                struct btrfs_root *root)
119 {
120         if (!root->ref_cows)
121                 return 0;
122
123         mutex_lock(&root->fs_info->trans_mutex);
124         if (root->last_trans == trans->transid) {
125                 mutex_unlock(&root->fs_info->trans_mutex);
126                 return 0;
127         }
128
129         record_root_in_trans(trans, root);
130         mutex_unlock(&root->fs_info->trans_mutex);
131         return 0;
132 }
133
134 /* wait for commit against the current transaction to become unblocked
135  * when this is done, it is safe to start a new transaction, but the current
136  * transaction might not be fully on disk.
137  */
138 static void wait_current_trans(struct btrfs_root *root)
139 {
140         struct btrfs_transaction *cur_trans;
141
142         cur_trans = root->fs_info->running_transaction;
143         if (cur_trans && cur_trans->blocked) {
144                 DEFINE_WAIT(wait);
145                 atomic_inc(&cur_trans->use_count);
146                 while (1) {
147                         prepare_to_wait(&root->fs_info->transaction_wait, &wait,
148                                         TASK_UNINTERRUPTIBLE);
149                         if (!cur_trans->blocked)
150                                 break;
151                         mutex_unlock(&root->fs_info->trans_mutex);
152                         schedule();
153                         mutex_lock(&root->fs_info->trans_mutex);
154                 }
155                 finish_wait(&root->fs_info->transaction_wait, &wait);
156                 put_transaction(cur_trans);
157         }
158 }
159
160 enum btrfs_trans_type {
161         TRANS_START,
162         TRANS_JOIN,
163         TRANS_USERSPACE,
164         TRANS_JOIN_NOLOCK,
165 };
166
167 static int may_wait_transaction(struct btrfs_root *root, int type)
168 {
169         if (!root->fs_info->log_root_recovering &&
170             ((type == TRANS_START && !root->fs_info->open_ioctl_trans) ||
171              type == TRANS_USERSPACE))
172                 return 1;
173         return 0;
174 }
175
176 static struct btrfs_trans_handle *start_transaction(struct btrfs_root *root,
177                                                     u64 num_items, int type)
178 {
179         struct btrfs_trans_handle *h;
180         struct btrfs_transaction *cur_trans;
181         int retries = 0;
182         int ret;
183
184         if (root->fs_info->fs_state & BTRFS_SUPER_FLAG_ERROR)
185                 return ERR_PTR(-EROFS);
186 again:
187         h = kmem_cache_alloc(btrfs_trans_handle_cachep, GFP_NOFS);
188         if (!h)
189                 return ERR_PTR(-ENOMEM);
190
191         if (type != TRANS_JOIN_NOLOCK)
192                 mutex_lock(&root->fs_info->trans_mutex);
193         if (may_wait_transaction(root, type))
194                 wait_current_trans(root);
195
196         ret = join_transaction(root);
197         if (ret < 0) {
198                 kmem_cache_free(btrfs_trans_handle_cachep, h);
199                 if (type != TRANS_JOIN_NOLOCK)
200                         mutex_unlock(&root->fs_info->trans_mutex);
201                 return ERR_PTR(ret);
202         }
203
204         cur_trans = root->fs_info->running_transaction;
205         atomic_inc(&cur_trans->use_count);
206         if (type != TRANS_JOIN_NOLOCK)
207                 mutex_unlock(&root->fs_info->trans_mutex);
208
209         h->transid = cur_trans->transid;
210         h->transaction = cur_trans;
211         h->blocks_used = 0;
212         h->block_group = 0;
213         h->bytes_reserved = 0;
214         h->delayed_ref_updates = 0;
215         h->block_rsv = NULL;
216
217         smp_mb();
218         if (cur_trans->blocked && may_wait_transaction(root, type)) {
219                 btrfs_commit_transaction(h, root);
220                 goto again;
221         }
222
223         if (num_items > 0) {
224                 ret = btrfs_trans_reserve_metadata(h, root, num_items);
225                 if (ret == -EAGAIN && !retries) {
226                         retries++;
227                         btrfs_commit_transaction(h, root);
228                         goto again;
229                 } else if (ret == -EAGAIN) {
230                         /*
231                          * We have already retried and got EAGAIN, so really we
232                          * don't have space, so set ret to -ENOSPC.
233                          */
234                         ret = -ENOSPC;
235                 }
236
237                 if (ret < 0) {
238                         btrfs_end_transaction(h, root);
239                         return ERR_PTR(ret);
240                 }
241         }
242
243         if (type != TRANS_JOIN_NOLOCK)
244                 mutex_lock(&root->fs_info->trans_mutex);
245         record_root_in_trans(h, root);
246         if (type != TRANS_JOIN_NOLOCK)
247                 mutex_unlock(&root->fs_info->trans_mutex);
248
249         if (!current->journal_info && type != TRANS_USERSPACE)
250                 current->journal_info = h;
251         return h;
252 }
253
254 struct btrfs_trans_handle *btrfs_start_transaction(struct btrfs_root *root,
255                                                    int num_items)
256 {
257         return start_transaction(root, num_items, TRANS_START);
258 }
259 struct btrfs_trans_handle *btrfs_join_transaction(struct btrfs_root *root,
260                                                    int num_blocks)
261 {
262         return start_transaction(root, 0, TRANS_JOIN);
263 }
264
265 struct btrfs_trans_handle *btrfs_join_transaction_nolock(struct btrfs_root *root,
266                                                           int num_blocks)
267 {
268         return start_transaction(root, 0, TRANS_JOIN_NOLOCK);
269 }
270
271 struct btrfs_trans_handle *btrfs_start_ioctl_transaction(struct btrfs_root *r,
272                                                          int num_blocks)
273 {
274         return start_transaction(r, 0, TRANS_USERSPACE);
275 }
276
277 /* wait for a transaction commit to be fully complete */
278 static noinline int wait_for_commit(struct btrfs_root *root,
279                                     struct btrfs_transaction *commit)
280 {
281         DEFINE_WAIT(wait);
282         mutex_lock(&root->fs_info->trans_mutex);
283         while (!commit->commit_done) {
284                 prepare_to_wait(&commit->commit_wait, &wait,
285                                 TASK_UNINTERRUPTIBLE);
286                 if (commit->commit_done)
287                         break;
288                 mutex_unlock(&root->fs_info->trans_mutex);
289                 schedule();
290                 mutex_lock(&root->fs_info->trans_mutex);
291         }
292         mutex_unlock(&root->fs_info->trans_mutex);
293         finish_wait(&commit->commit_wait, &wait);
294         return 0;
295 }
296
297 int btrfs_wait_for_commit(struct btrfs_root *root, u64 transid)
298 {
299         struct btrfs_transaction *cur_trans = NULL, *t;
300         int ret;
301
302         mutex_lock(&root->fs_info->trans_mutex);
303
304         ret = 0;
305         if (transid) {
306                 if (transid <= root->fs_info->last_trans_committed)
307                         goto out_unlock;
308
309                 /* find specified transaction */
310                 list_for_each_entry(t, &root->fs_info->trans_list, list) {
311                         if (t->transid == transid) {
312                                 cur_trans = t;
313                                 break;
314                         }
315                         if (t->transid > transid)
316                                 break;
317                 }
318                 ret = -EINVAL;
319                 if (!cur_trans)
320                         goto out_unlock;  /* bad transid */
321         } else {
322                 /* find newest transaction that is committing | committed */
323                 list_for_each_entry_reverse(t, &root->fs_info->trans_list,
324                                             list) {
325                         if (t->in_commit) {
326                                 if (t->commit_done)
327                                         goto out_unlock;
328                                 cur_trans = t;
329                                 break;
330                         }
331                 }
332                 if (!cur_trans)
333                         goto out_unlock;  /* nothing committing|committed */
334         }
335
336         atomic_inc(&cur_trans->use_count);
337         mutex_unlock(&root->fs_info->trans_mutex);
338
339         wait_for_commit(root, cur_trans);
340
341         mutex_lock(&root->fs_info->trans_mutex);
342         put_transaction(cur_trans);
343         ret = 0;
344 out_unlock:
345         mutex_unlock(&root->fs_info->trans_mutex);
346         return ret;
347 }
348
349 #if 0
350 /*
351  * rate limit against the drop_snapshot code.  This helps to slow down new
352  * operations if the drop_snapshot code isn't able to keep up.
353  */
354 static void throttle_on_drops(struct btrfs_root *root)
355 {
356         struct btrfs_fs_info *info = root->fs_info;
357         int harder_count = 0;
358
359 harder:
360         if (atomic_read(&info->throttles)) {
361                 DEFINE_WAIT(wait);
362                 int thr;
363                 thr = atomic_read(&info->throttle_gen);
364
365                 do {
366                         prepare_to_wait(&info->transaction_throttle,
367                                         &wait, TASK_UNINTERRUPTIBLE);
368                         if (!atomic_read(&info->throttles)) {
369                                 finish_wait(&info->transaction_throttle, &wait);
370                                 break;
371                         }
372                         schedule();
373                         finish_wait(&info->transaction_throttle, &wait);
374                 } while (thr == atomic_read(&info->throttle_gen));
375                 harder_count++;
376
377                 if (root->fs_info->total_ref_cache_size > 1 * 1024 * 1024 &&
378                     harder_count < 2)
379                         goto harder;
380
381                 if (root->fs_info->total_ref_cache_size > 5 * 1024 * 1024 &&
382                     harder_count < 10)
383                         goto harder;
384
385                 if (root->fs_info->total_ref_cache_size > 10 * 1024 * 1024 &&
386                     harder_count < 20)
387                         goto harder;
388         }
389 }
390 #endif
391
392 void btrfs_throttle(struct btrfs_root *root)
393 {
394         mutex_lock(&root->fs_info->trans_mutex);
395         if (!root->fs_info->open_ioctl_trans)
396                 wait_current_trans(root);
397         mutex_unlock(&root->fs_info->trans_mutex);
398 }
399
400 static int should_end_transaction(struct btrfs_trans_handle *trans,
401                                   struct btrfs_root *root)
402 {
403         int ret;
404         ret = btrfs_block_rsv_check(trans, root,
405                                     &root->fs_info->global_block_rsv, 0, 5);
406         return ret ? 1 : 0;
407 }
408
409 int btrfs_should_end_transaction(struct btrfs_trans_handle *trans,
410                                  struct btrfs_root *root)
411 {
412         struct btrfs_transaction *cur_trans = trans->transaction;
413         int updates;
414
415         if (cur_trans->blocked || cur_trans->delayed_refs.flushing)
416                 return 1;
417
418         updates = trans->delayed_ref_updates;
419         trans->delayed_ref_updates = 0;
420         if (updates)
421                 btrfs_run_delayed_refs(trans, root, updates);
422
423         return should_end_transaction(trans, root);
424 }
425
426 static int __btrfs_end_transaction(struct btrfs_trans_handle *trans,
427                           struct btrfs_root *root, int throttle, int lock)
428 {
429         struct btrfs_transaction *cur_trans = trans->transaction;
430         struct btrfs_fs_info *info = root->fs_info;
431         int count = 0;
432
433         while (count < 4) {
434                 unsigned long cur = trans->delayed_ref_updates;
435                 trans->delayed_ref_updates = 0;
436                 if (cur &&
437                     trans->transaction->delayed_refs.num_heads_ready > 64) {
438                         trans->delayed_ref_updates = 0;
439
440                         /*
441                          * do a full flush if the transaction is trying
442                          * to close
443                          */
444                         if (trans->transaction->delayed_refs.flushing)
445                                 cur = 0;
446                         btrfs_run_delayed_refs(trans, root, cur);
447                 } else {
448                         break;
449                 }
450                 count++;
451         }
452
453         btrfs_trans_release_metadata(trans, root);
454
455         if (lock && !root->fs_info->open_ioctl_trans &&
456             should_end_transaction(trans, root))
457                 trans->transaction->blocked = 1;
458
459         if (lock && cur_trans->blocked && !cur_trans->in_commit) {
460                 if (throttle)
461                         return btrfs_commit_transaction(trans, root);
462                 else
463                         wake_up_process(info->transaction_kthread);
464         }
465
466         WARN_ON(cur_trans != info->running_transaction);
467         WARN_ON(atomic_read(&cur_trans->num_writers) < 1);
468         atomic_dec(&cur_trans->num_writers);
469
470         smp_mb();
471         if (waitqueue_active(&cur_trans->writer_wait))
472                 wake_up(&cur_trans->writer_wait);
473         put_transaction(cur_trans);
474
475         if (current->journal_info == trans)
476                 current->journal_info = NULL;
477         memset(trans, 0, sizeof(*trans));
478         kmem_cache_free(btrfs_trans_handle_cachep, trans);
479
480         if (throttle)
481                 btrfs_run_delayed_iputs(root);
482
483         return 0;
484 }
485
486 int btrfs_end_transaction(struct btrfs_trans_handle *trans,
487                           struct btrfs_root *root)
488 {
489         return __btrfs_end_transaction(trans, root, 0, 1);
490 }
491
492 int btrfs_end_transaction_throttle(struct btrfs_trans_handle *trans,
493                                    struct btrfs_root *root)
494 {
495         return __btrfs_end_transaction(trans, root, 1, 1);
496 }
497
498 int btrfs_end_transaction_nolock(struct btrfs_trans_handle *trans,
499                                  struct btrfs_root *root)
500 {
501         return __btrfs_end_transaction(trans, root, 0, 0);
502 }
503
504 /*
505  * when btree blocks are allocated, they have some corresponding bits set for
506  * them in one of two extent_io trees.  This is used to make sure all of
507  * those extents are sent to disk but does not wait on them
508  */
509 int btrfs_write_marked_extents(struct btrfs_root *root,
510                                struct extent_io_tree *dirty_pages, int mark)
511 {
512         int ret;
513         int err = 0;
514         int werr = 0;
515         struct page *page;
516         struct inode *btree_inode = root->fs_info->btree_inode;
517         u64 start = 0;
518         u64 end;
519         unsigned long index;
520
521         while (1) {
522                 ret = find_first_extent_bit(dirty_pages, start, &start, &end,
523                                             mark);
524                 if (ret)
525                         break;
526                 while (start <= end) {
527                         cond_resched();
528
529                         index = start >> PAGE_CACHE_SHIFT;
530                         start = (u64)(index + 1) << PAGE_CACHE_SHIFT;
531                         page = find_get_page(btree_inode->i_mapping, index);
532                         if (!page)
533                                 continue;
534
535                         btree_lock_page_hook(page);
536                         if (!page->mapping) {
537                                 unlock_page(page);
538                                 page_cache_release(page);
539                                 continue;
540                         }
541
542                         if (PageWriteback(page)) {
543                                 if (PageDirty(page))
544                                         wait_on_page_writeback(page);
545                                 else {
546                                         unlock_page(page);
547                                         page_cache_release(page);
548                                         continue;
549                                 }
550                         }
551                         err = write_one_page(page, 0);
552                         if (err)
553                                 werr = err;
554                         page_cache_release(page);
555                 }
556         }
557         if (err)
558                 werr = err;
559         return werr;
560 }
561
562 /*
563  * when btree blocks are allocated, they have some corresponding bits set for
564  * them in one of two extent_io trees.  This is used to make sure all of
565  * those extents are on disk for transaction or log commit.  We wait
566  * on all the pages and clear them from the dirty pages state tree
567  */
568 int btrfs_wait_marked_extents(struct btrfs_root *root,
569                               struct extent_io_tree *dirty_pages, int mark)
570 {
571         int ret;
572         int err = 0;
573         int werr = 0;
574         struct page *page;
575         struct inode *btree_inode = root->fs_info->btree_inode;
576         u64 start = 0;
577         u64 end;
578         unsigned long index;
579
580         while (1) {
581                 ret = find_first_extent_bit(dirty_pages, start, &start, &end,
582                                             mark);
583                 if (ret)
584                         break;
585
586                 clear_extent_bits(dirty_pages, start, end, mark, GFP_NOFS);
587                 while (start <= end) {
588                         index = start >> PAGE_CACHE_SHIFT;
589                         start = (u64)(index + 1) << PAGE_CACHE_SHIFT;
590                         page = find_get_page(btree_inode->i_mapping, index);
591                         if (!page)
592                                 continue;
593                         if (PageDirty(page)) {
594                                 btree_lock_page_hook(page);
595                                 wait_on_page_writeback(page);
596                                 err = write_one_page(page, 0);
597                                 if (err)
598                                         werr = err;
599                         }
600                         wait_on_page_writeback(page);
601                         page_cache_release(page);
602                         cond_resched();
603                 }
604         }
605         if (err)
606                 werr = err;
607         return werr;
608 }
609
610 /*
611  * when btree blocks are allocated, they have some corresponding bits set for
612  * them in one of two extent_io trees.  This is used to make sure all of
613  * those extents are on disk for transaction or log commit
614  */
615 int btrfs_write_and_wait_marked_extents(struct btrfs_root *root,
616                                 struct extent_io_tree *dirty_pages, int mark)
617 {
618         int ret;
619         int ret2;
620
621         ret = btrfs_write_marked_extents(root, dirty_pages, mark);
622         ret2 = btrfs_wait_marked_extents(root, dirty_pages, mark);
623         return ret || ret2;
624 }
625
626 int btrfs_write_and_wait_transaction(struct btrfs_trans_handle *trans,
627                                      struct btrfs_root *root)
628 {
629         if (!trans || !trans->transaction) {
630                 struct inode *btree_inode;
631                 btree_inode = root->fs_info->btree_inode;
632                 return filemap_write_and_wait(btree_inode->i_mapping);
633         }
634         return btrfs_write_and_wait_marked_extents(root,
635                                            &trans->transaction->dirty_pages,
636                                            EXTENT_DIRTY);
637 }
638
639 /*
640  * this is used to update the root pointer in the tree of tree roots.
641  *
642  * But, in the case of the extent allocation tree, updating the root
643  * pointer may allocate blocks which may change the root of the extent
644  * allocation tree.
645  *
646  * So, this loops and repeats and makes sure the cowonly root didn't
647  * change while the root pointer was being updated in the metadata.
648  */
649 static int update_cowonly_root(struct btrfs_trans_handle *trans,
650                                struct btrfs_root *root)
651 {
652         int ret;
653         u64 old_root_bytenr;
654         u64 old_root_used;
655         struct btrfs_root *tree_root = root->fs_info->tree_root;
656
657         old_root_used = btrfs_root_used(&root->root_item);
658         btrfs_write_dirty_block_groups(trans, root);
659
660         while (1) {
661                 old_root_bytenr = btrfs_root_bytenr(&root->root_item);
662                 if (old_root_bytenr == root->node->start &&
663                     old_root_used == btrfs_root_used(&root->root_item))
664                         break;
665
666                 btrfs_set_root_node(&root->root_item, root->node);
667                 ret = btrfs_update_root(trans, tree_root,
668                                         &root->root_key,
669                                         &root->root_item);
670                 BUG_ON(ret);
671
672                 old_root_used = btrfs_root_used(&root->root_item);
673                 ret = btrfs_write_dirty_block_groups(trans, root);
674                 BUG_ON(ret);
675         }
676
677         if (root != root->fs_info->extent_root)
678                 switch_commit_root(root);
679
680         return 0;
681 }
682
683 /*
684  * update all the cowonly tree roots on disk
685  */
686 static noinline int commit_cowonly_roots(struct btrfs_trans_handle *trans,
687                                          struct btrfs_root *root)
688 {
689         struct btrfs_fs_info *fs_info = root->fs_info;
690         struct list_head *next;
691         struct extent_buffer *eb;
692         int ret;
693
694         ret = btrfs_run_delayed_refs(trans, root, (unsigned long)-1);
695         BUG_ON(ret);
696
697         eb = btrfs_lock_root_node(fs_info->tree_root);
698         btrfs_cow_block(trans, fs_info->tree_root, eb, NULL, 0, &eb);
699         btrfs_tree_unlock(eb);
700         free_extent_buffer(eb);
701
702         ret = btrfs_run_delayed_refs(trans, root, (unsigned long)-1);
703         BUG_ON(ret);
704
705         while (!list_empty(&fs_info->dirty_cowonly_roots)) {
706                 next = fs_info->dirty_cowonly_roots.next;
707                 list_del_init(next);
708                 root = list_entry(next, struct btrfs_root, dirty_list);
709
710                 update_cowonly_root(trans, root);
711         }
712
713         down_write(&fs_info->extent_commit_sem);
714         switch_commit_root(fs_info->extent_root);
715         up_write(&fs_info->extent_commit_sem);
716
717         return 0;
718 }
719
720 /*
721  * dead roots are old snapshots that need to be deleted.  This allocates
722  * a dirty root struct and adds it into the list of dead roots that need to
723  * be deleted
724  */
725 int btrfs_add_dead_root(struct btrfs_root *root)
726 {
727         mutex_lock(&root->fs_info->trans_mutex);
728         list_add(&root->root_list, &root->fs_info->dead_roots);
729         mutex_unlock(&root->fs_info->trans_mutex);
730         return 0;
731 }
732
733 /*
734  * update all the cowonly tree roots on disk
735  */
736 static noinline int commit_fs_roots(struct btrfs_trans_handle *trans,
737                                     struct btrfs_root *root)
738 {
739         struct btrfs_root *gang[8];
740         struct btrfs_fs_info *fs_info = root->fs_info;
741         int i;
742         int ret;
743         int err = 0;
744
745         while (1) {
746                 ret = radix_tree_gang_lookup_tag(&fs_info->fs_roots_radix,
747                                                  (void **)gang, 0,
748                                                  ARRAY_SIZE(gang),
749                                                  BTRFS_ROOT_TRANS_TAG);
750                 if (ret == 0)
751                         break;
752                 for (i = 0; i < ret; i++) {
753                         root = gang[i];
754                         radix_tree_tag_clear(&fs_info->fs_roots_radix,
755                                         (unsigned long)root->root_key.objectid,
756                                         BTRFS_ROOT_TRANS_TAG);
757
758                         btrfs_free_log(trans, root);
759                         btrfs_update_reloc_root(trans, root);
760                         btrfs_orphan_commit_root(trans, root);
761
762                         if (root->commit_root != root->node) {
763                                 switch_commit_root(root);
764                                 btrfs_set_root_node(&root->root_item,
765                                                     root->node);
766                         }
767
768                         err = btrfs_update_root(trans, fs_info->tree_root,
769                                                 &root->root_key,
770                                                 &root->root_item);
771                         if (err)
772                                 break;
773                 }
774         }
775         return err;
776 }
777
778 /*
779  * defrag a given btree.  If cacheonly == 1, this won't read from the disk,
780  * otherwise every leaf in the btree is read and defragged.
781  */
782 int btrfs_defrag_root(struct btrfs_root *root, int cacheonly)
783 {
784         struct btrfs_fs_info *info = root->fs_info;
785         struct btrfs_trans_handle *trans;
786         int ret;
787         unsigned long nr;
788
789         if (xchg(&root->defrag_running, 1))
790                 return 0;
791
792         while (1) {
793                 trans = btrfs_start_transaction(root, 0);
794                 if (IS_ERR(trans))
795                         return PTR_ERR(trans);
796
797                 ret = btrfs_defrag_leaves(trans, root, cacheonly);
798
799                 nr = trans->blocks_used;
800                 btrfs_end_transaction(trans, root);
801                 btrfs_btree_balance_dirty(info->tree_root, nr);
802                 cond_resched();
803
804                 if (root->fs_info->closing || ret != -EAGAIN)
805                         break;
806         }
807         root->defrag_running = 0;
808         return ret;
809 }
810
811 #if 0
812 /*
813  * when dropping snapshots, we generate a ton of delayed refs, and it makes
814  * sense not to join the transaction while it is trying to flush the current
815  * queue of delayed refs out.
816  *
817  * This is used by the drop snapshot code only
818  */
819 static noinline int wait_transaction_pre_flush(struct btrfs_fs_info *info)
820 {
821         DEFINE_WAIT(wait);
822
823         mutex_lock(&info->trans_mutex);
824         while (info->running_transaction &&
825                info->running_transaction->delayed_refs.flushing) {
826                 prepare_to_wait(&info->transaction_wait, &wait,
827                                 TASK_UNINTERRUPTIBLE);
828                 mutex_unlock(&info->trans_mutex);
829
830                 schedule();
831
832                 mutex_lock(&info->trans_mutex);
833                 finish_wait(&info->transaction_wait, &wait);
834         }
835         mutex_unlock(&info->trans_mutex);
836         return 0;
837 }
838
839 /*
840  * Given a list of roots that need to be deleted, call btrfs_drop_snapshot on
841  * all of them
842  */
843 int btrfs_drop_dead_root(struct btrfs_root *root)
844 {
845         struct btrfs_trans_handle *trans;
846         struct btrfs_root *tree_root = root->fs_info->tree_root;
847         unsigned long nr;
848         int ret;
849
850         while (1) {
851                 /*
852                  * we don't want to jump in and create a bunch of
853                  * delayed refs if the transaction is starting to close
854                  */
855                 wait_transaction_pre_flush(tree_root->fs_info);
856                 trans = btrfs_start_transaction(tree_root, 1);
857
858                 /*
859                  * we've joined a transaction, make sure it isn't
860                  * closing right now
861                  */
862                 if (trans->transaction->delayed_refs.flushing) {
863                         btrfs_end_transaction(trans, tree_root);
864                         continue;
865                 }
866
867                 ret = btrfs_drop_snapshot(trans, root);
868                 if (ret != -EAGAIN)
869                         break;
870
871                 ret = btrfs_update_root(trans, tree_root,
872                                         &root->root_key,
873                                         &root->root_item);
874                 if (ret)
875                         break;
876
877                 nr = trans->blocks_used;
878                 ret = btrfs_end_transaction(trans, tree_root);
879                 BUG_ON(ret);
880
881                 btrfs_btree_balance_dirty(tree_root, nr);
882                 cond_resched();
883         }
884         BUG_ON(ret);
885
886         ret = btrfs_del_root(trans, tree_root, &root->root_key);
887         BUG_ON(ret);
888
889         nr = trans->blocks_used;
890         ret = btrfs_end_transaction(trans, tree_root);
891         BUG_ON(ret);
892
893         free_extent_buffer(root->node);
894         free_extent_buffer(root->commit_root);
895         kfree(root);
896
897         btrfs_btree_balance_dirty(tree_root, nr);
898         return ret;
899 }
900 #endif
901
902 /*
903  * new snapshots need to be created at a very specific time in the
904  * transaction commit.  This does the actual creation
905  */
906 static noinline int create_pending_snapshot(struct btrfs_trans_handle *trans,
907                                    struct btrfs_fs_info *fs_info,
908                                    struct btrfs_pending_snapshot *pending)
909 {
910         struct btrfs_key key;
911         struct btrfs_root_item *new_root_item;
912         struct btrfs_root *tree_root = fs_info->tree_root;
913         struct btrfs_root *root = pending->root;
914         struct btrfs_root *parent_root;
915         struct inode *parent_inode;
916         struct dentry *parent;
917         struct dentry *dentry;
918         struct extent_buffer *tmp;
919         struct extent_buffer *old;
920         int ret;
921         u64 to_reserve = 0;
922         u64 index = 0;
923         u64 objectid;
924         u64 root_flags;
925
926         new_root_item = kmalloc(sizeof(*new_root_item), GFP_NOFS);
927         if (!new_root_item) {
928                 pending->error = -ENOMEM;
929                 goto fail;
930         }
931
932         ret = btrfs_find_free_objectid(trans, tree_root, 0, &objectid);
933         if (ret) {
934                 pending->error = ret;
935                 goto fail;
936         }
937
938         btrfs_reloc_pre_snapshot(trans, pending, &to_reserve);
939         btrfs_orphan_pre_snapshot(trans, pending, &to_reserve);
940
941         if (to_reserve > 0) {
942                 ret = btrfs_block_rsv_add(trans, root, &pending->block_rsv,
943                                           to_reserve);
944                 if (ret) {
945                         pending->error = ret;
946                         goto fail;
947                 }
948         }
949
950         key.objectid = objectid;
951         key.offset = (u64)-1;
952         key.type = BTRFS_ROOT_ITEM_KEY;
953
954         trans->block_rsv = &pending->block_rsv;
955
956         dentry = pending->dentry;
957         parent = dget_parent(dentry);
958         parent_inode = parent->d_inode;
959         parent_root = BTRFS_I(parent_inode)->root;
960         record_root_in_trans(trans, parent_root);
961
962         /*
963          * insert the directory item
964          */
965         ret = btrfs_set_inode_index(parent_inode, &index);
966         BUG_ON(ret);
967         ret = btrfs_insert_dir_item(trans, parent_root,
968                                 dentry->d_name.name, dentry->d_name.len,
969                                 parent_inode->i_ino, &key,
970                                 BTRFS_FT_DIR, index);
971         BUG_ON(ret);
972
973         btrfs_i_size_write(parent_inode, parent_inode->i_size +
974                                          dentry->d_name.len * 2);
975         ret = btrfs_update_inode(trans, parent_root, parent_inode);
976         BUG_ON(ret);
977
978         record_root_in_trans(trans, root);
979         btrfs_set_root_last_snapshot(&root->root_item, trans->transid);
980         memcpy(new_root_item, &root->root_item, sizeof(*new_root_item));
981         btrfs_check_and_init_root_item(new_root_item);
982
983         root_flags = btrfs_root_flags(new_root_item);
984         if (pending->readonly)
985                 root_flags |= BTRFS_ROOT_SUBVOL_RDONLY;
986         else
987                 root_flags &= ~BTRFS_ROOT_SUBVOL_RDONLY;
988         btrfs_set_root_flags(new_root_item, root_flags);
989
990         old = btrfs_lock_root_node(root);
991         btrfs_cow_block(trans, root, old, NULL, 0, &old);
992         btrfs_set_lock_blocking(old);
993
994         btrfs_copy_root(trans, root, old, &tmp, objectid);
995         btrfs_tree_unlock(old);
996         free_extent_buffer(old);
997
998         btrfs_set_root_node(new_root_item, tmp);
999         /* record when the snapshot was created in key.offset */
1000         key.offset = trans->transid;
1001         ret = btrfs_insert_root(trans, tree_root, &key, new_root_item);
1002         btrfs_tree_unlock(tmp);
1003         free_extent_buffer(tmp);
1004         BUG_ON(ret);
1005
1006         /*
1007          * insert root back/forward references
1008          */
1009         ret = btrfs_add_root_ref(trans, tree_root, objectid,
1010                                  parent_root->root_key.objectid,
1011                                  parent_inode->i_ino, index,
1012                                  dentry->d_name.name, dentry->d_name.len);
1013         BUG_ON(ret);
1014         dput(parent);
1015
1016         key.offset = (u64)-1;
1017         pending->snap = btrfs_read_fs_root_no_name(root->fs_info, &key);
1018         BUG_ON(IS_ERR(pending->snap));
1019
1020         btrfs_reloc_post_snapshot(trans, pending);
1021         btrfs_orphan_post_snapshot(trans, pending);
1022 fail:
1023         kfree(new_root_item);
1024         btrfs_block_rsv_release(root, &pending->block_rsv, (u64)-1);
1025         return 0;
1026 }
1027
1028 /*
1029  * create all the snapshots we've scheduled for creation
1030  */
1031 static noinline int create_pending_snapshots(struct btrfs_trans_handle *trans,
1032                                              struct btrfs_fs_info *fs_info)
1033 {
1034         struct btrfs_pending_snapshot *pending;
1035         struct list_head *head = &trans->transaction->pending_snapshots;
1036         int ret;
1037
1038         list_for_each_entry(pending, head, list) {
1039                 ret = create_pending_snapshot(trans, fs_info, pending);
1040                 BUG_ON(ret);
1041         }
1042         return 0;
1043 }
1044
1045 static void update_super_roots(struct btrfs_root *root)
1046 {
1047         struct btrfs_root_item *root_item;
1048         struct btrfs_super_block *super;
1049
1050         super = &root->fs_info->super_copy;
1051
1052         root_item = &root->fs_info->chunk_root->root_item;
1053         super->chunk_root = root_item->bytenr;
1054         super->chunk_root_generation = root_item->generation;
1055         super->chunk_root_level = root_item->level;
1056
1057         root_item = &root->fs_info->tree_root->root_item;
1058         super->root = root_item->bytenr;
1059         super->generation = root_item->generation;
1060         super->root_level = root_item->level;
1061         if (super->cache_generation != 0 || btrfs_test_opt(root, SPACE_CACHE))
1062                 super->cache_generation = root_item->generation;
1063 }
1064
1065 int btrfs_transaction_in_commit(struct btrfs_fs_info *info)
1066 {
1067         int ret = 0;
1068         spin_lock(&info->new_trans_lock);
1069         if (info->running_transaction)
1070                 ret = info->running_transaction->in_commit;
1071         spin_unlock(&info->new_trans_lock);
1072         return ret;
1073 }
1074
1075 int btrfs_transaction_blocked(struct btrfs_fs_info *info)
1076 {
1077         int ret = 0;
1078         spin_lock(&info->new_trans_lock);
1079         if (info->running_transaction)
1080                 ret = info->running_transaction->blocked;
1081         spin_unlock(&info->new_trans_lock);
1082         return ret;
1083 }
1084
1085 /*
1086  * wait for the current transaction commit to start and block subsequent
1087  * transaction joins
1088  */
1089 static void wait_current_trans_commit_start(struct btrfs_root *root,
1090                                             struct btrfs_transaction *trans)
1091 {
1092         DEFINE_WAIT(wait);
1093
1094         if (trans->in_commit)
1095                 return;
1096
1097         while (1) {
1098                 prepare_to_wait(&root->fs_info->transaction_blocked_wait, &wait,
1099                                 TASK_UNINTERRUPTIBLE);
1100                 if (trans->in_commit) {
1101                         finish_wait(&root->fs_info->transaction_blocked_wait,
1102                                     &wait);
1103                         break;
1104                 }
1105                 mutex_unlock(&root->fs_info->trans_mutex);
1106                 schedule();
1107                 mutex_lock(&root->fs_info->trans_mutex);
1108                 finish_wait(&root->fs_info->transaction_blocked_wait, &wait);
1109         }
1110 }
1111
1112 /*
1113  * wait for the current transaction to start and then become unblocked.
1114  * caller holds ref.
1115  */
1116 static void wait_current_trans_commit_start_and_unblock(struct btrfs_root *root,
1117                                          struct btrfs_transaction *trans)
1118 {
1119         DEFINE_WAIT(wait);
1120
1121         if (trans->commit_done || (trans->in_commit && !trans->blocked))
1122                 return;
1123
1124         while (1) {
1125                 prepare_to_wait(&root->fs_info->transaction_wait, &wait,
1126                                 TASK_UNINTERRUPTIBLE);
1127                 if (trans->commit_done ||
1128                     (trans->in_commit && !trans->blocked)) {
1129                         finish_wait(&root->fs_info->transaction_wait,
1130                                     &wait);
1131                         break;
1132                 }
1133                 mutex_unlock(&root->fs_info->trans_mutex);
1134                 schedule();
1135                 mutex_lock(&root->fs_info->trans_mutex);
1136                 finish_wait(&root->fs_info->transaction_wait,
1137                             &wait);
1138         }
1139 }
1140
1141 /*
1142  * commit transactions asynchronously. once btrfs_commit_transaction_async
1143  * returns, any subsequent transaction will not be allowed to join.
1144  */
1145 struct btrfs_async_commit {
1146         struct btrfs_trans_handle *newtrans;
1147         struct btrfs_root *root;
1148         struct delayed_work work;
1149 };
1150
1151 static void do_async_commit(struct work_struct *work)
1152 {
1153         struct btrfs_async_commit *ac =
1154                 container_of(work, struct btrfs_async_commit, work.work);
1155
1156         btrfs_commit_transaction(ac->newtrans, ac->root);
1157         kfree(ac);
1158 }
1159
1160 int btrfs_commit_transaction_async(struct btrfs_trans_handle *trans,
1161                                    struct btrfs_root *root,
1162                                    int wait_for_unblock)
1163 {
1164         struct btrfs_async_commit *ac;
1165         struct btrfs_transaction *cur_trans;
1166
1167         ac = kmalloc(sizeof(*ac), GFP_NOFS);
1168         if (!ac)
1169                 return -ENOMEM;
1170
1171         INIT_DELAYED_WORK(&ac->work, do_async_commit);
1172         ac->root = root;
1173         ac->newtrans = btrfs_join_transaction(root, 0);
1174         if (IS_ERR(ac->newtrans)) {
1175                 int err = PTR_ERR(ac->newtrans);
1176                 kfree(ac);
1177                 return err;
1178         }
1179
1180         /* take transaction reference */
1181         mutex_lock(&root->fs_info->trans_mutex);
1182         cur_trans = trans->transaction;
1183         atomic_inc(&cur_trans->use_count);
1184         mutex_unlock(&root->fs_info->trans_mutex);
1185
1186         btrfs_end_transaction(trans, root);
1187         schedule_delayed_work(&ac->work, 0);
1188
1189         /* wait for transaction to start and unblock */
1190         mutex_lock(&root->fs_info->trans_mutex);
1191         if (wait_for_unblock)
1192                 wait_current_trans_commit_start_and_unblock(root, cur_trans);
1193         else
1194                 wait_current_trans_commit_start(root, cur_trans);
1195         put_transaction(cur_trans);
1196         mutex_unlock(&root->fs_info->trans_mutex);
1197
1198         return 0;
1199 }
1200
1201 /*
1202  * btrfs_transaction state sequence:
1203  *    in_commit = 0, blocked = 0  (initial)
1204  *    in_commit = 1, blocked = 1
1205  *    blocked = 0
1206  *    commit_done = 1
1207  */
1208 int btrfs_commit_transaction(struct btrfs_trans_handle *trans,
1209                              struct btrfs_root *root)
1210 {
1211         unsigned long joined = 0;
1212         struct btrfs_transaction *cur_trans;
1213         struct btrfs_transaction *prev_trans = NULL;
1214         DEFINE_WAIT(wait);
1215         int ret;
1216         int should_grow = 0;
1217         unsigned long now = get_seconds();
1218         int flush_on_commit = btrfs_test_opt(root, FLUSHONCOMMIT);
1219
1220         btrfs_run_ordered_operations(root, 0);
1221
1222         /* make a pass through all the delayed refs we have so far
1223          * any runnings procs may add more while we are here
1224          */
1225         ret = btrfs_run_delayed_refs(trans, root, 0);
1226         BUG_ON(ret);
1227
1228         btrfs_trans_release_metadata(trans, root);
1229
1230         cur_trans = trans->transaction;
1231         /*
1232          * set the flushing flag so procs in this transaction have to
1233          * start sending their work down.
1234          */
1235         cur_trans->delayed_refs.flushing = 1;
1236
1237         ret = btrfs_run_delayed_refs(trans, root, 0);
1238         BUG_ON(ret);
1239
1240         mutex_lock(&root->fs_info->trans_mutex);
1241         if (cur_trans->in_commit) {
1242                 atomic_inc(&cur_trans->use_count);
1243                 mutex_unlock(&root->fs_info->trans_mutex);
1244                 btrfs_end_transaction(trans, root);
1245
1246                 ret = wait_for_commit(root, cur_trans);
1247                 BUG_ON(ret);
1248
1249                 mutex_lock(&root->fs_info->trans_mutex);
1250                 put_transaction(cur_trans);
1251                 mutex_unlock(&root->fs_info->trans_mutex);
1252
1253                 return 0;
1254         }
1255
1256         trans->transaction->in_commit = 1;
1257         trans->transaction->blocked = 1;
1258         wake_up(&root->fs_info->transaction_blocked_wait);
1259
1260         if (cur_trans->list.prev != &root->fs_info->trans_list) {
1261                 prev_trans = list_entry(cur_trans->list.prev,
1262                                         struct btrfs_transaction, list);
1263                 if (!prev_trans->commit_done) {
1264                         atomic_inc(&prev_trans->use_count);
1265                         mutex_unlock(&root->fs_info->trans_mutex);
1266
1267                         wait_for_commit(root, prev_trans);
1268
1269                         mutex_lock(&root->fs_info->trans_mutex);
1270                         put_transaction(prev_trans);
1271                 }
1272         }
1273
1274         if (now < cur_trans->start_time || now - cur_trans->start_time < 1)
1275                 should_grow = 1;
1276
1277         do {
1278                 int snap_pending = 0;
1279                 joined = cur_trans->num_joined;
1280                 if (!list_empty(&trans->transaction->pending_snapshots))
1281                         snap_pending = 1;
1282
1283                 WARN_ON(cur_trans != trans->transaction);
1284                 mutex_unlock(&root->fs_info->trans_mutex);
1285
1286                 if (flush_on_commit || snap_pending) {
1287                         btrfs_start_delalloc_inodes(root, 1);
1288                         ret = btrfs_wait_ordered_extents(root, 0, 1);
1289                         BUG_ON(ret);
1290                 }
1291
1292                 /*
1293                  * rename don't use btrfs_join_transaction, so, once we
1294                  * set the transaction to blocked above, we aren't going
1295                  * to get any new ordered operations.  We can safely run
1296                  * it here and no for sure that nothing new will be added
1297                  * to the list
1298                  */
1299                 btrfs_run_ordered_operations(root, 1);
1300
1301                 prepare_to_wait(&cur_trans->writer_wait, &wait,
1302                                 TASK_UNINTERRUPTIBLE);
1303
1304                 smp_mb();
1305                 if (atomic_read(&cur_trans->num_writers) > 1)
1306                         schedule_timeout(MAX_SCHEDULE_TIMEOUT);
1307                 else if (should_grow)
1308                         schedule_timeout(1);
1309
1310                 mutex_lock(&root->fs_info->trans_mutex);
1311                 finish_wait(&cur_trans->writer_wait, &wait);
1312         } while (atomic_read(&cur_trans->num_writers) > 1 ||
1313                  (should_grow && cur_trans->num_joined != joined));
1314
1315         ret = create_pending_snapshots(trans, root->fs_info);
1316         BUG_ON(ret);
1317
1318         ret = btrfs_run_delayed_refs(trans, root, (unsigned long)-1);
1319         BUG_ON(ret);
1320
1321         WARN_ON(cur_trans != trans->transaction);
1322
1323         /* btrfs_commit_tree_roots is responsible for getting the
1324          * various roots consistent with each other.  Every pointer
1325          * in the tree of tree roots has to point to the most up to date
1326          * root for every subvolume and other tree.  So, we have to keep
1327          * the tree logging code from jumping in and changing any
1328          * of the trees.
1329          *
1330          * At this point in the commit, there can't be any tree-log
1331          * writers, but a little lower down we drop the trans mutex
1332          * and let new people in.  By holding the tree_log_mutex
1333          * from now until after the super is written, we avoid races
1334          * with the tree-log code.
1335          */
1336         mutex_lock(&root->fs_info->tree_log_mutex);
1337
1338         ret = commit_fs_roots(trans, root);
1339         BUG_ON(ret);
1340
1341         /* commit_fs_roots gets rid of all the tree log roots, it is now
1342          * safe to free the root of tree log roots
1343          */
1344         btrfs_free_log_root_tree(trans, root->fs_info);
1345
1346         ret = commit_cowonly_roots(trans, root);
1347         BUG_ON(ret);
1348
1349         btrfs_prepare_extent_commit(trans, root);
1350
1351         cur_trans = root->fs_info->running_transaction;
1352         spin_lock(&root->fs_info->new_trans_lock);
1353         root->fs_info->running_transaction = NULL;
1354         spin_unlock(&root->fs_info->new_trans_lock);
1355
1356         btrfs_set_root_node(&root->fs_info->tree_root->root_item,
1357                             root->fs_info->tree_root->node);
1358         switch_commit_root(root->fs_info->tree_root);
1359
1360         btrfs_set_root_node(&root->fs_info->chunk_root->root_item,
1361                             root->fs_info->chunk_root->node);
1362         switch_commit_root(root->fs_info->chunk_root);
1363
1364         update_super_roots(root);
1365
1366         if (!root->fs_info->log_root_recovering) {
1367                 btrfs_set_super_log_root(&root->fs_info->super_copy, 0);
1368                 btrfs_set_super_log_root_level(&root->fs_info->super_copy, 0);
1369         }
1370
1371         memcpy(&root->fs_info->super_for_commit, &root->fs_info->super_copy,
1372                sizeof(root->fs_info->super_copy));
1373
1374         trans->transaction->blocked = 0;
1375
1376         wake_up(&root->fs_info->transaction_wait);
1377
1378         mutex_unlock(&root->fs_info->trans_mutex);
1379         ret = btrfs_write_and_wait_transaction(trans, root);
1380         BUG_ON(ret);
1381         write_ctree_super(trans, root, 0);
1382
1383         /*
1384          * the super is written, we can safely allow the tree-loggers
1385          * to go about their business
1386          */
1387         mutex_unlock(&root->fs_info->tree_log_mutex);
1388
1389         btrfs_finish_extent_commit(trans, root);
1390
1391         mutex_lock(&root->fs_info->trans_mutex);
1392
1393         cur_trans->commit_done = 1;
1394
1395         root->fs_info->last_trans_committed = cur_trans->transid;
1396
1397         wake_up(&cur_trans->commit_wait);
1398
1399         list_del_init(&cur_trans->list);
1400         put_transaction(cur_trans);
1401         put_transaction(cur_trans);
1402
1403         trace_btrfs_transaction_commit(root);
1404
1405         mutex_unlock(&root->fs_info->trans_mutex);
1406
1407         if (current->journal_info == trans)
1408                 current->journal_info = NULL;
1409
1410         kmem_cache_free(btrfs_trans_handle_cachep, trans);
1411
1412         if (current != root->fs_info->transaction_kthread)
1413                 btrfs_run_delayed_iputs(root);
1414
1415         return ret;
1416 }
1417
1418 /*
1419  * interface function to delete all the snapshots we have scheduled for deletion
1420  */
1421 int btrfs_clean_old_snapshots(struct btrfs_root *root)
1422 {
1423         LIST_HEAD(list);
1424         struct btrfs_fs_info *fs_info = root->fs_info;
1425
1426         mutex_lock(&fs_info->trans_mutex);
1427         list_splice_init(&fs_info->dead_roots, &list);
1428         mutex_unlock(&fs_info->trans_mutex);
1429
1430         while (!list_empty(&list)) {
1431                 root = list_entry(list.next, struct btrfs_root, root_list);
1432                 list_del(&root->root_list);
1433
1434                 if (btrfs_header_backref_rev(root->node) <
1435                     BTRFS_MIXED_BACKREF_REV)
1436                         btrfs_drop_snapshot(root, NULL, 0);
1437                 else
1438                         btrfs_drop_snapshot(root, NULL, 1);
1439         }
1440         return 0;
1441 }