]>
Commit | Line | Data |
---|---|---|
6cbd5570 CM |
1 | /* |
2 | * Copyright (C) 2007 Oracle. All rights reserved. | |
3 | * | |
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. | |
7 | * | |
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. | |
12 | * | |
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. | |
17 | */ | |
18 | ||
79154b1b | 19 | #include <linux/fs.h> |
34088780 | 20 | #include <linux/sched.h> |
d3c2fdcf | 21 | #include <linux/writeback.h> |
79154b1b CM |
22 | #include "ctree.h" |
23 | #include "disk-io.h" | |
24 | #include "transaction.h" | |
25 | ||
78fae27e | 26 | static int total_trans = 0; |
2c90e5d6 CM |
27 | extern struct kmem_cache *btrfs_trans_handle_cachep; |
28 | extern struct kmem_cache *btrfs_transaction_cachep; | |
29 | ||
08607c1b CM |
30 | static struct workqueue_struct *trans_wq; |
31 | ||
0f7d52f4 | 32 | #define BTRFS_ROOT_TRANS_TAG 0 |
6702ed49 | 33 | #define BTRFS_ROOT_DEFRAG_TAG 1 |
0f7d52f4 | 34 | |
79154b1b CM |
35 | static void put_transaction(struct btrfs_transaction *transaction) |
36 | { | |
2c90e5d6 | 37 | WARN_ON(transaction->use_count == 0); |
79154b1b | 38 | transaction->use_count--; |
78fae27e CM |
39 | if (transaction->use_count == 0) { |
40 | WARN_ON(total_trans == 0); | |
41 | total_trans--; | |
8fd17795 | 42 | list_del_init(&transaction->list); |
2c90e5d6 CM |
43 | memset(transaction, 0, sizeof(*transaction)); |
44 | kmem_cache_free(btrfs_transaction_cachep, transaction); | |
78fae27e | 45 | } |
79154b1b CM |
46 | } |
47 | ||
48 | static int join_transaction(struct btrfs_root *root) | |
49 | { | |
50 | struct btrfs_transaction *cur_trans; | |
51 | cur_trans = root->fs_info->running_transaction; | |
52 | if (!cur_trans) { | |
2c90e5d6 CM |
53 | cur_trans = kmem_cache_alloc(btrfs_transaction_cachep, |
54 | GFP_NOFS); | |
78fae27e | 55 | total_trans++; |
79154b1b | 56 | BUG_ON(!cur_trans); |
0f7d52f4 | 57 | root->fs_info->generation++; |
79154b1b | 58 | root->fs_info->running_transaction = cur_trans; |
15ee9bc7 JB |
59 | cur_trans->num_writers = 1; |
60 | cur_trans->num_joined = 0; | |
0f7d52f4 | 61 | cur_trans->transid = root->fs_info->generation; |
79154b1b CM |
62 | init_waitqueue_head(&cur_trans->writer_wait); |
63 | init_waitqueue_head(&cur_trans->commit_wait); | |
64 | cur_trans->in_commit = 0; | |
d5719762 | 65 | cur_trans->use_count = 1; |
79154b1b | 66 | cur_trans->commit_done = 0; |
08607c1b | 67 | cur_trans->start_time = get_seconds(); |
8fd17795 | 68 | list_add_tail(&cur_trans->list, &root->fs_info->trans_list); |
7c4452b9 | 69 | init_bit_radix(&cur_trans->dirty_pages); |
15ee9bc7 JB |
70 | } else { |
71 | cur_trans->num_writers++; | |
72 | cur_trans->num_joined++; | |
79154b1b | 73 | } |
15ee9bc7 | 74 | |
79154b1b CM |
75 | return 0; |
76 | } | |
77 | ||
6702ed49 CM |
78 | static int record_root_in_trans(struct btrfs_root *root) |
79 | { | |
80 | u64 running_trans_id = root->fs_info->running_transaction->transid; | |
81 | if (root->ref_cows && root->last_trans < running_trans_id) { | |
82 | WARN_ON(root == root->fs_info->extent_root); | |
83 | if (root->root_item.refs != 0) { | |
84 | radix_tree_tag_set(&root->fs_info->fs_roots_radix, | |
85 | (unsigned long)root->root_key.objectid, | |
86 | BTRFS_ROOT_TRANS_TAG); | |
87 | radix_tree_tag_set(&root->fs_info->fs_roots_radix, | |
88 | (unsigned long)root->root_key.objectid, | |
89 | BTRFS_ROOT_DEFRAG_TAG); | |
90 | root->commit_root = root->node; | |
91 | get_bh(root->node); | |
92 | } else { | |
93 | WARN_ON(1); | |
94 | } | |
95 | root->last_trans = running_trans_id; | |
96 | } | |
97 | return 0; | |
98 | } | |
99 | ||
79154b1b CM |
100 | struct btrfs_trans_handle *btrfs_start_transaction(struct btrfs_root *root, |
101 | int num_blocks) | |
102 | { | |
2c90e5d6 CM |
103 | struct btrfs_trans_handle *h = |
104 | kmem_cache_alloc(btrfs_trans_handle_cachep, GFP_NOFS); | |
79154b1b CM |
105 | int ret; |
106 | ||
107 | mutex_lock(&root->fs_info->trans_mutex); | |
108 | ret = join_transaction(root); | |
109 | BUG_ON(ret); | |
0f7d52f4 | 110 | |
6702ed49 CM |
111 | record_root_in_trans(root); |
112 | h->transid = root->fs_info->running_transaction->transid; | |
79154b1b CM |
113 | h->transaction = root->fs_info->running_transaction; |
114 | h->blocks_reserved = num_blocks; | |
115 | h->blocks_used = 0; | |
31f3c99b | 116 | h->block_group = NULL; |
26b8003f CM |
117 | h->alloc_exclude_nr = 0; |
118 | h->alloc_exclude_start = 0; | |
79154b1b CM |
119 | root->fs_info->running_transaction->use_count++; |
120 | mutex_unlock(&root->fs_info->trans_mutex); | |
121 | return h; | |
122 | } | |
123 | ||
124 | int btrfs_end_transaction(struct btrfs_trans_handle *trans, | |
125 | struct btrfs_root *root) | |
126 | { | |
127 | struct btrfs_transaction *cur_trans; | |
d6e4a428 | 128 | |
79154b1b CM |
129 | mutex_lock(&root->fs_info->trans_mutex); |
130 | cur_trans = root->fs_info->running_transaction; | |
ccd467d6 | 131 | WARN_ON(cur_trans != trans->transaction); |
d5719762 | 132 | WARN_ON(cur_trans->num_writers < 1); |
ccd467d6 | 133 | cur_trans->num_writers--; |
79154b1b CM |
134 | if (waitqueue_active(&cur_trans->writer_wait)) |
135 | wake_up(&cur_trans->writer_wait); | |
79154b1b CM |
136 | put_transaction(cur_trans); |
137 | mutex_unlock(&root->fs_info->trans_mutex); | |
d6025579 | 138 | memset(trans, 0, sizeof(*trans)); |
2c90e5d6 | 139 | kmem_cache_free(btrfs_trans_handle_cachep, trans); |
79154b1b CM |
140 | return 0; |
141 | } | |
142 | ||
143 | ||
144 | int btrfs_write_and_wait_transaction(struct btrfs_trans_handle *trans, | |
145 | struct btrfs_root *root) | |
146 | { | |
7c4452b9 CM |
147 | unsigned long gang[16]; |
148 | int ret; | |
149 | int i; | |
150 | int err; | |
151 | int werr = 0; | |
152 | struct page *page; | |
153 | struct radix_tree_root *dirty_pages; | |
154 | struct inode *btree_inode = root->fs_info->btree_inode; | |
155 | ||
156 | if (!trans || !trans->transaction) { | |
157 | return filemap_write_and_wait(btree_inode->i_mapping); | |
158 | } | |
159 | dirty_pages = &trans->transaction->dirty_pages; | |
160 | while(1) { | |
e37c9e69 CM |
161 | ret = find_first_radix_bit(dirty_pages, gang, |
162 | 0, ARRAY_SIZE(gang)); | |
7c4452b9 CM |
163 | if (!ret) |
164 | break; | |
165 | for (i = 0; i < ret; i++) { | |
166 | /* FIXME EIO */ | |
167 | clear_radix_bit(dirty_pages, gang[i]); | |
168 | page = find_lock_page(btree_inode->i_mapping, | |
169 | gang[i]); | |
170 | if (!page) | |
171 | continue; | |
6702ed49 CM |
172 | if (PageWriteback(page)) { |
173 | if (PageDirty(page)) | |
174 | wait_on_page_writeback(page); | |
175 | else { | |
176 | unlock_page(page); | |
177 | page_cache_release(page); | |
178 | continue; | |
179 | } | |
180 | } | |
7c4452b9 CM |
181 | err = write_one_page(page, 0); |
182 | if (err) | |
183 | werr = err; | |
184 | page_cache_release(page); | |
185 | } | |
186 | } | |
187 | err = filemap_fdatawait(btree_inode->i_mapping); | |
188 | if (err) | |
189 | werr = err; | |
190 | return werr; | |
79154b1b CM |
191 | } |
192 | ||
193 | int btrfs_commit_tree_roots(struct btrfs_trans_handle *trans, | |
194 | struct btrfs_root *root) | |
195 | { | |
196 | int ret; | |
197 | u64 old_extent_block; | |
198 | struct btrfs_fs_info *fs_info = root->fs_info; | |
199 | struct btrfs_root *tree_root = fs_info->tree_root; | |
200 | struct btrfs_root *extent_root = fs_info->extent_root; | |
79154b1b | 201 | |
9078a3e1 | 202 | btrfs_write_dirty_block_groups(trans, extent_root); |
79154b1b CM |
203 | while(1) { |
204 | old_extent_block = btrfs_root_blocknr(&extent_root->root_item); | |
7eccb903 | 205 | if (old_extent_block == bh_blocknr(extent_root->node)) |
79154b1b CM |
206 | break; |
207 | btrfs_set_root_blocknr(&extent_root->root_item, | |
7eccb903 | 208 | bh_blocknr(extent_root->node)); |
79154b1b CM |
209 | ret = btrfs_update_root(trans, tree_root, |
210 | &extent_root->root_key, | |
211 | &extent_root->root_item); | |
212 | BUG_ON(ret); | |
9078a3e1 | 213 | btrfs_write_dirty_block_groups(trans, extent_root); |
79154b1b CM |
214 | } |
215 | return 0; | |
216 | } | |
217 | ||
218 | static int wait_for_commit(struct btrfs_root *root, | |
219 | struct btrfs_transaction *commit) | |
220 | { | |
221 | DEFINE_WAIT(wait); | |
ccd467d6 | 222 | mutex_lock(&root->fs_info->trans_mutex); |
79154b1b CM |
223 | while(!commit->commit_done) { |
224 | prepare_to_wait(&commit->commit_wait, &wait, | |
225 | TASK_UNINTERRUPTIBLE); | |
226 | if (commit->commit_done) | |
227 | break; | |
228 | mutex_unlock(&root->fs_info->trans_mutex); | |
229 | schedule(); | |
230 | mutex_lock(&root->fs_info->trans_mutex); | |
231 | } | |
ccd467d6 | 232 | mutex_unlock(&root->fs_info->trans_mutex); |
79154b1b CM |
233 | finish_wait(&commit->commit_wait, &wait); |
234 | return 0; | |
235 | } | |
236 | ||
0f7d52f4 CM |
237 | struct dirty_root { |
238 | struct list_head list; | |
0f7d52f4 | 239 | struct btrfs_root *root; |
58176a96 | 240 | struct btrfs_root *latest_root; |
0f7d52f4 CM |
241 | }; |
242 | ||
5ce14bbc CM |
243 | int btrfs_add_dead_root(struct btrfs_root *root, |
244 | struct btrfs_root *latest, | |
245 | struct list_head *dead_list) | |
5eda7b5e CM |
246 | { |
247 | struct dirty_root *dirty; | |
248 | ||
249 | dirty = kmalloc(sizeof(*dirty), GFP_NOFS); | |
250 | if (!dirty) | |
251 | return -ENOMEM; | |
5eda7b5e | 252 | dirty->root = root; |
5ce14bbc | 253 | dirty->latest_root = latest; |
5eda7b5e CM |
254 | list_add(&dirty->list, dead_list); |
255 | return 0; | |
256 | } | |
257 | ||
35b7e476 CM |
258 | static int add_dirty_roots(struct btrfs_trans_handle *trans, |
259 | struct radix_tree_root *radix, | |
260 | struct list_head *list) | |
0f7d52f4 CM |
261 | { |
262 | struct dirty_root *dirty; | |
263 | struct btrfs_root *gang[8]; | |
264 | struct btrfs_root *root; | |
265 | int i; | |
266 | int ret; | |
54aa1f4d | 267 | int err = 0; |
5eda7b5e | 268 | u32 refs; |
54aa1f4d | 269 | |
0f7d52f4 CM |
270 | while(1) { |
271 | ret = radix_tree_gang_lookup_tag(radix, (void **)gang, 0, | |
272 | ARRAY_SIZE(gang), | |
273 | BTRFS_ROOT_TRANS_TAG); | |
274 | if (ret == 0) | |
275 | break; | |
276 | for (i = 0; i < ret; i++) { | |
277 | root = gang[i]; | |
2619ba1f CM |
278 | radix_tree_tag_clear(radix, |
279 | (unsigned long)root->root_key.objectid, | |
280 | BTRFS_ROOT_TRANS_TAG); | |
0f7d52f4 | 281 | if (root->commit_root == root->node) { |
7eccb903 | 282 | WARN_ON(bh_blocknr(root->node) != |
0f7d52f4 CM |
283 | btrfs_root_blocknr(&root->root_item)); |
284 | brelse(root->commit_root); | |
285 | root->commit_root = NULL; | |
58176a96 JB |
286 | |
287 | /* make sure to update the root on disk | |
288 | * so we get any updates to the block used | |
289 | * counts | |
290 | */ | |
291 | err = btrfs_update_root(trans, | |
292 | root->fs_info->tree_root, | |
293 | &root->root_key, | |
294 | &root->root_item); | |
0f7d52f4 CM |
295 | continue; |
296 | } | |
297 | dirty = kmalloc(sizeof(*dirty), GFP_NOFS); | |
298 | BUG_ON(!dirty); | |
9f3a7427 CM |
299 | dirty->root = kmalloc(sizeof(*dirty->root), GFP_NOFS); |
300 | BUG_ON(!dirty->root); | |
301 | ||
302 | memset(&root->root_item.drop_progress, 0, | |
303 | sizeof(struct btrfs_disk_key)); | |
304 | root->root_item.drop_level = 0; | |
305 | ||
306 | memcpy(dirty->root, root, sizeof(*root)); | |
307 | dirty->root->node = root->commit_root; | |
58176a96 | 308 | dirty->latest_root = root; |
0f7d52f4 | 309 | root->commit_root = NULL; |
5eda7b5e | 310 | |
0f7d52f4 CM |
311 | root->root_key.offset = root->fs_info->generation; |
312 | btrfs_set_root_blocknr(&root->root_item, | |
7eccb903 | 313 | bh_blocknr(root->node)); |
0f7d52f4 CM |
314 | err = btrfs_insert_root(trans, root->fs_info->tree_root, |
315 | &root->root_key, | |
316 | &root->root_item); | |
54aa1f4d CM |
317 | if (err) |
318 | break; | |
9f3a7427 CM |
319 | |
320 | refs = btrfs_root_refs(&dirty->root->root_item); | |
321 | btrfs_set_root_refs(&dirty->root->root_item, refs - 1); | |
5eda7b5e | 322 | err = btrfs_update_root(trans, root->fs_info->tree_root, |
9f3a7427 CM |
323 | &dirty->root->root_key, |
324 | &dirty->root->root_item); | |
5eda7b5e CM |
325 | |
326 | BUG_ON(err); | |
9f3a7427 | 327 | if (refs == 1) { |
5eda7b5e | 328 | list_add(&dirty->list, list); |
9f3a7427 CM |
329 | } else { |
330 | WARN_ON(1); | |
331 | kfree(dirty->root); | |
5eda7b5e | 332 | kfree(dirty); |
9f3a7427 | 333 | } |
0f7d52f4 CM |
334 | } |
335 | } | |
54aa1f4d | 336 | return err; |
0f7d52f4 CM |
337 | } |
338 | ||
e9d0b13b CM |
339 | int btrfs_defrag_root(struct btrfs_root *root, int cacheonly) |
340 | { | |
341 | struct btrfs_fs_info *info = root->fs_info; | |
342 | int ret; | |
343 | struct btrfs_trans_handle *trans; | |
d3c2fdcf | 344 | unsigned long nr; |
e9d0b13b CM |
345 | |
346 | if (root->defrag_running) | |
347 | return 0; | |
348 | ||
349 | trans = btrfs_start_transaction(root, 1); | |
350 | while (1) { | |
351 | root->defrag_running = 1; | |
352 | ret = btrfs_defrag_leaves(trans, root, cacheonly); | |
d3c2fdcf | 353 | nr = trans->blocks_used; |
e9d0b13b CM |
354 | btrfs_end_transaction(trans, root); |
355 | mutex_unlock(&info->fs_mutex); | |
356 | ||
d3c2fdcf | 357 | btrfs_btree_balance_dirty(info->tree_root, nr); |
e9d0b13b CM |
358 | cond_resched(); |
359 | ||
360 | mutex_lock(&info->fs_mutex); | |
361 | trans = btrfs_start_transaction(root, 1); | |
362 | if (ret != -EAGAIN) | |
363 | break; | |
364 | } | |
365 | root->defrag_running = 0; | |
366 | radix_tree_tag_clear(&info->fs_roots_radix, | |
367 | (unsigned long)root->root_key.objectid, | |
368 | BTRFS_ROOT_DEFRAG_TAG); | |
369 | btrfs_end_transaction(trans, root); | |
370 | return 0; | |
371 | } | |
372 | ||
6702ed49 CM |
373 | int btrfs_defrag_dirty_roots(struct btrfs_fs_info *info) |
374 | { | |
375 | struct btrfs_root *gang[1]; | |
376 | struct btrfs_root *root; | |
6702ed49 CM |
377 | int i; |
378 | int ret; | |
379 | int err = 0; | |
380 | u64 last = 0; | |
381 | ||
6702ed49 CM |
382 | while(1) { |
383 | ret = radix_tree_gang_lookup_tag(&info->fs_roots_radix, | |
384 | (void **)gang, last, | |
385 | ARRAY_SIZE(gang), | |
386 | BTRFS_ROOT_DEFRAG_TAG); | |
387 | if (ret == 0) | |
388 | break; | |
389 | for (i = 0; i < ret; i++) { | |
390 | root = gang[i]; | |
391 | last = root->root_key.objectid + 1; | |
e9d0b13b | 392 | btrfs_defrag_root(root, 1); |
6702ed49 CM |
393 | } |
394 | } | |
e9d0b13b | 395 | btrfs_defrag_root(info->extent_root, 1); |
6702ed49 CM |
396 | return err; |
397 | } | |
398 | ||
35b7e476 CM |
399 | static int drop_dirty_roots(struct btrfs_root *tree_root, |
400 | struct list_head *list) | |
0f7d52f4 CM |
401 | { |
402 | struct dirty_root *dirty; | |
403 | struct btrfs_trans_handle *trans; | |
d3c2fdcf | 404 | unsigned long nr; |
58176a96 JB |
405 | u64 num_blocks; |
406 | u64 blocks_used; | |
54aa1f4d | 407 | int ret = 0; |
9f3a7427 CM |
408 | int err; |
409 | ||
0f7d52f4 | 410 | while(!list_empty(list)) { |
58176a96 JB |
411 | struct btrfs_root *root; |
412 | ||
facda1e7 | 413 | mutex_lock(&tree_root->fs_info->fs_mutex); |
0f7d52f4 CM |
414 | dirty = list_entry(list->next, struct dirty_root, list); |
415 | list_del_init(&dirty->list); | |
5eda7b5e | 416 | |
58176a96 JB |
417 | num_blocks = btrfs_root_blocks_used(&dirty->root->root_item); |
418 | root = dirty->latest_root; | |
419 | ||
9f3a7427 CM |
420 | while(1) { |
421 | trans = btrfs_start_transaction(tree_root, 1); | |
422 | ret = btrfs_drop_snapshot(trans, dirty->root); | |
423 | if (ret != -EAGAIN) { | |
424 | break; | |
425 | } | |
58176a96 | 426 | |
9f3a7427 CM |
427 | err = btrfs_update_root(trans, |
428 | tree_root, | |
429 | &dirty->root->root_key, | |
430 | &dirty->root->root_item); | |
431 | if (err) | |
432 | ret = err; | |
d3c2fdcf | 433 | nr = trans->blocks_used; |
9f3a7427 CM |
434 | ret = btrfs_end_transaction(trans, tree_root); |
435 | BUG_ON(ret); | |
f4468e94 | 436 | mutex_unlock(&tree_root->fs_info->fs_mutex); |
d3c2fdcf | 437 | btrfs_btree_balance_dirty(tree_root, nr); |
f4468e94 CM |
438 | schedule(); |
439 | ||
440 | mutex_lock(&tree_root->fs_info->fs_mutex); | |
9f3a7427 | 441 | } |
0f7d52f4 | 442 | BUG_ON(ret); |
58176a96 JB |
443 | |
444 | num_blocks -= btrfs_root_blocks_used(&dirty->root->root_item); | |
445 | blocks_used = btrfs_root_blocks_used(&root->root_item); | |
446 | if (num_blocks) { | |
447 | record_root_in_trans(root); | |
448 | btrfs_set_root_blocks_used(&root->root_item, | |
449 | blocks_used - num_blocks); | |
450 | } | |
9f3a7427 | 451 | ret = btrfs_del_root(trans, tree_root, &dirty->root->root_key); |
58176a96 JB |
452 | if (ret) { |
453 | BUG(); | |
54aa1f4d | 454 | break; |
58176a96 | 455 | } |
d3c2fdcf | 456 | nr = trans->blocks_used; |
0f7d52f4 CM |
457 | ret = btrfs_end_transaction(trans, tree_root); |
458 | BUG_ON(ret); | |
5eda7b5e | 459 | |
9f3a7427 | 460 | kfree(dirty->root); |
0f7d52f4 | 461 | kfree(dirty); |
facda1e7 | 462 | mutex_unlock(&tree_root->fs_info->fs_mutex); |
d3c2fdcf CM |
463 | |
464 | btrfs_btree_balance_dirty(tree_root, nr); | |
f4468e94 | 465 | schedule(); |
0f7d52f4 | 466 | } |
54aa1f4d | 467 | return ret; |
0f7d52f4 CM |
468 | } |
469 | ||
79154b1b CM |
470 | int btrfs_commit_transaction(struct btrfs_trans_handle *trans, |
471 | struct btrfs_root *root) | |
472 | { | |
15ee9bc7 JB |
473 | unsigned long joined = 0; |
474 | unsigned long timeout = 1; | |
79154b1b | 475 | struct btrfs_transaction *cur_trans; |
8fd17795 | 476 | struct btrfs_transaction *prev_trans = NULL; |
0f7d52f4 | 477 | struct list_head dirty_fs_roots; |
ccd467d6 | 478 | struct radix_tree_root pinned_copy; |
79154b1b | 479 | DEFINE_WAIT(wait); |
15ee9bc7 | 480 | int ret; |
79154b1b | 481 | |
ccd467d6 | 482 | init_bit_radix(&pinned_copy); |
0f7d52f4 | 483 | INIT_LIST_HEAD(&dirty_fs_roots); |
d6e4a428 | 484 | |
79154b1b CM |
485 | mutex_lock(&root->fs_info->trans_mutex); |
486 | if (trans->transaction->in_commit) { | |
487 | cur_trans = trans->transaction; | |
488 | trans->transaction->use_count++; | |
ccd467d6 | 489 | mutex_unlock(&root->fs_info->trans_mutex); |
79154b1b | 490 | btrfs_end_transaction(trans, root); |
ccd467d6 CM |
491 | |
492 | mutex_unlock(&root->fs_info->fs_mutex); | |
79154b1b CM |
493 | ret = wait_for_commit(root, cur_trans); |
494 | BUG_ON(ret); | |
15ee9bc7 JB |
495 | |
496 | mutex_lock(&root->fs_info->trans_mutex); | |
79154b1b | 497 | put_transaction(cur_trans); |
15ee9bc7 JB |
498 | mutex_unlock(&root->fs_info->trans_mutex); |
499 | ||
ccd467d6 | 500 | mutex_lock(&root->fs_info->fs_mutex); |
79154b1b CM |
501 | return 0; |
502 | } | |
2c90e5d6 | 503 | trans->transaction->in_commit = 1; |
ccd467d6 CM |
504 | cur_trans = trans->transaction; |
505 | if (cur_trans->list.prev != &root->fs_info->trans_list) { | |
506 | prev_trans = list_entry(cur_trans->list.prev, | |
507 | struct btrfs_transaction, list); | |
508 | if (!prev_trans->commit_done) { | |
509 | prev_trans->use_count++; | |
510 | mutex_unlock(&root->fs_info->fs_mutex); | |
511 | mutex_unlock(&root->fs_info->trans_mutex); | |
512 | ||
513 | wait_for_commit(root, prev_trans); | |
ccd467d6 CM |
514 | |
515 | mutex_lock(&root->fs_info->fs_mutex); | |
516 | mutex_lock(&root->fs_info->trans_mutex); | |
15ee9bc7 | 517 | put_transaction(prev_trans); |
ccd467d6 CM |
518 | } |
519 | } | |
15ee9bc7 JB |
520 | |
521 | do { | |
522 | joined = cur_trans->num_joined; | |
2c90e5d6 | 523 | WARN_ON(cur_trans != trans->transaction); |
15ee9bc7 | 524 | prepare_to_wait(&cur_trans->writer_wait, &wait, |
79154b1b | 525 | TASK_UNINTERRUPTIBLE); |
15ee9bc7 JB |
526 | |
527 | if (cur_trans->num_writers > 1) | |
528 | timeout = MAX_SCHEDULE_TIMEOUT; | |
529 | else | |
530 | timeout = 1; | |
531 | ||
ccd467d6 | 532 | mutex_unlock(&root->fs_info->fs_mutex); |
79154b1b | 533 | mutex_unlock(&root->fs_info->trans_mutex); |
15ee9bc7 JB |
534 | |
535 | schedule_timeout(timeout); | |
536 | ||
ccd467d6 | 537 | mutex_lock(&root->fs_info->fs_mutex); |
79154b1b | 538 | mutex_lock(&root->fs_info->trans_mutex); |
15ee9bc7 JB |
539 | finish_wait(&cur_trans->writer_wait, &wait); |
540 | } while (cur_trans->num_writers > 1 || | |
541 | (cur_trans->num_joined != joined)); | |
542 | ||
2c90e5d6 | 543 | WARN_ON(cur_trans != trans->transaction); |
54aa1f4d CM |
544 | ret = add_dirty_roots(trans, &root->fs_info->fs_roots_radix, |
545 | &dirty_fs_roots); | |
546 | BUG_ON(ret); | |
547 | ||
79154b1b CM |
548 | ret = btrfs_commit_tree_roots(trans, root); |
549 | BUG_ON(ret); | |
54aa1f4d | 550 | |
78fae27e CM |
551 | cur_trans = root->fs_info->running_transaction; |
552 | root->fs_info->running_transaction = NULL; | |
4b52dff6 CM |
553 | btrfs_set_super_generation(&root->fs_info->super_copy, |
554 | cur_trans->transid); | |
555 | btrfs_set_super_root(&root->fs_info->super_copy, | |
556 | bh_blocknr(root->fs_info->tree_root->node)); | |
557 | memcpy(root->fs_info->disk_super, &root->fs_info->super_copy, | |
558 | sizeof(root->fs_info->super_copy)); | |
ccd467d6 CM |
559 | |
560 | btrfs_copy_pinned(root, &pinned_copy); | |
561 | ||
78fae27e | 562 | mutex_unlock(&root->fs_info->trans_mutex); |
8fd17795 | 563 | mutex_unlock(&root->fs_info->fs_mutex); |
79154b1b CM |
564 | ret = btrfs_write_and_wait_transaction(trans, root); |
565 | BUG_ON(ret); | |
79154b1b | 566 | write_ctree_super(trans, root); |
8fd17795 | 567 | mutex_lock(&root->fs_info->fs_mutex); |
ccd467d6 | 568 | btrfs_finish_extent_commit(trans, root, &pinned_copy); |
78fae27e | 569 | mutex_lock(&root->fs_info->trans_mutex); |
2c90e5d6 | 570 | cur_trans->commit_done = 1; |
15ee9bc7 | 571 | root->fs_info->last_trans_committed = cur_trans->transid; |
2c90e5d6 | 572 | wake_up(&cur_trans->commit_wait); |
78fae27e | 573 | put_transaction(cur_trans); |
79154b1b | 574 | put_transaction(cur_trans); |
58176a96 | 575 | |
facda1e7 CM |
576 | if (root->fs_info->closing) |
577 | list_splice_init(&root->fs_info->dead_roots, &dirty_fs_roots); | |
578 | else | |
579 | list_splice_init(&dirty_fs_roots, &root->fs_info->dead_roots); | |
58176a96 | 580 | |
78fae27e | 581 | mutex_unlock(&root->fs_info->trans_mutex); |
2c90e5d6 | 582 | kmem_cache_free(btrfs_trans_handle_cachep, trans); |
79154b1b | 583 | |
facda1e7 CM |
584 | if (root->fs_info->closing) { |
585 | mutex_unlock(&root->fs_info->fs_mutex); | |
586 | drop_dirty_roots(root->fs_info->tree_root, &dirty_fs_roots); | |
587 | mutex_lock(&root->fs_info->fs_mutex); | |
588 | } | |
79154b1b CM |
589 | return ret; |
590 | } | |
591 | ||
e9d0b13b CM |
592 | int btrfs_clean_old_snapshots(struct btrfs_root *root) |
593 | { | |
594 | struct list_head dirty_roots; | |
595 | INIT_LIST_HEAD(&dirty_roots); | |
596 | ||
597 | mutex_lock(&root->fs_info->trans_mutex); | |
598 | list_splice_init(&root->fs_info->dead_roots, &dirty_roots); | |
599 | mutex_unlock(&root->fs_info->trans_mutex); | |
600 | ||
601 | if (!list_empty(&dirty_roots)) { | |
602 | drop_dirty_roots(root, &dirty_roots); | |
603 | } | |
604 | return 0; | |
605 | } | |
08607c1b CM |
606 | void btrfs_transaction_cleaner(struct work_struct *work) |
607 | { | |
608 | struct btrfs_fs_info *fs_info = container_of(work, | |
609 | struct btrfs_fs_info, | |
610 | trans_work.work); | |
611 | ||
612 | struct btrfs_root *root = fs_info->tree_root; | |
613 | struct btrfs_transaction *cur; | |
614 | struct btrfs_trans_handle *trans; | |
615 | unsigned long now; | |
616 | unsigned long delay = HZ * 30; | |
617 | int ret; | |
618 | ||
08607c1b CM |
619 | mutex_lock(&root->fs_info->fs_mutex); |
620 | mutex_lock(&root->fs_info->trans_mutex); | |
621 | cur = root->fs_info->running_transaction; | |
622 | if (!cur) { | |
623 | mutex_unlock(&root->fs_info->trans_mutex); | |
624 | goto out; | |
625 | } | |
626 | now = get_seconds(); | |
627 | if (now < cur->start_time || now - cur->start_time < 30) { | |
628 | mutex_unlock(&root->fs_info->trans_mutex); | |
629 | delay = HZ * 5; | |
630 | goto out; | |
631 | } | |
632 | mutex_unlock(&root->fs_info->trans_mutex); | |
6702ed49 | 633 | btrfs_defrag_dirty_roots(root->fs_info); |
08607c1b CM |
634 | trans = btrfs_start_transaction(root, 1); |
635 | ret = btrfs_commit_transaction(trans, root); | |
636 | out: | |
637 | mutex_unlock(&root->fs_info->fs_mutex); | |
e9d0b13b | 638 | btrfs_clean_old_snapshots(root); |
08607c1b CM |
639 | btrfs_transaction_queue_work(root, delay); |
640 | } | |
641 | ||
642 | void btrfs_transaction_queue_work(struct btrfs_root *root, int delay) | |
643 | { | |
644 | queue_delayed_work(trans_wq, &root->fs_info->trans_work, delay); | |
645 | } | |
646 | ||
647 | void btrfs_transaction_flush_work(struct btrfs_root *root) | |
648 | { | |
649 | cancel_rearming_delayed_workqueue(trans_wq, &root->fs_info->trans_work); | |
650 | flush_workqueue(trans_wq); | |
651 | } | |
652 | ||
653 | void __init btrfs_init_transaction_sys(void) | |
654 | { | |
655 | trans_wq = create_workqueue("btrfs"); | |
656 | } | |
657 | ||
658 | void __exit btrfs_exit_transaction_sys(void) | |
659 | { | |
660 | destroy_workqueue(trans_wq); | |
661 | } | |
662 |