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 list_del_init(&transaction
->list
);
23 memset(transaction
, 0, sizeof(*transaction
));
24 kmem_cache_free(btrfs_transaction_cachep
, transaction
);
28 static int join_transaction(struct btrfs_root
*root
)
30 struct btrfs_transaction
*cur_trans
;
31 cur_trans
= root
->fs_info
->running_transaction
;
33 cur_trans
= kmem_cache_alloc(btrfs_transaction_cachep
,
37 root
->fs_info
->generation
++;
38 root
->fs_info
->running_transaction
= cur_trans
;
39 cur_trans
->num_writers
= 0;
40 cur_trans
->transid
= root
->fs_info
->generation
;
41 init_waitqueue_head(&cur_trans
->writer_wait
);
42 init_waitqueue_head(&cur_trans
->commit_wait
);
43 cur_trans
->magic
= TRANS_MAGIC
;
44 cur_trans
->in_commit
= 0;
45 cur_trans
->use_count
= 1;
46 cur_trans
->commit_done
= 0;
47 list_add_tail(&cur_trans
->list
, &root
->fs_info
->trans_list
);
48 init_bit_radix(&cur_trans
->dirty_pages
);
50 cur_trans
->num_writers
++;
54 struct btrfs_trans_handle
*btrfs_start_transaction(struct btrfs_root
*root
,
57 struct btrfs_trans_handle
*h
=
58 kmem_cache_alloc(btrfs_trans_handle_cachep
, GFP_NOFS
);
62 mutex_lock(&root
->fs_info
->trans_mutex
);
63 ret
= join_transaction(root
);
65 running_trans_id
= root
->fs_info
->running_transaction
->transid
;
67 if (root
!= root
->fs_info
->tree_root
&& root
->last_trans
<
69 radix_tree_tag_set(&root
->fs_info
->fs_roots_radix
,
70 (unsigned long)root
->root_key
.objectid
,
71 BTRFS_ROOT_TRANS_TAG
);
72 root
->commit_root
= root
->node
;
75 root
->last_trans
= running_trans_id
;
76 h
->transid
= running_trans_id
;
77 h
->transaction
= root
->fs_info
->running_transaction
;
78 h
->blocks_reserved
= num_blocks
;
80 h
->block_group
= NULL
;
81 root
->fs_info
->running_transaction
->use_count
++;
82 mutex_unlock(&root
->fs_info
->trans_mutex
);
83 h
->magic
= h
->magic2
= TRANS_MAGIC
;
87 int btrfs_end_transaction(struct btrfs_trans_handle
*trans
,
88 struct btrfs_root
*root
)
90 struct btrfs_transaction
*cur_trans
;
92 WARN_ON(trans
->magic
!= TRANS_MAGIC
);
93 WARN_ON(trans
->magic2
!= TRANS_MAGIC
);
94 mutex_lock(&root
->fs_info
->trans_mutex
);
95 cur_trans
= root
->fs_info
->running_transaction
;
96 WARN_ON(cur_trans
->num_writers
< 1);
97 if (waitqueue_active(&cur_trans
->writer_wait
))
98 wake_up(&cur_trans
->writer_wait
);
99 cur_trans
->num_writers
--;
100 put_transaction(cur_trans
);
101 mutex_unlock(&root
->fs_info
->trans_mutex
);
102 memset(trans
, 0, sizeof(*trans
));
103 kmem_cache_free(btrfs_trans_handle_cachep
, trans
);
108 int btrfs_write_and_wait_transaction(struct btrfs_trans_handle
*trans
,
109 struct btrfs_root
*root
)
111 unsigned long gang
[16];
117 struct radix_tree_root
*dirty_pages
;
118 struct inode
*btree_inode
= root
->fs_info
->btree_inode
;
120 if (!trans
|| !trans
->transaction
) {
121 return filemap_write_and_wait(btree_inode
->i_mapping
);
123 dirty_pages
= &trans
->transaction
->dirty_pages
;
125 ret
= find_first_radix_bit(dirty_pages
, gang
,
126 0, ARRAY_SIZE(gang
));
129 for (i
= 0; i
< ret
; i
++) {
131 clear_radix_bit(dirty_pages
, gang
[i
]);
132 page
= find_lock_page(btree_inode
->i_mapping
,
136 err
= write_one_page(page
, 0);
139 page_cache_release(page
);
142 err
= filemap_fdatawait(btree_inode
->i_mapping
);
148 int btrfs_commit_tree_roots(struct btrfs_trans_handle
*trans
,
149 struct btrfs_root
*root
)
152 u64 old_extent_block
;
153 struct btrfs_fs_info
*fs_info
= root
->fs_info
;
154 struct btrfs_root
*tree_root
= fs_info
->tree_root
;
155 struct btrfs_root
*extent_root
= fs_info
->extent_root
;
156 struct btrfs_root
*dev_root
= fs_info
->dev_root
;
158 if (btrfs_super_device_root(fs_info
->disk_super
) !=
159 bh_blocknr(dev_root
->node
)) {
160 btrfs_set_super_device_root(fs_info
->disk_super
,
161 bh_blocknr(dev_root
->node
));
163 btrfs_write_dirty_block_groups(trans
, extent_root
);
165 old_extent_block
= btrfs_root_blocknr(&extent_root
->root_item
);
166 if (old_extent_block
== bh_blocknr(extent_root
->node
))
168 btrfs_set_root_blocknr(&extent_root
->root_item
,
169 bh_blocknr(extent_root
->node
));
170 ret
= btrfs_update_root(trans
, tree_root
,
171 &extent_root
->root_key
,
172 &extent_root
->root_item
);
174 btrfs_write_dirty_block_groups(trans
, extent_root
);
179 static int wait_for_commit(struct btrfs_root
*root
,
180 struct btrfs_transaction
*commit
)
183 while(!commit
->commit_done
) {
184 prepare_to_wait(&commit
->commit_wait
, &wait
,
185 TASK_UNINTERRUPTIBLE
);
186 if (commit
->commit_done
)
188 mutex_unlock(&root
->fs_info
->trans_mutex
);
190 mutex_lock(&root
->fs_info
->trans_mutex
);
192 finish_wait(&commit
->commit_wait
, &wait
);
197 struct list_head list
;
198 struct btrfs_key snap_key
;
199 struct buffer_head
*commit_root
;
200 struct btrfs_root
*root
;
203 static int add_dirty_roots(struct btrfs_trans_handle
*trans
,
204 struct radix_tree_root
*radix
,
205 struct list_head
*list
)
207 struct dirty_root
*dirty
;
208 struct btrfs_root
*gang
[8];
209 struct btrfs_root
*root
;
214 ret
= radix_tree_gang_lookup_tag(radix
, (void **)gang
, 0,
216 BTRFS_ROOT_TRANS_TAG
);
219 for (i
= 0; i
< ret
; i
++) {
221 radix_tree_tag_clear(radix
,
222 (unsigned long)root
->root_key
.objectid
,
223 BTRFS_ROOT_TRANS_TAG
);
224 if (root
->commit_root
== root
->node
) {
225 WARN_ON(bh_blocknr(root
->node
) !=
226 btrfs_root_blocknr(&root
->root_item
));
227 brelse(root
->commit_root
);
228 root
->commit_root
= NULL
;
231 dirty
= kmalloc(sizeof(*dirty
), GFP_NOFS
);
233 memcpy(&dirty
->snap_key
, &root
->root_key
,
234 sizeof(root
->root_key
));
235 dirty
->commit_root
= root
->commit_root
;
236 root
->commit_root
= NULL
;
238 root
->root_key
.offset
= root
->fs_info
->generation
;
239 btrfs_set_root_blocknr(&root
->root_item
,
240 bh_blocknr(root
->node
));
241 err
= btrfs_insert_root(trans
, root
->fs_info
->tree_root
,
245 list_add(&dirty
->list
, list
);
251 static int drop_dirty_roots(struct btrfs_root
*tree_root
,
252 struct list_head
*list
)
254 struct dirty_root
*dirty
;
255 struct btrfs_trans_handle
*trans
;
258 while(!list_empty(list
)) {
259 dirty
= list_entry(list
->next
, struct dirty_root
, list
);
260 list_del_init(&dirty
->list
);
261 trans
= btrfs_start_transaction(tree_root
, 1);
262 ret
= btrfs_drop_snapshot(trans
, dirty
->root
,
266 ret
= btrfs_del_root(trans
, tree_root
, &dirty
->snap_key
);
268 ret
= btrfs_end_transaction(trans
, tree_root
);
275 int btrfs_commit_transaction(struct btrfs_trans_handle
*trans
,
276 struct btrfs_root
*root
)
279 struct btrfs_transaction
*cur_trans
;
280 struct btrfs_transaction
*prev_trans
= NULL
;
281 struct list_head dirty_fs_roots
;
284 INIT_LIST_HEAD(&dirty_fs_roots
);
286 mutex_lock(&root
->fs_info
->trans_mutex
);
287 if (trans
->transaction
->in_commit
) {
288 cur_trans
= trans
->transaction
;
289 trans
->transaction
->use_count
++;
290 btrfs_end_transaction(trans
, root
);
291 ret
= wait_for_commit(root
, cur_trans
);
293 put_transaction(cur_trans
);
294 mutex_unlock(&root
->fs_info
->trans_mutex
);
297 cur_trans
= trans
->transaction
;
298 trans
->transaction
->in_commit
= 1;
299 while (trans
->transaction
->num_writers
> 1) {
300 WARN_ON(cur_trans
!= trans
->transaction
);
301 prepare_to_wait(&trans
->transaction
->writer_wait
, &wait
,
302 TASK_UNINTERRUPTIBLE
);
303 if (trans
->transaction
->num_writers
<= 1)
305 mutex_unlock(&root
->fs_info
->trans_mutex
);
307 mutex_lock(&root
->fs_info
->trans_mutex
);
308 finish_wait(&trans
->transaction
->writer_wait
, &wait
);
310 finish_wait(&trans
->transaction
->writer_wait
, &wait
);
311 WARN_ON(cur_trans
!= trans
->transaction
);
312 add_dirty_roots(trans
, &root
->fs_info
->fs_roots_radix
, &dirty_fs_roots
);
313 ret
= btrfs_commit_tree_roots(trans
, root
);
315 cur_trans
= root
->fs_info
->running_transaction
;
316 root
->fs_info
->running_transaction
= NULL
;
317 if (cur_trans
->list
.prev
!= &root
->fs_info
->trans_list
) {
318 prev_trans
= list_entry(cur_trans
->list
.prev
,
319 struct btrfs_transaction
, list
);
320 if (prev_trans
->commit_done
)
323 prev_trans
->use_count
++;
325 mutex_unlock(&root
->fs_info
->trans_mutex
);
326 mutex_unlock(&root
->fs_info
->fs_mutex
);
327 ret
= btrfs_write_and_wait_transaction(trans
, root
);
329 mutex_lock(&root
->fs_info
->trans_mutex
);
330 wait_for_commit(root
, prev_trans
);
331 put_transaction(prev_trans
);
332 mutex_unlock(&root
->fs_info
->trans_mutex
);
334 btrfs_set_super_generation(root
->fs_info
->disk_super
,
337 write_ctree_super(trans
, root
);
339 mutex_lock(&root
->fs_info
->fs_mutex
);
340 btrfs_finish_extent_commit(trans
, root
);
341 mutex_lock(&root
->fs_info
->trans_mutex
);
342 cur_trans
->commit_done
= 1;
343 wake_up(&cur_trans
->commit_wait
);
344 put_transaction(cur_trans
);
345 put_transaction(cur_trans
);
346 mutex_unlock(&root
->fs_info
->trans_mutex
);
347 kmem_cache_free(btrfs_trans_handle_cachep
, trans
);
349 drop_dirty_roots(root
->fs_info
->tree_root
, &dirty_fs_roots
);