2 * This program is free software; you can redistribute it and/or
3 * modify it under the terms of the GNU General Public
4 * License v2 as published by the Free Software Foundation.
6 * This program is distributed in the hope that it will be useful,
7 * but WITHOUT ANY WARRANTY; without even the implied warranty of
8 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
9 * General Public License for more details.
11 * You should have received a copy of the GNU General Public
12 * License along with this program; if not, write to the
13 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
14 * Boston, MA 021110-1307, USA.
17 #include "kerncompat.h"
19 #include "transaction.h"
23 struct btrfs_trans_handle* btrfs_start_transaction(struct btrfs_root *root,
26 struct btrfs_fs_info *fs_info = root->fs_info;
27 struct btrfs_trans_handle *h = kzalloc(sizeof(*h), GFP_NOFS);
30 return ERR_PTR(-ENOMEM);
31 if (root->commit_root) {
32 error("commit_root aleady set when starting transaction");
34 return ERR_PTR(-EINVAL);
36 if (fs_info->running_transaction) {
37 error("attempt to start transaction over already running one");
39 return ERR_PTR(-EINVAL);
42 fs_info->running_transaction = h;
43 fs_info->generation++;
44 h->transid = fs_info->generation;
45 h->blocks_reserved = num_blocks;
46 root->last_trans = h->transid;
47 root->commit_root = root->node;
48 extent_buffer_get(root->node);
53 static int update_cowonly_root(struct btrfs_trans_handle *trans,
54 struct btrfs_root *root)
58 struct btrfs_root *tree_root = root->fs_info->tree_root;
60 btrfs_write_dirty_block_groups(trans, root);
62 old_root_bytenr = btrfs_root_bytenr(&root->root_item);
63 if (old_root_bytenr == root->node->start)
65 btrfs_set_root_bytenr(&root->root_item,
67 btrfs_set_root_generation(&root->root_item,
69 root->root_item.level = btrfs_header_level(root->node);
70 ret = btrfs_update_root(trans, tree_root,
74 btrfs_write_dirty_block_groups(trans, root);
79 int commit_tree_roots(struct btrfs_trans_handle *trans,
80 struct btrfs_fs_info *fs_info)
82 struct btrfs_root *root;
83 struct list_head *next;
84 struct extent_buffer *eb;
87 if (fs_info->readonly)
90 eb = fs_info->tree_root->node;
91 extent_buffer_get(eb);
92 ret = btrfs_cow_block(trans, fs_info->tree_root, eb, NULL, 0, &eb);
93 free_extent_buffer(eb);
97 while(!list_empty(&fs_info->dirty_cowonly_roots)) {
98 next = fs_info->dirty_cowonly_roots.next;
100 root = list_entry(next, struct btrfs_root, dirty_list);
101 update_cowonly_root(trans, root);
102 free_extent_buffer(root->commit_root);
103 root->commit_root = NULL;
109 int __commit_transaction(struct btrfs_trans_handle *trans,
110 struct btrfs_root *root)
114 struct btrfs_fs_info *fs_info = root->fs_info;
115 struct extent_buffer *eb;
116 struct extent_io_tree *tree = &fs_info->extent_cache;
120 ret = find_first_extent_bit(tree, 0, &start, &end,
124 while(start <= end) {
125 eb = find_first_extent_buffer(tree, start);
126 BUG_ON(!eb || eb->start != start);
127 ret = write_tree_block(trans, fs_info, eb);
130 clear_extent_buffer_dirty(eb);
131 free_extent_buffer(eb);
137 int btrfs_commit_transaction(struct btrfs_trans_handle *trans,
138 struct btrfs_root *root)
140 u64 transid = trans->transid;
142 struct btrfs_fs_info *fs_info = root->fs_info;
144 if (root->commit_root == root->node)
146 if (root == root->fs_info->tree_root)
148 if (root == root->fs_info->chunk_root)
151 free_extent_buffer(root->commit_root);
152 root->commit_root = NULL;
154 btrfs_set_root_bytenr(&root->root_item, root->node->start);
155 btrfs_set_root_generation(&root->root_item, trans->transid);
156 root->root_item.level = btrfs_header_level(root->node);
157 ret = btrfs_update_root(trans, root->fs_info->tree_root,
158 &root->root_key, &root->root_item);
161 ret = commit_tree_roots(trans, fs_info);
163 ret = __commit_transaction(trans, root);
165 write_ctree_super(trans, fs_info);
166 btrfs_finish_extent_commit(trans, fs_info->extent_root,
167 &fs_info->pinned_extents);
169 free_extent_buffer(root->commit_root);
170 root->commit_root = NULL;
171 fs_info->running_transaction = NULL;
172 fs_info->last_trans_committed = transid;
176 void btrfs_abort_transaction(struct btrfs_trans_handle *trans, int error)
178 trans->fs_info->transaction_aborted = error;