Btrfs: Add a thread pool just for submit_bio
[linux-2.6-block.git] / fs / btrfs / transaction.c
CommitLineData
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>
5f39d397 22#include <linux/pagemap.h>
79154b1b
CM
23#include "ctree.h"
24#include "disk-io.h"
25#include "transaction.h"
26
78fae27e 27static int total_trans = 0;
2c90e5d6
CM
28extern struct kmem_cache *btrfs_trans_handle_cachep;
29extern struct kmem_cache *btrfs_transaction_cachep;
30
08607c1b
CM
31static struct workqueue_struct *trans_wq;
32
0f7d52f4 33#define BTRFS_ROOT_TRANS_TAG 0
6702ed49 34#define BTRFS_ROOT_DEFRAG_TAG 1
0f7d52f4 35
80b6794d 36static noinline void put_transaction(struct btrfs_transaction *transaction)
79154b1b 37{
2c90e5d6 38 WARN_ON(transaction->use_count == 0);
79154b1b 39 transaction->use_count--;
78fae27e
CM
40 if (transaction->use_count == 0) {
41 WARN_ON(total_trans == 0);
42 total_trans--;
8fd17795 43 list_del_init(&transaction->list);
2c90e5d6
CM
44 memset(transaction, 0, sizeof(*transaction));
45 kmem_cache_free(btrfs_transaction_cachep, transaction);
78fae27e 46 }
79154b1b
CM
47}
48
80b6794d 49static noinline int join_transaction(struct btrfs_root *root)
79154b1b
CM
50{
51 struct btrfs_transaction *cur_trans;
52 cur_trans = root->fs_info->running_transaction;
53 if (!cur_trans) {
2c90e5d6
CM
54 cur_trans = kmem_cache_alloc(btrfs_transaction_cachep,
55 GFP_NOFS);
78fae27e 56 total_trans++;
79154b1b 57 BUG_ON(!cur_trans);
0f7d52f4 58 root->fs_info->generation++;
e18e4809 59 root->fs_info->last_alloc = 0;
4529ba49 60 root->fs_info->last_data_alloc = 0;
15ee9bc7
JB
61 cur_trans->num_writers = 1;
62 cur_trans->num_joined = 0;
0f7d52f4 63 cur_trans->transid = root->fs_info->generation;
79154b1b
CM
64 init_waitqueue_head(&cur_trans->writer_wait);
65 init_waitqueue_head(&cur_trans->commit_wait);
66 cur_trans->in_commit = 0;
d5719762 67 cur_trans->use_count = 1;
79154b1b 68 cur_trans->commit_done = 0;
08607c1b 69 cur_trans->start_time = get_seconds();
3063d29f 70 INIT_LIST_HEAD(&cur_trans->pending_snapshots);
8fd17795 71 list_add_tail(&cur_trans->list, &root->fs_info->trans_list);
dc17ff8f 72 btrfs_ordered_inode_tree_init(&cur_trans->ordered_inode_tree);
d1310b2e 73 extent_io_tree_init(&cur_trans->dirty_pages,
5f39d397
CM
74 root->fs_info->btree_inode->i_mapping,
75 GFP_NOFS);
48ec2cf8
CM
76 spin_lock(&root->fs_info->new_trans_lock);
77 root->fs_info->running_transaction = cur_trans;
78 spin_unlock(&root->fs_info->new_trans_lock);
15ee9bc7
JB
79 } else {
80 cur_trans->num_writers++;
81 cur_trans->num_joined++;
79154b1b 82 }
15ee9bc7 83
79154b1b
CM
84 return 0;
85}
86
80b6794d 87static noinline int record_root_in_trans(struct btrfs_root *root)
6702ed49
CM
88{
89 u64 running_trans_id = root->fs_info->running_transaction->transid;
90 if (root->ref_cows && root->last_trans < running_trans_id) {
91 WARN_ON(root == root->fs_info->extent_root);
92 if (root->root_item.refs != 0) {
93 radix_tree_tag_set(&root->fs_info->fs_roots_radix,
94 (unsigned long)root->root_key.objectid,
95 BTRFS_ROOT_TRANS_TAG);
96 radix_tree_tag_set(&root->fs_info->fs_roots_radix,
97 (unsigned long)root->root_key.objectid,
98 BTRFS_ROOT_DEFRAG_TAG);
99 root->commit_root = root->node;
5f39d397 100 extent_buffer_get(root->node);
6702ed49
CM
101 } else {
102 WARN_ON(1);
103 }
104 root->last_trans = running_trans_id;
105 }
106 return 0;
107}
108
79154b1b
CM
109struct btrfs_trans_handle *btrfs_start_transaction(struct btrfs_root *root,
110 int num_blocks)
111{
2c90e5d6
CM
112 struct btrfs_trans_handle *h =
113 kmem_cache_alloc(btrfs_trans_handle_cachep, GFP_NOFS);
79154b1b
CM
114 int ret;
115
116 mutex_lock(&root->fs_info->trans_mutex);
117 ret = join_transaction(root);
118 BUG_ON(ret);
0f7d52f4 119
6702ed49
CM
120 record_root_in_trans(root);
121 h->transid = root->fs_info->running_transaction->transid;
79154b1b
CM
122 h->transaction = root->fs_info->running_transaction;
123 h->blocks_reserved = num_blocks;
124 h->blocks_used = 0;
31f3c99b 125 h->block_group = NULL;
26b8003f
CM
126 h->alloc_exclude_nr = 0;
127 h->alloc_exclude_start = 0;
79154b1b
CM
128 root->fs_info->running_transaction->use_count++;
129 mutex_unlock(&root->fs_info->trans_mutex);
130 return h;
131}
132
133int btrfs_end_transaction(struct btrfs_trans_handle *trans,
134 struct btrfs_root *root)
135{
136 struct btrfs_transaction *cur_trans;
d6e4a428 137
79154b1b
CM
138 mutex_lock(&root->fs_info->trans_mutex);
139 cur_trans = root->fs_info->running_transaction;
ccd467d6 140 WARN_ON(cur_trans != trans->transaction);
d5719762 141 WARN_ON(cur_trans->num_writers < 1);
ccd467d6 142 cur_trans->num_writers--;
79154b1b
CM
143 if (waitqueue_active(&cur_trans->writer_wait))
144 wake_up(&cur_trans->writer_wait);
79154b1b
CM
145 put_transaction(cur_trans);
146 mutex_unlock(&root->fs_info->trans_mutex);
d6025579 147 memset(trans, 0, sizeof(*trans));
2c90e5d6 148 kmem_cache_free(btrfs_trans_handle_cachep, trans);
79154b1b
CM
149 return 0;
150}
151
152
153int btrfs_write_and_wait_transaction(struct btrfs_trans_handle *trans,
154 struct btrfs_root *root)
155{
7c4452b9 156 int ret;
7c4452b9
CM
157 int err;
158 int werr = 0;
d1310b2e 159 struct extent_io_tree *dirty_pages;
7c4452b9 160 struct page *page;
7c4452b9 161 struct inode *btree_inode = root->fs_info->btree_inode;
5f39d397
CM
162 u64 start;
163 u64 end;
164 unsigned long index;
7c4452b9
CM
165
166 if (!trans || !trans->transaction) {
167 return filemap_write_and_wait(btree_inode->i_mapping);
168 }
169 dirty_pages = &trans->transaction->dirty_pages;
170 while(1) {
5f39d397
CM
171 ret = find_first_extent_bit(dirty_pages, 0, &start, &end,
172 EXTENT_DIRTY);
173 if (ret)
7c4452b9 174 break;
5f39d397
CM
175 clear_extent_dirty(dirty_pages, start, end, GFP_NOFS);
176 while(start <= end) {
177 index = start >> PAGE_CACHE_SHIFT;
35ebb934 178 start = (u64)(index + 1) << PAGE_CACHE_SHIFT;
5f39d397 179 page = find_lock_page(btree_inode->i_mapping, index);
7c4452b9
CM
180 if (!page)
181 continue;
6702ed49
CM
182 if (PageWriteback(page)) {
183 if (PageDirty(page))
184 wait_on_page_writeback(page);
185 else {
186 unlock_page(page);
187 page_cache_release(page);
188 continue;
189 }
190 }
7c4452b9
CM
191 err = write_one_page(page, 0);
192 if (err)
193 werr = err;
194 page_cache_release(page);
195 }
196 }
197 err = filemap_fdatawait(btree_inode->i_mapping);
198 if (err)
199 werr = err;
200 return werr;
79154b1b
CM
201}
202
0b86a832
CM
203static int update_cowonly_root(struct btrfs_trans_handle *trans,
204 struct btrfs_root *root)
79154b1b
CM
205{
206 int ret;
0b86a832
CM
207 u64 old_root_bytenr;
208 struct btrfs_root *tree_root = root->fs_info->tree_root;
79154b1b 209
0b86a832 210 btrfs_write_dirty_block_groups(trans, root);
79154b1b 211 while(1) {
0b86a832
CM
212 old_root_bytenr = btrfs_root_bytenr(&root->root_item);
213 if (old_root_bytenr == root->node->start)
79154b1b 214 break;
0b86a832
CM
215 btrfs_set_root_bytenr(&root->root_item,
216 root->node->start);
217 btrfs_set_root_level(&root->root_item,
218 btrfs_header_level(root->node));
79154b1b 219 ret = btrfs_update_root(trans, tree_root,
0b86a832
CM
220 &root->root_key,
221 &root->root_item);
79154b1b 222 BUG_ON(ret);
0b86a832
CM
223 btrfs_write_dirty_block_groups(trans, root);
224 }
225 return 0;
226}
227
228int btrfs_commit_tree_roots(struct btrfs_trans_handle *trans,
229 struct btrfs_root *root)
230{
231 struct btrfs_fs_info *fs_info = root->fs_info;
232 struct list_head *next;
233
234 while(!list_empty(&fs_info->dirty_cowonly_roots)) {
235 next = fs_info->dirty_cowonly_roots.next;
236 list_del_init(next);
237 root = list_entry(next, struct btrfs_root, dirty_list);
238 update_cowonly_root(trans, root);
79154b1b
CM
239 }
240 return 0;
241}
242
80b6794d
CM
243static noinline int wait_for_commit(struct btrfs_root *root,
244 struct btrfs_transaction *commit)
79154b1b
CM
245{
246 DEFINE_WAIT(wait);
ccd467d6 247 mutex_lock(&root->fs_info->trans_mutex);
79154b1b
CM
248 while(!commit->commit_done) {
249 prepare_to_wait(&commit->commit_wait, &wait,
250 TASK_UNINTERRUPTIBLE);
251 if (commit->commit_done)
252 break;
253 mutex_unlock(&root->fs_info->trans_mutex);
254 schedule();
255 mutex_lock(&root->fs_info->trans_mutex);
256 }
ccd467d6 257 mutex_unlock(&root->fs_info->trans_mutex);
79154b1b
CM
258 finish_wait(&commit->commit_wait, &wait);
259 return 0;
260}
261
0f7d52f4
CM
262struct dirty_root {
263 struct list_head list;
0f7d52f4 264 struct btrfs_root *root;
58176a96 265 struct btrfs_root *latest_root;
0f7d52f4
CM
266};
267
5ce14bbc
CM
268int btrfs_add_dead_root(struct btrfs_root *root,
269 struct btrfs_root *latest,
270 struct list_head *dead_list)
5eda7b5e
CM
271{
272 struct dirty_root *dirty;
273
274 dirty = kmalloc(sizeof(*dirty), GFP_NOFS);
275 if (!dirty)
276 return -ENOMEM;
5eda7b5e 277 dirty->root = root;
5ce14bbc 278 dirty->latest_root = latest;
5eda7b5e
CM
279 list_add(&dirty->list, dead_list);
280 return 0;
281}
282
80b6794d
CM
283static noinline int add_dirty_roots(struct btrfs_trans_handle *trans,
284 struct radix_tree_root *radix,
285 struct list_head *list)
0f7d52f4
CM
286{
287 struct dirty_root *dirty;
288 struct btrfs_root *gang[8];
289 struct btrfs_root *root;
290 int i;
291 int ret;
54aa1f4d 292 int err = 0;
5eda7b5e 293 u32 refs;
54aa1f4d 294
0f7d52f4
CM
295 while(1) {
296 ret = radix_tree_gang_lookup_tag(radix, (void **)gang, 0,
297 ARRAY_SIZE(gang),
298 BTRFS_ROOT_TRANS_TAG);
299 if (ret == 0)
300 break;
301 for (i = 0; i < ret; i++) {
302 root = gang[i];
2619ba1f
CM
303 radix_tree_tag_clear(radix,
304 (unsigned long)root->root_key.objectid,
305 BTRFS_ROOT_TRANS_TAG);
0f7d52f4 306 if (root->commit_root == root->node) {
db94535d
CM
307 WARN_ON(root->node->start !=
308 btrfs_root_bytenr(&root->root_item));
5f39d397 309 free_extent_buffer(root->commit_root);
0f7d52f4 310 root->commit_root = NULL;
58176a96
JB
311
312 /* make sure to update the root on disk
313 * so we get any updates to the block used
314 * counts
315 */
316 err = btrfs_update_root(trans,
317 root->fs_info->tree_root,
318 &root->root_key,
319 &root->root_item);
0f7d52f4
CM
320 continue;
321 }
322 dirty = kmalloc(sizeof(*dirty), GFP_NOFS);
323 BUG_ON(!dirty);
9f3a7427
CM
324 dirty->root = kmalloc(sizeof(*dirty->root), GFP_NOFS);
325 BUG_ON(!dirty->root);
326
327 memset(&root->root_item.drop_progress, 0,
328 sizeof(struct btrfs_disk_key));
329 root->root_item.drop_level = 0;
330
331 memcpy(dirty->root, root, sizeof(*root));
332 dirty->root->node = root->commit_root;
58176a96 333 dirty->latest_root = root;
0f7d52f4 334 root->commit_root = NULL;
5eda7b5e 335
0f7d52f4 336 root->root_key.offset = root->fs_info->generation;
db94535d
CM
337 btrfs_set_root_bytenr(&root->root_item,
338 root->node->start);
339 btrfs_set_root_level(&root->root_item,
340 btrfs_header_level(root->node));
0f7d52f4
CM
341 err = btrfs_insert_root(trans, root->fs_info->tree_root,
342 &root->root_key,
343 &root->root_item);
54aa1f4d
CM
344 if (err)
345 break;
9f3a7427
CM
346
347 refs = btrfs_root_refs(&dirty->root->root_item);
348 btrfs_set_root_refs(&dirty->root->root_item, refs - 1);
5eda7b5e 349 err = btrfs_update_root(trans, root->fs_info->tree_root,
9f3a7427
CM
350 &dirty->root->root_key,
351 &dirty->root->root_item);
5eda7b5e
CM
352
353 BUG_ON(err);
9f3a7427 354 if (refs == 1) {
5eda7b5e 355 list_add(&dirty->list, list);
9f3a7427
CM
356 } else {
357 WARN_ON(1);
358 kfree(dirty->root);
5eda7b5e 359 kfree(dirty);
9f3a7427 360 }
0f7d52f4
CM
361 }
362 }
54aa1f4d 363 return err;
0f7d52f4
CM
364}
365
e9d0b13b
CM
366int btrfs_defrag_root(struct btrfs_root *root, int cacheonly)
367{
368 struct btrfs_fs_info *info = root->fs_info;
369 int ret;
370 struct btrfs_trans_handle *trans;
d3c2fdcf 371 unsigned long nr;
e9d0b13b
CM
372
373 if (root->defrag_running)
374 return 0;
e9d0b13b 375 trans = btrfs_start_transaction(root, 1);
6b80053d 376 while (1) {
e9d0b13b
CM
377 root->defrag_running = 1;
378 ret = btrfs_defrag_leaves(trans, root, cacheonly);
d3c2fdcf 379 nr = trans->blocks_used;
e9d0b13b
CM
380 btrfs_end_transaction(trans, root);
381 mutex_unlock(&info->fs_mutex);
d3c2fdcf 382 btrfs_btree_balance_dirty(info->tree_root, nr);
e9d0b13b
CM
383 cond_resched();
384
385 mutex_lock(&info->fs_mutex);
386 trans = btrfs_start_transaction(root, 1);
387 if (ret != -EAGAIN)
388 break;
389 }
390 root->defrag_running = 0;
391 radix_tree_tag_clear(&info->fs_roots_radix,
392 (unsigned long)root->root_key.objectid,
393 BTRFS_ROOT_DEFRAG_TAG);
394 btrfs_end_transaction(trans, root);
395 return 0;
396}
397
6702ed49
CM
398int btrfs_defrag_dirty_roots(struct btrfs_fs_info *info)
399{
400 struct btrfs_root *gang[1];
401 struct btrfs_root *root;
6702ed49
CM
402 int i;
403 int ret;
404 int err = 0;
405 u64 last = 0;
406
6702ed49
CM
407 while(1) {
408 ret = radix_tree_gang_lookup_tag(&info->fs_roots_radix,
409 (void **)gang, last,
410 ARRAY_SIZE(gang),
411 BTRFS_ROOT_DEFRAG_TAG);
412 if (ret == 0)
413 break;
414 for (i = 0; i < ret; i++) {
415 root = gang[i];
416 last = root->root_key.objectid + 1;
f510cfec 417 btrfs_defrag_root(root, 1);
6702ed49
CM
418 }
419 }
6b80053d 420 btrfs_defrag_root(info->extent_root, 1);
6702ed49
CM
421 return err;
422}
423
80b6794d
CM
424static noinline int drop_dirty_roots(struct btrfs_root *tree_root,
425 struct list_head *list)
0f7d52f4
CM
426{
427 struct dirty_root *dirty;
428 struct btrfs_trans_handle *trans;
d3c2fdcf 429 unsigned long nr;
db94535d
CM
430 u64 num_bytes;
431 u64 bytes_used;
54aa1f4d 432 int ret = 0;
9f3a7427
CM
433 int err;
434
0f7d52f4 435 while(!list_empty(list)) {
58176a96
JB
436 struct btrfs_root *root;
437
facda1e7 438 mutex_lock(&tree_root->fs_info->fs_mutex);
0f7d52f4
CM
439 dirty = list_entry(list->next, struct dirty_root, list);
440 list_del_init(&dirty->list);
5eda7b5e 441
db94535d 442 num_bytes = btrfs_root_used(&dirty->root->root_item);
58176a96 443 root = dirty->latest_root;
e2008b61 444 root->fs_info->throttles++;
58176a96 445
9f3a7427
CM
446 while(1) {
447 trans = btrfs_start_transaction(tree_root, 1);
448 ret = btrfs_drop_snapshot(trans, dirty->root);
449 if (ret != -EAGAIN) {
450 break;
451 }
58176a96 452
9f3a7427
CM
453 err = btrfs_update_root(trans,
454 tree_root,
455 &dirty->root->root_key,
456 &dirty->root->root_item);
457 if (err)
458 ret = err;
d3c2fdcf 459 nr = trans->blocks_used;
9f3a7427
CM
460 ret = btrfs_end_transaction(trans, tree_root);
461 BUG_ON(ret);
f4468e94 462 mutex_unlock(&tree_root->fs_info->fs_mutex);
d3c2fdcf 463 btrfs_btree_balance_dirty(tree_root, nr);
4dc11904 464 cond_resched();
f4468e94 465 mutex_lock(&tree_root->fs_info->fs_mutex);
9f3a7427 466 }
0f7d52f4 467 BUG_ON(ret);
e2008b61 468 root->fs_info->throttles--;
58176a96 469
db94535d
CM
470 num_bytes -= btrfs_root_used(&dirty->root->root_item);
471 bytes_used = btrfs_root_used(&root->root_item);
472 if (num_bytes) {
58176a96 473 record_root_in_trans(root);
5f39d397 474 btrfs_set_root_used(&root->root_item,
db94535d 475 bytes_used - num_bytes);
58176a96 476 }
9f3a7427 477 ret = btrfs_del_root(trans, tree_root, &dirty->root->root_key);
58176a96
JB
478 if (ret) {
479 BUG();
54aa1f4d 480 break;
58176a96 481 }
d3c2fdcf 482 nr = trans->blocks_used;
0f7d52f4
CM
483 ret = btrfs_end_transaction(trans, tree_root);
484 BUG_ON(ret);
5eda7b5e 485
f510cfec 486 free_extent_buffer(dirty->root->node);
9f3a7427 487 kfree(dirty->root);
0f7d52f4 488 kfree(dirty);
facda1e7 489 mutex_unlock(&tree_root->fs_info->fs_mutex);
d3c2fdcf
CM
490
491 btrfs_btree_balance_dirty(tree_root, nr);
4dc11904 492 cond_resched();
0f7d52f4 493 }
54aa1f4d 494 return ret;
0f7d52f4
CM
495}
496
dc17ff8f
CM
497int btrfs_write_ordered_inodes(struct btrfs_trans_handle *trans,
498 struct btrfs_root *root)
499{
500 struct btrfs_transaction *cur_trans = trans->transaction;
501 struct inode *inode;
502 u64 root_objectid = 0;
503 u64 objectid = 0;
dc17ff8f
CM
504 int ret;
505
e2008b61 506 root->fs_info->throttles++;
dc17ff8f
CM
507 while(1) {
508 ret = btrfs_find_first_ordered_inode(
509 &cur_trans->ordered_inode_tree,
4d5e74bc 510 &root_objectid, &objectid, &inode);
dc17ff8f
CM
511 if (!ret)
512 break;
513
514 mutex_unlock(&root->fs_info->trans_mutex);
515 mutex_unlock(&root->fs_info->fs_mutex);
4d5e74bc 516
81d7ed29
CM
517 if (S_ISREG(inode->i_mode)) {
518 atomic_inc(&BTRFS_I(inode)->ordered_writeback);
4d5e74bc 519 filemap_fdatawrite(inode->i_mapping);
81d7ed29
CM
520 atomic_dec(&BTRFS_I(inode)->ordered_writeback);
521 }
4d5e74bc
CM
522 iput(inode);
523
dc17ff8f
CM
524 mutex_lock(&root->fs_info->fs_mutex);
525 mutex_lock(&root->fs_info->trans_mutex);
526 }
527 while(1) {
528 root_objectid = 0;
529 objectid = 0;
530 ret = btrfs_find_del_first_ordered_inode(
531 &cur_trans->ordered_inode_tree,
4d5e74bc 532 &root_objectid, &objectid, &inode);
dc17ff8f
CM
533 if (!ret)
534 break;
535 mutex_unlock(&root->fs_info->trans_mutex);
536 mutex_unlock(&root->fs_info->fs_mutex);
4d5e74bc 537
81d7ed29
CM
538 if (S_ISREG(inode->i_mode)) {
539 atomic_inc(&BTRFS_I(inode)->ordered_writeback);
4d5e74bc 540 filemap_write_and_wait(inode->i_mapping);
81d7ed29
CM
541 atomic_dec(&BTRFS_I(inode)->ordered_writeback);
542 }
4d5e74bc
CM
543 atomic_dec(&inode->i_count);
544 iput(inode);
545
dc17ff8f
CM
546 mutex_lock(&root->fs_info->fs_mutex);
547 mutex_lock(&root->fs_info->trans_mutex);
548 }
e2008b61 549 root->fs_info->throttles--;
3063d29f
CM
550 return 0;
551}
552
80b6794d 553static noinline int create_pending_snapshot(struct btrfs_trans_handle *trans,
3063d29f
CM
554 struct btrfs_fs_info *fs_info,
555 struct btrfs_pending_snapshot *pending)
556{
557 struct btrfs_key key;
80b6794d 558 struct btrfs_root_item *new_root_item;
3063d29f
CM
559 struct btrfs_root *tree_root = fs_info->tree_root;
560 struct btrfs_root *root = pending->root;
561 struct extent_buffer *tmp;
562 int ret;
3b96362c 563 int namelen;
3063d29f
CM
564 u64 objectid;
565
80b6794d
CM
566 new_root_item = kmalloc(sizeof(*new_root_item), GFP_NOFS);
567 if (!new_root_item) {
568 ret = -ENOMEM;
569 goto fail;
570 }
3063d29f
CM
571 ret = btrfs_find_free_objectid(trans, tree_root, 0, &objectid);
572 if (ret)
573 goto fail;
574
80b6794d 575 memcpy(new_root_item, &root->root_item, sizeof(*new_root_item));
3063d29f
CM
576
577 key.objectid = objectid;
578 key.offset = 1;
579 btrfs_set_key_type(&key, BTRFS_ROOT_ITEM_KEY);
580
581 extent_buffer_get(root->node);
582 btrfs_cow_block(trans, root, root->node, NULL, 0, &tmp);
583 free_extent_buffer(tmp);
584
585 btrfs_copy_root(trans, root, root->node, &tmp, objectid);
586
80b6794d
CM
587 btrfs_set_root_bytenr(new_root_item, tmp->start);
588 btrfs_set_root_level(new_root_item, btrfs_header_level(tmp));
3063d29f 589 ret = btrfs_insert_root(trans, root->fs_info->tree_root, &key,
80b6794d 590 new_root_item);
3063d29f
CM
591 free_extent_buffer(tmp);
592 if (ret)
593 goto fail;
594
595 /*
596 * insert the directory item
597 */
598 key.offset = (u64)-1;
3b96362c 599 namelen = strlen(pending->name);
3063d29f 600 ret = btrfs_insert_dir_item(trans, root->fs_info->tree_root,
3b96362c 601 pending->name, namelen,
3063d29f
CM
602 root->fs_info->sb->s_root->d_inode->i_ino,
603 &key, BTRFS_FT_DIR);
604
605 if (ret)
606 goto fail;
607
608 ret = btrfs_insert_inode_ref(trans, root->fs_info->tree_root,
609 pending->name, strlen(pending->name), objectid,
610 root->fs_info->sb->s_root->d_inode->i_ino);
3b96362c
SW
611
612 /* Invalidate existing dcache entry for new snapshot. */
613 btrfs_invalidate_dcache_root(root, pending->name, namelen);
614
3063d29f 615fail:
80b6794d 616 kfree(new_root_item);
3063d29f
CM
617 return ret;
618}
619
80b6794d
CM
620static noinline int create_pending_snapshots(struct btrfs_trans_handle *trans,
621 struct btrfs_fs_info *fs_info)
3063d29f
CM
622{
623 struct btrfs_pending_snapshot *pending;
624 struct list_head *head = &trans->transaction->pending_snapshots;
625 int ret;
626
627 while(!list_empty(head)) {
628 pending = list_entry(head->next,
629 struct btrfs_pending_snapshot, list);
630 ret = create_pending_snapshot(trans, fs_info, pending);
631 BUG_ON(ret);
632 list_del(&pending->list);
633 kfree(pending->name);
634 kfree(pending);
635 }
dc17ff8f
CM
636 return 0;
637}
638
79154b1b
CM
639int btrfs_commit_transaction(struct btrfs_trans_handle *trans,
640 struct btrfs_root *root)
641{
15ee9bc7
JB
642 unsigned long joined = 0;
643 unsigned long timeout = 1;
79154b1b 644 struct btrfs_transaction *cur_trans;
8fd17795 645 struct btrfs_transaction *prev_trans = NULL;
0b86a832 646 struct btrfs_root *chunk_root = root->fs_info->chunk_root;
0f7d52f4 647 struct list_head dirty_fs_roots;
d1310b2e 648 struct extent_io_tree *pinned_copy;
79154b1b 649 DEFINE_WAIT(wait);
15ee9bc7 650 int ret;
79154b1b 651
0f7d52f4 652 INIT_LIST_HEAD(&dirty_fs_roots);
d6e4a428 653
79154b1b
CM
654 mutex_lock(&root->fs_info->trans_mutex);
655 if (trans->transaction->in_commit) {
656 cur_trans = trans->transaction;
657 trans->transaction->use_count++;
ccd467d6 658 mutex_unlock(&root->fs_info->trans_mutex);
79154b1b 659 btrfs_end_transaction(trans, root);
ccd467d6
CM
660
661 mutex_unlock(&root->fs_info->fs_mutex);
79154b1b
CM
662 ret = wait_for_commit(root, cur_trans);
663 BUG_ON(ret);
15ee9bc7
JB
664
665 mutex_lock(&root->fs_info->trans_mutex);
79154b1b 666 put_transaction(cur_trans);
15ee9bc7
JB
667 mutex_unlock(&root->fs_info->trans_mutex);
668
ccd467d6 669 mutex_lock(&root->fs_info->fs_mutex);
79154b1b
CM
670 return 0;
671 }
4313b399
CM
672
673 pinned_copy = kmalloc(sizeof(*pinned_copy), GFP_NOFS);
674 if (!pinned_copy)
675 return -ENOMEM;
676
d1310b2e 677 extent_io_tree_init(pinned_copy,
4313b399
CM
678 root->fs_info->btree_inode->i_mapping, GFP_NOFS);
679
2c90e5d6 680 trans->transaction->in_commit = 1;
ccd467d6
CM
681 cur_trans = trans->transaction;
682 if (cur_trans->list.prev != &root->fs_info->trans_list) {
683 prev_trans = list_entry(cur_trans->list.prev,
684 struct btrfs_transaction, list);
685 if (!prev_trans->commit_done) {
686 prev_trans->use_count++;
687 mutex_unlock(&root->fs_info->fs_mutex);
688 mutex_unlock(&root->fs_info->trans_mutex);
689
690 wait_for_commit(root, prev_trans);
ccd467d6
CM
691
692 mutex_lock(&root->fs_info->fs_mutex);
693 mutex_lock(&root->fs_info->trans_mutex);
15ee9bc7 694 put_transaction(prev_trans);
ccd467d6
CM
695 }
696 }
15ee9bc7
JB
697
698 do {
699 joined = cur_trans->num_joined;
2c90e5d6 700 WARN_ON(cur_trans != trans->transaction);
15ee9bc7 701 prepare_to_wait(&cur_trans->writer_wait, &wait,
79154b1b 702 TASK_UNINTERRUPTIBLE);
15ee9bc7
JB
703
704 if (cur_trans->num_writers > 1)
705 timeout = MAX_SCHEDULE_TIMEOUT;
706 else
707 timeout = 1;
708
ccd467d6 709 mutex_unlock(&root->fs_info->fs_mutex);
79154b1b 710 mutex_unlock(&root->fs_info->trans_mutex);
15ee9bc7
JB
711
712 schedule_timeout(timeout);
713
ccd467d6 714 mutex_lock(&root->fs_info->fs_mutex);
79154b1b 715 mutex_lock(&root->fs_info->trans_mutex);
15ee9bc7 716 finish_wait(&cur_trans->writer_wait, &wait);
dc17ff8f
CM
717 ret = btrfs_write_ordered_inodes(trans, root);
718
15ee9bc7
JB
719 } while (cur_trans->num_writers > 1 ||
720 (cur_trans->num_joined != joined));
721
3063d29f
CM
722 ret = create_pending_snapshots(trans, root->fs_info);
723 BUG_ON(ret);
724
2c90e5d6 725 WARN_ON(cur_trans != trans->transaction);
dc17ff8f 726
54aa1f4d
CM
727 ret = add_dirty_roots(trans, &root->fs_info->fs_roots_radix,
728 &dirty_fs_roots);
729 BUG_ON(ret);
730
79154b1b
CM
731 ret = btrfs_commit_tree_roots(trans, root);
732 BUG_ON(ret);
54aa1f4d 733
78fae27e 734 cur_trans = root->fs_info->running_transaction;
cee36a03 735 spin_lock(&root->fs_info->new_trans_lock);
78fae27e 736 root->fs_info->running_transaction = NULL;
cee36a03 737 spin_unlock(&root->fs_info->new_trans_lock);
4b52dff6
CM
738 btrfs_set_super_generation(&root->fs_info->super_copy,
739 cur_trans->transid);
740 btrfs_set_super_root(&root->fs_info->super_copy,
db94535d
CM
741 root->fs_info->tree_root->node->start);
742 btrfs_set_super_root_level(&root->fs_info->super_copy,
743 btrfs_header_level(root->fs_info->tree_root->node));
5f39d397 744
0b86a832
CM
745 btrfs_set_super_chunk_root(&root->fs_info->super_copy,
746 chunk_root->node->start);
747 btrfs_set_super_chunk_root_level(&root->fs_info->super_copy,
748 btrfs_header_level(chunk_root->node));
a061fc8d
CM
749 memcpy(&root->fs_info->super_for_commit, &root->fs_info->super_copy,
750 sizeof(root->fs_info->super_copy));
ccd467d6 751
4313b399 752 btrfs_copy_pinned(root, pinned_copy);
ccd467d6 753
78fae27e 754 mutex_unlock(&root->fs_info->trans_mutex);
8fd17795 755 mutex_unlock(&root->fs_info->fs_mutex);
79154b1b
CM
756 ret = btrfs_write_and_wait_transaction(trans, root);
757 BUG_ON(ret);
79154b1b 758 write_ctree_super(trans, root);
4313b399 759
8fd17795 760 mutex_lock(&root->fs_info->fs_mutex);
4313b399 761 btrfs_finish_extent_commit(trans, root, pinned_copy);
78fae27e 762 mutex_lock(&root->fs_info->trans_mutex);
4313b399
CM
763
764 kfree(pinned_copy);
765
2c90e5d6 766 cur_trans->commit_done = 1;
15ee9bc7 767 root->fs_info->last_trans_committed = cur_trans->transid;
2c90e5d6 768 wake_up(&cur_trans->commit_wait);
78fae27e 769 put_transaction(cur_trans);
79154b1b 770 put_transaction(cur_trans);
58176a96 771
facda1e7
CM
772 if (root->fs_info->closing)
773 list_splice_init(&root->fs_info->dead_roots, &dirty_fs_roots);
774 else
775 list_splice_init(&dirty_fs_roots, &root->fs_info->dead_roots);
58176a96 776
78fae27e 777 mutex_unlock(&root->fs_info->trans_mutex);
2c90e5d6 778 kmem_cache_free(btrfs_trans_handle_cachep, trans);
79154b1b 779
facda1e7
CM
780 if (root->fs_info->closing) {
781 mutex_unlock(&root->fs_info->fs_mutex);
782 drop_dirty_roots(root->fs_info->tree_root, &dirty_fs_roots);
783 mutex_lock(&root->fs_info->fs_mutex);
784 }
79154b1b
CM
785 return ret;
786}
787
e9d0b13b
CM
788int btrfs_clean_old_snapshots(struct btrfs_root *root)
789{
790 struct list_head dirty_roots;
791 INIT_LIST_HEAD(&dirty_roots);
792
793 mutex_lock(&root->fs_info->trans_mutex);
794 list_splice_init(&root->fs_info->dead_roots, &dirty_roots);
795 mutex_unlock(&root->fs_info->trans_mutex);
796
797 if (!list_empty(&dirty_roots)) {
798 drop_dirty_roots(root, &dirty_roots);
799 }
800 return 0;
801}
6da6abae
CM
802#if LINUX_VERSION_CODE <= KERNEL_VERSION(2,6,18)
803void btrfs_transaction_cleaner(void *p)
804#else
08607c1b 805void btrfs_transaction_cleaner(struct work_struct *work)
6da6abae 806#endif
08607c1b 807{
6da6abae
CM
808#if LINUX_VERSION_CODE <= KERNEL_VERSION(2,6,18)
809 struct btrfs_fs_info *fs_info = p;
810#else
08607c1b
CM
811 struct btrfs_fs_info *fs_info = container_of(work,
812 struct btrfs_fs_info,
813 trans_work.work);
814
6da6abae 815#endif
08607c1b
CM
816 struct btrfs_root *root = fs_info->tree_root;
817 struct btrfs_transaction *cur;
818 struct btrfs_trans_handle *trans;
819 unsigned long now;
820 unsigned long delay = HZ * 30;
821 int ret;
822
08607c1b 823 mutex_lock(&root->fs_info->fs_mutex);
d6bfde87
CM
824 if (root->fs_info->closing)
825 goto out;
826
08607c1b
CM
827 mutex_lock(&root->fs_info->trans_mutex);
828 cur = root->fs_info->running_transaction;
829 if (!cur) {
830 mutex_unlock(&root->fs_info->trans_mutex);
831 goto out;
832 }
833 now = get_seconds();
834 if (now < cur->start_time || now - cur->start_time < 30) {
835 mutex_unlock(&root->fs_info->trans_mutex);
836 delay = HZ * 5;
837 goto out;
838 }
839 mutex_unlock(&root->fs_info->trans_mutex);
6702ed49 840 btrfs_defrag_dirty_roots(root->fs_info);
08607c1b
CM
841 trans = btrfs_start_transaction(root, 1);
842 ret = btrfs_commit_transaction(trans, root);
843out:
844 mutex_unlock(&root->fs_info->fs_mutex);
e9d0b13b 845 btrfs_clean_old_snapshots(root);
08607c1b
CM
846 btrfs_transaction_queue_work(root, delay);
847}
848
849void btrfs_transaction_queue_work(struct btrfs_root *root, int delay)
850{
d6bfde87
CM
851 if (!root->fs_info->closing)
852 queue_delayed_work(trans_wq, &root->fs_info->trans_work, delay);
08607c1b
CM
853}
854
855void btrfs_transaction_flush_work(struct btrfs_root *root)
856{
d6bfde87 857 cancel_delayed_work(&root->fs_info->trans_work);
08607c1b
CM
858 flush_workqueue(trans_wq);
859}
860
861void __init btrfs_init_transaction_sys(void)
862{
ce9adaa5 863 trans_wq = create_workqueue("btrfs-transaction");
08607c1b
CM
864}
865
17636e03 866void btrfs_exit_transaction_sys(void)
08607c1b
CM
867{
868 destroy_workqueue(trans_wq);
869}
870