diff options
Diffstat (limited to 'fs/btrfs/transaction.c')
-rw-r--r-- | fs/btrfs/transaction.c | 283 |
1 files changed, 198 insertions, 85 deletions
diff --git a/fs/btrfs/transaction.c b/fs/btrfs/transaction.c index 27c26004e050..77db875b5116 100644 --- a/fs/btrfs/transaction.c +++ b/fs/btrfs/transaction.c | |||
@@ -53,7 +53,7 @@ static noinline void switch_commit_root(struct btrfs_root *root) | |||
53 | /* | 53 | /* |
54 | * either allocate a new transaction or hop into the existing one | 54 | * either allocate a new transaction or hop into the existing one |
55 | */ | 55 | */ |
56 | static noinline int join_transaction(struct btrfs_root *root, int nofail) | 56 | static noinline int join_transaction(struct btrfs_root *root, int type) |
57 | { | 57 | { |
58 | struct btrfs_transaction *cur_trans; | 58 | struct btrfs_transaction *cur_trans; |
59 | struct btrfs_fs_info *fs_info = root->fs_info; | 59 | struct btrfs_fs_info *fs_info = root->fs_info; |
@@ -67,7 +67,13 @@ loop: | |||
67 | } | 67 | } |
68 | 68 | ||
69 | if (fs_info->trans_no_join) { | 69 | if (fs_info->trans_no_join) { |
70 | if (!nofail) { | 70 | /* |
71 | * If we are JOIN_NOLOCK we're already committing a current | ||
72 | * transaction, we just need a handle to deal with something | ||
73 | * when committing the transaction, such as inode cache and | ||
74 | * space cache. It is a special case. | ||
75 | */ | ||
76 | if (type != TRANS_JOIN_NOLOCK) { | ||
71 | spin_unlock(&fs_info->trans_lock); | 77 | spin_unlock(&fs_info->trans_lock); |
72 | return -EBUSY; | 78 | return -EBUSY; |
73 | } | 79 | } |
@@ -87,6 +93,13 @@ loop: | |||
87 | } | 93 | } |
88 | spin_unlock(&fs_info->trans_lock); | 94 | spin_unlock(&fs_info->trans_lock); |
89 | 95 | ||
96 | /* | ||
97 | * If we are ATTACH, we just want to catch the current transaction, | ||
98 | * and commit it. If there is no transaction, just return ENOENT. | ||
99 | */ | ||
100 | if (type == TRANS_ATTACH) | ||
101 | return -ENOENT; | ||
102 | |||
90 | cur_trans = kmem_cache_alloc(btrfs_transaction_cachep, GFP_NOFS); | 103 | cur_trans = kmem_cache_alloc(btrfs_transaction_cachep, GFP_NOFS); |
91 | if (!cur_trans) | 104 | if (!cur_trans) |
92 | return -ENOMEM; | 105 | return -ENOMEM; |
@@ -267,13 +280,6 @@ static void wait_current_trans(struct btrfs_root *root) | |||
267 | } | 280 | } |
268 | } | 281 | } |
269 | 282 | ||
270 | enum btrfs_trans_type { | ||
271 | TRANS_START, | ||
272 | TRANS_JOIN, | ||
273 | TRANS_USERSPACE, | ||
274 | TRANS_JOIN_NOLOCK, | ||
275 | }; | ||
276 | |||
277 | static int may_wait_transaction(struct btrfs_root *root, int type) | 283 | static int may_wait_transaction(struct btrfs_root *root, int type) |
278 | { | 284 | { |
279 | if (root->fs_info->log_root_recovering) | 285 | if (root->fs_info->log_root_recovering) |
@@ -290,7 +296,8 @@ static int may_wait_transaction(struct btrfs_root *root, int type) | |||
290 | } | 296 | } |
291 | 297 | ||
292 | static struct btrfs_trans_handle *start_transaction(struct btrfs_root *root, | 298 | static struct btrfs_trans_handle *start_transaction(struct btrfs_root *root, |
293 | u64 num_items, int type) | 299 | u64 num_items, int type, |
300 | int noflush) | ||
294 | { | 301 | { |
295 | struct btrfs_trans_handle *h; | 302 | struct btrfs_trans_handle *h; |
296 | struct btrfs_transaction *cur_trans; | 303 | struct btrfs_transaction *cur_trans; |
@@ -324,9 +331,14 @@ static struct btrfs_trans_handle *start_transaction(struct btrfs_root *root, | |||
324 | } | 331 | } |
325 | 332 | ||
326 | num_bytes = btrfs_calc_trans_metadata_size(root, num_items); | 333 | num_bytes = btrfs_calc_trans_metadata_size(root, num_items); |
327 | ret = btrfs_block_rsv_add(root, | 334 | if (noflush) |
328 | &root->fs_info->trans_block_rsv, | 335 | ret = btrfs_block_rsv_add_noflush(root, |
329 | num_bytes); | 336 | &root->fs_info->trans_block_rsv, |
337 | num_bytes); | ||
338 | else | ||
339 | ret = btrfs_block_rsv_add(root, | ||
340 | &root->fs_info->trans_block_rsv, | ||
341 | num_bytes); | ||
330 | if (ret) | 342 | if (ret) |
331 | return ERR_PTR(ret); | 343 | return ERR_PTR(ret); |
332 | } | 344 | } |
@@ -335,19 +347,34 @@ again: | |||
335 | if (!h) | 347 | if (!h) |
336 | return ERR_PTR(-ENOMEM); | 348 | return ERR_PTR(-ENOMEM); |
337 | 349 | ||
338 | sb_start_intwrite(root->fs_info->sb); | 350 | /* |
351 | * If we are JOIN_NOLOCK we're already committing a transaction and | ||
352 | * waiting on this guy, so we don't need to do the sb_start_intwrite | ||
353 | * because we're already holding a ref. We need this because we could | ||
354 | * have raced in and did an fsync() on a file which can kick a commit | ||
355 | * and then we deadlock with somebody doing a freeze. | ||
356 | * | ||
357 | * If we are ATTACH, it means we just want to catch the current | ||
358 | * transaction and commit it, so we needn't do sb_start_intwrite(). | ||
359 | */ | ||
360 | if (type < TRANS_JOIN_NOLOCK) | ||
361 | sb_start_intwrite(root->fs_info->sb); | ||
339 | 362 | ||
340 | if (may_wait_transaction(root, type)) | 363 | if (may_wait_transaction(root, type)) |
341 | wait_current_trans(root); | 364 | wait_current_trans(root); |
342 | 365 | ||
343 | do { | 366 | do { |
344 | ret = join_transaction(root, type == TRANS_JOIN_NOLOCK); | 367 | ret = join_transaction(root, type); |
345 | if (ret == -EBUSY) | 368 | if (ret == -EBUSY) |
346 | wait_current_trans(root); | 369 | wait_current_trans(root); |
347 | } while (ret == -EBUSY); | 370 | } while (ret == -EBUSY); |
348 | 371 | ||
349 | if (ret < 0) { | 372 | if (ret < 0) { |
350 | sb_end_intwrite(root->fs_info->sb); | 373 | /* We must get the transaction if we are JOIN_NOLOCK. */ |
374 | BUG_ON(type == TRANS_JOIN_NOLOCK); | ||
375 | |||
376 | if (type < TRANS_JOIN_NOLOCK) | ||
377 | sb_end_intwrite(root->fs_info->sb); | ||
351 | kmem_cache_free(btrfs_trans_handle_cachep, h); | 378 | kmem_cache_free(btrfs_trans_handle_cachep, h); |
352 | return ERR_PTR(ret); | 379 | return ERR_PTR(ret); |
353 | } | 380 | } |
@@ -367,7 +394,9 @@ again: | |||
367 | h->aborted = 0; | 394 | h->aborted = 0; |
368 | h->qgroup_reserved = qgroup_reserved; | 395 | h->qgroup_reserved = qgroup_reserved; |
369 | h->delayed_ref_elem.seq = 0; | 396 | h->delayed_ref_elem.seq = 0; |
397 | h->type = type; | ||
370 | INIT_LIST_HEAD(&h->qgroup_ref_list); | 398 | INIT_LIST_HEAD(&h->qgroup_ref_list); |
399 | INIT_LIST_HEAD(&h->new_bgs); | ||
371 | 400 | ||
372 | smp_mb(); | 401 | smp_mb(); |
373 | if (cur_trans->blocked && may_wait_transaction(root, type)) { | 402 | if (cur_trans->blocked && may_wait_transaction(root, type)) { |
@@ -393,21 +422,33 @@ got_it: | |||
393 | struct btrfs_trans_handle *btrfs_start_transaction(struct btrfs_root *root, | 422 | struct btrfs_trans_handle *btrfs_start_transaction(struct btrfs_root *root, |
394 | int num_items) | 423 | int num_items) |
395 | { | 424 | { |
396 | return start_transaction(root, num_items, TRANS_START); | 425 | return start_transaction(root, num_items, TRANS_START, 0); |
426 | } | ||
427 | |||
428 | struct btrfs_trans_handle *btrfs_start_transaction_noflush( | ||
429 | struct btrfs_root *root, int num_items) | ||
430 | { | ||
431 | return start_transaction(root, num_items, TRANS_START, 1); | ||
397 | } | 432 | } |
433 | |||
398 | struct btrfs_trans_handle *btrfs_join_transaction(struct btrfs_root *root) | 434 | struct btrfs_trans_handle *btrfs_join_transaction(struct btrfs_root *root) |
399 | { | 435 | { |
400 | return start_transaction(root, 0, TRANS_JOIN); | 436 | return start_transaction(root, 0, TRANS_JOIN, 0); |
401 | } | 437 | } |
402 | 438 | ||
403 | struct btrfs_trans_handle *btrfs_join_transaction_nolock(struct btrfs_root *root) | 439 | struct btrfs_trans_handle *btrfs_join_transaction_nolock(struct btrfs_root *root) |
404 | { | 440 | { |
405 | return start_transaction(root, 0, TRANS_JOIN_NOLOCK); | 441 | return start_transaction(root, 0, TRANS_JOIN_NOLOCK, 0); |
406 | } | 442 | } |
407 | 443 | ||
408 | struct btrfs_trans_handle *btrfs_start_ioctl_transaction(struct btrfs_root *root) | 444 | struct btrfs_trans_handle *btrfs_start_ioctl_transaction(struct btrfs_root *root) |
409 | { | 445 | { |
410 | return start_transaction(root, 0, TRANS_USERSPACE); | 446 | return start_transaction(root, 0, TRANS_USERSPACE, 0); |
447 | } | ||
448 | |||
449 | struct btrfs_trans_handle *btrfs_attach_transaction(struct btrfs_root *root) | ||
450 | { | ||
451 | return start_transaction(root, 0, TRANS_ATTACH, 0); | ||
411 | } | 452 | } |
412 | 453 | ||
413 | /* wait for a transaction commit to be fully complete */ | 454 | /* wait for a transaction commit to be fully complete */ |
@@ -506,11 +547,12 @@ int btrfs_should_end_transaction(struct btrfs_trans_handle *trans, | |||
506 | } | 547 | } |
507 | 548 | ||
508 | static int __btrfs_end_transaction(struct btrfs_trans_handle *trans, | 549 | static int __btrfs_end_transaction(struct btrfs_trans_handle *trans, |
509 | struct btrfs_root *root, int throttle, int lock) | 550 | struct btrfs_root *root, int throttle) |
510 | { | 551 | { |
511 | struct btrfs_transaction *cur_trans = trans->transaction; | 552 | struct btrfs_transaction *cur_trans = trans->transaction; |
512 | struct btrfs_fs_info *info = root->fs_info; | 553 | struct btrfs_fs_info *info = root->fs_info; |
513 | int count = 0; | 554 | int count = 0; |
555 | int lock = (trans->type != TRANS_JOIN_NOLOCK); | ||
514 | int err = 0; | 556 | int err = 0; |
515 | 557 | ||
516 | if (--trans->use_count) { | 558 | if (--trans->use_count) { |
@@ -536,6 +578,9 @@ static int __btrfs_end_transaction(struct btrfs_trans_handle *trans, | |||
536 | trans->qgroup_reserved = 0; | 578 | trans->qgroup_reserved = 0; |
537 | } | 579 | } |
538 | 580 | ||
581 | if (!list_empty(&trans->new_bgs)) | ||
582 | btrfs_create_pending_block_groups(trans, root); | ||
583 | |||
539 | while (count < 2) { | 584 | while (count < 2) { |
540 | unsigned long cur = trans->delayed_ref_updates; | 585 | unsigned long cur = trans->delayed_ref_updates; |
541 | trans->delayed_ref_updates = 0; | 586 | trans->delayed_ref_updates = 0; |
@@ -551,7 +596,8 @@ static int __btrfs_end_transaction(struct btrfs_trans_handle *trans, | |||
551 | btrfs_trans_release_metadata(trans, root); | 596 | btrfs_trans_release_metadata(trans, root); |
552 | trans->block_rsv = NULL; | 597 | trans->block_rsv = NULL; |
553 | 598 | ||
554 | sb_end_intwrite(root->fs_info->sb); | 599 | if (!list_empty(&trans->new_bgs)) |
600 | btrfs_create_pending_block_groups(trans, root); | ||
555 | 601 | ||
556 | if (lock && !atomic_read(&root->fs_info->open_ioctl_trans) && | 602 | if (lock && !atomic_read(&root->fs_info->open_ioctl_trans) && |
557 | should_end_transaction(trans, root)) { | 603 | should_end_transaction(trans, root)) { |
@@ -573,6 +619,9 @@ static int __btrfs_end_transaction(struct btrfs_trans_handle *trans, | |||
573 | } | 619 | } |
574 | } | 620 | } |
575 | 621 | ||
622 | if (trans->type < TRANS_JOIN_NOLOCK) | ||
623 | sb_end_intwrite(root->fs_info->sb); | ||
624 | |||
576 | WARN_ON(cur_trans != info->running_transaction); | 625 | WARN_ON(cur_trans != info->running_transaction); |
577 | WARN_ON(atomic_read(&cur_trans->num_writers) < 1); | 626 | WARN_ON(atomic_read(&cur_trans->num_writers) < 1); |
578 | atomic_dec(&cur_trans->num_writers); | 627 | atomic_dec(&cur_trans->num_writers); |
@@ -604,7 +653,7 @@ int btrfs_end_transaction(struct btrfs_trans_handle *trans, | |||
604 | { | 653 | { |
605 | int ret; | 654 | int ret; |
606 | 655 | ||
607 | ret = __btrfs_end_transaction(trans, root, 0, 1); | 656 | ret = __btrfs_end_transaction(trans, root, 0); |
608 | if (ret) | 657 | if (ret) |
609 | return ret; | 658 | return ret; |
610 | return 0; | 659 | return 0; |
@@ -615,18 +664,7 @@ int btrfs_end_transaction_throttle(struct btrfs_trans_handle *trans, | |||
615 | { | 664 | { |
616 | int ret; | 665 | int ret; |
617 | 666 | ||
618 | ret = __btrfs_end_transaction(trans, root, 1, 1); | 667 | ret = __btrfs_end_transaction(trans, root, 1); |
619 | if (ret) | ||
620 | return ret; | ||
621 | return 0; | ||
622 | } | ||
623 | |||
624 | int btrfs_end_transaction_nolock(struct btrfs_trans_handle *trans, | ||
625 | struct btrfs_root *root) | ||
626 | { | ||
627 | int ret; | ||
628 | |||
629 | ret = __btrfs_end_transaction(trans, root, 0, 0); | ||
630 | if (ret) | 668 | if (ret) |
631 | return ret; | 669 | return ret; |
632 | return 0; | 670 | return 0; |
@@ -635,7 +673,7 @@ int btrfs_end_transaction_nolock(struct btrfs_trans_handle *trans, | |||
635 | int btrfs_end_transaction_dmeta(struct btrfs_trans_handle *trans, | 673 | int btrfs_end_transaction_dmeta(struct btrfs_trans_handle *trans, |
636 | struct btrfs_root *root) | 674 | struct btrfs_root *root) |
637 | { | 675 | { |
638 | return __btrfs_end_transaction(trans, root, 1, 1); | 676 | return __btrfs_end_transaction(trans, root, 1); |
639 | } | 677 | } |
640 | 678 | ||
641 | /* | 679 | /* |
@@ -649,13 +687,15 @@ int btrfs_write_marked_extents(struct btrfs_root *root, | |||
649 | int err = 0; | 687 | int err = 0; |
650 | int werr = 0; | 688 | int werr = 0; |
651 | struct address_space *mapping = root->fs_info->btree_inode->i_mapping; | 689 | struct address_space *mapping = root->fs_info->btree_inode->i_mapping; |
690 | struct extent_state *cached_state = NULL; | ||
652 | u64 start = 0; | 691 | u64 start = 0; |
653 | u64 end; | 692 | u64 end; |
654 | 693 | ||
655 | while (!find_first_extent_bit(dirty_pages, start, &start, &end, | 694 | while (!find_first_extent_bit(dirty_pages, start, &start, &end, |
656 | mark)) { | 695 | mark, &cached_state)) { |
657 | convert_extent_bit(dirty_pages, start, end, EXTENT_NEED_WAIT, mark, | 696 | convert_extent_bit(dirty_pages, start, end, EXTENT_NEED_WAIT, |
658 | GFP_NOFS); | 697 | mark, &cached_state, GFP_NOFS); |
698 | cached_state = NULL; | ||
659 | err = filemap_fdatawrite_range(mapping, start, end); | 699 | err = filemap_fdatawrite_range(mapping, start, end); |
660 | if (err) | 700 | if (err) |
661 | werr = err; | 701 | werr = err; |
@@ -679,12 +719,14 @@ int btrfs_wait_marked_extents(struct btrfs_root *root, | |||
679 | int err = 0; | 719 | int err = 0; |
680 | int werr = 0; | 720 | int werr = 0; |
681 | struct address_space *mapping = root->fs_info->btree_inode->i_mapping; | 721 | struct address_space *mapping = root->fs_info->btree_inode->i_mapping; |
722 | struct extent_state *cached_state = NULL; | ||
682 | u64 start = 0; | 723 | u64 start = 0; |
683 | u64 end; | 724 | u64 end; |
684 | 725 | ||
685 | while (!find_first_extent_bit(dirty_pages, start, &start, &end, | 726 | while (!find_first_extent_bit(dirty_pages, start, &start, &end, |
686 | EXTENT_NEED_WAIT)) { | 727 | EXTENT_NEED_WAIT, &cached_state)) { |
687 | clear_extent_bits(dirty_pages, start, end, EXTENT_NEED_WAIT, GFP_NOFS); | 728 | clear_extent_bit(dirty_pages, start, end, EXTENT_NEED_WAIT, |
729 | 0, 0, &cached_state, GFP_NOFS); | ||
688 | err = filemap_fdatawait_range(mapping, start, end); | 730 | err = filemap_fdatawait_range(mapping, start, end); |
689 | if (err) | 731 | if (err) |
690 | werr = err; | 732 | werr = err; |
@@ -955,6 +997,8 @@ static noinline int create_pending_snapshot(struct btrfs_trans_handle *trans, | |||
955 | struct btrfs_root *parent_root; | 997 | struct btrfs_root *parent_root; |
956 | struct btrfs_block_rsv *rsv; | 998 | struct btrfs_block_rsv *rsv; |
957 | struct inode *parent_inode; | 999 | struct inode *parent_inode; |
1000 | struct btrfs_path *path; | ||
1001 | struct btrfs_dir_item *dir_item; | ||
958 | struct dentry *parent; | 1002 | struct dentry *parent; |
959 | struct dentry *dentry; | 1003 | struct dentry *dentry; |
960 | struct extent_buffer *tmp; | 1004 | struct extent_buffer *tmp; |
@@ -967,18 +1011,22 @@ static noinline int create_pending_snapshot(struct btrfs_trans_handle *trans, | |||
967 | u64 root_flags; | 1011 | u64 root_flags; |
968 | uuid_le new_uuid; | 1012 | uuid_le new_uuid; |
969 | 1013 | ||
970 | rsv = trans->block_rsv; | 1014 | path = btrfs_alloc_path(); |
1015 | if (!path) { | ||
1016 | ret = pending->error = -ENOMEM; | ||
1017 | goto path_alloc_fail; | ||
1018 | } | ||
971 | 1019 | ||
972 | new_root_item = kmalloc(sizeof(*new_root_item), GFP_NOFS); | 1020 | new_root_item = kmalloc(sizeof(*new_root_item), GFP_NOFS); |
973 | if (!new_root_item) { | 1021 | if (!new_root_item) { |
974 | ret = pending->error = -ENOMEM; | 1022 | ret = pending->error = -ENOMEM; |
975 | goto fail; | 1023 | goto root_item_alloc_fail; |
976 | } | 1024 | } |
977 | 1025 | ||
978 | ret = btrfs_find_free_objectid(tree_root, &objectid); | 1026 | ret = btrfs_find_free_objectid(tree_root, &objectid); |
979 | if (ret) { | 1027 | if (ret) { |
980 | pending->error = ret; | 1028 | pending->error = ret; |
981 | goto fail; | 1029 | goto no_free_objectid; |
982 | } | 1030 | } |
983 | 1031 | ||
984 | btrfs_reloc_pre_snapshot(trans, pending, &to_reserve); | 1032 | btrfs_reloc_pre_snapshot(trans, pending, &to_reserve); |
@@ -988,22 +1036,22 @@ static noinline int create_pending_snapshot(struct btrfs_trans_handle *trans, | |||
988 | to_reserve); | 1036 | to_reserve); |
989 | if (ret) { | 1037 | if (ret) { |
990 | pending->error = ret; | 1038 | pending->error = ret; |
991 | goto fail; | 1039 | goto no_free_objectid; |
992 | } | 1040 | } |
993 | } | 1041 | } |
994 | 1042 | ||
995 | ret = btrfs_qgroup_inherit(trans, fs_info, root->root_key.objectid, | 1043 | ret = btrfs_qgroup_inherit(trans, fs_info, root->root_key.objectid, |
996 | objectid, pending->inherit); | 1044 | objectid, pending->inherit); |
997 | kfree(pending->inherit); | ||
998 | if (ret) { | 1045 | if (ret) { |
999 | pending->error = ret; | 1046 | pending->error = ret; |
1000 | goto fail; | 1047 | goto no_free_objectid; |
1001 | } | 1048 | } |
1002 | 1049 | ||
1003 | key.objectid = objectid; | 1050 | key.objectid = objectid; |
1004 | key.offset = (u64)-1; | 1051 | key.offset = (u64)-1; |
1005 | key.type = BTRFS_ROOT_ITEM_KEY; | 1052 | key.type = BTRFS_ROOT_ITEM_KEY; |
1006 | 1053 | ||
1054 | rsv = trans->block_rsv; | ||
1007 | trans->block_rsv = &pending->block_rsv; | 1055 | trans->block_rsv = &pending->block_rsv; |
1008 | 1056 | ||
1009 | dentry = pending->dentry; | 1057 | dentry = pending->dentry; |
@@ -1017,24 +1065,21 @@ static noinline int create_pending_snapshot(struct btrfs_trans_handle *trans, | |||
1017 | */ | 1065 | */ |
1018 | ret = btrfs_set_inode_index(parent_inode, &index); | 1066 | ret = btrfs_set_inode_index(parent_inode, &index); |
1019 | BUG_ON(ret); /* -ENOMEM */ | 1067 | BUG_ON(ret); /* -ENOMEM */ |
1020 | ret = btrfs_insert_dir_item(trans, parent_root, | 1068 | |
1021 | dentry->d_name.name, dentry->d_name.len, | 1069 | /* check if there is a file/dir which has the same name. */ |
1022 | parent_inode, &key, | 1070 | dir_item = btrfs_lookup_dir_item(NULL, parent_root, path, |
1023 | BTRFS_FT_DIR, index); | 1071 | btrfs_ino(parent_inode), |
1024 | if (ret == -EEXIST) { | 1072 | dentry->d_name.name, |
1073 | dentry->d_name.len, 0); | ||
1074 | if (dir_item != NULL && !IS_ERR(dir_item)) { | ||
1025 | pending->error = -EEXIST; | 1075 | pending->error = -EEXIST; |
1026 | dput(parent); | ||
1027 | goto fail; | 1076 | goto fail; |
1028 | } else if (ret) { | 1077 | } else if (IS_ERR(dir_item)) { |
1029 | goto abort_trans_dput; | 1078 | ret = PTR_ERR(dir_item); |
1079 | btrfs_abort_transaction(trans, root, ret); | ||
1080 | goto fail; | ||
1030 | } | 1081 | } |
1031 | 1082 | btrfs_release_path(path); | |
1032 | btrfs_i_size_write(parent_inode, parent_inode->i_size + | ||
1033 | dentry->d_name.len * 2); | ||
1034 | parent_inode->i_mtime = parent_inode->i_ctime = CURRENT_TIME; | ||
1035 | ret = btrfs_update_inode(trans, parent_root, parent_inode); | ||
1036 | if (ret) | ||
1037 | goto abort_trans_dput; | ||
1038 | 1083 | ||
1039 | /* | 1084 | /* |
1040 | * pull in the delayed directory update | 1085 | * pull in the delayed directory update |
@@ -1043,8 +1088,8 @@ static noinline int create_pending_snapshot(struct btrfs_trans_handle *trans, | |||
1043 | * snapshot | 1088 | * snapshot |
1044 | */ | 1089 | */ |
1045 | ret = btrfs_run_delayed_items(trans, root); | 1090 | ret = btrfs_run_delayed_items(trans, root); |
1046 | if (ret) { /* Transaction aborted */ | 1091 | if (ret) { /* Transaction aborted */ |
1047 | dput(parent); | 1092 | btrfs_abort_transaction(trans, root, ret); |
1048 | goto fail; | 1093 | goto fail; |
1049 | } | 1094 | } |
1050 | 1095 | ||
@@ -1079,7 +1124,8 @@ static noinline int create_pending_snapshot(struct btrfs_trans_handle *trans, | |||
1079 | if (ret) { | 1124 | if (ret) { |
1080 | btrfs_tree_unlock(old); | 1125 | btrfs_tree_unlock(old); |
1081 | free_extent_buffer(old); | 1126 | free_extent_buffer(old); |
1082 | goto abort_trans_dput; | 1127 | btrfs_abort_transaction(trans, root, ret); |
1128 | goto fail; | ||
1083 | } | 1129 | } |
1084 | 1130 | ||
1085 | btrfs_set_lock_blocking(old); | 1131 | btrfs_set_lock_blocking(old); |
@@ -1088,8 +1134,10 @@ static noinline int create_pending_snapshot(struct btrfs_trans_handle *trans, | |||
1088 | /* clean up in any case */ | 1134 | /* clean up in any case */ |
1089 | btrfs_tree_unlock(old); | 1135 | btrfs_tree_unlock(old); |
1090 | free_extent_buffer(old); | 1136 | free_extent_buffer(old); |
1091 | if (ret) | 1137 | if (ret) { |
1092 | goto abort_trans_dput; | 1138 | btrfs_abort_transaction(trans, root, ret); |
1139 | goto fail; | ||
1140 | } | ||
1093 | 1141 | ||
1094 | /* see comments in should_cow_block() */ | 1142 | /* see comments in should_cow_block() */ |
1095 | root->force_cow = 1; | 1143 | root->force_cow = 1; |
@@ -1101,8 +1149,10 @@ static noinline int create_pending_snapshot(struct btrfs_trans_handle *trans, | |||
1101 | ret = btrfs_insert_root(trans, tree_root, &key, new_root_item); | 1149 | ret = btrfs_insert_root(trans, tree_root, &key, new_root_item); |
1102 | btrfs_tree_unlock(tmp); | 1150 | btrfs_tree_unlock(tmp); |
1103 | free_extent_buffer(tmp); | 1151 | free_extent_buffer(tmp); |
1104 | if (ret) | 1152 | if (ret) { |
1105 | goto abort_trans_dput; | 1153 | btrfs_abort_transaction(trans, root, ret); |
1154 | goto fail; | ||
1155 | } | ||
1106 | 1156 | ||
1107 | /* | 1157 | /* |
1108 | * insert root back/forward references | 1158 | * insert root back/forward references |
@@ -1111,32 +1161,58 @@ static noinline int create_pending_snapshot(struct btrfs_trans_handle *trans, | |||
1111 | parent_root->root_key.objectid, | 1161 | parent_root->root_key.objectid, |
1112 | btrfs_ino(parent_inode), index, | 1162 | btrfs_ino(parent_inode), index, |
1113 | dentry->d_name.name, dentry->d_name.len); | 1163 | dentry->d_name.name, dentry->d_name.len); |
1114 | dput(parent); | 1164 | if (ret) { |
1115 | if (ret) | 1165 | btrfs_abort_transaction(trans, root, ret); |
1116 | goto fail; | 1166 | goto fail; |
1167 | } | ||
1117 | 1168 | ||
1118 | key.offset = (u64)-1; | 1169 | key.offset = (u64)-1; |
1119 | pending->snap = btrfs_read_fs_root_no_name(root->fs_info, &key); | 1170 | pending->snap = btrfs_read_fs_root_no_name(root->fs_info, &key); |
1120 | if (IS_ERR(pending->snap)) { | 1171 | if (IS_ERR(pending->snap)) { |
1121 | ret = PTR_ERR(pending->snap); | 1172 | ret = PTR_ERR(pending->snap); |
1122 | goto abort_trans; | 1173 | btrfs_abort_transaction(trans, root, ret); |
1174 | goto fail; | ||
1123 | } | 1175 | } |
1124 | 1176 | ||
1125 | ret = btrfs_reloc_post_snapshot(trans, pending); | 1177 | ret = btrfs_reloc_post_snapshot(trans, pending); |
1178 | if (ret) { | ||
1179 | btrfs_abort_transaction(trans, root, ret); | ||
1180 | goto fail; | ||
1181 | } | ||
1182 | |||
1183 | ret = btrfs_run_delayed_refs(trans, root, (unsigned long)-1); | ||
1184 | if (ret) { | ||
1185 | btrfs_abort_transaction(trans, root, ret); | ||
1186 | goto fail; | ||
1187 | } | ||
1188 | |||
1189 | ret = btrfs_insert_dir_item(trans, parent_root, | ||
1190 | dentry->d_name.name, dentry->d_name.len, | ||
1191 | parent_inode, &key, | ||
1192 | BTRFS_FT_DIR, index); | ||
1193 | /* We have check then name at the beginning, so it is impossible. */ | ||
1194 | BUG_ON(ret == -EEXIST); | ||
1195 | if (ret) { | ||
1196 | btrfs_abort_transaction(trans, root, ret); | ||
1197 | goto fail; | ||
1198 | } | ||
1199 | |||
1200 | btrfs_i_size_write(parent_inode, parent_inode->i_size + | ||
1201 | dentry->d_name.len * 2); | ||
1202 | parent_inode->i_mtime = parent_inode->i_ctime = CURRENT_TIME; | ||
1203 | ret = btrfs_update_inode(trans, parent_root, parent_inode); | ||
1126 | if (ret) | 1204 | if (ret) |
1127 | goto abort_trans; | 1205 | btrfs_abort_transaction(trans, root, ret); |
1128 | ret = 0; | ||
1129 | fail: | 1206 | fail: |
1130 | kfree(new_root_item); | 1207 | dput(parent); |
1131 | trans->block_rsv = rsv; | 1208 | trans->block_rsv = rsv; |
1209 | no_free_objectid: | ||
1210 | kfree(new_root_item); | ||
1211 | root_item_alloc_fail: | ||
1212 | btrfs_free_path(path); | ||
1213 | path_alloc_fail: | ||
1132 | btrfs_block_rsv_release(root, &pending->block_rsv, (u64)-1); | 1214 | btrfs_block_rsv_release(root, &pending->block_rsv, (u64)-1); |
1133 | return ret; | 1215 | return ret; |
1134 | |||
1135 | abort_trans_dput: | ||
1136 | dput(parent); | ||
1137 | abort_trans: | ||
1138 | btrfs_abort_transaction(trans, root, ret); | ||
1139 | goto fail; | ||
1140 | } | 1216 | } |
1141 | 1217 | ||
1142 | /* | 1218 | /* |
@@ -1229,6 +1305,16 @@ static void do_async_commit(struct work_struct *work) | |||
1229 | struct btrfs_async_commit *ac = | 1305 | struct btrfs_async_commit *ac = |
1230 | container_of(work, struct btrfs_async_commit, work.work); | 1306 | container_of(work, struct btrfs_async_commit, work.work); |
1231 | 1307 | ||
1308 | /* | ||
1309 | * We've got freeze protection passed with the transaction. | ||
1310 | * Tell lockdep about it. | ||
1311 | */ | ||
1312 | rwsem_acquire_read( | ||
1313 | &ac->root->fs_info->sb->s_writers.lock_map[SB_FREEZE_FS-1], | ||
1314 | 0, 1, _THIS_IP_); | ||
1315 | |||
1316 | current->journal_info = ac->newtrans; | ||
1317 | |||
1232 | btrfs_commit_transaction(ac->newtrans, ac->root); | 1318 | btrfs_commit_transaction(ac->newtrans, ac->root); |
1233 | kfree(ac); | 1319 | kfree(ac); |
1234 | } | 1320 | } |
@@ -1258,6 +1344,14 @@ int btrfs_commit_transaction_async(struct btrfs_trans_handle *trans, | |||
1258 | atomic_inc(&cur_trans->use_count); | 1344 | atomic_inc(&cur_trans->use_count); |
1259 | 1345 | ||
1260 | btrfs_end_transaction(trans, root); | 1346 | btrfs_end_transaction(trans, root); |
1347 | |||
1348 | /* | ||
1349 | * Tell lockdep we've released the freeze rwsem, since the | ||
1350 | * async commit thread will be the one to unlock it. | ||
1351 | */ | ||
1352 | rwsem_release(&root->fs_info->sb->s_writers.lock_map[SB_FREEZE_FS-1], | ||
1353 | 1, _THIS_IP_); | ||
1354 | |||
1261 | schedule_delayed_work(&ac->work, 0); | 1355 | schedule_delayed_work(&ac->work, 0); |
1262 | 1356 | ||
1263 | /* wait for transaction to start and unblock */ | 1357 | /* wait for transaction to start and unblock */ |
@@ -1348,6 +1442,9 @@ int btrfs_commit_transaction(struct btrfs_trans_handle *trans, | |||
1348 | */ | 1442 | */ |
1349 | cur_trans->delayed_refs.flushing = 1; | 1443 | cur_trans->delayed_refs.flushing = 1; |
1350 | 1444 | ||
1445 | if (!list_empty(&trans->new_bgs)) | ||
1446 | btrfs_create_pending_block_groups(trans, root); | ||
1447 | |||
1351 | ret = btrfs_run_delayed_refs(trans, root, 0); | 1448 | ret = btrfs_run_delayed_refs(trans, root, 0); |
1352 | if (ret) | 1449 | if (ret) |
1353 | goto cleanup_transaction; | 1450 | goto cleanup_transaction; |
@@ -1403,7 +1500,7 @@ int btrfs_commit_transaction(struct btrfs_trans_handle *trans, | |||
1403 | 1500 | ||
1404 | if (flush_on_commit || snap_pending) { | 1501 | if (flush_on_commit || snap_pending) { |
1405 | btrfs_start_delalloc_inodes(root, 1); | 1502 | btrfs_start_delalloc_inodes(root, 1); |
1406 | btrfs_wait_ordered_extents(root, 0, 1); | 1503 | btrfs_wait_ordered_extents(root, 1); |
1407 | } | 1504 | } |
1408 | 1505 | ||
1409 | ret = btrfs_run_delayed_items(trans, root); | 1506 | ret = btrfs_run_delayed_items(trans, root); |
@@ -1456,13 +1553,28 @@ int btrfs_commit_transaction(struct btrfs_trans_handle *trans, | |||
1456 | */ | 1553 | */ |
1457 | mutex_lock(&root->fs_info->reloc_mutex); | 1554 | mutex_lock(&root->fs_info->reloc_mutex); |
1458 | 1555 | ||
1459 | ret = btrfs_run_delayed_items(trans, root); | 1556 | /* |
1557 | * We needn't worry about the delayed items because we will | ||
1558 | * deal with them in create_pending_snapshot(), which is the | ||
1559 | * core function of the snapshot creation. | ||
1560 | */ | ||
1561 | ret = create_pending_snapshots(trans, root->fs_info); | ||
1460 | if (ret) { | 1562 | if (ret) { |
1461 | mutex_unlock(&root->fs_info->reloc_mutex); | 1563 | mutex_unlock(&root->fs_info->reloc_mutex); |
1462 | goto cleanup_transaction; | 1564 | goto cleanup_transaction; |
1463 | } | 1565 | } |
1464 | 1566 | ||
1465 | ret = create_pending_snapshots(trans, root->fs_info); | 1567 | /* |
1568 | * We insert the dir indexes of the snapshots and update the inode | ||
1569 | * of the snapshots' parents after the snapshot creation, so there | ||
1570 | * are some delayed items which are not dealt with. Now deal with | ||
1571 | * them. | ||
1572 | * | ||
1573 | * We needn't worry that this operation will corrupt the snapshots, | ||
1574 | * because all the tree which are snapshoted will be forced to COW | ||
1575 | * the nodes and leaves. | ||
1576 | */ | ||
1577 | ret = btrfs_run_delayed_items(trans, root); | ||
1466 | if (ret) { | 1578 | if (ret) { |
1467 | mutex_unlock(&root->fs_info->reloc_mutex); | 1579 | mutex_unlock(&root->fs_info->reloc_mutex); |
1468 | goto cleanup_transaction; | 1580 | goto cleanup_transaction; |
@@ -1584,7 +1696,8 @@ int btrfs_commit_transaction(struct btrfs_trans_handle *trans, | |||
1584 | put_transaction(cur_trans); | 1696 | put_transaction(cur_trans); |
1585 | put_transaction(cur_trans); | 1697 | put_transaction(cur_trans); |
1586 | 1698 | ||
1587 | sb_end_intwrite(root->fs_info->sb); | 1699 | if (trans->type < TRANS_JOIN_NOLOCK) |
1700 | sb_end_intwrite(root->fs_info->sb); | ||
1588 | 1701 | ||
1589 | trace_btrfs_transaction_commit(root); | 1702 | trace_btrfs_transaction_commit(root); |
1590 | 1703 | ||