Btrfs: Tree logging fixes
[pandora-kernel.git] / fs / btrfs / transaction.c
1 /*
2  * Copyright (C) 2007 Oracle.  All rights reserved.
3  *
4  * This program is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU General Public
6  * License v2 as published by the Free Software Foundation.
7  *
8  * This program is distributed in the hope that it will be useful,
9  * but WITHOUT ANY WARRANTY; without even the implied warranty of
10  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
11  * General Public License for more details.
12  *
13  * You should have received a copy of the GNU General Public
14  * License along with this program; if not, write to the
15  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
16  * Boston, MA 021110-1307, USA.
17  */
18
19 #include <linux/fs.h>
20 #include <linux/sched.h>
21 #include <linux/writeback.h>
22 #include <linux/pagemap.h>
23 #include "ctree.h"
24 #include "disk-io.h"
25 #include "transaction.h"
26 #include "locking.h"
27 #include "ref-cache.h"
28 #include "tree-log.h"
29
30 static int total_trans = 0;
31 extern struct kmem_cache *btrfs_trans_handle_cachep;
32 extern struct kmem_cache *btrfs_transaction_cachep;
33
34 #define BTRFS_ROOT_TRANS_TAG 0
35
36 static noinline void put_transaction(struct btrfs_transaction *transaction)
37 {
38         WARN_ON(transaction->use_count == 0);
39         transaction->use_count--;
40         if (transaction->use_count == 0) {
41                 WARN_ON(total_trans == 0);
42                 total_trans--;
43                 list_del_init(&transaction->list);
44                 memset(transaction, 0, sizeof(*transaction));
45                 kmem_cache_free(btrfs_transaction_cachep, transaction);
46         }
47 }
48
49 static noinline int join_transaction(struct btrfs_root *root)
50 {
51         struct btrfs_transaction *cur_trans;
52         cur_trans = root->fs_info->running_transaction;
53         if (!cur_trans) {
54                 cur_trans = kmem_cache_alloc(btrfs_transaction_cachep,
55                                              GFP_NOFS);
56                 total_trans++;
57                 BUG_ON(!cur_trans);
58                 root->fs_info->generation++;
59                 root->fs_info->last_alloc = 0;
60                 root->fs_info->last_data_alloc = 0;
61                 root->fs_info->last_log_alloc = 0;
62                 cur_trans->num_writers = 1;
63                 cur_trans->num_joined = 0;
64                 cur_trans->transid = root->fs_info->generation;
65                 init_waitqueue_head(&cur_trans->writer_wait);
66                 init_waitqueue_head(&cur_trans->commit_wait);
67                 cur_trans->in_commit = 0;
68                 cur_trans->blocked = 0;
69                 cur_trans->use_count = 1;
70                 cur_trans->commit_done = 0;
71                 cur_trans->start_time = get_seconds();
72                 INIT_LIST_HEAD(&cur_trans->pending_snapshots);
73                 list_add_tail(&cur_trans->list, &root->fs_info->trans_list);
74                 extent_io_tree_init(&cur_trans->dirty_pages,
75                                      root->fs_info->btree_inode->i_mapping,
76                                      GFP_NOFS);
77                 spin_lock(&root->fs_info->new_trans_lock);
78                 root->fs_info->running_transaction = cur_trans;
79                 spin_unlock(&root->fs_info->new_trans_lock);
80         } else {
81                 cur_trans->num_writers++;
82                 cur_trans->num_joined++;
83         }
84
85         return 0;
86 }
87
88 noinline int btrfs_record_root_in_trans(struct btrfs_root *root)
89 {
90         struct btrfs_dirty_root *dirty;
91         u64 running_trans_id = root->fs_info->running_transaction->transid;
92         if (root->ref_cows && root->last_trans < running_trans_id) {
93                 WARN_ON(root == root->fs_info->extent_root);
94                 if (root->root_item.refs != 0) {
95                         radix_tree_tag_set(&root->fs_info->fs_roots_radix,
96                                    (unsigned long)root->root_key.objectid,
97                                    BTRFS_ROOT_TRANS_TAG);
98
99                         dirty = kmalloc(sizeof(*dirty), GFP_NOFS);
100                         BUG_ON(!dirty);
101                         dirty->root = kmalloc(sizeof(*dirty->root), GFP_NOFS);
102                         BUG_ON(!dirty->root);
103                         dirty->latest_root = root;
104                         INIT_LIST_HEAD(&dirty->list);
105
106                         root->commit_root = btrfs_root_node(root);
107
108                         memcpy(dirty->root, root, sizeof(*root));
109                         spin_lock_init(&dirty->root->node_lock);
110                         spin_lock_init(&dirty->root->list_lock);
111                         mutex_init(&dirty->root->objectid_mutex);
112                         INIT_LIST_HEAD(&dirty->root->dead_list);
113                         dirty->root->node = root->commit_root;
114                         dirty->root->commit_root = NULL;
115
116                         spin_lock(&root->list_lock);
117                         list_add(&dirty->root->dead_list, &root->dead_list);
118                         spin_unlock(&root->list_lock);
119
120                         root->dirty_root = dirty;
121                 } else {
122                         WARN_ON(1);
123                 }
124                 root->last_trans = running_trans_id;
125         }
126         return 0;
127 }
128
129 static void wait_current_trans(struct btrfs_root *root)
130 {
131         struct btrfs_transaction *cur_trans;
132
133         cur_trans = root->fs_info->running_transaction;
134         if (cur_trans && cur_trans->blocked) {
135                 DEFINE_WAIT(wait);
136                 cur_trans->use_count++;
137                 while(1) {
138                         prepare_to_wait(&root->fs_info->transaction_wait, &wait,
139                                         TASK_UNINTERRUPTIBLE);
140                         if (cur_trans->blocked) {
141                                 mutex_unlock(&root->fs_info->trans_mutex);
142                                 schedule();
143                                 mutex_lock(&root->fs_info->trans_mutex);
144                                 finish_wait(&root->fs_info->transaction_wait,
145                                             &wait);
146                         } else {
147                                 finish_wait(&root->fs_info->transaction_wait,
148                                             &wait);
149                                 break;
150                         }
151                 }
152                 put_transaction(cur_trans);
153         }
154 }
155
156 static struct btrfs_trans_handle *start_transaction(struct btrfs_root *root,
157                                              int num_blocks, int wait)
158 {
159         struct btrfs_trans_handle *h =
160                 kmem_cache_alloc(btrfs_trans_handle_cachep, GFP_NOFS);
161         int ret;
162
163         mutex_lock(&root->fs_info->trans_mutex);
164         if (!root->fs_info->log_root_recovering &&
165             ((wait == 1 && !root->fs_info->open_ioctl_trans) || wait == 2))
166                 wait_current_trans(root);
167         ret = join_transaction(root);
168         BUG_ON(ret);
169
170         btrfs_record_root_in_trans(root);
171         h->transid = root->fs_info->running_transaction->transid;
172         h->transaction = root->fs_info->running_transaction;
173         h->blocks_reserved = num_blocks;
174         h->blocks_used = 0;
175         h->block_group = NULL;
176         h->alloc_exclude_nr = 0;
177         h->alloc_exclude_start = 0;
178         root->fs_info->running_transaction->use_count++;
179         mutex_unlock(&root->fs_info->trans_mutex);
180         return h;
181 }
182
183 struct btrfs_trans_handle *btrfs_start_transaction(struct btrfs_root *root,
184                                                    int num_blocks)
185 {
186         return start_transaction(root, num_blocks, 1);
187 }
188 struct btrfs_trans_handle *btrfs_join_transaction(struct btrfs_root *root,
189                                                    int num_blocks)
190 {
191         return start_transaction(root, num_blocks, 0);
192 }
193
194 struct btrfs_trans_handle *btrfs_start_ioctl_transaction(struct btrfs_root *r,
195                                                          int num_blocks)
196 {
197         return start_transaction(r, num_blocks, 2);
198 }
199
200
201 static noinline int wait_for_commit(struct btrfs_root *root,
202                                     struct btrfs_transaction *commit)
203 {
204         DEFINE_WAIT(wait);
205         mutex_lock(&root->fs_info->trans_mutex);
206         while(!commit->commit_done) {
207                 prepare_to_wait(&commit->commit_wait, &wait,
208                                 TASK_UNINTERRUPTIBLE);
209                 if (commit->commit_done)
210                         break;
211                 mutex_unlock(&root->fs_info->trans_mutex);
212                 schedule();
213                 mutex_lock(&root->fs_info->trans_mutex);
214         }
215         mutex_unlock(&root->fs_info->trans_mutex);
216         finish_wait(&commit->commit_wait, &wait);
217         return 0;
218 }
219
220 static void throttle_on_drops(struct btrfs_root *root)
221 {
222         struct btrfs_fs_info *info = root->fs_info;
223         int harder_count = 0;
224
225 harder:
226         if (atomic_read(&info->throttles)) {
227                 DEFINE_WAIT(wait);
228                 int thr;
229                 thr = atomic_read(&info->throttle_gen);
230
231                 do {
232                         prepare_to_wait(&info->transaction_throttle,
233                                         &wait, TASK_UNINTERRUPTIBLE);
234                         if (!atomic_read(&info->throttles)) {
235                                 finish_wait(&info->transaction_throttle, &wait);
236                                 break;
237                         }
238                         schedule();
239                         finish_wait(&info->transaction_throttle, &wait);
240                 } while (thr == atomic_read(&info->throttle_gen));
241                 harder_count++;
242
243                 if (root->fs_info->total_ref_cache_size > 1 * 1024 * 1024 &&
244                     harder_count < 2)
245                         goto harder;
246
247                 if (root->fs_info->total_ref_cache_size > 5 * 1024 * 1024 &&
248                     harder_count < 10)
249                         goto harder;
250
251                 if (root->fs_info->total_ref_cache_size > 10 * 1024 * 1024 &&
252                     harder_count < 20)
253                         goto harder;
254         }
255 }
256
257 void btrfs_throttle(struct btrfs_root *root)
258 {
259         mutex_lock(&root->fs_info->trans_mutex);
260         if (!root->fs_info->open_ioctl_trans)
261                 wait_current_trans(root);
262         mutex_unlock(&root->fs_info->trans_mutex);
263
264         throttle_on_drops(root);
265 }
266
267 static int __btrfs_end_transaction(struct btrfs_trans_handle *trans,
268                           struct btrfs_root *root, int throttle)
269 {
270         struct btrfs_transaction *cur_trans;
271         struct btrfs_fs_info *info = root->fs_info;
272
273         mutex_lock(&info->trans_mutex);
274         cur_trans = info->running_transaction;
275         WARN_ON(cur_trans != trans->transaction);
276         WARN_ON(cur_trans->num_writers < 1);
277         cur_trans->num_writers--;
278
279         if (waitqueue_active(&cur_trans->writer_wait))
280                 wake_up(&cur_trans->writer_wait);
281         put_transaction(cur_trans);
282         mutex_unlock(&info->trans_mutex);
283         memset(trans, 0, sizeof(*trans));
284         kmem_cache_free(btrfs_trans_handle_cachep, trans);
285
286         if (throttle)
287                 throttle_on_drops(root);
288
289         return 0;
290 }
291
292 int btrfs_end_transaction(struct btrfs_trans_handle *trans,
293                           struct btrfs_root *root)
294 {
295         return __btrfs_end_transaction(trans, root, 0);
296 }
297
298 int btrfs_end_transaction_throttle(struct btrfs_trans_handle *trans,
299                                    struct btrfs_root *root)
300 {
301         return __btrfs_end_transaction(trans, root, 1);
302 }
303
304
305 int btrfs_write_and_wait_transaction(struct btrfs_trans_handle *trans,
306                                      struct btrfs_root *root)
307 {
308         int ret;
309         int err = 0;
310         int werr = 0;
311         struct extent_io_tree *dirty_pages;
312         struct page *page;
313         struct inode *btree_inode = root->fs_info->btree_inode;
314         u64 start = 0;
315         u64 end;
316         unsigned long index;
317
318         if (!trans || !trans->transaction) {
319                 return filemap_write_and_wait(btree_inode->i_mapping);
320         }
321         dirty_pages = &trans->transaction->dirty_pages;
322         while(1) {
323                 ret = find_first_extent_bit(dirty_pages, start, &start, &end,
324                                             EXTENT_DIRTY);
325                 if (ret)
326                         break;
327                 while(start <= end) {
328                         cond_resched();
329
330                         index = start >> PAGE_CACHE_SHIFT;
331                         start = (u64)(index + 1) << PAGE_CACHE_SHIFT;
332                         page = find_get_page(btree_inode->i_mapping, index);
333                         if (!page)
334                                 continue;
335
336                         btree_lock_page_hook(page);
337                         if (!page->mapping) {
338                                 unlock_page(page);
339                                 page_cache_release(page);
340                                 continue;
341                         }
342
343                         if (PageWriteback(page)) {
344                                 if (PageDirty(page))
345                                         wait_on_page_writeback(page);
346                                 else {
347                                         unlock_page(page);
348                                         page_cache_release(page);
349                                         continue;
350                                 }
351                         }
352                         err = write_one_page(page, 0);
353                         if (err)
354                                 werr = err;
355                         page_cache_release(page);
356                 }
357         }
358         while(1) {
359                 ret = find_first_extent_bit(dirty_pages, 0, &start, &end,
360                                             EXTENT_DIRTY);
361                 if (ret)
362                         break;
363
364                 clear_extent_dirty(dirty_pages, start, end, GFP_NOFS);
365                 while(start <= end) {
366                         index = start >> PAGE_CACHE_SHIFT;
367                         start = (u64)(index + 1) << PAGE_CACHE_SHIFT;
368                         page = find_get_page(btree_inode->i_mapping, index);
369                         if (!page)
370                                 continue;
371                         if (PageDirty(page)) {
372                                 btree_lock_page_hook(page);
373                                 wait_on_page_writeback(page);
374                                 err = write_one_page(page, 0);
375                                 if (err)
376                                         werr = err;
377                         }
378                         wait_on_page_writeback(page);
379                         page_cache_release(page);
380                         cond_resched();
381                 }
382         }
383         if (err)
384                 werr = err;
385         return werr;
386 }
387
388 static int update_cowonly_root(struct btrfs_trans_handle *trans,
389                                struct btrfs_root *root)
390 {
391         int ret;
392         u64 old_root_bytenr;
393         struct btrfs_root *tree_root = root->fs_info->tree_root;
394
395         btrfs_write_dirty_block_groups(trans, root);
396         while(1) {
397                 old_root_bytenr = btrfs_root_bytenr(&root->root_item);
398                 if (old_root_bytenr == root->node->start)
399                         break;
400                 btrfs_set_root_bytenr(&root->root_item,
401                                        root->node->start);
402                 btrfs_set_root_level(&root->root_item,
403                                      btrfs_header_level(root->node));
404                 ret = btrfs_update_root(trans, tree_root,
405                                         &root->root_key,
406                                         &root->root_item);
407                 BUG_ON(ret);
408                 btrfs_write_dirty_block_groups(trans, root);
409         }
410         return 0;
411 }
412
413 int btrfs_commit_tree_roots(struct btrfs_trans_handle *trans,
414                             struct btrfs_root *root)
415 {
416         struct btrfs_fs_info *fs_info = root->fs_info;
417         struct list_head *next;
418
419         while(!list_empty(&fs_info->dirty_cowonly_roots)) {
420                 next = fs_info->dirty_cowonly_roots.next;
421                 list_del_init(next);
422                 root = list_entry(next, struct btrfs_root, dirty_list);
423                 update_cowonly_root(trans, root);
424         }
425         return 0;
426 }
427
428 int btrfs_add_dead_root(struct btrfs_root *root, struct btrfs_root *latest)
429 {
430         struct btrfs_dirty_root *dirty;
431
432         dirty = kmalloc(sizeof(*dirty), GFP_NOFS);
433         if (!dirty)
434                 return -ENOMEM;
435         dirty->root = root;
436         dirty->latest_root = latest;
437
438         mutex_lock(&root->fs_info->trans_mutex);
439         list_add(&dirty->list, &latest->fs_info->dead_roots);
440         mutex_unlock(&root->fs_info->trans_mutex);
441         return 0;
442 }
443
444 static noinline int add_dirty_roots(struct btrfs_trans_handle *trans,
445                                     struct radix_tree_root *radix,
446                                     struct list_head *list)
447 {
448         struct btrfs_dirty_root *dirty;
449         struct btrfs_root *gang[8];
450         struct btrfs_root *root;
451         int i;
452         int ret;
453         int err = 0;
454         u32 refs;
455
456         while(1) {
457                 ret = radix_tree_gang_lookup_tag(radix, (void **)gang, 0,
458                                                  ARRAY_SIZE(gang),
459                                                  BTRFS_ROOT_TRANS_TAG);
460                 if (ret == 0)
461                         break;
462                 for (i = 0; i < ret; i++) {
463                         root = gang[i];
464                         radix_tree_tag_clear(radix,
465                                      (unsigned long)root->root_key.objectid,
466                                      BTRFS_ROOT_TRANS_TAG);
467
468                         BUG_ON(!root->ref_tree);
469                         dirty = root->dirty_root;
470
471                         btrfs_free_log(trans, root);
472
473                         if (root->commit_root == root->node) {
474                                 WARN_ON(root->node->start !=
475                                         btrfs_root_bytenr(&root->root_item));
476
477                                 free_extent_buffer(root->commit_root);
478                                 root->commit_root = NULL;
479                                 root->dirty_root = NULL;
480
481                                 spin_lock(&root->list_lock);
482                                 list_del_init(&dirty->root->dead_list);
483                                 spin_unlock(&root->list_lock);
484
485                                 kfree(dirty->root);
486                                 kfree(dirty);
487
488                                 /* make sure to update the root on disk
489                                  * so we get any updates to the block used
490                                  * counts
491                                  */
492                                 err = btrfs_update_root(trans,
493                                                 root->fs_info->tree_root,
494                                                 &root->root_key,
495                                                 &root->root_item);
496                                 continue;
497                         }
498
499                         memset(&root->root_item.drop_progress, 0,
500                                sizeof(struct btrfs_disk_key));
501                         root->root_item.drop_level = 0;
502                         root->commit_root = NULL;
503                         root->dirty_root = NULL;
504                         root->root_key.offset = root->fs_info->generation;
505                         btrfs_set_root_bytenr(&root->root_item,
506                                               root->node->start);
507                         btrfs_set_root_level(&root->root_item,
508                                              btrfs_header_level(root->node));
509                         err = btrfs_insert_root(trans, root->fs_info->tree_root,
510                                                 &root->root_key,
511                                                 &root->root_item);
512                         if (err)
513                                 break;
514
515                         refs = btrfs_root_refs(&dirty->root->root_item);
516                         btrfs_set_root_refs(&dirty->root->root_item, refs - 1);
517                         err = btrfs_update_root(trans, root->fs_info->tree_root,
518                                                 &dirty->root->root_key,
519                                                 &dirty->root->root_item);
520
521                         BUG_ON(err);
522                         if (refs == 1) {
523                                 list_add(&dirty->list, list);
524                         } else {
525                                 WARN_ON(1);
526                                 free_extent_buffer(dirty->root->node);
527                                 kfree(dirty->root);
528                                 kfree(dirty);
529                         }
530                 }
531         }
532         return err;
533 }
534
535 int btrfs_defrag_root(struct btrfs_root *root, int cacheonly)
536 {
537         struct btrfs_fs_info *info = root->fs_info;
538         int ret;
539         struct btrfs_trans_handle *trans;
540         unsigned long nr;
541
542         smp_mb();
543         if (root->defrag_running)
544                 return 0;
545         trans = btrfs_start_transaction(root, 1);
546         while (1) {
547                 root->defrag_running = 1;
548                 ret = btrfs_defrag_leaves(trans, root, cacheonly);
549                 nr = trans->blocks_used;
550                 btrfs_end_transaction(trans, root);
551                 btrfs_btree_balance_dirty(info->tree_root, nr);
552                 cond_resched();
553
554                 trans = btrfs_start_transaction(root, 1);
555                 if (root->fs_info->closing || ret != -EAGAIN)
556                         break;
557         }
558         root->defrag_running = 0;
559         smp_mb();
560         btrfs_end_transaction(trans, root);
561         return 0;
562 }
563
564 static noinline int drop_dirty_roots(struct btrfs_root *tree_root,
565                                      struct list_head *list)
566 {
567         struct btrfs_dirty_root *dirty;
568         struct btrfs_trans_handle *trans;
569         unsigned long nr;
570         u64 num_bytes;
571         u64 bytes_used;
572         u64 max_useless;
573         int ret = 0;
574         int err;
575
576         while(!list_empty(list)) {
577                 struct btrfs_root *root;
578
579                 dirty = list_entry(list->prev, struct btrfs_dirty_root, list);
580                 list_del_init(&dirty->list);
581
582                 num_bytes = btrfs_root_used(&dirty->root->root_item);
583                 root = dirty->latest_root;
584                 atomic_inc(&root->fs_info->throttles);
585
586                 mutex_lock(&root->fs_info->drop_mutex);
587                 while(1) {
588                         trans = btrfs_start_transaction(tree_root, 1);
589                         ret = btrfs_drop_snapshot(trans, dirty->root);
590                         if (ret != -EAGAIN) {
591                                 break;
592                         }
593
594                         err = btrfs_update_root(trans,
595                                         tree_root,
596                                         &dirty->root->root_key,
597                                         &dirty->root->root_item);
598                         if (err)
599                                 ret = err;
600                         nr = trans->blocks_used;
601                         ret = btrfs_end_transaction(trans, tree_root);
602                         BUG_ON(ret);
603
604                         mutex_unlock(&root->fs_info->drop_mutex);
605                         btrfs_btree_balance_dirty(tree_root, nr);
606                         cond_resched();
607                         mutex_lock(&root->fs_info->drop_mutex);
608                 }
609                 BUG_ON(ret);
610                 atomic_dec(&root->fs_info->throttles);
611                 wake_up(&root->fs_info->transaction_throttle);
612
613                 mutex_lock(&root->fs_info->alloc_mutex);
614                 num_bytes -= btrfs_root_used(&dirty->root->root_item);
615                 bytes_used = btrfs_root_used(&root->root_item);
616                 if (num_bytes) {
617                         btrfs_record_root_in_trans(root);
618                         btrfs_set_root_used(&root->root_item,
619                                             bytes_used - num_bytes);
620                 }
621                 mutex_unlock(&root->fs_info->alloc_mutex);
622
623                 ret = btrfs_del_root(trans, tree_root, &dirty->root->root_key);
624                 if (ret) {
625                         BUG();
626                         break;
627                 }
628                 mutex_unlock(&root->fs_info->drop_mutex);
629
630                 spin_lock(&root->list_lock);
631                 list_del_init(&dirty->root->dead_list);
632                 if (!list_empty(&root->dead_list)) {
633                         struct btrfs_root *oldest;
634                         oldest = list_entry(root->dead_list.prev,
635                                             struct btrfs_root, dead_list);
636                         max_useless = oldest->root_key.offset - 1;
637                 } else {
638                         max_useless = root->root_key.offset - 1;
639                 }
640                 spin_unlock(&root->list_lock);
641
642                 nr = trans->blocks_used;
643                 ret = btrfs_end_transaction(trans, tree_root);
644                 BUG_ON(ret);
645
646                 ret = btrfs_remove_leaf_refs(root, max_useless);
647                 BUG_ON(ret);
648
649                 free_extent_buffer(dirty->root->node);
650                 kfree(dirty->root);
651                 kfree(dirty);
652
653                 btrfs_btree_balance_dirty(tree_root, nr);
654                 cond_resched();
655         }
656         return ret;
657 }
658
659 static noinline int create_pending_snapshot(struct btrfs_trans_handle *trans,
660                                    struct btrfs_fs_info *fs_info,
661                                    struct btrfs_pending_snapshot *pending)
662 {
663         struct btrfs_key key;
664         struct btrfs_root_item *new_root_item;
665         struct btrfs_root *tree_root = fs_info->tree_root;
666         struct btrfs_root *root = pending->root;
667         struct extent_buffer *tmp;
668         struct extent_buffer *old;
669         int ret;
670         int namelen;
671         u64 objectid;
672
673         new_root_item = kmalloc(sizeof(*new_root_item), GFP_NOFS);
674         if (!new_root_item) {
675                 ret = -ENOMEM;
676                 goto fail;
677         }
678         ret = btrfs_find_free_objectid(trans, tree_root, 0, &objectid);
679         if (ret)
680                 goto fail;
681
682         memcpy(new_root_item, &root->root_item, sizeof(*new_root_item));
683
684         key.objectid = objectid;
685         key.offset = 1;
686         btrfs_set_key_type(&key, BTRFS_ROOT_ITEM_KEY);
687
688         old = btrfs_lock_root_node(root);
689         btrfs_cow_block(trans, root, old, NULL, 0, &old, 0);
690
691         btrfs_copy_root(trans, root, old, &tmp, objectid);
692         btrfs_tree_unlock(old);
693         free_extent_buffer(old);
694
695         btrfs_set_root_bytenr(new_root_item, tmp->start);
696         btrfs_set_root_level(new_root_item, btrfs_header_level(tmp));
697         ret = btrfs_insert_root(trans, root->fs_info->tree_root, &key,
698                                 new_root_item);
699         btrfs_tree_unlock(tmp);
700         free_extent_buffer(tmp);
701         if (ret)
702                 goto fail;
703
704         /*
705          * insert the directory item
706          */
707         key.offset = (u64)-1;
708         namelen = strlen(pending->name);
709         ret = btrfs_insert_dir_item(trans, root->fs_info->tree_root,
710                                     pending->name, namelen,
711                                     root->fs_info->sb->s_root->d_inode->i_ino,
712                                     &key, BTRFS_FT_DIR, 0);
713
714         if (ret)
715                 goto fail;
716
717         ret = btrfs_insert_inode_ref(trans, root->fs_info->tree_root,
718                              pending->name, strlen(pending->name), objectid,
719                              root->fs_info->sb->s_root->d_inode->i_ino, 0);
720
721         /* Invalidate existing dcache entry for new snapshot. */
722         btrfs_invalidate_dcache_root(root, pending->name, namelen);
723
724 fail:
725         kfree(new_root_item);
726         return ret;
727 }
728
729 static noinline int create_pending_snapshots(struct btrfs_trans_handle *trans,
730                                              struct btrfs_fs_info *fs_info)
731 {
732         struct btrfs_pending_snapshot *pending;
733         struct list_head *head = &trans->transaction->pending_snapshots;
734         int ret;
735
736         while(!list_empty(head)) {
737                 pending = list_entry(head->next,
738                                      struct btrfs_pending_snapshot, list);
739                 ret = create_pending_snapshot(trans, fs_info, pending);
740                 BUG_ON(ret);
741                 list_del(&pending->list);
742                 kfree(pending->name);
743                 kfree(pending);
744         }
745         return 0;
746 }
747
748 int btrfs_commit_transaction(struct btrfs_trans_handle *trans,
749                              struct btrfs_root *root)
750 {
751         unsigned long joined = 0;
752         unsigned long timeout = 1;
753         struct btrfs_transaction *cur_trans;
754         struct btrfs_transaction *prev_trans = NULL;
755         struct btrfs_root *chunk_root = root->fs_info->chunk_root;
756         struct list_head dirty_fs_roots;
757         struct extent_io_tree *pinned_copy;
758         DEFINE_WAIT(wait);
759         int ret;
760
761         INIT_LIST_HEAD(&dirty_fs_roots);
762         mutex_lock(&root->fs_info->trans_mutex);
763         if (trans->transaction->in_commit) {
764                 cur_trans = trans->transaction;
765                 trans->transaction->use_count++;
766                 mutex_unlock(&root->fs_info->trans_mutex);
767                 btrfs_end_transaction(trans, root);
768
769                 ret = wait_for_commit(root, cur_trans);
770                 BUG_ON(ret);
771
772                 mutex_lock(&root->fs_info->trans_mutex);
773                 put_transaction(cur_trans);
774                 mutex_unlock(&root->fs_info->trans_mutex);
775
776                 return 0;
777         }
778
779         pinned_copy = kmalloc(sizeof(*pinned_copy), GFP_NOFS);
780         if (!pinned_copy)
781                 return -ENOMEM;
782
783         extent_io_tree_init(pinned_copy,
784                              root->fs_info->btree_inode->i_mapping, GFP_NOFS);
785
786         trans->transaction->in_commit = 1;
787         trans->transaction->blocked = 1;
788         cur_trans = trans->transaction;
789         if (cur_trans->list.prev != &root->fs_info->trans_list) {
790                 prev_trans = list_entry(cur_trans->list.prev,
791                                         struct btrfs_transaction, list);
792                 if (!prev_trans->commit_done) {
793                         prev_trans->use_count++;
794                         mutex_unlock(&root->fs_info->trans_mutex);
795
796                         wait_for_commit(root, prev_trans);
797
798                         mutex_lock(&root->fs_info->trans_mutex);
799                         put_transaction(prev_trans);
800                 }
801         }
802
803         do {
804                 int snap_pending = 0;
805                 joined = cur_trans->num_joined;
806                 if (!list_empty(&trans->transaction->pending_snapshots))
807                         snap_pending = 1;
808
809                 WARN_ON(cur_trans != trans->transaction);
810                 prepare_to_wait(&cur_trans->writer_wait, &wait,
811                                 TASK_UNINTERRUPTIBLE);
812
813                 if (cur_trans->num_writers > 1)
814                         timeout = MAX_SCHEDULE_TIMEOUT;
815                 else
816                         timeout = 1;
817
818                 mutex_unlock(&root->fs_info->trans_mutex);
819
820                 if (snap_pending) {
821                         ret = btrfs_wait_ordered_extents(root, 1);
822                         BUG_ON(ret);
823                 }
824
825                 schedule_timeout(timeout);
826
827                 mutex_lock(&root->fs_info->trans_mutex);
828                 finish_wait(&cur_trans->writer_wait, &wait);
829         } while (cur_trans->num_writers > 1 ||
830                  (cur_trans->num_joined != joined));
831
832         ret = create_pending_snapshots(trans, root->fs_info);
833         BUG_ON(ret);
834
835         WARN_ON(cur_trans != trans->transaction);
836
837         /* btrfs_commit_tree_roots is responsible for getting the
838          * various roots consistent with each other.  Every pointer
839          * in the tree of tree roots has to point to the most up to date
840          * root for every subvolume and other tree.  So, we have to keep
841          * the tree logging code from jumping in and changing any
842          * of the trees.
843          *
844          * At this point in the commit, there can't be any tree-log
845          * writers, but a little lower down we drop the trans mutex
846          * and let new people in.  By holding the tree_log_mutex
847          * from now until after the super is written, we avoid races
848          * with the tree-log code.
849          */
850         mutex_lock(&root->fs_info->tree_log_mutex);
851
852         ret = add_dirty_roots(trans, &root->fs_info->fs_roots_radix,
853                               &dirty_fs_roots);
854         BUG_ON(ret);
855
856         /* add_dirty_roots gets rid of all the tree log roots, it is now
857          * safe to free the root of tree log roots
858          */
859         btrfs_free_log_root_tree(trans, root->fs_info);
860
861         ret = btrfs_commit_tree_roots(trans, root);
862         BUG_ON(ret);
863
864         cur_trans = root->fs_info->running_transaction;
865         spin_lock(&root->fs_info->new_trans_lock);
866         root->fs_info->running_transaction = NULL;
867         spin_unlock(&root->fs_info->new_trans_lock);
868         btrfs_set_super_generation(&root->fs_info->super_copy,
869                                    cur_trans->transid);
870         btrfs_set_super_root(&root->fs_info->super_copy,
871                              root->fs_info->tree_root->node->start);
872         btrfs_set_super_root_level(&root->fs_info->super_copy,
873                            btrfs_header_level(root->fs_info->tree_root->node));
874
875         btrfs_set_super_chunk_root(&root->fs_info->super_copy,
876                                    chunk_root->node->start);
877         btrfs_set_super_chunk_root_level(&root->fs_info->super_copy,
878                                          btrfs_header_level(chunk_root->node));
879
880         if (!root->fs_info->log_root_recovering) {
881                 btrfs_set_super_log_root(&root->fs_info->super_copy, 0);
882                 btrfs_set_super_log_root_level(&root->fs_info->super_copy, 0);
883         }
884
885         memcpy(&root->fs_info->super_for_commit, &root->fs_info->super_copy,
886                sizeof(root->fs_info->super_copy));
887
888         btrfs_copy_pinned(root, pinned_copy);
889
890         trans->transaction->blocked = 0;
891         wake_up(&root->fs_info->transaction_throttle);
892         wake_up(&root->fs_info->transaction_wait);
893
894         mutex_unlock(&root->fs_info->trans_mutex);
895         ret = btrfs_write_and_wait_transaction(trans, root);
896         BUG_ON(ret);
897         write_ctree_super(trans, root);
898
899         /*
900          * the super is written, we can safely allow the tree-loggers
901          * to go about their business
902          */
903         mutex_unlock(&root->fs_info->tree_log_mutex);
904
905         btrfs_finish_extent_commit(trans, root, pinned_copy);
906         mutex_lock(&root->fs_info->trans_mutex);
907
908         kfree(pinned_copy);
909
910         cur_trans->commit_done = 1;
911         root->fs_info->last_trans_committed = cur_trans->transid;
912         wake_up(&cur_trans->commit_wait);
913         put_transaction(cur_trans);
914         put_transaction(cur_trans);
915
916         list_splice_init(&dirty_fs_roots, &root->fs_info->dead_roots);
917         if (root->fs_info->closing)
918                 list_splice_init(&root->fs_info->dead_roots, &dirty_fs_roots);
919
920         mutex_unlock(&root->fs_info->trans_mutex);
921         kmem_cache_free(btrfs_trans_handle_cachep, trans);
922
923         if (root->fs_info->closing) {
924                 drop_dirty_roots(root->fs_info->tree_root, &dirty_fs_roots);
925         }
926         return ret;
927 }
928
929 int btrfs_clean_old_snapshots(struct btrfs_root *root)
930 {
931         struct list_head dirty_roots;
932         INIT_LIST_HEAD(&dirty_roots);
933 again:
934         mutex_lock(&root->fs_info->trans_mutex);
935         list_splice_init(&root->fs_info->dead_roots, &dirty_roots);
936         mutex_unlock(&root->fs_info->trans_mutex);
937
938         if (!list_empty(&dirty_roots)) {
939                 drop_dirty_roots(root, &dirty_roots);
940                 goto again;
941         }
942         return 0;
943 }