Btrfs: Update on disk i_size only after pending ordered extents are done
[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"
925baedd 26#include "locking.h"
79154b1b 27
78fae27e 28static int total_trans = 0;
2c90e5d6
CM
29extern struct kmem_cache *btrfs_trans_handle_cachep;
30extern struct kmem_cache *btrfs_transaction_cachep;
31
0f7d52f4
CM
32#define BTRFS_ROOT_TRANS_TAG 0
33
80b6794d 34static noinline void put_transaction(struct btrfs_transaction *transaction)
79154b1b 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
80b6794d 47static noinline int join_transaction(struct btrfs_root *root)
79154b1b
CM
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++;
e18e4809 57 root->fs_info->last_alloc = 0;
4529ba49 58 root->fs_info->last_data_alloc = 0;
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();
3063d29f 68 INIT_LIST_HEAD(&cur_trans->pending_snapshots);
8fd17795 69 list_add_tail(&cur_trans->list, &root->fs_info->trans_list);
d1310b2e 70 extent_io_tree_init(&cur_trans->dirty_pages,
5f39d397
CM
71 root->fs_info->btree_inode->i_mapping,
72 GFP_NOFS);
48ec2cf8
CM
73 spin_lock(&root->fs_info->new_trans_lock);
74 root->fs_info->running_transaction = cur_trans;
75 spin_unlock(&root->fs_info->new_trans_lock);
15ee9bc7
JB
76 } else {
77 cur_trans->num_writers++;
78 cur_trans->num_joined++;
79154b1b 79 }
15ee9bc7 80
79154b1b
CM
81 return 0;
82}
83
80b6794d 84static noinline int record_root_in_trans(struct btrfs_root *root)
6702ed49
CM
85{
86 u64 running_trans_id = root->fs_info->running_transaction->transid;
87 if (root->ref_cows && root->last_trans < running_trans_id) {
88 WARN_ON(root == root->fs_info->extent_root);
89 if (root->root_item.refs != 0) {
90 radix_tree_tag_set(&root->fs_info->fs_roots_radix,
91 (unsigned long)root->root_key.objectid,
92 BTRFS_ROOT_TRANS_TAG);
925baedd 93 root->commit_root = btrfs_root_node(root);
6702ed49
CM
94 } else {
95 WARN_ON(1);
96 }
97 root->last_trans = running_trans_id;
98 }
99 return 0;
100}
101
79154b1b
CM
102struct btrfs_trans_handle *btrfs_start_transaction(struct btrfs_root *root,
103 int num_blocks)
104{
2c90e5d6
CM
105 struct btrfs_trans_handle *h =
106 kmem_cache_alloc(btrfs_trans_handle_cachep, GFP_NOFS);
79154b1b
CM
107 int ret;
108
109 mutex_lock(&root->fs_info->trans_mutex);
110 ret = join_transaction(root);
111 BUG_ON(ret);
0f7d52f4 112
6702ed49
CM
113 record_root_in_trans(root);
114 h->transid = root->fs_info->running_transaction->transid;
79154b1b
CM
115 h->transaction = root->fs_info->running_transaction;
116 h->blocks_reserved = num_blocks;
117 h->blocks_used = 0;
31f3c99b 118 h->block_group = NULL;
26b8003f
CM
119 h->alloc_exclude_nr = 0;
120 h->alloc_exclude_start = 0;
79154b1b
CM
121 root->fs_info->running_transaction->use_count++;
122 mutex_unlock(&root->fs_info->trans_mutex);
123 return h;
124}
125
89ce8a63
CM
126static noinline int wait_for_commit(struct btrfs_root *root,
127 struct btrfs_transaction *commit)
128{
129 DEFINE_WAIT(wait);
130 mutex_lock(&root->fs_info->trans_mutex);
131 while(!commit->commit_done) {
132 prepare_to_wait(&commit->commit_wait, &wait,
133 TASK_UNINTERRUPTIBLE);
134 if (commit->commit_done)
135 break;
136 mutex_unlock(&root->fs_info->trans_mutex);
137 schedule();
138 mutex_lock(&root->fs_info->trans_mutex);
139 }
140 mutex_unlock(&root->fs_info->trans_mutex);
141 finish_wait(&commit->commit_wait, &wait);
142 return 0;
143}
144
145static int __btrfs_end_transaction(struct btrfs_trans_handle *trans,
146 struct btrfs_root *root, int throttle)
79154b1b
CM
147{
148 struct btrfs_transaction *cur_trans;
d6e4a428 149
79154b1b
CM
150 mutex_lock(&root->fs_info->trans_mutex);
151 cur_trans = root->fs_info->running_transaction;
ccd467d6 152 WARN_ON(cur_trans != trans->transaction);
d5719762 153 WARN_ON(cur_trans->num_writers < 1);
ccd467d6 154 cur_trans->num_writers--;
89ce8a63 155
79154b1b
CM
156 if (waitqueue_active(&cur_trans->writer_wait))
157 wake_up(&cur_trans->writer_wait);
89ce8a63
CM
158
159 if (cur_trans->in_commit && throttle) {
e6dcd2dc 160 DEFINE_WAIT(wait);
89ce8a63 161 mutex_unlock(&root->fs_info->trans_mutex);
e6dcd2dc
CM
162 prepare_to_wait(&root->fs_info->transaction_throttle, &wait,
163 TASK_UNINTERRUPTIBLE);
164 schedule();
165 finish_wait(&root->fs_info->transaction_throttle, &wait);
89ce8a63
CM
166 mutex_lock(&root->fs_info->trans_mutex);
167 }
168
79154b1b
CM
169 put_transaction(cur_trans);
170 mutex_unlock(&root->fs_info->trans_mutex);
d6025579 171 memset(trans, 0, sizeof(*trans));
2c90e5d6 172 kmem_cache_free(btrfs_trans_handle_cachep, trans);
79154b1b
CM
173 return 0;
174}
175
89ce8a63
CM
176int btrfs_end_transaction(struct btrfs_trans_handle *trans,
177 struct btrfs_root *root)
178{
179 return __btrfs_end_transaction(trans, root, 0);
180}
181
182int btrfs_end_transaction_throttle(struct btrfs_trans_handle *trans,
183 struct btrfs_root *root)
184{
185 return __btrfs_end_transaction(trans, root, 1);
186}
187
79154b1b
CM
188
189int btrfs_write_and_wait_transaction(struct btrfs_trans_handle *trans,
190 struct btrfs_root *root)
191{
7c4452b9 192 int ret;
7c4452b9
CM
193 int err;
194 int werr = 0;
d1310b2e 195 struct extent_io_tree *dirty_pages;
7c4452b9 196 struct page *page;
7c4452b9 197 struct inode *btree_inode = root->fs_info->btree_inode;
5f39d397
CM
198 u64 start;
199 u64 end;
200 unsigned long index;
7c4452b9
CM
201
202 if (!trans || !trans->transaction) {
203 return filemap_write_and_wait(btree_inode->i_mapping);
204 }
205 dirty_pages = &trans->transaction->dirty_pages;
206 while(1) {
5f39d397
CM
207 ret = find_first_extent_bit(dirty_pages, 0, &start, &end,
208 EXTENT_DIRTY);
209 if (ret)
7c4452b9 210 break;
5f39d397
CM
211 clear_extent_dirty(dirty_pages, start, end, GFP_NOFS);
212 while(start <= end) {
213 index = start >> PAGE_CACHE_SHIFT;
35ebb934 214 start = (u64)(index + 1) << PAGE_CACHE_SHIFT;
5f39d397 215 page = find_lock_page(btree_inode->i_mapping, index);
7c4452b9
CM
216 if (!page)
217 continue;
6702ed49
CM
218 if (PageWriteback(page)) {
219 if (PageDirty(page))
220 wait_on_page_writeback(page);
221 else {
222 unlock_page(page);
223 page_cache_release(page);
224 continue;
225 }
226 }
7c4452b9
CM
227 err = write_one_page(page, 0);
228 if (err)
229 werr = err;
230 page_cache_release(page);
231 }
232 }
233 err = filemap_fdatawait(btree_inode->i_mapping);
234 if (err)
235 werr = err;
236 return werr;
79154b1b
CM
237}
238
0b86a832
CM
239static int update_cowonly_root(struct btrfs_trans_handle *trans,
240 struct btrfs_root *root)
79154b1b
CM
241{
242 int ret;
0b86a832
CM
243 u64 old_root_bytenr;
244 struct btrfs_root *tree_root = root->fs_info->tree_root;
79154b1b 245
0b86a832 246 btrfs_write_dirty_block_groups(trans, root);
79154b1b 247 while(1) {
0b86a832
CM
248 old_root_bytenr = btrfs_root_bytenr(&root->root_item);
249 if (old_root_bytenr == root->node->start)
79154b1b 250 break;
0b86a832
CM
251 btrfs_set_root_bytenr(&root->root_item,
252 root->node->start);
253 btrfs_set_root_level(&root->root_item,
254 btrfs_header_level(root->node));
79154b1b 255 ret = btrfs_update_root(trans, tree_root,
0b86a832
CM
256 &root->root_key,
257 &root->root_item);
79154b1b 258 BUG_ON(ret);
0b86a832
CM
259 btrfs_write_dirty_block_groups(trans, root);
260 }
261 return 0;
262}
263
264int btrfs_commit_tree_roots(struct btrfs_trans_handle *trans,
265 struct btrfs_root *root)
266{
267 struct btrfs_fs_info *fs_info = root->fs_info;
268 struct list_head *next;
269
270 while(!list_empty(&fs_info->dirty_cowonly_roots)) {
271 next = fs_info->dirty_cowonly_roots.next;
272 list_del_init(next);
273 root = list_entry(next, struct btrfs_root, dirty_list);
274 update_cowonly_root(trans, root);
79154b1b
CM
275 }
276 return 0;
277}
278
0f7d52f4
CM
279struct dirty_root {
280 struct list_head list;
0f7d52f4 281 struct btrfs_root *root;
58176a96 282 struct btrfs_root *latest_root;
0f7d52f4
CM
283};
284
5ce14bbc
CM
285int btrfs_add_dead_root(struct btrfs_root *root,
286 struct btrfs_root *latest,
287 struct list_head *dead_list)
5eda7b5e
CM
288{
289 struct dirty_root *dirty;
290
291 dirty = kmalloc(sizeof(*dirty), GFP_NOFS);
292 if (!dirty)
293 return -ENOMEM;
5eda7b5e 294 dirty->root = root;
5ce14bbc 295 dirty->latest_root = latest;
5eda7b5e
CM
296 list_add(&dirty->list, dead_list);
297 return 0;
298}
299
80b6794d
CM
300static noinline int add_dirty_roots(struct btrfs_trans_handle *trans,
301 struct radix_tree_root *radix,
302 struct list_head *list)
0f7d52f4
CM
303{
304 struct dirty_root *dirty;
305 struct btrfs_root *gang[8];
306 struct btrfs_root *root;
307 int i;
308 int ret;
54aa1f4d 309 int err = 0;
5eda7b5e 310 u32 refs;
54aa1f4d 311
0f7d52f4
CM
312 while(1) {
313 ret = radix_tree_gang_lookup_tag(radix, (void **)gang, 0,
314 ARRAY_SIZE(gang),
315 BTRFS_ROOT_TRANS_TAG);
316 if (ret == 0)
317 break;
318 for (i = 0; i < ret; i++) {
319 root = gang[i];
2619ba1f
CM
320 radix_tree_tag_clear(radix,
321 (unsigned long)root->root_key.objectid,
322 BTRFS_ROOT_TRANS_TAG);
0f7d52f4 323 if (root->commit_root == root->node) {
db94535d
CM
324 WARN_ON(root->node->start !=
325 btrfs_root_bytenr(&root->root_item));
5f39d397 326 free_extent_buffer(root->commit_root);
0f7d52f4 327 root->commit_root = NULL;
58176a96
JB
328
329 /* make sure to update the root on disk
330 * so we get any updates to the block used
331 * counts
332 */
333 err = btrfs_update_root(trans,
334 root->fs_info->tree_root,
335 &root->root_key,
336 &root->root_item);
0f7d52f4
CM
337 continue;
338 }
339 dirty = kmalloc(sizeof(*dirty), GFP_NOFS);
340 BUG_ON(!dirty);
9f3a7427
CM
341 dirty->root = kmalloc(sizeof(*dirty->root), GFP_NOFS);
342 BUG_ON(!dirty->root);
343
344 memset(&root->root_item.drop_progress, 0,
345 sizeof(struct btrfs_disk_key));
346 root->root_item.drop_level = 0;
347
348 memcpy(dirty->root, root, sizeof(*root));
349 dirty->root->node = root->commit_root;
58176a96 350 dirty->latest_root = root;
0f7d52f4 351 root->commit_root = NULL;
5eda7b5e 352
0f7d52f4 353 root->root_key.offset = root->fs_info->generation;
db94535d
CM
354 btrfs_set_root_bytenr(&root->root_item,
355 root->node->start);
356 btrfs_set_root_level(&root->root_item,
357 btrfs_header_level(root->node));
0f7d52f4
CM
358 err = btrfs_insert_root(trans, root->fs_info->tree_root,
359 &root->root_key,
360 &root->root_item);
54aa1f4d
CM
361 if (err)
362 break;
9f3a7427
CM
363
364 refs = btrfs_root_refs(&dirty->root->root_item);
365 btrfs_set_root_refs(&dirty->root->root_item, refs - 1);
5eda7b5e 366 err = btrfs_update_root(trans, root->fs_info->tree_root,
9f3a7427
CM
367 &dirty->root->root_key,
368 &dirty->root->root_item);
5eda7b5e
CM
369
370 BUG_ON(err);
9f3a7427 371 if (refs == 1) {
5eda7b5e 372 list_add(&dirty->list, list);
9f3a7427
CM
373 } else {
374 WARN_ON(1);
375 kfree(dirty->root);
5eda7b5e 376 kfree(dirty);
9f3a7427 377 }
0f7d52f4
CM
378 }
379 }
54aa1f4d 380 return err;
0f7d52f4
CM
381}
382
e9d0b13b
CM
383int btrfs_defrag_root(struct btrfs_root *root, int cacheonly)
384{
385 struct btrfs_fs_info *info = root->fs_info;
386 int ret;
387 struct btrfs_trans_handle *trans;
d3c2fdcf 388 unsigned long nr;
e9d0b13b 389
a2135011 390 smp_mb();
e9d0b13b
CM
391 if (root->defrag_running)
392 return 0;
e9d0b13b 393 trans = btrfs_start_transaction(root, 1);
6b80053d 394 while (1) {
e9d0b13b
CM
395 root->defrag_running = 1;
396 ret = btrfs_defrag_leaves(trans, root, cacheonly);
d3c2fdcf 397 nr = trans->blocks_used;
e9d0b13b 398 btrfs_end_transaction(trans, root);
d3c2fdcf 399 btrfs_btree_balance_dirty(info->tree_root, nr);
e9d0b13b
CM
400 cond_resched();
401
e9d0b13b 402 trans = btrfs_start_transaction(root, 1);
3f157a2f 403 if (root->fs_info->closing || ret != -EAGAIN)
e9d0b13b
CM
404 break;
405 }
406 root->defrag_running = 0;
a2135011 407 smp_mb();
e9d0b13b
CM
408 btrfs_end_transaction(trans, root);
409 return 0;
410}
411
80b6794d
CM
412static noinline int drop_dirty_roots(struct btrfs_root *tree_root,
413 struct list_head *list)
0f7d52f4
CM
414{
415 struct dirty_root *dirty;
416 struct btrfs_trans_handle *trans;
d3c2fdcf 417 unsigned long nr;
db94535d
CM
418 u64 num_bytes;
419 u64 bytes_used;
54aa1f4d 420 int ret = 0;
9f3a7427
CM
421 int err;
422
0f7d52f4 423 while(!list_empty(list)) {
58176a96
JB
424 struct btrfs_root *root;
425
0f7d52f4
CM
426 dirty = list_entry(list->next, struct dirty_root, list);
427 list_del_init(&dirty->list);
5eda7b5e 428
db94535d 429 num_bytes = btrfs_root_used(&dirty->root->root_item);
58176a96 430 root = dirty->latest_root;
a2135011 431 atomic_inc(&root->fs_info->throttles);
58176a96 432
a2135011 433 mutex_lock(&root->fs_info->drop_mutex);
9f3a7427
CM
434 while(1) {
435 trans = btrfs_start_transaction(tree_root, 1);
436 ret = btrfs_drop_snapshot(trans, dirty->root);
437 if (ret != -EAGAIN) {
438 break;
439 }
58176a96 440
9f3a7427
CM
441 err = btrfs_update_root(trans,
442 tree_root,
443 &dirty->root->root_key,
444 &dirty->root->root_item);
445 if (err)
446 ret = err;
d3c2fdcf 447 nr = trans->blocks_used;
1b1e2135 448 ret = btrfs_end_transaction_throttle(trans, tree_root);
9f3a7427 449 BUG_ON(ret);
a2135011
CM
450
451 mutex_unlock(&root->fs_info->drop_mutex);
d3c2fdcf 452 btrfs_btree_balance_dirty(tree_root, nr);
4dc11904 453 cond_resched();
a2135011 454 mutex_lock(&root->fs_info->drop_mutex);
9f3a7427 455 }
0f7d52f4 456 BUG_ON(ret);
a2135011 457 atomic_dec(&root->fs_info->throttles);
58176a96 458
a2135011 459 mutex_lock(&root->fs_info->alloc_mutex);
db94535d
CM
460 num_bytes -= btrfs_root_used(&dirty->root->root_item);
461 bytes_used = btrfs_root_used(&root->root_item);
462 if (num_bytes) {
58176a96 463 record_root_in_trans(root);
5f39d397 464 btrfs_set_root_used(&root->root_item,
db94535d 465 bytes_used - num_bytes);
58176a96 466 }
a2135011
CM
467 mutex_unlock(&root->fs_info->alloc_mutex);
468
9f3a7427 469 ret = btrfs_del_root(trans, tree_root, &dirty->root->root_key);
58176a96
JB
470 if (ret) {
471 BUG();
54aa1f4d 472 break;
58176a96 473 }
a2135011
CM
474 mutex_unlock(&root->fs_info->drop_mutex);
475
d3c2fdcf 476 nr = trans->blocks_used;
0f7d52f4
CM
477 ret = btrfs_end_transaction(trans, tree_root);
478 BUG_ON(ret);
5eda7b5e 479
f510cfec 480 free_extent_buffer(dirty->root->node);
9f3a7427 481 kfree(dirty->root);
0f7d52f4 482 kfree(dirty);
d3c2fdcf
CM
483
484 btrfs_btree_balance_dirty(tree_root, nr);
4dc11904 485 cond_resched();
0f7d52f4 486 }
54aa1f4d 487 return ret;
0f7d52f4
CM
488}
489
80b6794d 490static noinline int create_pending_snapshot(struct btrfs_trans_handle *trans,
3063d29f
CM
491 struct btrfs_fs_info *fs_info,
492 struct btrfs_pending_snapshot *pending)
493{
494 struct btrfs_key key;
80b6794d 495 struct btrfs_root_item *new_root_item;
3063d29f
CM
496 struct btrfs_root *tree_root = fs_info->tree_root;
497 struct btrfs_root *root = pending->root;
498 struct extent_buffer *tmp;
925baedd 499 struct extent_buffer *old;
3063d29f 500 int ret;
3b96362c 501 int namelen;
3063d29f
CM
502 u64 objectid;
503
80b6794d
CM
504 new_root_item = kmalloc(sizeof(*new_root_item), GFP_NOFS);
505 if (!new_root_item) {
506 ret = -ENOMEM;
507 goto fail;
508 }
3063d29f
CM
509 ret = btrfs_find_free_objectid(trans, tree_root, 0, &objectid);
510 if (ret)
511 goto fail;
512
80b6794d 513 memcpy(new_root_item, &root->root_item, sizeof(*new_root_item));
3063d29f
CM
514
515 key.objectid = objectid;
516 key.offset = 1;
517 btrfs_set_key_type(&key, BTRFS_ROOT_ITEM_KEY);
518
925baedd
CM
519 old = btrfs_lock_root_node(root);
520 btrfs_cow_block(trans, root, old, NULL, 0, &old);
3063d29f 521
925baedd
CM
522 btrfs_copy_root(trans, root, old, &tmp, objectid);
523 btrfs_tree_unlock(old);
524 free_extent_buffer(old);
3063d29f 525
80b6794d
CM
526 btrfs_set_root_bytenr(new_root_item, tmp->start);
527 btrfs_set_root_level(new_root_item, btrfs_header_level(tmp));
3063d29f 528 ret = btrfs_insert_root(trans, root->fs_info->tree_root, &key,
80b6794d 529 new_root_item);
925baedd 530 btrfs_tree_unlock(tmp);
3063d29f
CM
531 free_extent_buffer(tmp);
532 if (ret)
533 goto fail;
534
535 /*
536 * insert the directory item
537 */
538 key.offset = (u64)-1;
3b96362c 539 namelen = strlen(pending->name);
3063d29f 540 ret = btrfs_insert_dir_item(trans, root->fs_info->tree_root,
3b96362c 541 pending->name, namelen,
3063d29f
CM
542 root->fs_info->sb->s_root->d_inode->i_ino,
543 &key, BTRFS_FT_DIR);
544
545 if (ret)
546 goto fail;
547
548 ret = btrfs_insert_inode_ref(trans, root->fs_info->tree_root,
549 pending->name, strlen(pending->name), objectid,
550 root->fs_info->sb->s_root->d_inode->i_ino);
3b96362c
SW
551
552 /* Invalidate existing dcache entry for new snapshot. */
553 btrfs_invalidate_dcache_root(root, pending->name, namelen);
554
3063d29f 555fail:
80b6794d 556 kfree(new_root_item);
3063d29f
CM
557 return ret;
558}
559
80b6794d
CM
560static noinline int create_pending_snapshots(struct btrfs_trans_handle *trans,
561 struct btrfs_fs_info *fs_info)
3063d29f
CM
562{
563 struct btrfs_pending_snapshot *pending;
564 struct list_head *head = &trans->transaction->pending_snapshots;
565 int ret;
566
567 while(!list_empty(head)) {
568 pending = list_entry(head->next,
569 struct btrfs_pending_snapshot, list);
570 ret = create_pending_snapshot(trans, fs_info, pending);
571 BUG_ON(ret);
572 list_del(&pending->list);
573 kfree(pending->name);
574 kfree(pending);
575 }
dc17ff8f
CM
576 return 0;
577}
578
79154b1b
CM
579int btrfs_commit_transaction(struct btrfs_trans_handle *trans,
580 struct btrfs_root *root)
581{
15ee9bc7
JB
582 unsigned long joined = 0;
583 unsigned long timeout = 1;
79154b1b 584 struct btrfs_transaction *cur_trans;
8fd17795 585 struct btrfs_transaction *prev_trans = NULL;
0b86a832 586 struct btrfs_root *chunk_root = root->fs_info->chunk_root;
0f7d52f4 587 struct list_head dirty_fs_roots;
d1310b2e 588 struct extent_io_tree *pinned_copy;
79154b1b 589 DEFINE_WAIT(wait);
15ee9bc7 590 int ret;
79154b1b 591
0f7d52f4 592 INIT_LIST_HEAD(&dirty_fs_roots);
d6e4a428 593
79154b1b
CM
594 mutex_lock(&root->fs_info->trans_mutex);
595 if (trans->transaction->in_commit) {
596 cur_trans = trans->transaction;
597 trans->transaction->use_count++;
ccd467d6 598 mutex_unlock(&root->fs_info->trans_mutex);
79154b1b 599 btrfs_end_transaction(trans, root);
ccd467d6 600
79154b1b
CM
601 ret = wait_for_commit(root, cur_trans);
602 BUG_ON(ret);
15ee9bc7
JB
603
604 mutex_lock(&root->fs_info->trans_mutex);
79154b1b 605 put_transaction(cur_trans);
15ee9bc7
JB
606 mutex_unlock(&root->fs_info->trans_mutex);
607
79154b1b
CM
608 return 0;
609 }
4313b399
CM
610
611 pinned_copy = kmalloc(sizeof(*pinned_copy), GFP_NOFS);
612 if (!pinned_copy)
613 return -ENOMEM;
614
d1310b2e 615 extent_io_tree_init(pinned_copy,
4313b399
CM
616 root->fs_info->btree_inode->i_mapping, GFP_NOFS);
617
e6dcd2dc 618printk("commit trans %Lu\n", trans->transid);
2c90e5d6 619 trans->transaction->in_commit = 1;
ccd467d6
CM
620 cur_trans = trans->transaction;
621 if (cur_trans->list.prev != &root->fs_info->trans_list) {
622 prev_trans = list_entry(cur_trans->list.prev,
623 struct btrfs_transaction, list);
624 if (!prev_trans->commit_done) {
625 prev_trans->use_count++;
ccd467d6
CM
626 mutex_unlock(&root->fs_info->trans_mutex);
627
628 wait_for_commit(root, prev_trans);
ccd467d6 629
ccd467d6 630 mutex_lock(&root->fs_info->trans_mutex);
15ee9bc7 631 put_transaction(prev_trans);
ccd467d6
CM
632 }
633 }
15ee9bc7
JB
634
635 do {
636 joined = cur_trans->num_joined;
2c90e5d6 637 WARN_ON(cur_trans != trans->transaction);
15ee9bc7 638 prepare_to_wait(&cur_trans->writer_wait, &wait,
79154b1b 639 TASK_UNINTERRUPTIBLE);
15ee9bc7
JB
640
641 if (cur_trans->num_writers > 1)
642 timeout = MAX_SCHEDULE_TIMEOUT;
643 else
644 timeout = 1;
645
79154b1b 646 mutex_unlock(&root->fs_info->trans_mutex);
15ee9bc7
JB
647
648 schedule_timeout(timeout);
649
79154b1b 650 mutex_lock(&root->fs_info->trans_mutex);
15ee9bc7
JB
651 finish_wait(&cur_trans->writer_wait, &wait);
652 } while (cur_trans->num_writers > 1 ||
653 (cur_trans->num_joined != joined));
654
3063d29f
CM
655 ret = create_pending_snapshots(trans, root->fs_info);
656 BUG_ON(ret);
657
2c90e5d6 658 WARN_ON(cur_trans != trans->transaction);
dc17ff8f 659
54aa1f4d
CM
660 ret = add_dirty_roots(trans, &root->fs_info->fs_roots_radix,
661 &dirty_fs_roots);
662 BUG_ON(ret);
663
79154b1b
CM
664 ret = btrfs_commit_tree_roots(trans, root);
665 BUG_ON(ret);
54aa1f4d 666
78fae27e 667 cur_trans = root->fs_info->running_transaction;
cee36a03 668 spin_lock(&root->fs_info->new_trans_lock);
78fae27e 669 root->fs_info->running_transaction = NULL;
cee36a03 670 spin_unlock(&root->fs_info->new_trans_lock);
4b52dff6
CM
671 btrfs_set_super_generation(&root->fs_info->super_copy,
672 cur_trans->transid);
673 btrfs_set_super_root(&root->fs_info->super_copy,
db94535d
CM
674 root->fs_info->tree_root->node->start);
675 btrfs_set_super_root_level(&root->fs_info->super_copy,
676 btrfs_header_level(root->fs_info->tree_root->node));
5f39d397 677
0b86a832
CM
678 btrfs_set_super_chunk_root(&root->fs_info->super_copy,
679 chunk_root->node->start);
680 btrfs_set_super_chunk_root_level(&root->fs_info->super_copy,
681 btrfs_header_level(chunk_root->node));
a061fc8d
CM
682 memcpy(&root->fs_info->super_for_commit, &root->fs_info->super_copy,
683 sizeof(root->fs_info->super_copy));
ccd467d6 684
4313b399 685 btrfs_copy_pinned(root, pinned_copy);
ccd467d6 686
e6dcd2dc
CM
687 wake_up(&root->fs_info->transaction_throttle);
688
78fae27e 689 mutex_unlock(&root->fs_info->trans_mutex);
79154b1b
CM
690 ret = btrfs_write_and_wait_transaction(trans, root);
691 BUG_ON(ret);
79154b1b 692 write_ctree_super(trans, root);
4313b399 693
4313b399 694 btrfs_finish_extent_commit(trans, root, pinned_copy);
78fae27e 695 mutex_lock(&root->fs_info->trans_mutex);
4313b399
CM
696
697 kfree(pinned_copy);
698
2c90e5d6 699 cur_trans->commit_done = 1;
15ee9bc7 700 root->fs_info->last_trans_committed = cur_trans->transid;
2c90e5d6 701 wake_up(&cur_trans->commit_wait);
78fae27e 702 put_transaction(cur_trans);
79154b1b 703 put_transaction(cur_trans);
58176a96 704
facda1e7
CM
705 if (root->fs_info->closing)
706 list_splice_init(&root->fs_info->dead_roots, &dirty_fs_roots);
707 else
708 list_splice_init(&dirty_fs_roots, &root->fs_info->dead_roots);
58176a96 709
78fae27e 710 mutex_unlock(&root->fs_info->trans_mutex);
e6dcd2dc 711printk("done commit trans %Lu\n", trans->transid);
2c90e5d6 712 kmem_cache_free(btrfs_trans_handle_cachep, trans);
79154b1b 713
facda1e7 714 if (root->fs_info->closing) {
facda1e7 715 drop_dirty_roots(root->fs_info->tree_root, &dirty_fs_roots);
facda1e7 716 }
79154b1b
CM
717 return ret;
718}
719
e9d0b13b
CM
720int btrfs_clean_old_snapshots(struct btrfs_root *root)
721{
722 struct list_head dirty_roots;
723 INIT_LIST_HEAD(&dirty_roots);
a74a4b97 724again:
e9d0b13b
CM
725 mutex_lock(&root->fs_info->trans_mutex);
726 list_splice_init(&root->fs_info->dead_roots, &dirty_roots);
727 mutex_unlock(&root->fs_info->trans_mutex);
728
729 if (!list_empty(&dirty_roots)) {
730 drop_dirty_roots(root, &dirty_roots);
a74a4b97 731 goto again;
e9d0b13b
CM
732 }
733 return 0;
734}
08607c1b 735