]> rtime.felk.cvut.cz Git - linux-imx.git/blob - fs/btrfs/transaction.c
time: x86: report_lost_ticks doesn't exist any more
[linux-imx.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 <linux/uuid.h>
26 #include "ctree.h"
27 #include "disk-io.h"
28 #include "transaction.h"
29 #include "locking.h"
30 #include "tree-log.h"
31 #include "inode-map.h"
32 #include "volumes.h"
33
34 #define BTRFS_ROOT_TRANS_TAG 0
35
36 void put_transaction(struct btrfs_transaction *transaction)
37 {
38         WARN_ON(atomic_read(&transaction->use_count) == 0);
39         if (atomic_dec_and_test(&transaction->use_count)) {
40                 BUG_ON(!list_empty(&transaction->list));
41                 WARN_ON(transaction->delayed_refs.root.rb_node);
42                 memset(transaction, 0, sizeof(*transaction));
43                 kmem_cache_free(btrfs_transaction_cachep, transaction);
44         }
45 }
46
47 static noinline void switch_commit_root(struct btrfs_root *root)
48 {
49         free_extent_buffer(root->commit_root);
50         root->commit_root = btrfs_root_node(root);
51 }
52
53 /*
54  * either allocate a new transaction or hop into the existing one
55  */
56 static noinline int join_transaction(struct btrfs_root *root, int type)
57 {
58         struct btrfs_transaction *cur_trans;
59         struct btrfs_fs_info *fs_info = root->fs_info;
60
61         spin_lock(&fs_info->trans_lock);
62 loop:
63         /* The file system has been taken offline. No new transactions. */
64         if (fs_info->fs_state & BTRFS_SUPER_FLAG_ERROR) {
65                 spin_unlock(&fs_info->trans_lock);
66                 return -EROFS;
67         }
68
69         if (fs_info->trans_no_join) {
70                 /* 
71                  * If we are JOIN_NOLOCK we're already committing a current
72                  * transaction, we just need a handle to deal with something
73                  * when committing the transaction, such as inode cache and
74                  * space cache. It is a special case.
75                  */
76                 if (type != TRANS_JOIN_NOLOCK) {
77                         spin_unlock(&fs_info->trans_lock);
78                         return -EBUSY;
79                 }
80         }
81
82         cur_trans = fs_info->running_transaction;
83         if (cur_trans) {
84                 if (cur_trans->aborted) {
85                         spin_unlock(&fs_info->trans_lock);
86                         return cur_trans->aborted;
87                 }
88                 atomic_inc(&cur_trans->use_count);
89                 atomic_inc(&cur_trans->num_writers);
90                 cur_trans->num_joined++;
91                 spin_unlock(&fs_info->trans_lock);
92                 return 0;
93         }
94         spin_unlock(&fs_info->trans_lock);
95
96         /*
97          * If we are ATTACH, we just want to catch the current transaction,
98          * and commit it. If there is no transaction, just return ENOENT.
99          */
100         if (type == TRANS_ATTACH)
101                 return -ENOENT;
102
103         cur_trans = kmem_cache_alloc(btrfs_transaction_cachep, GFP_NOFS);
104         if (!cur_trans)
105                 return -ENOMEM;
106
107         spin_lock(&fs_info->trans_lock);
108         if (fs_info->running_transaction) {
109                 /*
110                  * someone started a transaction after we unlocked.  Make sure
111                  * to redo the trans_no_join checks above
112                  */
113                 kmem_cache_free(btrfs_transaction_cachep, cur_trans);
114                 goto loop;
115         } else if (fs_info->fs_state & BTRFS_SUPER_FLAG_ERROR) {
116                 spin_unlock(&fs_info->trans_lock);
117                 kmem_cache_free(btrfs_transaction_cachep, cur_trans);
118                 return -EROFS;
119         }
120
121         atomic_set(&cur_trans->num_writers, 1);
122         cur_trans->num_joined = 0;
123         init_waitqueue_head(&cur_trans->writer_wait);
124         init_waitqueue_head(&cur_trans->commit_wait);
125         cur_trans->in_commit = 0;
126         cur_trans->blocked = 0;
127         /*
128          * One for this trans handle, one so it will live on until we
129          * commit the transaction.
130          */
131         atomic_set(&cur_trans->use_count, 2);
132         cur_trans->commit_done = 0;
133         cur_trans->start_time = get_seconds();
134
135         cur_trans->delayed_refs.root = RB_ROOT;
136         cur_trans->delayed_refs.num_entries = 0;
137         cur_trans->delayed_refs.num_heads_ready = 0;
138         cur_trans->delayed_refs.num_heads = 0;
139         cur_trans->delayed_refs.flushing = 0;
140         cur_trans->delayed_refs.run_delayed_start = 0;
141
142         /*
143          * although the tree mod log is per file system and not per transaction,
144          * the log must never go across transaction boundaries.
145          */
146         smp_mb();
147         if (!list_empty(&fs_info->tree_mod_seq_list)) {
148                 printk(KERN_ERR "btrfs: tree_mod_seq_list not empty when "
149                         "creating a fresh transaction\n");
150                 WARN_ON(1);
151         }
152         if (!RB_EMPTY_ROOT(&fs_info->tree_mod_log)) {
153                 printk(KERN_ERR "btrfs: tree_mod_log rb tree not empty when "
154                         "creating a fresh transaction\n");
155                 WARN_ON(1);
156         }
157         atomic_set(&fs_info->tree_mod_seq, 0);
158
159         spin_lock_init(&cur_trans->commit_lock);
160         spin_lock_init(&cur_trans->delayed_refs.lock);
161
162         INIT_LIST_HEAD(&cur_trans->pending_snapshots);
163         list_add_tail(&cur_trans->list, &fs_info->trans_list);
164         extent_io_tree_init(&cur_trans->dirty_pages,
165                              fs_info->btree_inode->i_mapping);
166         fs_info->generation++;
167         cur_trans->transid = fs_info->generation;
168         fs_info->running_transaction = cur_trans;
169         cur_trans->aborted = 0;
170         spin_unlock(&fs_info->trans_lock);
171
172         return 0;
173 }
174
175 /*
176  * this does all the record keeping required to make sure that a reference
177  * counted root is properly recorded in a given transaction.  This is required
178  * to make sure the old root from before we joined the transaction is deleted
179  * when the transaction commits
180  */
181 static int record_root_in_trans(struct btrfs_trans_handle *trans,
182                                struct btrfs_root *root)
183 {
184         if (root->ref_cows && root->last_trans < trans->transid) {
185                 WARN_ON(root == root->fs_info->extent_root);
186                 WARN_ON(root->commit_root != root->node);
187
188                 /*
189                  * see below for in_trans_setup usage rules
190                  * we have the reloc mutex held now, so there
191                  * is only one writer in this function
192                  */
193                 root->in_trans_setup = 1;
194
195                 /* make sure readers find in_trans_setup before
196                  * they find our root->last_trans update
197                  */
198                 smp_wmb();
199
200                 spin_lock(&root->fs_info->fs_roots_radix_lock);
201                 if (root->last_trans == trans->transid) {
202                         spin_unlock(&root->fs_info->fs_roots_radix_lock);
203                         return 0;
204                 }
205                 radix_tree_tag_set(&root->fs_info->fs_roots_radix,
206                            (unsigned long)root->root_key.objectid,
207                            BTRFS_ROOT_TRANS_TAG);
208                 spin_unlock(&root->fs_info->fs_roots_radix_lock);
209                 root->last_trans = trans->transid;
210
211                 /* this is pretty tricky.  We don't want to
212                  * take the relocation lock in btrfs_record_root_in_trans
213                  * unless we're really doing the first setup for this root in
214                  * this transaction.
215                  *
216                  * Normally we'd use root->last_trans as a flag to decide
217                  * if we want to take the expensive mutex.
218                  *
219                  * But, we have to set root->last_trans before we
220                  * init the relocation root, otherwise, we trip over warnings
221                  * in ctree.c.  The solution used here is to flag ourselves
222                  * with root->in_trans_setup.  When this is 1, we're still
223                  * fixing up the reloc trees and everyone must wait.
224                  *
225                  * When this is zero, they can trust root->last_trans and fly
226                  * through btrfs_record_root_in_trans without having to take the
227                  * lock.  smp_wmb() makes sure that all the writes above are
228                  * done before we pop in the zero below
229                  */
230                 btrfs_init_reloc_root(trans, root);
231                 smp_wmb();
232                 root->in_trans_setup = 0;
233         }
234         return 0;
235 }
236
237
238 int btrfs_record_root_in_trans(struct btrfs_trans_handle *trans,
239                                struct btrfs_root *root)
240 {
241         if (!root->ref_cows)
242                 return 0;
243
244         /*
245          * see record_root_in_trans for comments about in_trans_setup usage
246          * and barriers
247          */
248         smp_rmb();
249         if (root->last_trans == trans->transid &&
250             !root->in_trans_setup)
251                 return 0;
252
253         mutex_lock(&root->fs_info->reloc_mutex);
254         record_root_in_trans(trans, root);
255         mutex_unlock(&root->fs_info->reloc_mutex);
256
257         return 0;
258 }
259
260 /* wait for commit against the current transaction to become unblocked
261  * when this is done, it is safe to start a new transaction, but the current
262  * transaction might not be fully on disk.
263  */
264 static void wait_current_trans(struct btrfs_root *root)
265 {
266         struct btrfs_transaction *cur_trans;
267
268         spin_lock(&root->fs_info->trans_lock);
269         cur_trans = root->fs_info->running_transaction;
270         if (cur_trans && cur_trans->blocked) {
271                 atomic_inc(&cur_trans->use_count);
272                 spin_unlock(&root->fs_info->trans_lock);
273
274                 wait_event(root->fs_info->transaction_wait,
275                            !cur_trans->blocked);
276                 put_transaction(cur_trans);
277         } else {
278                 spin_unlock(&root->fs_info->trans_lock);
279         }
280 }
281
282 static int may_wait_transaction(struct btrfs_root *root, int type)
283 {
284         if (root->fs_info->log_root_recovering)
285                 return 0;
286
287         if (type == TRANS_USERSPACE)
288                 return 1;
289
290         if (type == TRANS_START &&
291             !atomic_read(&root->fs_info->open_ioctl_trans))
292                 return 1;
293
294         return 0;
295 }
296
297 static struct btrfs_trans_handle *start_transaction(struct btrfs_root *root,
298                                                     u64 num_items, int type,
299                                                     int noflush)
300 {
301         struct btrfs_trans_handle *h;
302         struct btrfs_transaction *cur_trans;
303         u64 num_bytes = 0;
304         int ret;
305         u64 qgroup_reserved = 0;
306
307         if (root->fs_info->fs_state & BTRFS_SUPER_FLAG_ERROR)
308                 return ERR_PTR(-EROFS);
309
310         if (current->journal_info) {
311                 WARN_ON(type != TRANS_JOIN && type != TRANS_JOIN_NOLOCK);
312                 h = current->journal_info;
313                 h->use_count++;
314                 h->orig_rsv = h->block_rsv;
315                 h->block_rsv = NULL;
316                 goto got_it;
317         }
318
319         /*
320          * Do the reservation before we join the transaction so we can do all
321          * the appropriate flushing if need be.
322          */
323         if (num_items > 0 && root != root->fs_info->chunk_root) {
324                 if (root->fs_info->quota_enabled &&
325                     is_fstree(root->root_key.objectid)) {
326                         qgroup_reserved = num_items * root->leafsize;
327                         ret = btrfs_qgroup_reserve(root, qgroup_reserved);
328                         if (ret)
329                                 return ERR_PTR(ret);
330                 }
331
332                 num_bytes = btrfs_calc_trans_metadata_size(root, num_items);
333                 if (noflush)
334                         ret = btrfs_block_rsv_add_noflush(root,
335                                                 &root->fs_info->trans_block_rsv,
336                                                 num_bytes);
337                 else
338                         ret = btrfs_block_rsv_add(root,
339                                                 &root->fs_info->trans_block_rsv,
340                                                 num_bytes);
341                 if (ret)
342                         return ERR_PTR(ret);
343         }
344 again:
345         h = kmem_cache_alloc(btrfs_trans_handle_cachep, GFP_NOFS);
346         if (!h)
347                 return ERR_PTR(-ENOMEM);
348
349         /*
350          * If we are JOIN_NOLOCK we're already committing a transaction and
351          * waiting on this guy, so we don't need to do the sb_start_intwrite
352          * because we're already holding a ref.  We need this because we could
353          * have raced in and did an fsync() on a file which can kick a commit
354          * and then we deadlock with somebody doing a freeze.
355          *
356          * If we are ATTACH, it means we just want to catch the current
357          * transaction and commit it, so we needn't do sb_start_intwrite(). 
358          */
359         if (type < TRANS_JOIN_NOLOCK)
360                 sb_start_intwrite(root->fs_info->sb);
361
362         if (may_wait_transaction(root, type))
363                 wait_current_trans(root);
364
365         do {
366                 ret = join_transaction(root, type);
367                 if (ret == -EBUSY)
368                         wait_current_trans(root);
369         } while (ret == -EBUSY);
370
371         if (ret < 0) {
372                 /* We must get the transaction if we are JOIN_NOLOCK. */
373                 BUG_ON(type == TRANS_JOIN_NOLOCK);
374
375                 if (type < TRANS_JOIN_NOLOCK)
376                         sb_end_intwrite(root->fs_info->sb);
377                 kmem_cache_free(btrfs_trans_handle_cachep, h);
378                 return ERR_PTR(ret);
379         }
380
381         cur_trans = root->fs_info->running_transaction;
382
383         h->transid = cur_trans->transid;
384         h->transaction = cur_trans;
385         h->blocks_used = 0;
386         h->bytes_reserved = 0;
387         h->root = root;
388         h->delayed_ref_updates = 0;
389         h->use_count = 1;
390         h->adding_csums = 0;
391         h->block_rsv = NULL;
392         h->orig_rsv = NULL;
393         h->aborted = 0;
394         h->qgroup_reserved = qgroup_reserved;
395         h->delayed_ref_elem.seq = 0;
396         h->type = type;
397         INIT_LIST_HEAD(&h->qgroup_ref_list);
398         INIT_LIST_HEAD(&h->new_bgs);
399
400         smp_mb();
401         if (cur_trans->blocked && may_wait_transaction(root, type)) {
402                 btrfs_commit_transaction(h, root);
403                 goto again;
404         }
405
406         if (num_bytes) {
407                 trace_btrfs_space_reservation(root->fs_info, "transaction",
408                                               h->transid, num_bytes, 1);
409                 h->block_rsv = &root->fs_info->trans_block_rsv;
410                 h->bytes_reserved = num_bytes;
411         }
412
413 got_it:
414         btrfs_record_root_in_trans(h, root);
415
416         if (!current->journal_info && type != TRANS_USERSPACE)
417                 current->journal_info = h;
418         return h;
419 }
420
421 struct btrfs_trans_handle *btrfs_start_transaction(struct btrfs_root *root,
422                                                    int num_items)
423 {
424         return start_transaction(root, num_items, TRANS_START, 0);
425 }
426
427 struct btrfs_trans_handle *btrfs_start_transaction_noflush(
428                                         struct btrfs_root *root, int num_items)
429 {
430         return start_transaction(root, num_items, TRANS_START, 1);
431 }
432
433 struct btrfs_trans_handle *btrfs_join_transaction(struct btrfs_root *root)
434 {
435         return start_transaction(root, 0, TRANS_JOIN, 0);
436 }
437
438 struct btrfs_trans_handle *btrfs_join_transaction_nolock(struct btrfs_root *root)
439 {
440         return start_transaction(root, 0, TRANS_JOIN_NOLOCK, 0);
441 }
442
443 struct btrfs_trans_handle *btrfs_start_ioctl_transaction(struct btrfs_root *root)
444 {
445         return start_transaction(root, 0, TRANS_USERSPACE, 0);
446 }
447
448 struct btrfs_trans_handle *btrfs_attach_transaction(struct btrfs_root *root)
449 {
450         return start_transaction(root, 0, TRANS_ATTACH, 0);
451 }
452
453 /* wait for a transaction commit to be fully complete */
454 static noinline void wait_for_commit(struct btrfs_root *root,
455                                     struct btrfs_transaction *commit)
456 {
457         wait_event(commit->commit_wait, commit->commit_done);
458 }
459
460 int btrfs_wait_for_commit(struct btrfs_root *root, u64 transid)
461 {
462         struct btrfs_transaction *cur_trans = NULL, *t;
463         int ret;
464
465         ret = 0;
466         if (transid) {
467                 if (transid <= root->fs_info->last_trans_committed)
468                         goto out;
469
470                 /* find specified transaction */
471                 spin_lock(&root->fs_info->trans_lock);
472                 list_for_each_entry(t, &root->fs_info->trans_list, list) {
473                         if (t->transid == transid) {
474                                 cur_trans = t;
475                                 atomic_inc(&cur_trans->use_count);
476                                 break;
477                         }
478                         if (t->transid > transid)
479                                 break;
480                 }
481                 spin_unlock(&root->fs_info->trans_lock);
482                 ret = -EINVAL;
483                 if (!cur_trans)
484                         goto out;  /* bad transid */
485         } else {
486                 /* find newest transaction that is committing | committed */
487                 spin_lock(&root->fs_info->trans_lock);
488                 list_for_each_entry_reverse(t, &root->fs_info->trans_list,
489                                             list) {
490                         if (t->in_commit) {
491                                 if (t->commit_done)
492                                         break;
493                                 cur_trans = t;
494                                 atomic_inc(&cur_trans->use_count);
495                                 break;
496                         }
497                 }
498                 spin_unlock(&root->fs_info->trans_lock);
499                 if (!cur_trans)
500                         goto out;  /* nothing committing|committed */
501         }
502
503         wait_for_commit(root, cur_trans);
504
505         put_transaction(cur_trans);
506         ret = 0;
507 out:
508         return ret;
509 }
510
511 void btrfs_throttle(struct btrfs_root *root)
512 {
513         if (!atomic_read(&root->fs_info->open_ioctl_trans))
514                 wait_current_trans(root);
515 }
516
517 static int should_end_transaction(struct btrfs_trans_handle *trans,
518                                   struct btrfs_root *root)
519 {
520         int ret;
521
522         ret = btrfs_block_rsv_check(root, &root->fs_info->global_block_rsv, 5);
523         return ret ? 1 : 0;
524 }
525
526 int btrfs_should_end_transaction(struct btrfs_trans_handle *trans,
527                                  struct btrfs_root *root)
528 {
529         struct btrfs_transaction *cur_trans = trans->transaction;
530         int updates;
531         int err;
532
533         smp_mb();
534         if (cur_trans->blocked || cur_trans->delayed_refs.flushing)
535                 return 1;
536
537         updates = trans->delayed_ref_updates;
538         trans->delayed_ref_updates = 0;
539         if (updates) {
540                 err = btrfs_run_delayed_refs(trans, root, updates);
541                 if (err) /* Error code will also eval true */
542                         return err;
543         }
544
545         return should_end_transaction(trans, root);
546 }
547
548 static int __btrfs_end_transaction(struct btrfs_trans_handle *trans,
549                           struct btrfs_root *root, int throttle)
550 {
551         struct btrfs_transaction *cur_trans = trans->transaction;
552         struct btrfs_fs_info *info = root->fs_info;
553         int count = 0;
554         int lock = (trans->type != TRANS_JOIN_NOLOCK);
555         int err = 0;
556
557         if (--trans->use_count) {
558                 trans->block_rsv = trans->orig_rsv;
559                 return 0;
560         }
561
562         /*
563          * do the qgroup accounting as early as possible
564          */
565         err = btrfs_delayed_refs_qgroup_accounting(trans, info);
566
567         btrfs_trans_release_metadata(trans, root);
568         trans->block_rsv = NULL;
569         /*
570          * the same root has to be passed to start_transaction and
571          * end_transaction. Subvolume quota depends on this.
572          */
573         WARN_ON(trans->root != root);
574
575         if (trans->qgroup_reserved) {
576                 btrfs_qgroup_free(root, trans->qgroup_reserved);
577                 trans->qgroup_reserved = 0;
578         }
579
580         if (!list_empty(&trans->new_bgs))
581                 btrfs_create_pending_block_groups(trans, root);
582
583         while (count < 2) {
584                 unsigned long cur = trans->delayed_ref_updates;
585                 trans->delayed_ref_updates = 0;
586                 if (cur &&
587                     trans->transaction->delayed_refs.num_heads_ready > 64) {
588                         trans->delayed_ref_updates = 0;
589                         btrfs_run_delayed_refs(trans, root, cur);
590                 } else {
591                         break;
592                 }
593                 count++;
594         }
595         btrfs_trans_release_metadata(trans, root);
596         trans->block_rsv = NULL;
597
598         if (!list_empty(&trans->new_bgs))
599                 btrfs_create_pending_block_groups(trans, root);
600
601         if (lock && !atomic_read(&root->fs_info->open_ioctl_trans) &&
602             should_end_transaction(trans, root)) {
603                 trans->transaction->blocked = 1;
604                 smp_wmb();
605         }
606
607         if (lock && cur_trans->blocked && !cur_trans->in_commit) {
608                 if (throttle) {
609                         /*
610                          * We may race with somebody else here so end up having
611                          * to call end_transaction on ourselves again, so inc
612                          * our use_count.
613                          */
614                         trans->use_count++;
615                         return btrfs_commit_transaction(trans, root);
616                 } else {
617                         wake_up_process(info->transaction_kthread);
618                 }
619         }
620
621         if (trans->type < TRANS_JOIN_NOLOCK)
622                 sb_end_intwrite(root->fs_info->sb);
623
624         WARN_ON(cur_trans != info->running_transaction);
625         WARN_ON(atomic_read(&cur_trans->num_writers) < 1);
626         atomic_dec(&cur_trans->num_writers);
627
628         smp_mb();
629         if (waitqueue_active(&cur_trans->writer_wait))
630                 wake_up(&cur_trans->writer_wait);
631         put_transaction(cur_trans);
632
633         if (current->journal_info == trans)
634                 current->journal_info = NULL;
635
636         if (throttle)
637                 btrfs_run_delayed_iputs(root);
638
639         if (trans->aborted ||
640             root->fs_info->fs_state & BTRFS_SUPER_FLAG_ERROR) {
641                 err = -EIO;
642         }
643         assert_qgroups_uptodate(trans);
644
645         memset(trans, 0, sizeof(*trans));
646         kmem_cache_free(btrfs_trans_handle_cachep, trans);
647         return err;
648 }
649
650 int btrfs_end_transaction(struct btrfs_trans_handle *trans,
651                           struct btrfs_root *root)
652 {
653         int ret;
654
655         ret = __btrfs_end_transaction(trans, root, 0);
656         if (ret)
657                 return ret;
658         return 0;
659 }
660
661 int btrfs_end_transaction_throttle(struct btrfs_trans_handle *trans,
662                                    struct btrfs_root *root)
663 {
664         int ret;
665
666         ret = __btrfs_end_transaction(trans, root, 1);
667         if (ret)
668                 return ret;
669         return 0;
670 }
671
672 int btrfs_end_transaction_dmeta(struct btrfs_trans_handle *trans,
673                                 struct btrfs_root *root)
674 {
675         return __btrfs_end_transaction(trans, root, 1);
676 }
677
678 /*
679  * when btree blocks are allocated, they have some corresponding bits set for
680  * them in one of two extent_io trees.  This is used to make sure all of
681  * those extents are sent to disk but does not wait on them
682  */
683 int btrfs_write_marked_extents(struct btrfs_root *root,
684                                struct extent_io_tree *dirty_pages, int mark)
685 {
686         int err = 0;
687         int werr = 0;
688         struct address_space *mapping = root->fs_info->btree_inode->i_mapping;
689         struct extent_state *cached_state = NULL;
690         u64 start = 0;
691         u64 end;
692
693         while (!find_first_extent_bit(dirty_pages, start, &start, &end,
694                                       mark, &cached_state)) {
695                 convert_extent_bit(dirty_pages, start, end, EXTENT_NEED_WAIT,
696                                    mark, &cached_state, GFP_NOFS);
697                 cached_state = NULL;
698                 err = filemap_fdatawrite_range(mapping, start, end);
699                 if (err)
700                         werr = err;
701                 cond_resched();
702                 start = end + 1;
703         }
704         if (err)
705                 werr = err;
706         return werr;
707 }
708
709 /*
710  * when btree blocks are allocated, they have some corresponding bits set for
711  * them in one of two extent_io trees.  This is used to make sure all of
712  * those extents are on disk for transaction or log commit.  We wait
713  * on all the pages and clear them from the dirty pages state tree
714  */
715 int btrfs_wait_marked_extents(struct btrfs_root *root,
716                               struct extent_io_tree *dirty_pages, int mark)
717 {
718         int err = 0;
719         int werr = 0;
720         struct address_space *mapping = root->fs_info->btree_inode->i_mapping;
721         struct extent_state *cached_state = NULL;
722         u64 start = 0;
723         u64 end;
724
725         while (!find_first_extent_bit(dirty_pages, start, &start, &end,
726                                       EXTENT_NEED_WAIT, &cached_state)) {
727                 clear_extent_bit(dirty_pages, start, end, EXTENT_NEED_WAIT,
728                                  0, 0, &cached_state, GFP_NOFS);
729                 err = filemap_fdatawait_range(mapping, start, end);
730                 if (err)
731                         werr = err;
732                 cond_resched();
733                 start = end + 1;
734         }
735         if (err)
736                 werr = err;
737         return werr;
738 }
739
740 /*
741  * when btree blocks are allocated, they have some corresponding bits set for
742  * them in one of two extent_io trees.  This is used to make sure all of
743  * those extents are on disk for transaction or log commit
744  */
745 int btrfs_write_and_wait_marked_extents(struct btrfs_root *root,
746                                 struct extent_io_tree *dirty_pages, int mark)
747 {
748         int ret;
749         int ret2;
750
751         ret = btrfs_write_marked_extents(root, dirty_pages, mark);
752         ret2 = btrfs_wait_marked_extents(root, dirty_pages, mark);
753
754         if (ret)
755                 return ret;
756         if (ret2)
757                 return ret2;
758         return 0;
759 }
760
761 int btrfs_write_and_wait_transaction(struct btrfs_trans_handle *trans,
762                                      struct btrfs_root *root)
763 {
764         if (!trans || !trans->transaction) {
765                 struct inode *btree_inode;
766                 btree_inode = root->fs_info->btree_inode;
767                 return filemap_write_and_wait(btree_inode->i_mapping);
768         }
769         return btrfs_write_and_wait_marked_extents(root,
770                                            &trans->transaction->dirty_pages,
771                                            EXTENT_DIRTY);
772 }
773
774 /*
775  * this is used to update the root pointer in the tree of tree roots.
776  *
777  * But, in the case of the extent allocation tree, updating the root
778  * pointer may allocate blocks which may change the root of the extent
779  * allocation tree.
780  *
781  * So, this loops and repeats and makes sure the cowonly root didn't
782  * change while the root pointer was being updated in the metadata.
783  */
784 static int update_cowonly_root(struct btrfs_trans_handle *trans,
785                                struct btrfs_root *root)
786 {
787         int ret;
788         u64 old_root_bytenr;
789         u64 old_root_used;
790         struct btrfs_root *tree_root = root->fs_info->tree_root;
791
792         old_root_used = btrfs_root_used(&root->root_item);
793         btrfs_write_dirty_block_groups(trans, root);
794
795         while (1) {
796                 old_root_bytenr = btrfs_root_bytenr(&root->root_item);
797                 if (old_root_bytenr == root->node->start &&
798                     old_root_used == btrfs_root_used(&root->root_item))
799                         break;
800
801                 btrfs_set_root_node(&root->root_item, root->node);
802                 ret = btrfs_update_root(trans, tree_root,
803                                         &root->root_key,
804                                         &root->root_item);
805                 if (ret)
806                         return ret;
807
808                 old_root_used = btrfs_root_used(&root->root_item);
809                 ret = btrfs_write_dirty_block_groups(trans, root);
810                 if (ret)
811                         return ret;
812         }
813
814         if (root != root->fs_info->extent_root)
815                 switch_commit_root(root);
816
817         return 0;
818 }
819
820 /*
821  * update all the cowonly tree roots on disk
822  *
823  * The error handling in this function may not be obvious. Any of the
824  * failures will cause the file system to go offline. We still need
825  * to clean up the delayed refs.
826  */
827 static noinline int commit_cowonly_roots(struct btrfs_trans_handle *trans,
828                                          struct btrfs_root *root)
829 {
830         struct btrfs_fs_info *fs_info = root->fs_info;
831         struct list_head *next;
832         struct extent_buffer *eb;
833         int ret;
834
835         ret = btrfs_run_delayed_refs(trans, root, (unsigned long)-1);
836         if (ret)
837                 return ret;
838
839         eb = btrfs_lock_root_node(fs_info->tree_root);
840         ret = btrfs_cow_block(trans, fs_info->tree_root, eb, NULL,
841                               0, &eb);
842         btrfs_tree_unlock(eb);
843         free_extent_buffer(eb);
844
845         if (ret)
846                 return ret;
847
848         ret = btrfs_run_delayed_refs(trans, root, (unsigned long)-1);
849         if (ret)
850                 return ret;
851
852         ret = btrfs_run_dev_stats(trans, root->fs_info);
853         BUG_ON(ret);
854
855         ret = btrfs_run_qgroups(trans, root->fs_info);
856         BUG_ON(ret);
857
858         /* run_qgroups might have added some more refs */
859         ret = btrfs_run_delayed_refs(trans, root, (unsigned long)-1);
860         BUG_ON(ret);
861
862         while (!list_empty(&fs_info->dirty_cowonly_roots)) {
863                 next = fs_info->dirty_cowonly_roots.next;
864                 list_del_init(next);
865                 root = list_entry(next, struct btrfs_root, dirty_list);
866
867                 ret = update_cowonly_root(trans, root);
868                 if (ret)
869                         return ret;
870         }
871
872         down_write(&fs_info->extent_commit_sem);
873         switch_commit_root(fs_info->extent_root);
874         up_write(&fs_info->extent_commit_sem);
875
876         return 0;
877 }
878
879 /*
880  * dead roots are old snapshots that need to be deleted.  This allocates
881  * a dirty root struct and adds it into the list of dead roots that need to
882  * be deleted
883  */
884 int btrfs_add_dead_root(struct btrfs_root *root)
885 {
886         spin_lock(&root->fs_info->trans_lock);
887         list_add(&root->root_list, &root->fs_info->dead_roots);
888         spin_unlock(&root->fs_info->trans_lock);
889         return 0;
890 }
891
892 /*
893  * update all the cowonly tree roots on disk
894  */
895 static noinline int commit_fs_roots(struct btrfs_trans_handle *trans,
896                                     struct btrfs_root *root)
897 {
898         struct btrfs_root *gang[8];
899         struct btrfs_fs_info *fs_info = root->fs_info;
900         int i;
901         int ret;
902         int err = 0;
903
904         spin_lock(&fs_info->fs_roots_radix_lock);
905         while (1) {
906                 ret = radix_tree_gang_lookup_tag(&fs_info->fs_roots_radix,
907                                                  (void **)gang, 0,
908                                                  ARRAY_SIZE(gang),
909                                                  BTRFS_ROOT_TRANS_TAG);
910                 if (ret == 0)
911                         break;
912                 for (i = 0; i < ret; i++) {
913                         root = gang[i];
914                         radix_tree_tag_clear(&fs_info->fs_roots_radix,
915                                         (unsigned long)root->root_key.objectid,
916                                         BTRFS_ROOT_TRANS_TAG);
917                         spin_unlock(&fs_info->fs_roots_radix_lock);
918
919                         btrfs_free_log(trans, root);
920                         btrfs_update_reloc_root(trans, root);
921                         btrfs_orphan_commit_root(trans, root);
922
923                         btrfs_save_ino_cache(root, trans);
924
925                         /* see comments in should_cow_block() */
926                         root->force_cow = 0;
927                         smp_wmb();
928
929                         if (root->commit_root != root->node) {
930                                 mutex_lock(&root->fs_commit_mutex);
931                                 switch_commit_root(root);
932                                 btrfs_unpin_free_ino(root);
933                                 mutex_unlock(&root->fs_commit_mutex);
934
935                                 btrfs_set_root_node(&root->root_item,
936                                                     root->node);
937                         }
938
939                         err = btrfs_update_root(trans, fs_info->tree_root,
940                                                 &root->root_key,
941                                                 &root->root_item);
942                         spin_lock(&fs_info->fs_roots_radix_lock);
943                         if (err)
944                                 break;
945                 }
946         }
947         spin_unlock(&fs_info->fs_roots_radix_lock);
948         return err;
949 }
950
951 /*
952  * defrag a given btree.  If cacheonly == 1, this won't read from the disk,
953  * otherwise every leaf in the btree is read and defragged.
954  */
955 int btrfs_defrag_root(struct btrfs_root *root, int cacheonly)
956 {
957         struct btrfs_fs_info *info = root->fs_info;
958         struct btrfs_trans_handle *trans;
959         int ret;
960         unsigned long nr;
961
962         if (xchg(&root->defrag_running, 1))
963                 return 0;
964
965         while (1) {
966                 trans = btrfs_start_transaction(root, 0);
967                 if (IS_ERR(trans))
968                         return PTR_ERR(trans);
969
970                 ret = btrfs_defrag_leaves(trans, root, cacheonly);
971
972                 nr = trans->blocks_used;
973                 btrfs_end_transaction(trans, root);
974                 btrfs_btree_balance_dirty(info->tree_root, nr);
975                 cond_resched();
976
977                 if (btrfs_fs_closing(root->fs_info) || ret != -EAGAIN)
978                         break;
979         }
980         root->defrag_running = 0;
981         return ret;
982 }
983
984 /*
985  * new snapshots need to be created at a very specific time in the
986  * transaction commit.  This does the actual creation
987  */
988 static noinline int create_pending_snapshot(struct btrfs_trans_handle *trans,
989                                    struct btrfs_fs_info *fs_info,
990                                    struct btrfs_pending_snapshot *pending)
991 {
992         struct btrfs_key key;
993         struct btrfs_root_item *new_root_item;
994         struct btrfs_root *tree_root = fs_info->tree_root;
995         struct btrfs_root *root = pending->root;
996         struct btrfs_root *parent_root;
997         struct btrfs_block_rsv *rsv;
998         struct inode *parent_inode;
999         struct btrfs_path *path;
1000         struct btrfs_dir_item *dir_item;
1001         struct dentry *parent;
1002         struct dentry *dentry;
1003         struct extent_buffer *tmp;
1004         struct extent_buffer *old;
1005         struct timespec cur_time = CURRENT_TIME;
1006         int ret;
1007         u64 to_reserve = 0;
1008         u64 index = 0;
1009         u64 objectid;
1010         u64 root_flags;
1011         uuid_le new_uuid;
1012
1013         path = btrfs_alloc_path();
1014         if (!path) {
1015                 ret = pending->error = -ENOMEM;
1016                 goto path_alloc_fail;
1017         }
1018
1019         new_root_item = kmalloc(sizeof(*new_root_item), GFP_NOFS);
1020         if (!new_root_item) {
1021                 ret = pending->error = -ENOMEM;
1022                 goto root_item_alloc_fail;
1023         }
1024
1025         ret = btrfs_find_free_objectid(tree_root, &objectid);
1026         if (ret) {
1027                 pending->error = ret;
1028                 goto no_free_objectid;
1029         }
1030
1031         btrfs_reloc_pre_snapshot(trans, pending, &to_reserve);
1032
1033         if (to_reserve > 0) {
1034                 ret = btrfs_block_rsv_add_noflush(root, &pending->block_rsv,
1035                                                   to_reserve);
1036                 if (ret) {
1037                         pending->error = ret;
1038                         goto no_free_objectid;
1039                 }
1040         }
1041
1042         ret = btrfs_qgroup_inherit(trans, fs_info, root->root_key.objectid,
1043                                    objectid, pending->inherit);
1044         if (ret) {
1045                 pending->error = ret;
1046                 goto no_free_objectid;
1047         }
1048
1049         key.objectid = objectid;
1050         key.offset = (u64)-1;
1051         key.type = BTRFS_ROOT_ITEM_KEY;
1052
1053         rsv = trans->block_rsv;
1054         trans->block_rsv = &pending->block_rsv;
1055
1056         dentry = pending->dentry;
1057         parent = dget_parent(dentry);
1058         parent_inode = parent->d_inode;
1059         parent_root = BTRFS_I(parent_inode)->root;
1060         record_root_in_trans(trans, parent_root);
1061
1062         /*
1063          * insert the directory item
1064          */
1065         ret = btrfs_set_inode_index(parent_inode, &index);
1066         BUG_ON(ret); /* -ENOMEM */
1067
1068         /* check if there is a file/dir which has the same name. */
1069         dir_item = btrfs_lookup_dir_item(NULL, parent_root, path,
1070                                          btrfs_ino(parent_inode),
1071                                          dentry->d_name.name,
1072                                          dentry->d_name.len, 0);
1073         if (dir_item != NULL && !IS_ERR(dir_item)) {
1074                 pending->error = -EEXIST;
1075                 goto fail;
1076         } else if (IS_ERR(dir_item)) {
1077                 ret = PTR_ERR(dir_item);
1078                 btrfs_abort_transaction(trans, root, ret);
1079                 goto fail;
1080         }
1081         btrfs_release_path(path);
1082
1083         /*
1084          * pull in the delayed directory update
1085          * and the delayed inode item
1086          * otherwise we corrupt the FS during
1087          * snapshot
1088          */
1089         ret = btrfs_run_delayed_items(trans, root);
1090         if (ret) {      /* Transaction aborted */
1091                 btrfs_abort_transaction(trans, root, ret);
1092                 goto fail;
1093         }
1094
1095         record_root_in_trans(trans, root);
1096         btrfs_set_root_last_snapshot(&root->root_item, trans->transid);
1097         memcpy(new_root_item, &root->root_item, sizeof(*new_root_item));
1098         btrfs_check_and_init_root_item(new_root_item);
1099
1100         root_flags = btrfs_root_flags(new_root_item);
1101         if (pending->readonly)
1102                 root_flags |= BTRFS_ROOT_SUBVOL_RDONLY;
1103         else
1104                 root_flags &= ~BTRFS_ROOT_SUBVOL_RDONLY;
1105         btrfs_set_root_flags(new_root_item, root_flags);
1106
1107         btrfs_set_root_generation_v2(new_root_item,
1108                         trans->transid);
1109         uuid_le_gen(&new_uuid);
1110         memcpy(new_root_item->uuid, new_uuid.b, BTRFS_UUID_SIZE);
1111         memcpy(new_root_item->parent_uuid, root->root_item.uuid,
1112                         BTRFS_UUID_SIZE);
1113         new_root_item->otime.sec = cpu_to_le64(cur_time.tv_sec);
1114         new_root_item->otime.nsec = cpu_to_le32(cur_time.tv_nsec);
1115         btrfs_set_root_otransid(new_root_item, trans->transid);
1116         memset(&new_root_item->stime, 0, sizeof(new_root_item->stime));
1117         memset(&new_root_item->rtime, 0, sizeof(new_root_item->rtime));
1118         btrfs_set_root_stransid(new_root_item, 0);
1119         btrfs_set_root_rtransid(new_root_item, 0);
1120
1121         old = btrfs_lock_root_node(root);
1122         ret = btrfs_cow_block(trans, root, old, NULL, 0, &old);
1123         if (ret) {
1124                 btrfs_tree_unlock(old);
1125                 free_extent_buffer(old);
1126                 btrfs_abort_transaction(trans, root, ret);
1127                 goto fail;
1128         }
1129
1130         btrfs_set_lock_blocking(old);
1131
1132         ret = btrfs_copy_root(trans, root, old, &tmp, objectid);
1133         /* clean up in any case */
1134         btrfs_tree_unlock(old);
1135         free_extent_buffer(old);
1136         if (ret) {
1137                 btrfs_abort_transaction(trans, root, ret);
1138                 goto fail;
1139         }
1140
1141         /* see comments in should_cow_block() */
1142         root->force_cow = 1;
1143         smp_wmb();
1144
1145         btrfs_set_root_node(new_root_item, tmp);
1146         /* record when the snapshot was created in key.offset */
1147         key.offset = trans->transid;
1148         ret = btrfs_insert_root(trans, tree_root, &key, new_root_item);
1149         btrfs_tree_unlock(tmp);
1150         free_extent_buffer(tmp);
1151         if (ret) {
1152                 btrfs_abort_transaction(trans, root, ret);
1153                 goto fail;
1154         }
1155
1156         /*
1157          * insert root back/forward references
1158          */
1159         ret = btrfs_add_root_ref(trans, tree_root, objectid,
1160                                  parent_root->root_key.objectid,
1161                                  btrfs_ino(parent_inode), index,
1162                                  dentry->d_name.name, dentry->d_name.len);
1163         if (ret) {
1164                 btrfs_abort_transaction(trans, root, ret);
1165                 goto fail;
1166         }
1167
1168         key.offset = (u64)-1;
1169         pending->snap = btrfs_read_fs_root_no_name(root->fs_info, &key);
1170         if (IS_ERR(pending->snap)) {
1171                 ret = PTR_ERR(pending->snap);
1172                 btrfs_abort_transaction(trans, root, ret);
1173                 goto fail;
1174         }
1175
1176         ret = btrfs_reloc_post_snapshot(trans, pending);
1177         if (ret) {
1178                 btrfs_abort_transaction(trans, root, ret);
1179                 goto fail;
1180         }
1181
1182         ret = btrfs_run_delayed_refs(trans, root, (unsigned long)-1);
1183         if (ret) {
1184                 btrfs_abort_transaction(trans, root, ret);
1185                 goto fail;
1186         }
1187
1188         ret = btrfs_insert_dir_item(trans, parent_root,
1189                                     dentry->d_name.name, dentry->d_name.len,
1190                                     parent_inode, &key,
1191                                     BTRFS_FT_DIR, index);
1192         /* We have check then name at the beginning, so it is impossible. */
1193         BUG_ON(ret == -EEXIST);
1194         if (ret) {
1195                 btrfs_abort_transaction(trans, root, ret);
1196                 goto fail;
1197         }
1198
1199         btrfs_i_size_write(parent_inode, parent_inode->i_size +
1200                                          dentry->d_name.len * 2);
1201         parent_inode->i_mtime = parent_inode->i_ctime = CURRENT_TIME;
1202         ret = btrfs_update_inode_fallback(trans, parent_root, parent_inode);
1203         if (ret)
1204                 btrfs_abort_transaction(trans, root, ret);
1205 fail:
1206         dput(parent);
1207         trans->block_rsv = rsv;
1208 no_free_objectid:
1209         kfree(new_root_item);
1210 root_item_alloc_fail:
1211         btrfs_free_path(path);
1212 path_alloc_fail:
1213         btrfs_block_rsv_release(root, &pending->block_rsv, (u64)-1);
1214         return ret;
1215 }
1216
1217 /*
1218  * create all the snapshots we've scheduled for creation
1219  */
1220 static noinline int create_pending_snapshots(struct btrfs_trans_handle *trans,
1221                                              struct btrfs_fs_info *fs_info)
1222 {
1223         struct btrfs_pending_snapshot *pending;
1224         struct list_head *head = &trans->transaction->pending_snapshots;
1225
1226         list_for_each_entry(pending, head, list)
1227                 create_pending_snapshot(trans, fs_info, pending);
1228         return 0;
1229 }
1230
1231 static void update_super_roots(struct btrfs_root *root)
1232 {
1233         struct btrfs_root_item *root_item;
1234         struct btrfs_super_block *super;
1235
1236         super = root->fs_info->super_copy;
1237
1238         root_item = &root->fs_info->chunk_root->root_item;
1239         super->chunk_root = root_item->bytenr;
1240         super->chunk_root_generation = root_item->generation;
1241         super->chunk_root_level = root_item->level;
1242
1243         root_item = &root->fs_info->tree_root->root_item;
1244         super->root = root_item->bytenr;
1245         super->generation = root_item->generation;
1246         super->root_level = root_item->level;
1247         if (btrfs_test_opt(root, SPACE_CACHE))
1248                 super->cache_generation = root_item->generation;
1249 }
1250
1251 int btrfs_transaction_in_commit(struct btrfs_fs_info *info)
1252 {
1253         int ret = 0;
1254         spin_lock(&info->trans_lock);
1255         if (info->running_transaction)
1256                 ret = info->running_transaction->in_commit;
1257         spin_unlock(&info->trans_lock);
1258         return ret;
1259 }
1260
1261 int btrfs_transaction_blocked(struct btrfs_fs_info *info)
1262 {
1263         int ret = 0;
1264         spin_lock(&info->trans_lock);
1265         if (info->running_transaction)
1266                 ret = info->running_transaction->blocked;
1267         spin_unlock(&info->trans_lock);
1268         return ret;
1269 }
1270
1271 /*
1272  * wait for the current transaction commit to start and block subsequent
1273  * transaction joins
1274  */
1275 static void wait_current_trans_commit_start(struct btrfs_root *root,
1276                                             struct btrfs_transaction *trans)
1277 {
1278         wait_event(root->fs_info->transaction_blocked_wait, trans->in_commit);
1279 }
1280
1281 /*
1282  * wait for the current transaction to start and then become unblocked.
1283  * caller holds ref.
1284  */
1285 static void wait_current_trans_commit_start_and_unblock(struct btrfs_root *root,
1286                                          struct btrfs_transaction *trans)
1287 {
1288         wait_event(root->fs_info->transaction_wait,
1289                    trans->commit_done || (trans->in_commit && !trans->blocked));
1290 }
1291
1292 /*
1293  * commit transactions asynchronously. once btrfs_commit_transaction_async
1294  * returns, any subsequent transaction will not be allowed to join.
1295  */
1296 struct btrfs_async_commit {
1297         struct btrfs_trans_handle *newtrans;
1298         struct btrfs_root *root;
1299         struct delayed_work work;
1300 };
1301
1302 static void do_async_commit(struct work_struct *work)
1303 {
1304         struct btrfs_async_commit *ac =
1305                 container_of(work, struct btrfs_async_commit, work.work);
1306
1307         /*
1308          * We've got freeze protection passed with the transaction.
1309          * Tell lockdep about it.
1310          */
1311         rwsem_acquire_read(
1312                 &ac->root->fs_info->sb->s_writers.lock_map[SB_FREEZE_FS-1],
1313                 0, 1, _THIS_IP_);
1314
1315         current->journal_info = ac->newtrans;
1316
1317         btrfs_commit_transaction(ac->newtrans, ac->root);
1318         kfree(ac);
1319 }
1320
1321 int btrfs_commit_transaction_async(struct btrfs_trans_handle *trans,
1322                                    struct btrfs_root *root,
1323                                    int wait_for_unblock)
1324 {
1325         struct btrfs_async_commit *ac;
1326         struct btrfs_transaction *cur_trans;
1327
1328         ac = kmalloc(sizeof(*ac), GFP_NOFS);
1329         if (!ac)
1330                 return -ENOMEM;
1331
1332         INIT_DELAYED_WORK(&ac->work, do_async_commit);
1333         ac->root = root;
1334         ac->newtrans = btrfs_join_transaction(root);
1335         if (IS_ERR(ac->newtrans)) {
1336                 int err = PTR_ERR(ac->newtrans);
1337                 kfree(ac);
1338                 return err;
1339         }
1340
1341         /* take transaction reference */
1342         cur_trans = trans->transaction;
1343         atomic_inc(&cur_trans->use_count);
1344
1345         btrfs_end_transaction(trans, root);
1346
1347         /*
1348          * Tell lockdep we've released the freeze rwsem, since the
1349          * async commit thread will be the one to unlock it.
1350          */
1351         rwsem_release(&root->fs_info->sb->s_writers.lock_map[SB_FREEZE_FS-1],
1352                       1, _THIS_IP_);
1353
1354         schedule_delayed_work(&ac->work, 0);
1355
1356         /* wait for transaction to start and unblock */
1357         if (wait_for_unblock)
1358                 wait_current_trans_commit_start_and_unblock(root, cur_trans);
1359         else
1360                 wait_current_trans_commit_start(root, cur_trans);
1361
1362         if (current->journal_info == trans)
1363                 current->journal_info = NULL;
1364
1365         put_transaction(cur_trans);
1366         return 0;
1367 }
1368
1369
1370 static void cleanup_transaction(struct btrfs_trans_handle *trans,
1371                                 struct btrfs_root *root, int err)
1372 {
1373         struct btrfs_transaction *cur_trans = trans->transaction;
1374
1375         WARN_ON(trans->use_count > 1);
1376
1377         btrfs_abort_transaction(trans, root, err);
1378
1379         spin_lock(&root->fs_info->trans_lock);
1380         list_del_init(&cur_trans->list);
1381         if (cur_trans == root->fs_info->running_transaction) {
1382                 root->fs_info->running_transaction = NULL;
1383                 root->fs_info->trans_no_join = 0;
1384         }
1385         spin_unlock(&root->fs_info->trans_lock);
1386
1387         btrfs_cleanup_one_transaction(trans->transaction, root);
1388
1389         put_transaction(cur_trans);
1390         put_transaction(cur_trans);
1391
1392         trace_btrfs_transaction_commit(root);
1393
1394         btrfs_scrub_continue(root);
1395
1396         if (current->journal_info == trans)
1397                 current->journal_info = NULL;
1398
1399         kmem_cache_free(btrfs_trans_handle_cachep, trans);
1400 }
1401
1402 /*
1403  * btrfs_transaction state sequence:
1404  *    in_commit = 0, blocked = 0  (initial)
1405  *    in_commit = 1, blocked = 1
1406  *    blocked = 0
1407  *    commit_done = 1
1408  */
1409 int btrfs_commit_transaction(struct btrfs_trans_handle *trans,
1410                              struct btrfs_root *root)
1411 {
1412         unsigned long joined = 0;
1413         struct btrfs_transaction *cur_trans = trans->transaction;
1414         struct btrfs_transaction *prev_trans = NULL;
1415         DEFINE_WAIT(wait);
1416         int ret = -EIO;
1417         int should_grow = 0;
1418         unsigned long now = get_seconds();
1419         int flush_on_commit = btrfs_test_opt(root, FLUSHONCOMMIT);
1420
1421         btrfs_run_ordered_operations(root, 0);
1422
1423         if (cur_trans->aborted)
1424                 goto cleanup_transaction;
1425
1426         /* make a pass through all the delayed refs we have so far
1427          * any runnings procs may add more while we are here
1428          */
1429         ret = btrfs_run_delayed_refs(trans, root, 0);
1430         if (ret)
1431                 goto cleanup_transaction;
1432
1433         btrfs_trans_release_metadata(trans, root);
1434         trans->block_rsv = NULL;
1435
1436         cur_trans = trans->transaction;
1437
1438         /*
1439          * set the flushing flag so procs in this transaction have to
1440          * start sending their work down.
1441          */
1442         cur_trans->delayed_refs.flushing = 1;
1443
1444         if (!list_empty(&trans->new_bgs))
1445                 btrfs_create_pending_block_groups(trans, root);
1446
1447         ret = btrfs_run_delayed_refs(trans, root, 0);
1448         if (ret)
1449                 goto cleanup_transaction;
1450
1451         spin_lock(&cur_trans->commit_lock);
1452         if (cur_trans->in_commit) {
1453                 spin_unlock(&cur_trans->commit_lock);
1454                 atomic_inc(&cur_trans->use_count);
1455                 ret = btrfs_end_transaction(trans, root);
1456
1457                 wait_for_commit(root, cur_trans);
1458
1459                 put_transaction(cur_trans);
1460
1461                 return ret;
1462         }
1463
1464         trans->transaction->in_commit = 1;
1465         trans->transaction->blocked = 1;
1466         spin_unlock(&cur_trans->commit_lock);
1467         wake_up(&root->fs_info->transaction_blocked_wait);
1468
1469         spin_lock(&root->fs_info->trans_lock);
1470         if (cur_trans->list.prev != &root->fs_info->trans_list) {
1471                 prev_trans = list_entry(cur_trans->list.prev,
1472                                         struct btrfs_transaction, list);
1473                 if (!prev_trans->commit_done) {
1474                         atomic_inc(&prev_trans->use_count);
1475                         spin_unlock(&root->fs_info->trans_lock);
1476
1477                         wait_for_commit(root, prev_trans);
1478
1479                         put_transaction(prev_trans);
1480                 } else {
1481                         spin_unlock(&root->fs_info->trans_lock);
1482                 }
1483         } else {
1484                 spin_unlock(&root->fs_info->trans_lock);
1485         }
1486
1487         if (!btrfs_test_opt(root, SSD) &&
1488             (now < cur_trans->start_time || now - cur_trans->start_time < 1))
1489                 should_grow = 1;
1490
1491         do {
1492                 int snap_pending = 0;
1493
1494                 joined = cur_trans->num_joined;
1495                 if (!list_empty(&trans->transaction->pending_snapshots))
1496                         snap_pending = 1;
1497
1498                 WARN_ON(cur_trans != trans->transaction);
1499
1500                 if (flush_on_commit || snap_pending) {
1501                         btrfs_start_delalloc_inodes(root, 1);
1502                         btrfs_wait_ordered_extents(root, 1);
1503                 }
1504
1505                 ret = btrfs_run_delayed_items(trans, root);
1506                 if (ret)
1507                         goto cleanup_transaction;
1508
1509                 /*
1510                  * running the delayed items may have added new refs. account
1511                  * them now so that they hinder processing of more delayed refs
1512                  * as little as possible.
1513                  */
1514                 btrfs_delayed_refs_qgroup_accounting(trans, root->fs_info);
1515
1516                 /*
1517                  * rename don't use btrfs_join_transaction, so, once we
1518                  * set the transaction to blocked above, we aren't going
1519                  * to get any new ordered operations.  We can safely run
1520                  * it here and no for sure that nothing new will be added
1521                  * to the list
1522                  */
1523                 btrfs_run_ordered_operations(root, 1);
1524
1525                 prepare_to_wait(&cur_trans->writer_wait, &wait,
1526                                 TASK_UNINTERRUPTIBLE);
1527
1528                 if (atomic_read(&cur_trans->num_writers) > 1)
1529                         schedule_timeout(MAX_SCHEDULE_TIMEOUT);
1530                 else if (should_grow)
1531                         schedule_timeout(1);
1532
1533                 finish_wait(&cur_trans->writer_wait, &wait);
1534         } while (atomic_read(&cur_trans->num_writers) > 1 ||
1535                  (should_grow && cur_trans->num_joined != joined));
1536
1537         /*
1538          * Ok now we need to make sure to block out any other joins while we
1539          * commit the transaction.  We could have started a join before setting
1540          * no_join so make sure to wait for num_writers to == 1 again.
1541          */
1542         spin_lock(&root->fs_info->trans_lock);
1543         root->fs_info->trans_no_join = 1;
1544         spin_unlock(&root->fs_info->trans_lock);
1545         wait_event(cur_trans->writer_wait,
1546                    atomic_read(&cur_trans->num_writers) == 1);
1547
1548         /*
1549          * the reloc mutex makes sure that we stop
1550          * the balancing code from coming in and moving
1551          * extents around in the middle of the commit
1552          */
1553         mutex_lock(&root->fs_info->reloc_mutex);
1554
1555         /*
1556          * We needn't worry about the delayed items because we will
1557          * deal with them in create_pending_snapshot(), which is the
1558          * core function of the snapshot creation.
1559          */
1560         ret = create_pending_snapshots(trans, root->fs_info);
1561         if (ret) {
1562                 mutex_unlock(&root->fs_info->reloc_mutex);
1563                 goto cleanup_transaction;
1564         }
1565
1566         /*
1567          * We insert the dir indexes of the snapshots and update the inode
1568          * of the snapshots' parents after the snapshot creation, so there
1569          * are some delayed items which are not dealt with. Now deal with
1570          * them.
1571          *
1572          * We needn't worry that this operation will corrupt the snapshots,
1573          * because all the tree which are snapshoted will be forced to COW
1574          * the nodes and leaves.
1575          */
1576         ret = btrfs_run_delayed_items(trans, root);
1577         if (ret) {
1578                 mutex_unlock(&root->fs_info->reloc_mutex);
1579                 goto cleanup_transaction;
1580         }
1581
1582         ret = btrfs_run_delayed_refs(trans, root, (unsigned long)-1);
1583         if (ret) {
1584                 mutex_unlock(&root->fs_info->reloc_mutex);
1585                 goto cleanup_transaction;
1586         }
1587
1588         /*
1589          * make sure none of the code above managed to slip in a
1590          * delayed item
1591          */
1592         btrfs_assert_delayed_root_empty(root);
1593
1594         WARN_ON(cur_trans != trans->transaction);
1595
1596         btrfs_scrub_pause(root);
1597         /* btrfs_commit_tree_roots is responsible for getting the
1598          * various roots consistent with each other.  Every pointer
1599          * in the tree of tree roots has to point to the most up to date
1600          * root for every subvolume and other tree.  So, we have to keep
1601          * the tree logging code from jumping in and changing any
1602          * of the trees.
1603          *
1604          * At this point in the commit, there can't be any tree-log
1605          * writers, but a little lower down we drop the trans mutex
1606          * and let new people in.  By holding the tree_log_mutex
1607          * from now until after the super is written, we avoid races
1608          * with the tree-log code.
1609          */
1610         mutex_lock(&root->fs_info->tree_log_mutex);
1611
1612         ret = commit_fs_roots(trans, root);
1613         if (ret) {
1614                 mutex_unlock(&root->fs_info->tree_log_mutex);
1615                 mutex_unlock(&root->fs_info->reloc_mutex);
1616                 goto cleanup_transaction;
1617         }
1618
1619         /* commit_fs_roots gets rid of all the tree log roots, it is now
1620          * safe to free the root of tree log roots
1621          */
1622         btrfs_free_log_root_tree(trans, root->fs_info);
1623
1624         ret = commit_cowonly_roots(trans, root);
1625         if (ret) {
1626                 mutex_unlock(&root->fs_info->tree_log_mutex);
1627                 mutex_unlock(&root->fs_info->reloc_mutex);
1628                 goto cleanup_transaction;
1629         }
1630
1631         btrfs_prepare_extent_commit(trans, root);
1632
1633         cur_trans = root->fs_info->running_transaction;
1634
1635         btrfs_set_root_node(&root->fs_info->tree_root->root_item,
1636                             root->fs_info->tree_root->node);
1637         switch_commit_root(root->fs_info->tree_root);
1638
1639         btrfs_set_root_node(&root->fs_info->chunk_root->root_item,
1640                             root->fs_info->chunk_root->node);
1641         switch_commit_root(root->fs_info->chunk_root);
1642
1643         assert_qgroups_uptodate(trans);
1644         update_super_roots(root);
1645
1646         if (!root->fs_info->log_root_recovering) {
1647                 btrfs_set_super_log_root(root->fs_info->super_copy, 0);
1648                 btrfs_set_super_log_root_level(root->fs_info->super_copy, 0);
1649         }
1650
1651         memcpy(root->fs_info->super_for_commit, root->fs_info->super_copy,
1652                sizeof(*root->fs_info->super_copy));
1653
1654         trans->transaction->blocked = 0;
1655         spin_lock(&root->fs_info->trans_lock);
1656         root->fs_info->running_transaction = NULL;
1657         root->fs_info->trans_no_join = 0;
1658         spin_unlock(&root->fs_info->trans_lock);
1659         mutex_unlock(&root->fs_info->reloc_mutex);
1660
1661         wake_up(&root->fs_info->transaction_wait);
1662
1663         ret = btrfs_write_and_wait_transaction(trans, root);
1664         if (ret) {
1665                 btrfs_error(root->fs_info, ret,
1666                             "Error while writing out transaction.");
1667                 mutex_unlock(&root->fs_info->tree_log_mutex);
1668                 goto cleanup_transaction;
1669         }
1670
1671         ret = write_ctree_super(trans, root, 0);
1672         if (ret) {
1673                 mutex_unlock(&root->fs_info->tree_log_mutex);
1674                 goto cleanup_transaction;
1675         }
1676
1677         /*
1678          * the super is written, we can safely allow the tree-loggers
1679          * to go about their business
1680          */
1681         mutex_unlock(&root->fs_info->tree_log_mutex);
1682
1683         btrfs_finish_extent_commit(trans, root);
1684
1685         cur_trans->commit_done = 1;
1686
1687         root->fs_info->last_trans_committed = cur_trans->transid;
1688
1689         wake_up(&cur_trans->commit_wait);
1690
1691         spin_lock(&root->fs_info->trans_lock);
1692         list_del_init(&cur_trans->list);
1693         spin_unlock(&root->fs_info->trans_lock);
1694
1695         put_transaction(cur_trans);
1696         put_transaction(cur_trans);
1697
1698         if (trans->type < TRANS_JOIN_NOLOCK)
1699                 sb_end_intwrite(root->fs_info->sb);
1700
1701         trace_btrfs_transaction_commit(root);
1702
1703         btrfs_scrub_continue(root);
1704
1705         if (current->journal_info == trans)
1706                 current->journal_info = NULL;
1707
1708         kmem_cache_free(btrfs_trans_handle_cachep, trans);
1709
1710         if (current != root->fs_info->transaction_kthread)
1711                 btrfs_run_delayed_iputs(root);
1712
1713         return ret;
1714
1715 cleanup_transaction:
1716         btrfs_trans_release_metadata(trans, root);
1717         trans->block_rsv = NULL;
1718         btrfs_printk(root->fs_info, "Skipping commit of aborted transaction.\n");
1719 //      WARN_ON(1);
1720         if (current->journal_info == trans)
1721                 current->journal_info = NULL;
1722         cleanup_transaction(trans, root, ret);
1723
1724         return ret;
1725 }
1726
1727 /*
1728  * interface function to delete all the snapshots we have scheduled for deletion
1729  */
1730 int btrfs_clean_old_snapshots(struct btrfs_root *root)
1731 {
1732         LIST_HEAD(list);
1733         struct btrfs_fs_info *fs_info = root->fs_info;
1734
1735         spin_lock(&fs_info->trans_lock);
1736         list_splice_init(&fs_info->dead_roots, &list);
1737         spin_unlock(&fs_info->trans_lock);
1738
1739         while (!list_empty(&list)) {
1740                 int ret;
1741
1742                 root = list_entry(list.next, struct btrfs_root, root_list);
1743                 list_del(&root->root_list);
1744
1745                 btrfs_kill_all_delayed_nodes(root);
1746
1747                 if (btrfs_header_backref_rev(root->node) <
1748                     BTRFS_MIXED_BACKREF_REV)
1749                         ret = btrfs_drop_snapshot(root, NULL, 0, 0);
1750                 else
1751                         ret =btrfs_drop_snapshot(root, NULL, 1, 0);
1752                 BUG_ON(ret < 0);
1753         }
1754         return 0;
1755 }