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