2 * Copyright (C) 2007 Oracle. All rights reserved.
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.
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.
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.
20 #include <linux/sched.h>
21 #include <linux/writeback.h>
22 #include <linux/pagemap.h>
25 #include "transaction.h"
28 static int total_trans = 0;
29 extern struct kmem_cache *btrfs_trans_handle_cachep;
30 extern struct kmem_cache *btrfs_transaction_cachep;
32 #define BTRFS_ROOT_TRANS_TAG 0
34 static noinline void put_transaction(struct btrfs_transaction *transaction)
36 WARN_ON(transaction->use_count == 0);
37 transaction->use_count--;
38 if (transaction->use_count == 0) {
39 WARN_ON(total_trans == 0);
41 list_del_init(&transaction->list);
42 memset(transaction, 0, sizeof(*transaction));
43 kmem_cache_free(btrfs_transaction_cachep, transaction);
47 static noinline int join_transaction(struct btrfs_root *root)
49 struct btrfs_transaction *cur_trans;
50 cur_trans = root->fs_info->running_transaction;
52 cur_trans = kmem_cache_alloc(btrfs_transaction_cachep,
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,
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);
77 cur_trans->num_writers++;
78 cur_trans->num_joined++;
84 static noinline int record_root_in_trans(struct btrfs_root *root)
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);
97 root->last_trans = running_trans_id;
102 struct btrfs_trans_handle *btrfs_start_transaction(struct btrfs_root *root,
105 struct btrfs_trans_handle *h =
106 kmem_cache_alloc(btrfs_trans_handle_cachep, GFP_NOFS);
109 mutex_lock(&root->fs_info->trans_mutex);
110 ret = join_transaction(root);
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;
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);
126 static noinline int wait_for_commit(struct btrfs_root *root,
127 struct btrfs_transaction *commit)
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)
136 mutex_unlock(&root->fs_info->trans_mutex);
138 mutex_lock(&root->fs_info->trans_mutex);
140 mutex_unlock(&root->fs_info->trans_mutex);
141 finish_wait(&commit->commit_wait, &wait);
145 static int __btrfs_end_transaction(struct btrfs_trans_handle *trans,
146 struct btrfs_root *root, int throttle)
148 struct btrfs_transaction *cur_trans;
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--;
156 if (waitqueue_active(&cur_trans->writer_wait))
157 wake_up(&cur_trans->writer_wait);
159 if (cur_trans->in_commit && throttle) {
161 mutex_unlock(&root->fs_info->trans_mutex);
162 prepare_to_wait(&root->fs_info->transaction_throttle, &wait,
163 TASK_UNINTERRUPTIBLE);
165 finish_wait(&root->fs_info->transaction_throttle, &wait);
166 mutex_lock(&root->fs_info->trans_mutex);
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);
176 int btrfs_end_transaction(struct btrfs_trans_handle *trans,
177 struct btrfs_root *root)
179 return __btrfs_end_transaction(trans, root, 0);
182 int btrfs_end_transaction_throttle(struct btrfs_trans_handle *trans,
183 struct btrfs_root *root)
185 return __btrfs_end_transaction(trans, root, 1);
189 int btrfs_write_and_wait_transaction(struct btrfs_trans_handle *trans,
190 struct btrfs_root *root)
195 struct extent_io_tree *dirty_pages;
197 struct inode *btree_inode = root->fs_info->btree_inode;
202 if (!trans || !trans->transaction) {
203 return filemap_write_and_wait(btree_inode->i_mapping);
205 dirty_pages = &trans->transaction->dirty_pages;
207 ret = find_first_extent_bit(dirty_pages, 0, &start, &end,
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);
218 if (PageWriteback(page)) {
220 wait_on_page_writeback(page);
223 page_cache_release(page);
227 err = write_one_page(page, 0);
230 page_cache_release(page);
233 err = filemap_fdatawait(btree_inode->i_mapping);
239 static int update_cowonly_root(struct btrfs_trans_handle *trans,
240 struct btrfs_root *root)
244 struct btrfs_root *tree_root = root->fs_info->tree_root;
246 btrfs_write_dirty_block_groups(trans, root);
248 old_root_bytenr = btrfs_root_bytenr(&root->root_item);
249 if (old_root_bytenr == root->node->start)
251 btrfs_set_root_bytenr(&root->root_item,
253 btrfs_set_root_level(&root->root_item,
254 btrfs_header_level(root->node));
255 ret = btrfs_update_root(trans, tree_root,
259 btrfs_write_dirty_block_groups(trans, root);
264 int btrfs_commit_tree_roots(struct btrfs_trans_handle *trans,
265 struct btrfs_root *root)
267 struct btrfs_fs_info *fs_info = root->fs_info;
268 struct list_head *next;
270 while(!list_empty(&fs_info->dirty_cowonly_roots)) {
271 next = fs_info->dirty_cowonly_roots.next;
273 root = list_entry(next, struct btrfs_root, dirty_list);
274 update_cowonly_root(trans, root);
280 struct list_head list;
281 struct btrfs_root *root;
282 struct btrfs_root *latest_root;
285 int btrfs_add_dead_root(struct btrfs_root *root,
286 struct btrfs_root *latest,
287 struct list_head *dead_list)
289 struct dirty_root *dirty;
291 dirty = kmalloc(sizeof(*dirty), GFP_NOFS);
295 dirty->latest_root = latest;
296 list_add(&dirty->list, dead_list);
300 static noinline int add_dirty_roots(struct btrfs_trans_handle *trans,
301 struct radix_tree_root *radix,
302 struct list_head *list)
304 struct dirty_root *dirty;
305 struct btrfs_root *gang[8];
306 struct btrfs_root *root;
313 ret = radix_tree_gang_lookup_tag(radix, (void **)gang, 0,
315 BTRFS_ROOT_TRANS_TAG);
318 for (i = 0; i < ret; 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;
329 /* make sure to update the root on disk
330 * so we get any updates to the block used
333 err = btrfs_update_root(trans,
334 root->fs_info->tree_root,
339 dirty = kmalloc(sizeof(*dirty), GFP_NOFS);
341 dirty->root = kmalloc(sizeof(*dirty->root), GFP_NOFS);
342 BUG_ON(!dirty->root);
344 memset(&root->root_item.drop_progress, 0,
345 sizeof(struct btrfs_disk_key));
346 root->root_item.drop_level = 0;
348 memcpy(dirty->root, root, sizeof(*root));
349 dirty->root->node = root->commit_root;
350 dirty->latest_root = root;
351 root->commit_root = NULL;
353 root->root_key.offset = root->fs_info->generation;
354 btrfs_set_root_bytenr(&root->root_item,
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,
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);
372 list_add(&dirty->list, list);
383 int btrfs_defrag_root(struct btrfs_root *root, int cacheonly)
385 struct btrfs_fs_info *info = root->fs_info;
387 struct btrfs_trans_handle *trans;
391 if (root->defrag_running)
393 trans = btrfs_start_transaction(root, 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);
402 trans = btrfs_start_transaction(root, 1);
403 if (root->fs_info->closing || ret != -EAGAIN)
406 root->defrag_running = 0;
408 btrfs_end_transaction(trans, root);
412 static noinline int drop_dirty_roots(struct btrfs_root *tree_root,
413 struct list_head *list)
415 struct dirty_root *dirty;
416 struct btrfs_trans_handle *trans;
423 while(!list_empty(list)) {
424 struct btrfs_root *root;
426 dirty = list_entry(list->next, struct dirty_root, list);
427 list_del_init(&dirty->list);
429 num_bytes = btrfs_root_used(&dirty->root->root_item);
430 root = dirty->latest_root;
431 atomic_inc(&root->fs_info->throttles);
433 mutex_lock(&root->fs_info->drop_mutex);
435 trans = btrfs_start_transaction(tree_root, 1);
436 ret = btrfs_drop_snapshot(trans, dirty->root);
437 if (ret != -EAGAIN) {
441 err = btrfs_update_root(trans,
443 &dirty->root->root_key,
444 &dirty->root->root_item);
447 nr = trans->blocks_used;
448 ret = btrfs_end_transaction_throttle(trans, tree_root);
451 mutex_unlock(&root->fs_info->drop_mutex);
452 btrfs_btree_balance_dirty(tree_root, nr);
454 mutex_lock(&root->fs_info->drop_mutex);
457 atomic_dec(&root->fs_info->throttles);
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);
463 record_root_in_trans(root);
464 btrfs_set_root_used(&root->root_item,
465 bytes_used - num_bytes);
467 mutex_unlock(&root->fs_info->alloc_mutex);
469 ret = btrfs_del_root(trans, tree_root, &dirty->root->root_key);
474 mutex_unlock(&root->fs_info->drop_mutex);
476 nr = trans->blocks_used;
477 ret = btrfs_end_transaction(trans, tree_root);
480 free_extent_buffer(dirty->root->node);
484 btrfs_btree_balance_dirty(tree_root, nr);
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)
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;
504 new_root_item = kmalloc(sizeof(*new_root_item), GFP_NOFS);
505 if (!new_root_item) {
509 ret = btrfs_find_free_objectid(trans, tree_root, 0, &objectid);
513 memcpy(new_root_item, &root->root_item, sizeof(*new_root_item));
515 key.objectid = objectid;
517 btrfs_set_key_type(&key, BTRFS_ROOT_ITEM_KEY);
519 old = btrfs_lock_root_node(root);
520 btrfs_cow_block(trans, root, old, NULL, 0, &old);
522 btrfs_copy_root(trans, root, old, &tmp, objectid);
523 btrfs_tree_unlock(old);
524 free_extent_buffer(old);
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,
530 btrfs_tree_unlock(tmp);
531 free_extent_buffer(tmp);
536 * insert the directory item
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,
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);
552 /* Invalidate existing dcache entry for new snapshot. */
553 btrfs_invalidate_dcache_root(root, pending->name, namelen);
556 kfree(new_root_item);
560 static noinline int create_pending_snapshots(struct btrfs_trans_handle *trans,
561 struct btrfs_fs_info *fs_info)
563 struct btrfs_pending_snapshot *pending;
564 struct list_head *head = &trans->transaction->pending_snapshots;
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);
572 list_del(&pending->list);
573 kfree(pending->name);
579 int btrfs_commit_transaction(struct btrfs_trans_handle *trans,
580 struct btrfs_root *root)
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;
592 INIT_LIST_HEAD(&dirty_fs_roots);
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);
601 ret = wait_for_commit(root, cur_trans);
604 mutex_lock(&root->fs_info->trans_mutex);
605 put_transaction(cur_trans);
606 mutex_unlock(&root->fs_info->trans_mutex);
611 pinned_copy = kmalloc(sizeof(*pinned_copy), GFP_NOFS);
615 extent_io_tree_init(pinned_copy,
616 root->fs_info->btree_inode->i_mapping, GFP_NOFS);
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);
628 wait_for_commit(root, prev_trans);
630 mutex_lock(&root->fs_info->trans_mutex);
631 put_transaction(prev_trans);
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);
641 if (cur_trans->num_writers > 1)
642 timeout = MAX_SCHEDULE_TIMEOUT;
646 mutex_unlock(&root->fs_info->trans_mutex);
648 schedule_timeout(timeout);
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));
655 ret = create_pending_snapshots(trans, root->fs_info);
658 WARN_ON(cur_trans != trans->transaction);
660 ret = add_dirty_roots(trans, &root->fs_info->fs_roots_radix,
664 ret = btrfs_commit_tree_roots(trans, root);
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,
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));
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));
685 btrfs_copy_pinned(root, pinned_copy);
687 wake_up(&root->fs_info->transaction_throttle);
689 mutex_unlock(&root->fs_info->trans_mutex);
690 ret = btrfs_write_and_wait_transaction(trans, root);
692 write_ctree_super(trans, root);
694 btrfs_finish_extent_commit(trans, root, pinned_copy);
695 mutex_lock(&root->fs_info->trans_mutex);
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);
705 if (root->fs_info->closing)
706 list_splice_init(&root->fs_info->dead_roots, &dirty_fs_roots);
708 list_splice_init(&dirty_fs_roots, &root->fs_info->dead_roots);
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);
714 if (root->fs_info->closing) {
715 drop_dirty_roots(root->fs_info->tree_root, &dirty_fs_roots);
720 int btrfs_clean_old_snapshots(struct btrfs_root *root)
722 struct list_head dirty_roots;
723 INIT_LIST_HEAD(&dirty_roots);
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);
729 if (!list_empty(&dirty_roots)) {
730 drop_dirty_roots(root, &dirty_roots);