Btrfs: simplify iteration codes
[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>
5f2cc086 23#include <linux/blkdev.h>
79154b1b
CM
24#include "ctree.h"
25#include "disk-io.h"
26#include "transaction.h"
925baedd 27#include "locking.h"
31153d81 28#include "ref-cache.h"
e02119d5 29#include "tree-log.h"
79154b1b 30
0f7d52f4
CM
31#define BTRFS_ROOT_TRANS_TAG 0
32
80b6794d 33static noinline void put_transaction(struct btrfs_transaction *transaction)
79154b1b 34{
2c90e5d6 35 WARN_ON(transaction->use_count == 0);
79154b1b 36 transaction->use_count--;
78fae27e 37 if (transaction->use_count == 0) {
8fd17795 38 list_del_init(&transaction->list);
2c90e5d6
CM
39 memset(transaction, 0, sizeof(*transaction));
40 kmem_cache_free(btrfs_transaction_cachep, transaction);
78fae27e 41 }
79154b1b
CM
42}
43
d352ac68
CM
44/*
45 * either allocate a new transaction or hop into the existing one
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);
79154b1b 54 BUG_ON(!cur_trans);
0f7d52f4 55 root->fs_info->generation++;
e18e4809 56 root->fs_info->last_alloc = 0;
4529ba49 57 root->fs_info->last_data_alloc = 0;
15ee9bc7
JB
58 cur_trans->num_writers = 1;
59 cur_trans->num_joined = 0;
0f7d52f4 60 cur_trans->transid = root->fs_info->generation;
79154b1b
CM
61 init_waitqueue_head(&cur_trans->writer_wait);
62 init_waitqueue_head(&cur_trans->commit_wait);
63 cur_trans->in_commit = 0;
f9295749 64 cur_trans->blocked = 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
d352ac68 84/*
d397712b
CM
85 * this does all the record keeping required to make sure that a reference
86 * counted root is properly recorded in a given transaction. This is required
87 * to make sure the old root from before we joined the transaction is deleted
88 * when the transaction commits
d352ac68 89 */
e02119d5 90noinline int btrfs_record_root_in_trans(struct btrfs_root *root)
6702ed49 91{
f321e491 92 struct btrfs_dirty_root *dirty;
6702ed49
CM
93 u64 running_trans_id = root->fs_info->running_transaction->transid;
94 if (root->ref_cows && root->last_trans < running_trans_id) {
95 WARN_ON(root == root->fs_info->extent_root);
96 if (root->root_item.refs != 0) {
97 radix_tree_tag_set(&root->fs_info->fs_roots_radix,
98 (unsigned long)root->root_key.objectid,
99 BTRFS_ROOT_TRANS_TAG);
31153d81
YZ
100
101 dirty = kmalloc(sizeof(*dirty), GFP_NOFS);
102 BUG_ON(!dirty);
103 dirty->root = kmalloc(sizeof(*dirty->root), GFP_NOFS);
104 BUG_ON(!dirty->root);
31153d81
YZ
105 dirty->latest_root = root;
106 INIT_LIST_HEAD(&dirty->list);
31153d81 107
925baedd 108 root->commit_root = btrfs_root_node(root);
31153d81
YZ
109
110 memcpy(dirty->root, root, sizeof(*root));
111 spin_lock_init(&dirty->root->node_lock);
bcc63abb 112 spin_lock_init(&dirty->root->list_lock);
31153d81 113 mutex_init(&dirty->root->objectid_mutex);
5b21f2ed 114 mutex_init(&dirty->root->log_mutex);
bcc63abb 115 INIT_LIST_HEAD(&dirty->root->dead_list);
31153d81
YZ
116 dirty->root->node = root->commit_root;
117 dirty->root->commit_root = NULL;
bcc63abb
Y
118
119 spin_lock(&root->list_lock);
120 list_add(&dirty->root->dead_list, &root->dead_list);
121 spin_unlock(&root->list_lock);
122
123 root->dirty_root = dirty;
6702ed49
CM
124 } else {
125 WARN_ON(1);
126 }
127 root->last_trans = running_trans_id;
128 }
129 return 0;
130}
131
d352ac68
CM
132/* wait for commit against the current transaction to become unblocked
133 * when this is done, it is safe to start a new transaction, but the current
134 * transaction might not be fully on disk.
135 */
37d1aeee 136static void wait_current_trans(struct btrfs_root *root)
79154b1b 137{
f9295749 138 struct btrfs_transaction *cur_trans;
79154b1b 139
f9295749 140 cur_trans = root->fs_info->running_transaction;
37d1aeee 141 if (cur_trans && cur_trans->blocked) {
f9295749
CM
142 DEFINE_WAIT(wait);
143 cur_trans->use_count++;
d397712b 144 while (1) {
f9295749
CM
145 prepare_to_wait(&root->fs_info->transaction_wait, &wait,
146 TASK_UNINTERRUPTIBLE);
147 if (cur_trans->blocked) {
148 mutex_unlock(&root->fs_info->trans_mutex);
149 schedule();
150 mutex_lock(&root->fs_info->trans_mutex);
151 finish_wait(&root->fs_info->transaction_wait,
152 &wait);
153 } else {
154 finish_wait(&root->fs_info->transaction_wait,
155 &wait);
156 break;
157 }
158 }
159 put_transaction(cur_trans);
160 }
37d1aeee
CM
161}
162
e02119d5 163static struct btrfs_trans_handle *start_transaction(struct btrfs_root *root,
9ca9ee09 164 int num_blocks, int wait)
37d1aeee
CM
165{
166 struct btrfs_trans_handle *h =
167 kmem_cache_alloc(btrfs_trans_handle_cachep, GFP_NOFS);
168 int ret;
169
170 mutex_lock(&root->fs_info->trans_mutex);
4bef0848
CM
171 if (!root->fs_info->log_root_recovering &&
172 ((wait == 1 && !root->fs_info->open_ioctl_trans) || wait == 2))
37d1aeee 173 wait_current_trans(root);
79154b1b
CM
174 ret = join_transaction(root);
175 BUG_ON(ret);
0f7d52f4 176
e02119d5 177 btrfs_record_root_in_trans(root);
6702ed49 178 h->transid = root->fs_info->running_transaction->transid;
79154b1b
CM
179 h->transaction = root->fs_info->running_transaction;
180 h->blocks_reserved = num_blocks;
181 h->blocks_used = 0;
d2fb3437 182 h->block_group = 0;
26b8003f
CM
183 h->alloc_exclude_nr = 0;
184 h->alloc_exclude_start = 0;
79154b1b
CM
185 root->fs_info->running_transaction->use_count++;
186 mutex_unlock(&root->fs_info->trans_mutex);
187 return h;
188}
189
f9295749
CM
190struct btrfs_trans_handle *btrfs_start_transaction(struct btrfs_root *root,
191 int num_blocks)
192{
9ca9ee09 193 return start_transaction(root, num_blocks, 1);
f9295749
CM
194}
195struct btrfs_trans_handle *btrfs_join_transaction(struct btrfs_root *root,
196 int num_blocks)
197{
9ca9ee09 198 return start_transaction(root, num_blocks, 0);
f9295749
CM
199}
200
9ca9ee09
SW
201struct btrfs_trans_handle *btrfs_start_ioctl_transaction(struct btrfs_root *r,
202 int num_blocks)
203{
204 return start_transaction(r, num_blocks, 2);
205}
206
d352ac68 207/* wait for a transaction commit to be fully complete */
89ce8a63
CM
208static noinline int wait_for_commit(struct btrfs_root *root,
209 struct btrfs_transaction *commit)
210{
211 DEFINE_WAIT(wait);
212 mutex_lock(&root->fs_info->trans_mutex);
d397712b 213 while (!commit->commit_done) {
89ce8a63
CM
214 prepare_to_wait(&commit->commit_wait, &wait,
215 TASK_UNINTERRUPTIBLE);
216 if (commit->commit_done)
217 break;
218 mutex_unlock(&root->fs_info->trans_mutex);
219 schedule();
220 mutex_lock(&root->fs_info->trans_mutex);
221 }
222 mutex_unlock(&root->fs_info->trans_mutex);
223 finish_wait(&commit->commit_wait, &wait);
224 return 0;
225}
226
d352ac68 227/*
d397712b
CM
228 * rate limit against the drop_snapshot code. This helps to slow down new
229 * operations if the drop_snapshot code isn't able to keep up.
d352ac68 230 */
37d1aeee 231static void throttle_on_drops(struct btrfs_root *root)
ab78c84d
CM
232{
233 struct btrfs_fs_info *info = root->fs_info;
2dd3e67b 234 int harder_count = 0;
ab78c84d 235
2dd3e67b 236harder:
ab78c84d
CM
237 if (atomic_read(&info->throttles)) {
238 DEFINE_WAIT(wait);
239 int thr;
ab78c84d
CM
240 thr = atomic_read(&info->throttle_gen);
241
242 do {
243 prepare_to_wait(&info->transaction_throttle,
244 &wait, TASK_UNINTERRUPTIBLE);
245 if (!atomic_read(&info->throttles)) {
246 finish_wait(&info->transaction_throttle, &wait);
247 break;
248 }
249 schedule();
250 finish_wait(&info->transaction_throttle, &wait);
251 } while (thr == atomic_read(&info->throttle_gen));
2dd3e67b
CM
252 harder_count++;
253
254 if (root->fs_info->total_ref_cache_size > 1 * 1024 * 1024 &&
255 harder_count < 2)
256 goto harder;
257
258 if (root->fs_info->total_ref_cache_size > 5 * 1024 * 1024 &&
259 harder_count < 10)
260 goto harder;
261
262 if (root->fs_info->total_ref_cache_size > 10 * 1024 * 1024 &&
263 harder_count < 20)
264 goto harder;
ab78c84d
CM
265 }
266}
267
37d1aeee
CM
268void btrfs_throttle(struct btrfs_root *root)
269{
270 mutex_lock(&root->fs_info->trans_mutex);
9ca9ee09
SW
271 if (!root->fs_info->open_ioctl_trans)
272 wait_current_trans(root);
37d1aeee
CM
273 mutex_unlock(&root->fs_info->trans_mutex);
274
275 throttle_on_drops(root);
276}
277
89ce8a63
CM
278static int __btrfs_end_transaction(struct btrfs_trans_handle *trans,
279 struct btrfs_root *root, int throttle)
79154b1b
CM
280{
281 struct btrfs_transaction *cur_trans;
ab78c84d 282 struct btrfs_fs_info *info = root->fs_info;
d6e4a428 283
ab78c84d
CM
284 mutex_lock(&info->trans_mutex);
285 cur_trans = info->running_transaction;
ccd467d6 286 WARN_ON(cur_trans != trans->transaction);
d5719762 287 WARN_ON(cur_trans->num_writers < 1);
ccd467d6 288 cur_trans->num_writers--;
89ce8a63 289
79154b1b
CM
290 if (waitqueue_active(&cur_trans->writer_wait))
291 wake_up(&cur_trans->writer_wait);
79154b1b 292 put_transaction(cur_trans);
ab78c84d 293 mutex_unlock(&info->trans_mutex);
d6025579 294 memset(trans, 0, sizeof(*trans));
2c90e5d6 295 kmem_cache_free(btrfs_trans_handle_cachep, trans);
ab78c84d
CM
296
297 if (throttle)
37d1aeee 298 throttle_on_drops(root);
ab78c84d 299
79154b1b
CM
300 return 0;
301}
302
89ce8a63
CM
303int btrfs_end_transaction(struct btrfs_trans_handle *trans,
304 struct btrfs_root *root)
305{
306 return __btrfs_end_transaction(trans, root, 0);
307}
308
309int btrfs_end_transaction_throttle(struct btrfs_trans_handle *trans,
310 struct btrfs_root *root)
311{
312 return __btrfs_end_transaction(trans, root, 1);
313}
314
d352ac68
CM
315/*
316 * when btree blocks are allocated, they have some corresponding bits set for
317 * them in one of two extent_io trees. This is used to make sure all of
318 * those extents are on disk for transaction or log commit
319 */
d0c803c4
CM
320int btrfs_write_and_wait_marked_extents(struct btrfs_root *root,
321 struct extent_io_tree *dirty_pages)
79154b1b 322{
7c4452b9 323 int ret;
777e6bd7 324 int err = 0;
7c4452b9
CM
325 int werr = 0;
326 struct page *page;
7c4452b9 327 struct inode *btree_inode = root->fs_info->btree_inode;
777e6bd7 328 u64 start = 0;
5f39d397
CM
329 u64 end;
330 unsigned long index;
7c4452b9 331
d397712b 332 while (1) {
777e6bd7 333 ret = find_first_extent_bit(dirty_pages, start, &start, &end,
5f39d397
CM
334 EXTENT_DIRTY);
335 if (ret)
7c4452b9 336 break;
d397712b 337 while (start <= end) {
777e6bd7
CM
338 cond_resched();
339
5f39d397 340 index = start >> PAGE_CACHE_SHIFT;
35ebb934 341 start = (u64)(index + 1) << PAGE_CACHE_SHIFT;
4bef0848 342 page = find_get_page(btree_inode->i_mapping, index);
7c4452b9
CM
343 if (!page)
344 continue;
4bef0848
CM
345
346 btree_lock_page_hook(page);
347 if (!page->mapping) {
348 unlock_page(page);
349 page_cache_release(page);
350 continue;
351 }
352
6702ed49
CM
353 if (PageWriteback(page)) {
354 if (PageDirty(page))
355 wait_on_page_writeback(page);
356 else {
357 unlock_page(page);
358 page_cache_release(page);
359 continue;
360 }
361 }
7c4452b9
CM
362 err = write_one_page(page, 0);
363 if (err)
364 werr = err;
365 page_cache_release(page);
366 }
367 }
d397712b 368 while (1) {
777e6bd7
CM
369 ret = find_first_extent_bit(dirty_pages, 0, &start, &end,
370 EXTENT_DIRTY);
371 if (ret)
372 break;
373
374 clear_extent_dirty(dirty_pages, start, end, GFP_NOFS);
d397712b 375 while (start <= end) {
777e6bd7
CM
376 index = start >> PAGE_CACHE_SHIFT;
377 start = (u64)(index + 1) << PAGE_CACHE_SHIFT;
378 page = find_get_page(btree_inode->i_mapping, index);
379 if (!page)
380 continue;
381 if (PageDirty(page)) {
4bef0848
CM
382 btree_lock_page_hook(page);
383 wait_on_page_writeback(page);
777e6bd7
CM
384 err = write_one_page(page, 0);
385 if (err)
386 werr = err;
387 }
105d931d 388 wait_on_page_writeback(page);
777e6bd7
CM
389 page_cache_release(page);
390 cond_resched();
391 }
392 }
7c4452b9
CM
393 if (err)
394 werr = err;
395 return werr;
79154b1b
CM
396}
397
d0c803c4
CM
398int btrfs_write_and_wait_transaction(struct btrfs_trans_handle *trans,
399 struct btrfs_root *root)
400{
401 if (!trans || !trans->transaction) {
402 struct inode *btree_inode;
403 btree_inode = root->fs_info->btree_inode;
404 return filemap_write_and_wait(btree_inode->i_mapping);
405 }
406 return btrfs_write_and_wait_marked_extents(root,
407 &trans->transaction->dirty_pages);
408}
409
d352ac68
CM
410/*
411 * this is used to update the root pointer in the tree of tree roots.
412 *
413 * But, in the case of the extent allocation tree, updating the root
414 * pointer may allocate blocks which may change the root of the extent
415 * allocation tree.
416 *
417 * So, this loops and repeats and makes sure the cowonly root didn't
418 * change while the root pointer was being updated in the metadata.
419 */
0b86a832
CM
420static int update_cowonly_root(struct btrfs_trans_handle *trans,
421 struct btrfs_root *root)
79154b1b
CM
422{
423 int ret;
0b86a832
CM
424 u64 old_root_bytenr;
425 struct btrfs_root *tree_root = root->fs_info->tree_root;
79154b1b 426
87ef2bb4 427 btrfs_extent_post_op(trans, root);
0b86a832 428 btrfs_write_dirty_block_groups(trans, root);
87ef2bb4
CM
429 btrfs_extent_post_op(trans, root);
430
d397712b 431 while (1) {
0b86a832
CM
432 old_root_bytenr = btrfs_root_bytenr(&root->root_item);
433 if (old_root_bytenr == root->node->start)
79154b1b 434 break;
0b86a832
CM
435 btrfs_set_root_bytenr(&root->root_item,
436 root->node->start);
437 btrfs_set_root_level(&root->root_item,
438 btrfs_header_level(root->node));
84234f3a 439 btrfs_set_root_generation(&root->root_item, trans->transid);
87ef2bb4
CM
440
441 btrfs_extent_post_op(trans, root);
442
79154b1b 443 ret = btrfs_update_root(trans, tree_root,
0b86a832
CM
444 &root->root_key,
445 &root->root_item);
79154b1b 446 BUG_ON(ret);
0b86a832 447 btrfs_write_dirty_block_groups(trans, root);
87ef2bb4 448 btrfs_extent_post_op(trans, root);
0b86a832
CM
449 }
450 return 0;
451}
452
d352ac68
CM
453/*
454 * update all the cowonly tree roots on disk
455 */
0b86a832
CM
456int btrfs_commit_tree_roots(struct btrfs_trans_handle *trans,
457 struct btrfs_root *root)
458{
459 struct btrfs_fs_info *fs_info = root->fs_info;
460 struct list_head *next;
84234f3a
YZ
461 struct extent_buffer *eb;
462
87ef2bb4
CM
463 btrfs_extent_post_op(trans, fs_info->tree_root);
464
84234f3a
YZ
465 eb = btrfs_lock_root_node(fs_info->tree_root);
466 btrfs_cow_block(trans, fs_info->tree_root, eb, NULL, 0, &eb, 0);
467 btrfs_tree_unlock(eb);
468 free_extent_buffer(eb);
0b86a832 469
87ef2bb4
CM
470 btrfs_extent_post_op(trans, fs_info->tree_root);
471
d397712b 472 while (!list_empty(&fs_info->dirty_cowonly_roots)) {
0b86a832
CM
473 next = fs_info->dirty_cowonly_roots.next;
474 list_del_init(next);
475 root = list_entry(next, struct btrfs_root, dirty_list);
87ef2bb4 476
0b86a832 477 update_cowonly_root(trans, root);
79154b1b
CM
478 }
479 return 0;
480}
481
d352ac68
CM
482/*
483 * dead roots are old snapshots that need to be deleted. This allocates
484 * a dirty root struct and adds it into the list of dead roots that need to
485 * be deleted
486 */
b48652c1 487int btrfs_add_dead_root(struct btrfs_root *root, struct btrfs_root *latest)
5eda7b5e 488{
f321e491 489 struct btrfs_dirty_root *dirty;
5eda7b5e
CM
490
491 dirty = kmalloc(sizeof(*dirty), GFP_NOFS);
492 if (!dirty)
493 return -ENOMEM;
5eda7b5e 494 dirty->root = root;
5ce14bbc 495 dirty->latest_root = latest;
b48652c1
YZ
496
497 mutex_lock(&root->fs_info->trans_mutex);
498 list_add(&dirty->list, &latest->fs_info->dead_roots);
499 mutex_unlock(&root->fs_info->trans_mutex);
5eda7b5e
CM
500 return 0;
501}
502
d352ac68
CM
503/*
504 * at transaction commit time we need to schedule the old roots for
505 * deletion via btrfs_drop_snapshot. This runs through all the
506 * reference counted roots that were modified in the current
507 * transaction and puts them into the drop list
508 */
80b6794d
CM
509static noinline int add_dirty_roots(struct btrfs_trans_handle *trans,
510 struct radix_tree_root *radix,
511 struct list_head *list)
0f7d52f4 512{
f321e491 513 struct btrfs_dirty_root *dirty;
0f7d52f4
CM
514 struct btrfs_root *gang[8];
515 struct btrfs_root *root;
516 int i;
517 int ret;
54aa1f4d 518 int err = 0;
5eda7b5e 519 u32 refs;
54aa1f4d 520
d397712b 521 while (1) {
0f7d52f4
CM
522 ret = radix_tree_gang_lookup_tag(radix, (void **)gang, 0,
523 ARRAY_SIZE(gang),
524 BTRFS_ROOT_TRANS_TAG);
525 if (ret == 0)
526 break;
527 for (i = 0; i < ret; i++) {
528 root = gang[i];
2619ba1f
CM
529 radix_tree_tag_clear(radix,
530 (unsigned long)root->root_key.objectid,
531 BTRFS_ROOT_TRANS_TAG);
31153d81
YZ
532
533 BUG_ON(!root->ref_tree);
017e5369 534 dirty = root->dirty_root;
31153d81 535
e02119d5 536 btrfs_free_log(trans, root);
f82d02d9 537 btrfs_free_reloc_root(trans, root);
e02119d5 538
0f7d52f4 539 if (root->commit_root == root->node) {
db94535d
CM
540 WARN_ON(root->node->start !=
541 btrfs_root_bytenr(&root->root_item));
31153d81 542
5f39d397 543 free_extent_buffer(root->commit_root);
0f7d52f4 544 root->commit_root = NULL;
7ea394f1 545 root->dirty_root = NULL;
bcc63abb
Y
546
547 spin_lock(&root->list_lock);
548 list_del_init(&dirty->root->dead_list);
549 spin_unlock(&root->list_lock);
550
31153d81
YZ
551 kfree(dirty->root);
552 kfree(dirty);
58176a96
JB
553
554 /* make sure to update the root on disk
555 * so we get any updates to the block used
556 * counts
557 */
558 err = btrfs_update_root(trans,
559 root->fs_info->tree_root,
560 &root->root_key,
561 &root->root_item);
0f7d52f4
CM
562 continue;
563 }
9f3a7427
CM
564
565 memset(&root->root_item.drop_progress, 0,
566 sizeof(struct btrfs_disk_key));
567 root->root_item.drop_level = 0;
0f7d52f4 568 root->commit_root = NULL;
7ea394f1 569 root->dirty_root = NULL;
0f7d52f4 570 root->root_key.offset = root->fs_info->generation;
db94535d
CM
571 btrfs_set_root_bytenr(&root->root_item,
572 root->node->start);
573 btrfs_set_root_level(&root->root_item,
574 btrfs_header_level(root->node));
84234f3a
YZ
575 btrfs_set_root_generation(&root->root_item,
576 root->root_key.offset);
577
0f7d52f4
CM
578 err = btrfs_insert_root(trans, root->fs_info->tree_root,
579 &root->root_key,
580 &root->root_item);
54aa1f4d
CM
581 if (err)
582 break;
9f3a7427
CM
583
584 refs = btrfs_root_refs(&dirty->root->root_item);
585 btrfs_set_root_refs(&dirty->root->root_item, refs - 1);
5eda7b5e 586 err = btrfs_update_root(trans, root->fs_info->tree_root,
9f3a7427
CM
587 &dirty->root->root_key,
588 &dirty->root->root_item);
5eda7b5e
CM
589
590 BUG_ON(err);
9f3a7427 591 if (refs == 1) {
5eda7b5e 592 list_add(&dirty->list, list);
9f3a7427
CM
593 } else {
594 WARN_ON(1);
31153d81 595 free_extent_buffer(dirty->root->node);
9f3a7427 596 kfree(dirty->root);
5eda7b5e 597 kfree(dirty);
9f3a7427 598 }
0f7d52f4
CM
599 }
600 }
54aa1f4d 601 return err;
0f7d52f4
CM
602}
603
d352ac68
CM
604/*
605 * defrag a given btree. If cacheonly == 1, this won't read from the disk,
606 * otherwise every leaf in the btree is read and defragged.
607 */
e9d0b13b
CM
608int btrfs_defrag_root(struct btrfs_root *root, int cacheonly)
609{
610 struct btrfs_fs_info *info = root->fs_info;
611 int ret;
612 struct btrfs_trans_handle *trans;
d3c2fdcf 613 unsigned long nr;
e9d0b13b 614
a2135011 615 smp_mb();
e9d0b13b
CM
616 if (root->defrag_running)
617 return 0;
e9d0b13b 618 trans = btrfs_start_transaction(root, 1);
6b80053d 619 while (1) {
e9d0b13b
CM
620 root->defrag_running = 1;
621 ret = btrfs_defrag_leaves(trans, root, cacheonly);
d3c2fdcf 622 nr = trans->blocks_used;
e9d0b13b 623 btrfs_end_transaction(trans, root);
d3c2fdcf 624 btrfs_btree_balance_dirty(info->tree_root, nr);
e9d0b13b
CM
625 cond_resched();
626
e9d0b13b 627 trans = btrfs_start_transaction(root, 1);
3f157a2f 628 if (root->fs_info->closing || ret != -EAGAIN)
e9d0b13b
CM
629 break;
630 }
631 root->defrag_running = 0;
a2135011 632 smp_mb();
e9d0b13b
CM
633 btrfs_end_transaction(trans, root);
634 return 0;
635}
636
d352ac68
CM
637/*
638 * Given a list of roots that need to be deleted, call btrfs_drop_snapshot on
639 * all of them
640 */
80b6794d
CM
641static noinline int drop_dirty_roots(struct btrfs_root *tree_root,
642 struct list_head *list)
0f7d52f4 643{
f321e491 644 struct btrfs_dirty_root *dirty;
0f7d52f4 645 struct btrfs_trans_handle *trans;
d3c2fdcf 646 unsigned long nr;
db94535d
CM
647 u64 num_bytes;
648 u64 bytes_used;
bcc63abb 649 u64 max_useless;
54aa1f4d 650 int ret = 0;
9f3a7427
CM
651 int err;
652
d397712b 653 while (!list_empty(list)) {
58176a96
JB
654 struct btrfs_root *root;
655
f321e491 656 dirty = list_entry(list->prev, struct btrfs_dirty_root, list);
0f7d52f4 657 list_del_init(&dirty->list);
5eda7b5e 658
db94535d 659 num_bytes = btrfs_root_used(&dirty->root->root_item);
58176a96 660 root = dirty->latest_root;
a2135011 661 atomic_inc(&root->fs_info->throttles);
58176a96 662
d397712b 663 while (1) {
9f3a7427 664 trans = btrfs_start_transaction(tree_root, 1);
5b21f2ed 665 mutex_lock(&root->fs_info->drop_mutex);
9f3a7427 666 ret = btrfs_drop_snapshot(trans, dirty->root);
d397712b 667 if (ret != -EAGAIN)
9f3a7427 668 break;
5b21f2ed 669 mutex_unlock(&root->fs_info->drop_mutex);
58176a96 670
9f3a7427
CM
671 err = btrfs_update_root(trans,
672 tree_root,
673 &dirty->root->root_key,
674 &dirty->root->root_item);
675 if (err)
676 ret = err;
d3c2fdcf 677 nr = trans->blocks_used;
017e5369 678 ret = btrfs_end_transaction(trans, tree_root);
9f3a7427 679 BUG_ON(ret);
a2135011 680
d3c2fdcf 681 btrfs_btree_balance_dirty(tree_root, nr);
4dc11904 682 cond_resched();
9f3a7427 683 }
0f7d52f4 684 BUG_ON(ret);
a2135011 685 atomic_dec(&root->fs_info->throttles);
017e5369 686 wake_up(&root->fs_info->transaction_throttle);
58176a96 687
db94535d
CM
688 num_bytes -= btrfs_root_used(&dirty->root->root_item);
689 bytes_used = btrfs_root_used(&root->root_item);
690 if (num_bytes) {
e02119d5 691 btrfs_record_root_in_trans(root);
5f39d397 692 btrfs_set_root_used(&root->root_item,
db94535d 693 bytes_used - num_bytes);
58176a96 694 }
a2135011 695
9f3a7427 696 ret = btrfs_del_root(trans, tree_root, &dirty->root->root_key);
58176a96
JB
697 if (ret) {
698 BUG();
54aa1f4d 699 break;
58176a96 700 }
a2135011
CM
701 mutex_unlock(&root->fs_info->drop_mutex);
702
bcc63abb
Y
703 spin_lock(&root->list_lock);
704 list_del_init(&dirty->root->dead_list);
705 if (!list_empty(&root->dead_list)) {
706 struct btrfs_root *oldest;
707 oldest = list_entry(root->dead_list.prev,
708 struct btrfs_root, dead_list);
709 max_useless = oldest->root_key.offset - 1;
710 } else {
711 max_useless = root->root_key.offset - 1;
712 }
713 spin_unlock(&root->list_lock);
714
d3c2fdcf 715 nr = trans->blocks_used;
0f7d52f4
CM
716 ret = btrfs_end_transaction(trans, tree_root);
717 BUG_ON(ret);
5eda7b5e 718
e4657689 719 ret = btrfs_remove_leaf_refs(root, max_useless, 0);
bcc63abb
Y
720 BUG_ON(ret);
721
f510cfec 722 free_extent_buffer(dirty->root->node);
9f3a7427 723 kfree(dirty->root);
0f7d52f4 724 kfree(dirty);
d3c2fdcf
CM
725
726 btrfs_btree_balance_dirty(tree_root, nr);
4dc11904 727 cond_resched();
0f7d52f4 728 }
54aa1f4d 729 return ret;
0f7d52f4
CM
730}
731
d352ac68
CM
732/*
733 * new snapshots need to be created at a very specific time in the
734 * transaction commit. This does the actual creation
735 */
80b6794d 736static noinline int create_pending_snapshot(struct btrfs_trans_handle *trans,
3063d29f
CM
737 struct btrfs_fs_info *fs_info,
738 struct btrfs_pending_snapshot *pending)
739{
740 struct btrfs_key key;
80b6794d 741 struct btrfs_root_item *new_root_item;
3063d29f
CM
742 struct btrfs_root *tree_root = fs_info->tree_root;
743 struct btrfs_root *root = pending->root;
744 struct extent_buffer *tmp;
925baedd 745 struct extent_buffer *old;
3063d29f
CM
746 int ret;
747 u64 objectid;
748
80b6794d
CM
749 new_root_item = kmalloc(sizeof(*new_root_item), GFP_NOFS);
750 if (!new_root_item) {
751 ret = -ENOMEM;
752 goto fail;
753 }
3063d29f
CM
754 ret = btrfs_find_free_objectid(trans, tree_root, 0, &objectid);
755 if (ret)
756 goto fail;
757
80ff3856
YZ
758 btrfs_record_root_in_trans(root);
759 btrfs_set_root_last_snapshot(&root->root_item, trans->transid);
80b6794d 760 memcpy(new_root_item, &root->root_item, sizeof(*new_root_item));
3063d29f
CM
761
762 key.objectid = objectid;
5b21f2ed 763 key.offset = trans->transid;
3063d29f
CM
764 btrfs_set_key_type(&key, BTRFS_ROOT_ITEM_KEY);
765
925baedd 766 old = btrfs_lock_root_node(root);
65b51a00 767 btrfs_cow_block(trans, root, old, NULL, 0, &old, 0);
3063d29f 768
925baedd
CM
769 btrfs_copy_root(trans, root, old, &tmp, objectid);
770 btrfs_tree_unlock(old);
771 free_extent_buffer(old);
3063d29f 772
80b6794d
CM
773 btrfs_set_root_bytenr(new_root_item, tmp->start);
774 btrfs_set_root_level(new_root_item, btrfs_header_level(tmp));
84234f3a 775 btrfs_set_root_generation(new_root_item, trans->transid);
3063d29f 776 ret = btrfs_insert_root(trans, root->fs_info->tree_root, &key,
80b6794d 777 new_root_item);
925baedd 778 btrfs_tree_unlock(tmp);
3063d29f
CM
779 free_extent_buffer(tmp);
780 if (ret)
781 goto fail;
782
3de4586c
CM
783 key.offset = (u64)-1;
784 memcpy(&pending->root_key, &key, sizeof(key));
785fail:
786 kfree(new_root_item);
787 return ret;
788}
789
790static noinline int finish_pending_snapshot(struct btrfs_fs_info *fs_info,
791 struct btrfs_pending_snapshot *pending)
792{
793 int ret;
794 int namelen;
795 u64 index = 0;
796 struct btrfs_trans_handle *trans;
797 struct inode *parent_inode;
798 struct inode *inode;
0660b5af 799 struct btrfs_root *parent_root;
3de4586c 800
3394e160 801 parent_inode = pending->dentry->d_parent->d_inode;
0660b5af 802 parent_root = BTRFS_I(parent_inode)->root;
180591bc 803 trans = btrfs_join_transaction(parent_root, 1);
3de4586c 804
3063d29f
CM
805 /*
806 * insert the directory item
807 */
3b96362c 808 namelen = strlen(pending->name);
3de4586c 809 ret = btrfs_set_inode_index(parent_inode, &index);
0660b5af 810 ret = btrfs_insert_dir_item(trans, parent_root,
3de4586c
CM
811 pending->name, namelen,
812 parent_inode->i_ino,
813 &pending->root_key, BTRFS_FT_DIR, index);
3063d29f
CM
814
815 if (ret)
816 goto fail;
0660b5af 817
52c26179
YZ
818 btrfs_i_size_write(parent_inode, parent_inode->i_size + namelen * 2);
819 ret = btrfs_update_inode(trans, parent_root, parent_inode);
820 BUG_ON(ret);
821
0660b5af
CM
822 /* add the backref first */
823 ret = btrfs_add_root_ref(trans, parent_root->fs_info->tree_root,
824 pending->root_key.objectid,
825 BTRFS_ROOT_BACKREF_KEY,
826 parent_root->root_key.objectid,
827 parent_inode->i_ino, index, pending->name,
828 namelen);
829
830 BUG_ON(ret);
831
832 /* now add the forward ref */
833 ret = btrfs_add_root_ref(trans, parent_root->fs_info->tree_root,
834 parent_root->root_key.objectid,
835 BTRFS_ROOT_REF_KEY,
836 pending->root_key.objectid,
837 parent_inode->i_ino, index, pending->name,
838 namelen);
839
3de4586c
CM
840 inode = btrfs_lookup_dentry(parent_inode, pending->dentry);
841 d_instantiate(pending->dentry, inode);
3063d29f 842fail:
3de4586c 843 btrfs_end_transaction(trans, fs_info->fs_root);
3063d29f
CM
844 return ret;
845}
846
d352ac68
CM
847/*
848 * create all the snapshots we've scheduled for creation
849 */
80b6794d
CM
850static noinline int create_pending_snapshots(struct btrfs_trans_handle *trans,
851 struct btrfs_fs_info *fs_info)
3de4586c
CM
852{
853 struct btrfs_pending_snapshot *pending;
854 struct list_head *head = &trans->transaction->pending_snapshots;
3de4586c
CM
855 int ret;
856
c6e30871 857 list_for_each_entry(pending, head, list) {
3de4586c
CM
858 ret = create_pending_snapshot(trans, fs_info, pending);
859 BUG_ON(ret);
860 }
861 return 0;
862}
863
864static noinline int finish_pending_snapshots(struct btrfs_trans_handle *trans,
865 struct btrfs_fs_info *fs_info)
3063d29f
CM
866{
867 struct btrfs_pending_snapshot *pending;
868 struct list_head *head = &trans->transaction->pending_snapshots;
869 int ret;
870
d397712b 871 while (!list_empty(head)) {
3063d29f
CM
872 pending = list_entry(head->next,
873 struct btrfs_pending_snapshot, list);
3de4586c 874 ret = finish_pending_snapshot(fs_info, pending);
3063d29f
CM
875 BUG_ON(ret);
876 list_del(&pending->list);
877 kfree(pending->name);
878 kfree(pending);
879 }
dc17ff8f
CM
880 return 0;
881}
882
79154b1b
CM
883int btrfs_commit_transaction(struct btrfs_trans_handle *trans,
884 struct btrfs_root *root)
885{
15ee9bc7
JB
886 unsigned long joined = 0;
887 unsigned long timeout = 1;
79154b1b 888 struct btrfs_transaction *cur_trans;
8fd17795 889 struct btrfs_transaction *prev_trans = NULL;
0b86a832 890 struct btrfs_root *chunk_root = root->fs_info->chunk_root;
0f7d52f4 891 struct list_head dirty_fs_roots;
d1310b2e 892 struct extent_io_tree *pinned_copy;
79154b1b 893 DEFINE_WAIT(wait);
15ee9bc7 894 int ret;
79154b1b 895
0f7d52f4 896 INIT_LIST_HEAD(&dirty_fs_roots);
79154b1b
CM
897 mutex_lock(&root->fs_info->trans_mutex);
898 if (trans->transaction->in_commit) {
899 cur_trans = trans->transaction;
900 trans->transaction->use_count++;
ccd467d6 901 mutex_unlock(&root->fs_info->trans_mutex);
79154b1b 902 btrfs_end_transaction(trans, root);
ccd467d6 903
79154b1b
CM
904 ret = wait_for_commit(root, cur_trans);
905 BUG_ON(ret);
15ee9bc7
JB
906
907 mutex_lock(&root->fs_info->trans_mutex);
79154b1b 908 put_transaction(cur_trans);
15ee9bc7
JB
909 mutex_unlock(&root->fs_info->trans_mutex);
910
79154b1b
CM
911 return 0;
912 }
4313b399
CM
913
914 pinned_copy = kmalloc(sizeof(*pinned_copy), GFP_NOFS);
915 if (!pinned_copy)
916 return -ENOMEM;
917
d1310b2e 918 extent_io_tree_init(pinned_copy,
4313b399
CM
919 root->fs_info->btree_inode->i_mapping, GFP_NOFS);
920
2c90e5d6 921 trans->transaction->in_commit = 1;
f9295749 922 trans->transaction->blocked = 1;
ccd467d6
CM
923 cur_trans = trans->transaction;
924 if (cur_trans->list.prev != &root->fs_info->trans_list) {
925 prev_trans = list_entry(cur_trans->list.prev,
926 struct btrfs_transaction, list);
927 if (!prev_trans->commit_done) {
928 prev_trans->use_count++;
ccd467d6
CM
929 mutex_unlock(&root->fs_info->trans_mutex);
930
931 wait_for_commit(root, prev_trans);
ccd467d6 932
ccd467d6 933 mutex_lock(&root->fs_info->trans_mutex);
15ee9bc7 934 put_transaction(prev_trans);
ccd467d6
CM
935 }
936 }
15ee9bc7
JB
937
938 do {
7ea394f1 939 int snap_pending = 0;
15ee9bc7 940 joined = cur_trans->num_joined;
7ea394f1
YZ
941 if (!list_empty(&trans->transaction->pending_snapshots))
942 snap_pending = 1;
943
2c90e5d6 944 WARN_ON(cur_trans != trans->transaction);
15ee9bc7 945 prepare_to_wait(&cur_trans->writer_wait, &wait,
79154b1b 946 TASK_UNINTERRUPTIBLE);
15ee9bc7
JB
947
948 if (cur_trans->num_writers > 1)
949 timeout = MAX_SCHEDULE_TIMEOUT;
950 else
951 timeout = 1;
952
79154b1b 953 mutex_unlock(&root->fs_info->trans_mutex);
15ee9bc7 954
7ea394f1
YZ
955 if (snap_pending) {
956 ret = btrfs_wait_ordered_extents(root, 1);
957 BUG_ON(ret);
958 }
959
15ee9bc7
JB
960 schedule_timeout(timeout);
961
79154b1b 962 mutex_lock(&root->fs_info->trans_mutex);
15ee9bc7
JB
963 finish_wait(&cur_trans->writer_wait, &wait);
964 } while (cur_trans->num_writers > 1 ||
965 (cur_trans->num_joined != joined));
966
3063d29f
CM
967 ret = create_pending_snapshots(trans, root->fs_info);
968 BUG_ON(ret);
969
2c90e5d6 970 WARN_ON(cur_trans != trans->transaction);
dc17ff8f 971
e02119d5
CM
972 /* btrfs_commit_tree_roots is responsible for getting the
973 * various roots consistent with each other. Every pointer
974 * in the tree of tree roots has to point to the most up to date
975 * root for every subvolume and other tree. So, we have to keep
976 * the tree logging code from jumping in and changing any
977 * of the trees.
978 *
979 * At this point in the commit, there can't be any tree-log
980 * writers, but a little lower down we drop the trans mutex
981 * and let new people in. By holding the tree_log_mutex
982 * from now until after the super is written, we avoid races
983 * with the tree-log code.
984 */
985 mutex_lock(&root->fs_info->tree_log_mutex);
1a40e23b
ZY
986 /*
987 * keep tree reloc code from adding new reloc trees
988 */
989 mutex_lock(&root->fs_info->tree_reloc_mutex);
990
e02119d5 991
54aa1f4d
CM
992 ret = add_dirty_roots(trans, &root->fs_info->fs_roots_radix,
993 &dirty_fs_roots);
994 BUG_ON(ret);
995
e02119d5
CM
996 /* add_dirty_roots gets rid of all the tree log roots, it is now
997 * safe to free the root of tree log roots
998 */
999 btrfs_free_log_root_tree(trans, root->fs_info);
1000
79154b1b
CM
1001 ret = btrfs_commit_tree_roots(trans, root);
1002 BUG_ON(ret);
54aa1f4d 1003
78fae27e 1004 cur_trans = root->fs_info->running_transaction;
cee36a03 1005 spin_lock(&root->fs_info->new_trans_lock);
78fae27e 1006 root->fs_info->running_transaction = NULL;
cee36a03 1007 spin_unlock(&root->fs_info->new_trans_lock);
4b52dff6
CM
1008 btrfs_set_super_generation(&root->fs_info->super_copy,
1009 cur_trans->transid);
1010 btrfs_set_super_root(&root->fs_info->super_copy,
db94535d
CM
1011 root->fs_info->tree_root->node->start);
1012 btrfs_set_super_root_level(&root->fs_info->super_copy,
1013 btrfs_header_level(root->fs_info->tree_root->node));
5f39d397 1014
0b86a832
CM
1015 btrfs_set_super_chunk_root(&root->fs_info->super_copy,
1016 chunk_root->node->start);
1017 btrfs_set_super_chunk_root_level(&root->fs_info->super_copy,
1018 btrfs_header_level(chunk_root->node));
84234f3a
YZ
1019 btrfs_set_super_chunk_root_generation(&root->fs_info->super_copy,
1020 btrfs_header_generation(chunk_root->node));
e02119d5
CM
1021
1022 if (!root->fs_info->log_root_recovering) {
1023 btrfs_set_super_log_root(&root->fs_info->super_copy, 0);
1024 btrfs_set_super_log_root_level(&root->fs_info->super_copy, 0);
1025 }
1026
a061fc8d
CM
1027 memcpy(&root->fs_info->super_for_commit, &root->fs_info->super_copy,
1028 sizeof(root->fs_info->super_copy));
ccd467d6 1029
4313b399 1030 btrfs_copy_pinned(root, pinned_copy);
ccd467d6 1031
f9295749 1032 trans->transaction->blocked = 0;
e6dcd2dc 1033 wake_up(&root->fs_info->transaction_throttle);
f9295749 1034 wake_up(&root->fs_info->transaction_wait);
e6dcd2dc 1035
78fae27e 1036 mutex_unlock(&root->fs_info->trans_mutex);
79154b1b
CM
1037 ret = btrfs_write_and_wait_transaction(trans, root);
1038 BUG_ON(ret);
a512bbf8 1039 write_ctree_super(trans, root, 0);
4313b399 1040
e02119d5
CM
1041 /*
1042 * the super is written, we can safely allow the tree-loggers
1043 * to go about their business
1044 */
1045 mutex_unlock(&root->fs_info->tree_log_mutex);
1046
4313b399 1047 btrfs_finish_extent_commit(trans, root, pinned_copy);
4313b399
CM
1048 kfree(pinned_copy);
1049
1a40e23b
ZY
1050 btrfs_drop_dead_reloc_roots(root);
1051 mutex_unlock(&root->fs_info->tree_reloc_mutex);
1052
3de4586c
CM
1053 /* do the directory inserts of any pending snapshot creations */
1054 finish_pending_snapshots(trans, root->fs_info);
1055
1a40e23b
ZY
1056 mutex_lock(&root->fs_info->trans_mutex);
1057
2c90e5d6 1058 cur_trans->commit_done = 1;
15ee9bc7 1059 root->fs_info->last_trans_committed = cur_trans->transid;
2c90e5d6 1060 wake_up(&cur_trans->commit_wait);
3de4586c 1061
78fae27e 1062 put_transaction(cur_trans);
79154b1b 1063 put_transaction(cur_trans);
58176a96 1064
bcc63abb 1065 list_splice_init(&dirty_fs_roots, &root->fs_info->dead_roots);
facda1e7
CM
1066 if (root->fs_info->closing)
1067 list_splice_init(&root->fs_info->dead_roots, &dirty_fs_roots);
58176a96 1068
78fae27e 1069 mutex_unlock(&root->fs_info->trans_mutex);
3de4586c 1070
2c90e5d6 1071 kmem_cache_free(btrfs_trans_handle_cachep, trans);
79154b1b 1072
d397712b 1073 if (root->fs_info->closing)
facda1e7 1074 drop_dirty_roots(root->fs_info->tree_root, &dirty_fs_roots);
79154b1b
CM
1075 return ret;
1076}
1077
d352ac68
CM
1078/*
1079 * interface function to delete all the snapshots we have scheduled for deletion
1080 */
e9d0b13b
CM
1081int btrfs_clean_old_snapshots(struct btrfs_root *root)
1082{
1083 struct list_head dirty_roots;
1084 INIT_LIST_HEAD(&dirty_roots);
a74a4b97 1085again:
e9d0b13b
CM
1086 mutex_lock(&root->fs_info->trans_mutex);
1087 list_splice_init(&root->fs_info->dead_roots, &dirty_roots);
1088 mutex_unlock(&root->fs_info->trans_mutex);
1089
1090 if (!list_empty(&dirty_roots)) {
1091 drop_dirty_roots(root, &dirty_roots);
a74a4b97 1092 goto again;
e9d0b13b
CM
1093 }
1094 return 0;
1095}