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