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