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