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