1 #include <linux/module.h>
5 #include "transaction.h"
7 static int total_trans = 0;
8 extern struct kmem_cache *btrfs_trans_handle_cachep;
9 extern struct kmem_cache *btrfs_transaction_cachep;
11 #define BTRFS_ROOT_TRANS_TAG 0
13 #define TRANS_MAGIC 0xE1E10E
14 static void put_transaction(struct btrfs_transaction *transaction)
16 WARN_ON(transaction->use_count == 0);
17 transaction->use_count--;
18 WARN_ON(transaction->magic != TRANS_MAGIC);
19 if (transaction->use_count == 0) {
20 WARN_ON(total_trans == 0);
22 memset(transaction, 0, sizeof(*transaction));
23 kmem_cache_free(btrfs_transaction_cachep, transaction);
27 static int join_transaction(struct btrfs_root *root)
29 struct btrfs_transaction *cur_trans;
30 cur_trans = root->fs_info->running_transaction;
32 cur_trans = kmem_cache_alloc(btrfs_transaction_cachep,
36 root->fs_info->generation++;
37 root->fs_info->running_transaction = cur_trans;
38 cur_trans->num_writers = 0;
39 cur_trans->transid = root->fs_info->generation;
40 init_waitqueue_head(&cur_trans->writer_wait);
41 init_waitqueue_head(&cur_trans->commit_wait);
42 cur_trans->magic = TRANS_MAGIC;
43 cur_trans->in_commit = 0;
44 cur_trans->use_count = 1;
45 cur_trans->commit_done = 0;
47 cur_trans->num_writers++;
51 struct btrfs_trans_handle *btrfs_start_transaction(struct btrfs_root *root,
54 struct btrfs_trans_handle *h =
55 kmem_cache_alloc(btrfs_trans_handle_cachep, GFP_NOFS);
59 mutex_lock(&root->fs_info->trans_mutex);
60 ret = join_transaction(root);
62 running_trans_id = root->fs_info->running_transaction->transid;
64 if (root != root->fs_info->tree_root && root->last_trans <
66 radix_tree_tag_set(&root->fs_info->fs_roots_radix,
67 (unsigned long)root, BTRFS_ROOT_TRANS_TAG);
68 root->commit_root = root->node;
71 root->last_trans = running_trans_id;
72 h->transid = running_trans_id;
73 h->transaction = root->fs_info->running_transaction;
74 h->blocks_reserved = num_blocks;
76 root->fs_info->running_transaction->use_count++;
77 mutex_unlock(&root->fs_info->trans_mutex);
78 h->magic = h->magic2 = TRANS_MAGIC;
82 int btrfs_end_transaction(struct btrfs_trans_handle *trans,
83 struct btrfs_root *root)
85 struct btrfs_transaction *cur_trans;
87 WARN_ON(trans->magic != TRANS_MAGIC);
88 WARN_ON(trans->magic2 != TRANS_MAGIC);
89 mutex_lock(&root->fs_info->trans_mutex);
90 cur_trans = root->fs_info->running_transaction;
91 WARN_ON(cur_trans->num_writers < 1);
92 if (waitqueue_active(&cur_trans->writer_wait))
93 wake_up(&cur_trans->writer_wait);
94 cur_trans->num_writers--;
95 put_transaction(cur_trans);
96 mutex_unlock(&root->fs_info->trans_mutex);
97 memset(trans, 0, sizeof(*trans));
98 kmem_cache_free(btrfs_trans_handle_cachep, trans);
103 int btrfs_write_and_wait_transaction(struct btrfs_trans_handle *trans,
104 struct btrfs_root *root)
106 filemap_write_and_wait(root->fs_info->btree_inode->i_mapping);
110 int btrfs_commit_tree_roots(struct btrfs_trans_handle *trans,
111 struct btrfs_root *root)
114 u64 old_extent_block;
115 struct btrfs_fs_info *fs_info = root->fs_info;
116 struct btrfs_root *tree_root = fs_info->tree_root;
117 struct btrfs_root *extent_root = fs_info->extent_root;
120 old_extent_block = btrfs_root_blocknr(&extent_root->root_item);
121 if (old_extent_block == extent_root->node->b_blocknr)
123 btrfs_set_root_blocknr(&extent_root->root_item,
124 extent_root->node->b_blocknr);
125 ret = btrfs_update_root(trans, tree_root,
126 &extent_root->root_key,
127 &extent_root->root_item);
133 static int wait_for_commit(struct btrfs_root *root,
134 struct btrfs_transaction *commit)
137 while(!commit->commit_done) {
138 prepare_to_wait(&commit->commit_wait, &wait,
139 TASK_UNINTERRUPTIBLE);
140 if (commit->commit_done)
142 mutex_unlock(&root->fs_info->trans_mutex);
144 mutex_lock(&root->fs_info->trans_mutex);
146 finish_wait(&commit->commit_wait, &wait);
151 struct list_head list;
152 struct btrfs_key snap_key;
153 struct buffer_head *commit_root;
154 struct btrfs_root *root;
157 int add_dirty_roots(struct btrfs_trans_handle *trans,
158 struct radix_tree_root *radix, struct list_head *list)
160 struct dirty_root *dirty;
161 struct btrfs_root *gang[8];
162 struct btrfs_root *root;
167 ret = radix_tree_gang_lookup_tag(radix, (void **)gang, 0,
169 BTRFS_ROOT_TRANS_TAG);
172 for (i = 0; i < ret; i++) {
174 radix_tree_tag_clear(radix, (unsigned long)root,
175 BTRFS_ROOT_TRANS_TAG);
176 if (root->commit_root == root->node) {
177 WARN_ON(root->node->b_blocknr !=
178 btrfs_root_blocknr(&root->root_item));
179 brelse(root->commit_root);
180 root->commit_root = NULL;
183 dirty = kmalloc(sizeof(*dirty), GFP_NOFS);
185 memcpy(&dirty->snap_key, &root->root_key,
186 sizeof(root->root_key));
187 dirty->commit_root = root->commit_root;
188 root->commit_root = NULL;
190 root->root_key.offset = root->fs_info->generation;
191 btrfs_set_root_blocknr(&root->root_item,
192 root->node->b_blocknr);
193 err = btrfs_insert_root(trans, root->fs_info->tree_root,
197 list_add(&dirty->list, list);
203 int drop_dirty_roots(struct btrfs_root *tree_root, struct list_head *list)
205 struct dirty_root *dirty;
206 struct btrfs_trans_handle *trans;
209 while(!list_empty(list)) {
210 dirty = list_entry(list->next, struct dirty_root, list);
211 list_del_init(&dirty->list);
212 trans = btrfs_start_transaction(tree_root, 1);
213 ret = btrfs_drop_snapshot(trans, dirty->root,
217 ret = btrfs_del_root(trans, tree_root, &dirty->snap_key);
219 ret = btrfs_end_transaction(trans, tree_root);
226 int btrfs_commit_transaction(struct btrfs_trans_handle *trans,
227 struct btrfs_root *root)
230 struct btrfs_transaction *cur_trans;
231 struct list_head dirty_fs_roots;
234 INIT_LIST_HEAD(&dirty_fs_roots);
236 mutex_lock(&root->fs_info->trans_mutex);
237 if (trans->transaction->in_commit) {
238 cur_trans = trans->transaction;
239 trans->transaction->use_count++;
240 btrfs_end_transaction(trans, root);
241 ret = wait_for_commit(root, cur_trans);
243 put_transaction(cur_trans);
244 mutex_unlock(&root->fs_info->trans_mutex);
247 cur_trans = trans->transaction;
248 trans->transaction->in_commit = 1;
249 while (trans->transaction->num_writers > 1) {
250 WARN_ON(cur_trans != trans->transaction);
251 prepare_to_wait(&trans->transaction->writer_wait, &wait,
252 TASK_UNINTERRUPTIBLE);
253 if (trans->transaction->num_writers <= 1)
255 mutex_unlock(&root->fs_info->trans_mutex);
257 mutex_lock(&root->fs_info->trans_mutex);
258 finish_wait(&trans->transaction->writer_wait, &wait);
260 finish_wait(&trans->transaction->writer_wait, &wait);
261 WARN_ON(cur_trans != trans->transaction);
262 add_dirty_roots(trans, &root->fs_info->fs_roots_radix, &dirty_fs_roots);
263 ret = btrfs_commit_tree_roots(trans, root);
265 cur_trans = root->fs_info->running_transaction;
266 root->fs_info->running_transaction = NULL;
267 btrfs_set_super_generation(root->fs_info->disk_super,
268 root->fs_info->generation + 1);
269 mutex_unlock(&root->fs_info->trans_mutex);
270 ret = btrfs_write_and_wait_transaction(trans, root);
273 write_ctree_super(trans, root);
274 btrfs_finish_extent_commit(trans, root);
275 mutex_lock(&root->fs_info->trans_mutex);
276 cur_trans->commit_done = 1;
277 wake_up(&cur_trans->commit_wait);
278 put_transaction(cur_trans);
279 put_transaction(cur_trans);
280 mutex_unlock(&root->fs_info->trans_mutex);
281 kmem_cache_free(btrfs_trans_handle_cachep, trans);
283 drop_dirty_roots(root->fs_info->tree_root, &dirty_fs_roots);