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