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