btrfs-progs: tests/fuzz: Add fuzzed test image for btrfs check BUG_ON
[btrfs-progs-unstable/devel.git] / transaction.c
blobecafbb156610b5f5b89c4cf8d3c4877cf1be3130
1 /*
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"
18 #include "disk-io.h"
19 #include "transaction.h"
21 #include "messages.h"
23 struct btrfs_trans_handle* btrfs_start_transaction(struct btrfs_root *root,
24 int num_blocks)
26 struct btrfs_fs_info *fs_info = root->fs_info;
27 struct btrfs_trans_handle *h = kzalloc(sizeof(*h), GFP_NOFS);
29 if (fs_info->transaction_aborted)
30 return ERR_PTR(-EROFS);
32 if (!h)
33 return ERR_PTR(-ENOMEM);
34 if (root->commit_root) {
35 error("commit_root aleady set when starting transaction");
36 kfree(h);
37 return ERR_PTR(-EINVAL);
39 if (fs_info->running_transaction) {
40 error("attempt to start transaction over already running one");
41 kfree(h);
42 return ERR_PTR(-EINVAL);
44 h->fs_info = fs_info;
45 fs_info->running_transaction = h;
46 fs_info->generation++;
47 h->transid = fs_info->generation;
48 h->blocks_reserved = num_blocks;
49 h->reinit_extent_tree = false;
50 root->last_trans = h->transid;
51 root->commit_root = root->node;
52 extent_buffer_get(root->node);
54 return h;
57 static int update_cowonly_root(struct btrfs_trans_handle *trans,
58 struct btrfs_root *root)
60 int ret;
61 u64 old_root_bytenr;
62 struct btrfs_root *tree_root = root->fs_info->tree_root;
64 btrfs_write_dirty_block_groups(trans, root);
65 while(1) {
66 old_root_bytenr = btrfs_root_bytenr(&root->root_item);
67 if (old_root_bytenr == root->node->start)
68 break;
69 btrfs_set_root_bytenr(&root->root_item,
70 root->node->start);
71 btrfs_set_root_generation(&root->root_item,
72 trans->transid);
73 root->root_item.level = btrfs_header_level(root->node);
74 ret = btrfs_update_root(trans, tree_root,
75 &root->root_key,
76 &root->root_item);
77 BUG_ON(ret);
78 btrfs_write_dirty_block_groups(trans, root);
80 return 0;
83 int commit_tree_roots(struct btrfs_trans_handle *trans,
84 struct btrfs_fs_info *fs_info)
86 struct btrfs_root *root;
87 struct list_head *next;
88 struct extent_buffer *eb;
89 int ret;
91 if (fs_info->readonly)
92 return 0;
94 eb = fs_info->tree_root->node;
95 extent_buffer_get(eb);
96 ret = btrfs_cow_block(trans, fs_info->tree_root, eb, NULL, 0, &eb);
97 free_extent_buffer(eb);
98 if (ret)
99 return ret;
101 while(!list_empty(&fs_info->dirty_cowonly_roots)) {
102 next = fs_info->dirty_cowonly_roots.next;
103 list_del_init(next);
104 root = list_entry(next, struct btrfs_root, dirty_list);
105 update_cowonly_root(trans, root);
106 free_extent_buffer(root->commit_root);
107 root->commit_root = NULL;
110 return 0;
113 int __commit_transaction(struct btrfs_trans_handle *trans,
114 struct btrfs_root *root)
116 u64 start;
117 u64 end;
118 struct btrfs_fs_info *fs_info = root->fs_info;
119 struct extent_buffer *eb;
120 struct extent_io_tree *tree = &fs_info->extent_cache;
121 int ret;
123 while(1) {
124 ret = find_first_extent_bit(tree, 0, &start, &end,
125 EXTENT_DIRTY);
126 if (ret)
127 break;
128 while(start <= end) {
129 eb = find_first_extent_buffer(tree, start);
130 BUG_ON(!eb || eb->start != start);
131 ret = write_tree_block(trans, fs_info, eb);
132 BUG_ON(ret);
133 start += eb->len;
134 clear_extent_buffer_dirty(eb);
135 free_extent_buffer(eb);
138 return 0;
141 int btrfs_commit_transaction(struct btrfs_trans_handle *trans,
142 struct btrfs_root *root)
144 u64 transid = trans->transid;
145 int ret = 0;
146 struct btrfs_fs_info *fs_info = root->fs_info;
148 if (trans->fs_info->transaction_aborted)
149 return -EROFS;
151 if (root->commit_root == root->node)
152 goto commit_tree;
153 if (root == root->fs_info->tree_root)
154 goto commit_tree;
155 if (root == root->fs_info->chunk_root)
156 goto commit_tree;
158 free_extent_buffer(root->commit_root);
159 root->commit_root = NULL;
161 btrfs_set_root_bytenr(&root->root_item, root->node->start);
162 btrfs_set_root_generation(&root->root_item, trans->transid);
163 root->root_item.level = btrfs_header_level(root->node);
164 ret = btrfs_update_root(trans, root->fs_info->tree_root,
165 &root->root_key, &root->root_item);
166 BUG_ON(ret);
167 commit_tree:
168 ret = commit_tree_roots(trans, fs_info);
169 BUG_ON(ret);
170 ret = __commit_transaction(trans, root);
171 BUG_ON(ret);
172 write_ctree_super(trans);
173 btrfs_finish_extent_commit(trans, fs_info->extent_root,
174 &fs_info->pinned_extents);
175 kfree(trans);
176 free_extent_buffer(root->commit_root);
177 root->commit_root = NULL;
178 fs_info->running_transaction = NULL;
179 fs_info->last_trans_committed = transid;
180 return 0;
183 void btrfs_abort_transaction(struct btrfs_trans_handle *trans, int error)
185 trans->fs_info->transaction_aborted = error;