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