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