aboutsummaryrefslogtreecommitdiffstats
path: root/fs/btrfs/inode.c
diff options
context:
space:
mode:
Diffstat (limited to 'fs/btrfs/inode.c')
-rw-r--r--fs/btrfs/inode.c405
1 files changed, 243 insertions, 162 deletions
diff --git a/fs/btrfs/inode.c b/fs/btrfs/inode.c
index 1d1017f9155..8ae72c3eedb 100644
--- a/fs/btrfs/inode.c
+++ b/fs/btrfs/inode.c
@@ -52,6 +52,7 @@
52#include "compression.h" 52#include "compression.h"
53#include "locking.h" 53#include "locking.h"
54#include "free-space-cache.h" 54#include "free-space-cache.h"
55#include "inode-map.h"
55 56
56struct btrfs_iget_args { 57struct btrfs_iget_args {
57 u64 ino; 58 u64 ino;
@@ -139,7 +140,7 @@ static noinline int insert_inline_extent(struct btrfs_trans_handle *trans,
139 path->leave_spinning = 1; 140 path->leave_spinning = 1;
140 btrfs_set_trans_block_group(trans, inode); 141 btrfs_set_trans_block_group(trans, inode);
141 142
142 key.objectid = inode->i_ino; 143 key.objectid = btrfs_ino(inode);
143 key.offset = start; 144 key.offset = start;
144 btrfs_set_key_type(&key, BTRFS_EXTENT_DATA_KEY); 145 btrfs_set_key_type(&key, BTRFS_EXTENT_DATA_KEY);
145 datasize = btrfs_file_extent_calc_inline_size(cur_size); 146 datasize = btrfs_file_extent_calc_inline_size(cur_size);
@@ -746,6 +747,15 @@ static u64 get_extent_allocation_hint(struct inode *inode, u64 start,
746 return alloc_hint; 747 return alloc_hint;
747} 748}
748 749
750static inline bool is_free_space_inode(struct btrfs_root *root,
751 struct inode *inode)
752{
753 if (root == root->fs_info->tree_root ||
754 BTRFS_I(inode)->location.objectid == BTRFS_FREE_INO_OBJECTID)
755 return true;
756 return false;
757}
758
749/* 759/*
750 * when extent_io.c finds a delayed allocation range in the file, 760 * when extent_io.c finds a delayed allocation range in the file,
751 * the call backs end up in this code. The basic idea is to 761 * the call backs end up in this code. The basic idea is to
@@ -778,7 +788,7 @@ static noinline int cow_file_range(struct inode *inode,
778 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree; 788 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
779 int ret = 0; 789 int ret = 0;
780 790
781 BUG_ON(root == root->fs_info->tree_root); 791 BUG_ON(is_free_space_inode(root, inode));
782 trans = btrfs_join_transaction(root, 1); 792 trans = btrfs_join_transaction(root, 1);
783 BUG_ON(IS_ERR(trans)); 793 BUG_ON(IS_ERR(trans));
784 btrfs_set_trans_block_group(trans, inode); 794 btrfs_set_trans_block_group(trans, inode);
@@ -1050,29 +1060,31 @@ static noinline int run_delalloc_nocow(struct inode *inode,
1050 int type; 1060 int type;
1051 int nocow; 1061 int nocow;
1052 int check_prev = 1; 1062 int check_prev = 1;
1053 bool nolock = false; 1063 bool nolock;
1064 u64 ino = btrfs_ino(inode);
1054 1065
1055 path = btrfs_alloc_path(); 1066 path = btrfs_alloc_path();
1056 BUG_ON(!path); 1067 BUG_ON(!path);
1057 if (root == root->fs_info->tree_root) { 1068
1058 nolock = true; 1069 nolock = is_free_space_inode(root, inode);
1070
1071 if (nolock)
1059 trans = btrfs_join_transaction_nolock(root, 1); 1072 trans = btrfs_join_transaction_nolock(root, 1);
1060 } else { 1073 else
1061 trans = btrfs_join_transaction(root, 1); 1074 trans = btrfs_join_transaction(root, 1);
1062 }
1063 BUG_ON(IS_ERR(trans)); 1075 BUG_ON(IS_ERR(trans));
1064 1076
1065 cow_start = (u64)-1; 1077 cow_start = (u64)-1;
1066 cur_offset = start; 1078 cur_offset = start;
1067 while (1) { 1079 while (1) {
1068 ret = btrfs_lookup_file_extent(trans, root, path, inode->i_ino, 1080 ret = btrfs_lookup_file_extent(trans, root, path, ino,
1069 cur_offset, 0); 1081 cur_offset, 0);
1070 BUG_ON(ret < 0); 1082 BUG_ON(ret < 0);
1071 if (ret > 0 && path->slots[0] > 0 && check_prev) { 1083 if (ret > 0 && path->slots[0] > 0 && check_prev) {
1072 leaf = path->nodes[0]; 1084 leaf = path->nodes[0];
1073 btrfs_item_key_to_cpu(leaf, &found_key, 1085 btrfs_item_key_to_cpu(leaf, &found_key,
1074 path->slots[0] - 1); 1086 path->slots[0] - 1);
1075 if (found_key.objectid == inode->i_ino && 1087 if (found_key.objectid == ino &&
1076 found_key.type == BTRFS_EXTENT_DATA_KEY) 1088 found_key.type == BTRFS_EXTENT_DATA_KEY)
1077 path->slots[0]--; 1089 path->slots[0]--;
1078 } 1090 }
@@ -1093,7 +1105,7 @@ next_slot:
1093 num_bytes = 0; 1105 num_bytes = 0;
1094 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]); 1106 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
1095 1107
1096 if (found_key.objectid > inode->i_ino || 1108 if (found_key.objectid > ino ||
1097 found_key.type > BTRFS_EXTENT_DATA_KEY || 1109 found_key.type > BTRFS_EXTENT_DATA_KEY ||
1098 found_key.offset > end) 1110 found_key.offset > end)
1099 break; 1111 break;
@@ -1128,7 +1140,7 @@ next_slot:
1128 goto out_check; 1140 goto out_check;
1129 if (btrfs_extent_readonly(root, disk_bytenr)) 1141 if (btrfs_extent_readonly(root, disk_bytenr))
1130 goto out_check; 1142 goto out_check;
1131 if (btrfs_cross_ref_exist(trans, root, inode->i_ino, 1143 if (btrfs_cross_ref_exist(trans, root, ino,
1132 found_key.offset - 1144 found_key.offset -
1133 extent_offset, disk_bytenr)) 1145 extent_offset, disk_bytenr))
1134 goto out_check; 1146 goto out_check;
@@ -1317,8 +1329,7 @@ static int btrfs_set_bit_hook(struct inode *inode,
1317 if (!(state->state & EXTENT_DELALLOC) && (*bits & EXTENT_DELALLOC)) { 1329 if (!(state->state & EXTENT_DELALLOC) && (*bits & EXTENT_DELALLOC)) {
1318 struct btrfs_root *root = BTRFS_I(inode)->root; 1330 struct btrfs_root *root = BTRFS_I(inode)->root;
1319 u64 len = state->end + 1 - state->start; 1331 u64 len = state->end + 1 - state->start;
1320 int do_list = (root->root_key.objectid != 1332 bool do_list = !is_free_space_inode(root, inode);
1321 BTRFS_ROOT_TREE_OBJECTID);
1322 1333
1323 if (*bits & EXTENT_FIRST_DELALLOC) 1334 if (*bits & EXTENT_FIRST_DELALLOC)
1324 *bits &= ~EXTENT_FIRST_DELALLOC; 1335 *bits &= ~EXTENT_FIRST_DELALLOC;
@@ -1351,8 +1362,7 @@ static int btrfs_clear_bit_hook(struct inode *inode,
1351 if ((state->state & EXTENT_DELALLOC) && (*bits & EXTENT_DELALLOC)) { 1362 if ((state->state & EXTENT_DELALLOC) && (*bits & EXTENT_DELALLOC)) {
1352 struct btrfs_root *root = BTRFS_I(inode)->root; 1363 struct btrfs_root *root = BTRFS_I(inode)->root;
1353 u64 len = state->end + 1 - state->start; 1364 u64 len = state->end + 1 - state->start;
1354 int do_list = (root->root_key.objectid != 1365 bool do_list = !is_free_space_inode(root, inode);
1355 BTRFS_ROOT_TREE_OBJECTID);
1356 1366
1357 if (*bits & EXTENT_FIRST_DELALLOC) 1367 if (*bits & EXTENT_FIRST_DELALLOC)
1358 *bits &= ~EXTENT_FIRST_DELALLOC; 1368 *bits &= ~EXTENT_FIRST_DELALLOC;
@@ -1459,7 +1469,7 @@ static int btrfs_submit_bio_hook(struct inode *inode, int rw, struct bio *bio,
1459 1469
1460 skip_sum = BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM; 1470 skip_sum = BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM;
1461 1471
1462 if (root == root->fs_info->tree_root) 1472 if (is_free_space_inode(root, inode))
1463 ret = btrfs_bio_wq_end_io(root->fs_info, bio, 2); 1473 ret = btrfs_bio_wq_end_io(root->fs_info, bio, 2);
1464 else 1474 else
1465 ret = btrfs_bio_wq_end_io(root->fs_info, bio, 0); 1475 ret = btrfs_bio_wq_end_io(root->fs_info, bio, 0);
@@ -1645,7 +1655,7 @@ static int insert_reserved_file_extent(struct btrfs_trans_handle *trans,
1645 &hint, 0); 1655 &hint, 0);
1646 BUG_ON(ret); 1656 BUG_ON(ret);
1647 1657
1648 ins.objectid = inode->i_ino; 1658 ins.objectid = btrfs_ino(inode);
1649 ins.offset = file_pos; 1659 ins.offset = file_pos;
1650 ins.type = BTRFS_EXTENT_DATA_KEY; 1660 ins.type = BTRFS_EXTENT_DATA_KEY;
1651 ret = btrfs_insert_empty_item(trans, root, path, &ins, sizeof(*fi)); 1661 ret = btrfs_insert_empty_item(trans, root, path, &ins, sizeof(*fi));
@@ -1676,7 +1686,7 @@ static int insert_reserved_file_extent(struct btrfs_trans_handle *trans,
1676 ins.type = BTRFS_EXTENT_ITEM_KEY; 1686 ins.type = BTRFS_EXTENT_ITEM_KEY;
1677 ret = btrfs_alloc_reserved_file_extent(trans, root, 1687 ret = btrfs_alloc_reserved_file_extent(trans, root,
1678 root->root_key.objectid, 1688 root->root_key.objectid,
1679 inode->i_ino, file_pos, &ins); 1689 btrfs_ino(inode), file_pos, &ins);
1680 BUG_ON(ret); 1690 BUG_ON(ret);
1681 btrfs_free_path(path); 1691 btrfs_free_path(path);
1682 1692
@@ -1702,7 +1712,7 @@ static int btrfs_finish_ordered_io(struct inode *inode, u64 start, u64 end)
1702 struct extent_state *cached_state = NULL; 1712 struct extent_state *cached_state = NULL;
1703 int compress_type = 0; 1713 int compress_type = 0;
1704 int ret; 1714 int ret;
1705 bool nolock = false; 1715 bool nolock;
1706 1716
1707 ret = btrfs_dec_test_ordered_pending(inode, &ordered_extent, start, 1717 ret = btrfs_dec_test_ordered_pending(inode, &ordered_extent, start,
1708 end - start + 1); 1718 end - start + 1);
@@ -1710,7 +1720,7 @@ static int btrfs_finish_ordered_io(struct inode *inode, u64 start, u64 end)
1710 return 0; 1720 return 0;
1711 BUG_ON(!ordered_extent); 1721 BUG_ON(!ordered_extent);
1712 1722
1713 nolock = (root == root->fs_info->tree_root); 1723 nolock = is_free_space_inode(root, inode);
1714 1724
1715 if (test_bit(BTRFS_ORDERED_NOCOW, &ordered_extent->flags)) { 1725 if (test_bit(BTRFS_ORDERED_NOCOW, &ordered_extent->flags)) {
1716 BUG_ON(!list_empty(&ordered_extent->list)); 1726 BUG_ON(!list_empty(&ordered_extent->list));
@@ -2005,8 +2015,9 @@ good:
2005 return 0; 2015 return 0;
2006 2016
2007zeroit: 2017zeroit:
2008 printk_ratelimited(KERN_INFO "btrfs csum failed ino %lu off %llu csum %u " 2018 printk_ratelimited(KERN_INFO "btrfs csum failed ino %llu off %llu csum %u "
2009 "private %llu\n", page->mapping->host->i_ino, 2019 "private %llu\n",
2020 (unsigned long long)btrfs_ino(page->mapping->host),
2010 (unsigned long long)start, csum, 2021 (unsigned long long)start, csum,
2011 (unsigned long long)private); 2022 (unsigned long long)private);
2012 memset(kaddr + offset, 1, end - start + 1); 2023 memset(kaddr + offset, 1, end - start + 1);
@@ -2243,7 +2254,7 @@ int btrfs_orphan_add(struct btrfs_trans_handle *trans, struct inode *inode)
2243 2254
2244 /* insert an orphan item to track this unlinked/truncated file */ 2255 /* insert an orphan item to track this unlinked/truncated file */
2245 if (insert >= 1) { 2256 if (insert >= 1) {
2246 ret = btrfs_insert_orphan_item(trans, root, inode->i_ino); 2257 ret = btrfs_insert_orphan_item(trans, root, btrfs_ino(inode));
2247 BUG_ON(ret); 2258 BUG_ON(ret);
2248 } 2259 }
2249 2260
@@ -2280,7 +2291,7 @@ int btrfs_orphan_del(struct btrfs_trans_handle *trans, struct inode *inode)
2280 spin_unlock(&root->orphan_lock); 2291 spin_unlock(&root->orphan_lock);
2281 2292
2282 if (trans && delete_item) { 2293 if (trans && delete_item) {
2283 ret = btrfs_del_orphan_item(trans, root, inode->i_ino); 2294 ret = btrfs_del_orphan_item(trans, root, btrfs_ino(inode));
2284 BUG_ON(ret); 2295 BUG_ON(ret);
2285 } 2296 }
2286 2297
@@ -2542,7 +2553,8 @@ static void btrfs_read_locked_inode(struct inode *inode)
2542 * try to precache a NULL acl entry for files that don't have 2553 * try to precache a NULL acl entry for files that don't have
2543 * any xattrs or acls 2554 * any xattrs or acls
2544 */ 2555 */
2545 maybe_acls = acls_after_inode_item(leaf, path->slots[0], inode->i_ino); 2556 maybe_acls = acls_after_inode_item(leaf, path->slots[0],
2557 btrfs_ino(inode));
2546 if (!maybe_acls) 2558 if (!maybe_acls)
2547 cache_no_acl(inode); 2559 cache_no_acl(inode);
2548 2560
@@ -2646,11 +2658,26 @@ noinline int btrfs_update_inode(struct btrfs_trans_handle *trans,
2646 struct extent_buffer *leaf; 2658 struct extent_buffer *leaf;
2647 int ret; 2659 int ret;
2648 2660
2661 /*
2662 * If root is tree root, it means this inode is used to
2663 * store free space information. And these inodes are updated
2664 * when committing the transaction, so they needn't delaye to
2665 * be updated, or deadlock will occured.
2666 */
2667 if (!is_free_space_inode(root, inode)) {
2668 ret = btrfs_delayed_update_inode(trans, root, inode);
2669 if (!ret)
2670 btrfs_set_inode_last_trans(trans, inode);
2671 return ret;
2672 }
2673
2649 path = btrfs_alloc_path(); 2674 path = btrfs_alloc_path();
2650 BUG_ON(!path); 2675 if (!path)
2676 return -ENOMEM;
2677
2651 path->leave_spinning = 1; 2678 path->leave_spinning = 1;
2652 ret = btrfs_lookup_inode(trans, root, path, 2679 ret = btrfs_lookup_inode(trans, root, path, &BTRFS_I(inode)->location,
2653 &BTRFS_I(inode)->location, 1); 2680 1);
2654 if (ret) { 2681 if (ret) {
2655 if (ret > 0) 2682 if (ret > 0)
2656 ret = -ENOENT; 2683 ret = -ENOENT;
@@ -2660,7 +2687,7 @@ noinline int btrfs_update_inode(struct btrfs_trans_handle *trans,
2660 btrfs_unlock_up_safe(path, 1); 2687 btrfs_unlock_up_safe(path, 1);
2661 leaf = path->nodes[0]; 2688 leaf = path->nodes[0];
2662 inode_item = btrfs_item_ptr(leaf, path->slots[0], 2689 inode_item = btrfs_item_ptr(leaf, path->slots[0],
2663 struct btrfs_inode_item); 2690 struct btrfs_inode_item);
2664 2691
2665 fill_inode_item(trans, leaf, inode_item, inode); 2692 fill_inode_item(trans, leaf, inode_item, inode);
2666 btrfs_mark_buffer_dirty(leaf); 2693 btrfs_mark_buffer_dirty(leaf);
@@ -2671,7 +2698,6 @@ failed:
2671 return ret; 2698 return ret;
2672} 2699}
2673 2700
2674
2675/* 2701/*
2676 * unlink helper that gets used here in inode.c and in the tree logging 2702 * unlink helper that gets used here in inode.c and in the tree logging
2677 * recovery code. It remove a link in a directory with a given name, and 2703 * recovery code. It remove a link in a directory with a given name, and
@@ -2688,6 +2714,8 @@ static int __btrfs_unlink_inode(struct btrfs_trans_handle *trans,
2688 struct btrfs_dir_item *di; 2714 struct btrfs_dir_item *di;
2689 struct btrfs_key key; 2715 struct btrfs_key key;
2690 u64 index; 2716 u64 index;
2717 u64 ino = btrfs_ino(inode);
2718 u64 dir_ino = btrfs_ino(dir);
2691 2719
2692 path = btrfs_alloc_path(); 2720 path = btrfs_alloc_path();
2693 if (!path) { 2721 if (!path) {
@@ -2696,7 +2724,7 @@ static int __btrfs_unlink_inode(struct btrfs_trans_handle *trans,
2696 } 2724 }
2697 2725
2698 path->leave_spinning = 1; 2726 path->leave_spinning = 1;
2699 di = btrfs_lookup_dir_item(trans, root, path, dir->i_ino, 2727 di = btrfs_lookup_dir_item(trans, root, path, dir_ino,
2700 name, name_len, -1); 2728 name, name_len, -1);
2701 if (IS_ERR(di)) { 2729 if (IS_ERR(di)) {
2702 ret = PTR_ERR(di); 2730 ret = PTR_ERR(di);
@@ -2713,31 +2741,21 @@ static int __btrfs_unlink_inode(struct btrfs_trans_handle *trans,
2713 goto err; 2741 goto err;
2714 btrfs_release_path(path); 2742 btrfs_release_path(path);
2715 2743
2716 ret = btrfs_del_inode_ref(trans, root, name, name_len, 2744 ret = btrfs_del_inode_ref(trans, root, name, name_len, ino,
2717 inode->i_ino, 2745 dir_ino, &index);
2718 dir->i_ino, &index);
2719 if (ret) { 2746 if (ret) {
2720 printk(KERN_INFO "btrfs failed to delete reference to %.*s, " 2747 printk(KERN_INFO "btrfs failed to delete reference to %.*s, "
2721 "inode %lu parent %lu\n", name_len, name, 2748 "inode %llu parent %llu\n", name_len, name,
2722 inode->i_ino, dir->i_ino); 2749 (unsigned long long)ino, (unsigned long long)dir_ino);
2723 goto err; 2750 goto err;
2724 } 2751 }
2725 2752
2726 di = btrfs_lookup_dir_index_item(trans, root, path, dir->i_ino, 2753 ret = btrfs_delete_delayed_dir_index(trans, root, dir, index);
2727 index, name, name_len, -1); 2754 if (ret)
2728 if (IS_ERR(di)) {
2729 ret = PTR_ERR(di);
2730 goto err;
2731 }
2732 if (!di) {
2733 ret = -ENOENT;
2734 goto err; 2755 goto err;
2735 }
2736 ret = btrfs_delete_one_dir_name(trans, root, path, di);
2737 btrfs_release_path(path);
2738 2756
2739 ret = btrfs_del_inode_ref_in_log(trans, root, name, name_len, 2757 ret = btrfs_del_inode_ref_in_log(trans, root, name, name_len,
2740 inode, dir->i_ino); 2758 inode, dir_ino);
2741 BUG_ON(ret != 0 && ret != -ENOENT); 2759 BUG_ON(ret != 0 && ret != -ENOENT);
2742 2760
2743 ret = btrfs_del_dir_entries_in_log(trans, root, name, name_len, 2761 ret = btrfs_del_dir_entries_in_log(trans, root, name, name_len,
@@ -2815,12 +2833,14 @@ static struct btrfs_trans_handle *__unlink_start_trans(struct inode *dir,
2815 int check_link = 1; 2833 int check_link = 1;
2816 int err = -ENOSPC; 2834 int err = -ENOSPC;
2817 int ret; 2835 int ret;
2836 u64 ino = btrfs_ino(inode);
2837 u64 dir_ino = btrfs_ino(dir);
2818 2838
2819 trans = btrfs_start_transaction(root, 10); 2839 trans = btrfs_start_transaction(root, 10);
2820 if (!IS_ERR(trans) || PTR_ERR(trans) != -ENOSPC) 2840 if (!IS_ERR(trans) || PTR_ERR(trans) != -ENOSPC)
2821 return trans; 2841 return trans;
2822 2842
2823 if (inode->i_ino == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID) 2843 if (ino == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID)
2824 return ERR_PTR(-ENOSPC); 2844 return ERR_PTR(-ENOSPC);
2825 2845
2826 /* check if there is someone else holds reference */ 2846 /* check if there is someone else holds reference */
@@ -2879,7 +2899,7 @@ static struct btrfs_trans_handle *__unlink_start_trans(struct inode *dir,
2879 2899
2880 if (ret == 0 && S_ISREG(inode->i_mode)) { 2900 if (ret == 0 && S_ISREG(inode->i_mode)) {
2881 ret = btrfs_lookup_file_extent(trans, root, path, 2901 ret = btrfs_lookup_file_extent(trans, root, path,
2882 inode->i_ino, (u64)-1, 0); 2902 ino, (u64)-1, 0);
2883 if (ret < 0) { 2903 if (ret < 0) {
2884 err = ret; 2904 err = ret;
2885 goto out; 2905 goto out;
@@ -2895,7 +2915,7 @@ static struct btrfs_trans_handle *__unlink_start_trans(struct inode *dir,
2895 goto out; 2915 goto out;
2896 } 2916 }
2897 2917
2898 di = btrfs_lookup_dir_item(trans, root, path, dir->i_ino, 2918 di = btrfs_lookup_dir_item(trans, root, path, dir_ino,
2899 dentry->d_name.name, dentry->d_name.len, 0); 2919 dentry->d_name.name, dentry->d_name.len, 0);
2900 if (IS_ERR(di)) { 2920 if (IS_ERR(di)) {
2901 err = PTR_ERR(di); 2921 err = PTR_ERR(di);
@@ -2912,7 +2932,7 @@ static struct btrfs_trans_handle *__unlink_start_trans(struct inode *dir,
2912 2932
2913 ref = btrfs_lookup_inode_ref(trans, root, path, 2933 ref = btrfs_lookup_inode_ref(trans, root, path,
2914 dentry->d_name.name, dentry->d_name.len, 2934 dentry->d_name.name, dentry->d_name.len,
2915 inode->i_ino, dir->i_ino, 0); 2935 ino, dir_ino, 0);
2916 if (IS_ERR(ref)) { 2936 if (IS_ERR(ref)) {
2917 err = PTR_ERR(ref); 2937 err = PTR_ERR(ref);
2918 goto out; 2938 goto out;
@@ -2923,7 +2943,15 @@ static struct btrfs_trans_handle *__unlink_start_trans(struct inode *dir,
2923 index = btrfs_inode_ref_index(path->nodes[0], ref); 2943 index = btrfs_inode_ref_index(path->nodes[0], ref);
2924 btrfs_release_path(path); 2944 btrfs_release_path(path);
2925 2945
2926 di = btrfs_lookup_dir_index_item(trans, root, path, dir->i_ino, index, 2946 /*
2947 * This is a commit root search, if we can lookup inode item and other
2948 * relative items in the commit root, it means the transaction of
2949 * dir/file creation has been committed, and the dir index item that we
2950 * delay to insert has also been inserted into the commit root. So
2951 * we needn't worry about the delayed insertion of the dir index item
2952 * here.
2953 */
2954 di = btrfs_lookup_dir_index_item(trans, root, path, dir_ino, index,
2927 dentry->d_name.name, dentry->d_name.len, 0); 2955 dentry->d_name.name, dentry->d_name.len, 0);
2928 if (IS_ERR(di)) { 2956 if (IS_ERR(di)) {
2929 err = PTR_ERR(di); 2957 err = PTR_ERR(di);
@@ -2998,12 +3026,13 @@ int btrfs_unlink_subvol(struct btrfs_trans_handle *trans,
2998 struct btrfs_key key; 3026 struct btrfs_key key;
2999 u64 index; 3027 u64 index;
3000 int ret; 3028 int ret;
3029 u64 dir_ino = btrfs_ino(dir);
3001 3030
3002 path = btrfs_alloc_path(); 3031 path = btrfs_alloc_path();
3003 if (!path) 3032 if (!path)
3004 return -ENOMEM; 3033 return -ENOMEM;
3005 3034
3006 di = btrfs_lookup_dir_item(trans, root, path, dir->i_ino, 3035 di = btrfs_lookup_dir_item(trans, root, path, dir_ino,
3007 name, name_len, -1); 3036 name, name_len, -1);
3008 BUG_ON(IS_ERR_OR_NULL(di)); 3037 BUG_ON(IS_ERR_OR_NULL(di));
3009 3038
@@ -3016,10 +3045,10 @@ int btrfs_unlink_subvol(struct btrfs_trans_handle *trans,
3016 3045
3017 ret = btrfs_del_root_ref(trans, root->fs_info->tree_root, 3046 ret = btrfs_del_root_ref(trans, root->fs_info->tree_root,
3018 objectid, root->root_key.objectid, 3047 objectid, root->root_key.objectid,
3019 dir->i_ino, &index, name, name_len); 3048 dir_ino, &index, name, name_len);
3020 if (ret < 0) { 3049 if (ret < 0) {
3021 BUG_ON(ret != -ENOENT); 3050 BUG_ON(ret != -ENOENT);
3022 di = btrfs_search_dir_index_item(root, path, dir->i_ino, 3051 di = btrfs_search_dir_index_item(root, path, dir_ino,
3023 name, name_len); 3052 name, name_len);
3024 BUG_ON(IS_ERR_OR_NULL(di)); 3053 BUG_ON(IS_ERR_OR_NULL(di));
3025 3054
@@ -3028,24 +3057,16 @@ int btrfs_unlink_subvol(struct btrfs_trans_handle *trans,
3028 btrfs_release_path(path); 3057 btrfs_release_path(path);
3029 index = key.offset; 3058 index = key.offset;
3030 } 3059 }
3060 btrfs_release_path(path);
3031 3061
3032 di = btrfs_lookup_dir_index_item(trans, root, path, dir->i_ino, 3062 ret = btrfs_delete_delayed_dir_index(trans, root, dir, index);
3033 index, name, name_len, -1);
3034 BUG_ON(IS_ERR_OR_NULL(di));
3035
3036 leaf = path->nodes[0];
3037 btrfs_dir_item_key_to_cpu(leaf, di, &key);
3038 WARN_ON(key.type != BTRFS_ROOT_ITEM_KEY || key.objectid != objectid);
3039 ret = btrfs_delete_one_dir_name(trans, root, path, di);
3040 BUG_ON(ret); 3063 BUG_ON(ret);
3041 btrfs_release_path(path);
3042 3064
3043 btrfs_i_size_write(dir, dir->i_size - name_len * 2); 3065 btrfs_i_size_write(dir, dir->i_size - name_len * 2);
3044 dir->i_mtime = dir->i_ctime = CURRENT_TIME; 3066 dir->i_mtime = dir->i_ctime = CURRENT_TIME;
3045 ret = btrfs_update_inode(trans, root, dir); 3067 ret = btrfs_update_inode(trans, root, dir);
3046 BUG_ON(ret); 3068 BUG_ON(ret);
3047 3069
3048 btrfs_free_path(path);
3049 return 0; 3070 return 0;
3050} 3071}
3051 3072
@@ -3058,7 +3079,7 @@ static int btrfs_rmdir(struct inode *dir, struct dentry *dentry)
3058 unsigned long nr = 0; 3079 unsigned long nr = 0;
3059 3080
3060 if (inode->i_size > BTRFS_EMPTY_DIR_SIZE || 3081 if (inode->i_size > BTRFS_EMPTY_DIR_SIZE ||
3061 inode->i_ino == BTRFS_FIRST_FREE_OBJECTID) 3082 btrfs_ino(inode) == BTRFS_FIRST_FREE_OBJECTID)
3062 return -ENOTEMPTY; 3083 return -ENOTEMPTY;
3063 3084
3064 trans = __unlink_start_trans(dir, dentry); 3085 trans = __unlink_start_trans(dir, dentry);
@@ -3067,7 +3088,7 @@ static int btrfs_rmdir(struct inode *dir, struct dentry *dentry)
3067 3088
3068 btrfs_set_trans_block_group(trans, dir); 3089 btrfs_set_trans_block_group(trans, dir);
3069 3090
3070 if (unlikely(inode->i_ino == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID)) { 3091 if (unlikely(btrfs_ino(inode) == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID)) {
3071 err = btrfs_unlink_subvol(trans, root, dir, 3092 err = btrfs_unlink_subvol(trans, root, dir,
3072 BTRFS_I(inode)->location.objectid, 3093 BTRFS_I(inode)->location.objectid,
3073 dentry->d_name.name, 3094 dentry->d_name.name,
@@ -3127,17 +3148,27 @@ int btrfs_truncate_inode_items(struct btrfs_trans_handle *trans,
3127 int encoding; 3148 int encoding;
3128 int ret; 3149 int ret;
3129 int err = 0; 3150 int err = 0;
3151 u64 ino = btrfs_ino(inode);
3130 3152
3131 BUG_ON(new_size > 0 && min_type != BTRFS_EXTENT_DATA_KEY); 3153 BUG_ON(new_size > 0 && min_type != BTRFS_EXTENT_DATA_KEY);
3132 3154
3133 if (root->ref_cows || root == root->fs_info->tree_root) 3155 if (root->ref_cows || root == root->fs_info->tree_root)
3134 btrfs_drop_extent_cache(inode, new_size & (~mask), (u64)-1, 0); 3156 btrfs_drop_extent_cache(inode, new_size & (~mask), (u64)-1, 0);
3135 3157
3158 /*
3159 * This function is also used to drop the items in the log tree before
3160 * we relog the inode, so if root != BTRFS_I(inode)->root, it means
3161 * it is used to drop the loged items. So we shouldn't kill the delayed
3162 * items.
3163 */
3164 if (min_type == 0 && root == BTRFS_I(inode)->root)
3165 btrfs_kill_delayed_inode_items(inode);
3166
3136 path = btrfs_alloc_path(); 3167 path = btrfs_alloc_path();
3137 BUG_ON(!path); 3168 BUG_ON(!path);
3138 path->reada = -1; 3169 path->reada = -1;
3139 3170
3140 key.objectid = inode->i_ino; 3171 key.objectid = ino;
3141 key.offset = (u64)-1; 3172 key.offset = (u64)-1;
3142 key.type = (u8)-1; 3173 key.type = (u8)-1;
3143 3174
@@ -3165,7 +3196,7 @@ search_again:
3165 found_type = btrfs_key_type(&found_key); 3196 found_type = btrfs_key_type(&found_key);
3166 encoding = 0; 3197 encoding = 0;
3167 3198
3168 if (found_key.objectid != inode->i_ino) 3199 if (found_key.objectid != ino)
3169 break; 3200 break;
3170 3201
3171 if (found_type < min_type) 3202 if (found_type < min_type)
@@ -3284,7 +3315,7 @@ delete:
3284 ret = btrfs_free_extent(trans, root, extent_start, 3315 ret = btrfs_free_extent(trans, root, extent_start,
3285 extent_num_bytes, 0, 3316 extent_num_bytes, 0,
3286 btrfs_header_owner(leaf), 3317 btrfs_header_owner(leaf),
3287 inode->i_ino, extent_offset); 3318 ino, extent_offset);
3288 BUG_ON(ret); 3319 BUG_ON(ret);
3289 } 3320 }
3290 3321
@@ -3293,7 +3324,9 @@ delete:
3293 3324
3294 if (path->slots[0] == 0 || 3325 if (path->slots[0] == 0 ||
3295 path->slots[0] != pending_del_slot) { 3326 path->slots[0] != pending_del_slot) {
3296 if (root->ref_cows) { 3327 if (root->ref_cows &&
3328 BTRFS_I(inode)->location.objectid !=
3329 BTRFS_FREE_INO_OBJECTID) {
3297 err = -EAGAIN; 3330 err = -EAGAIN;
3298 goto out; 3331 goto out;
3299 } 3332 }
@@ -3483,7 +3516,7 @@ int btrfs_cont_expand(struct inode *inode, loff_t oldsize, loff_t size)
3483 break; 3516 break;
3484 3517
3485 err = btrfs_insert_file_extent(trans, root, 3518 err = btrfs_insert_file_extent(trans, root,
3486 inode->i_ino, cur_offset, 0, 3519 btrfs_ino(inode), cur_offset, 0,
3487 0, hole_size, 0, hole_size, 3520 0, hole_size, 0, hole_size,
3488 0, 0, 0); 3521 0, 0, 0);
3489 if (err) 3522 if (err)
@@ -3585,7 +3618,7 @@ void btrfs_evict_inode(struct inode *inode)
3585 3618
3586 truncate_inode_pages(&inode->i_data, 0); 3619 truncate_inode_pages(&inode->i_data, 0);
3587 if (inode->i_nlink && (btrfs_root_refs(&root->root_item) != 0 || 3620 if (inode->i_nlink && (btrfs_root_refs(&root->root_item) != 0 ||
3588 root == root->fs_info->tree_root)) 3621 is_free_space_inode(root, inode)))
3589 goto no_delete; 3622 goto no_delete;
3590 3623
3591 if (is_bad_inode(inode)) { 3624 if (is_bad_inode(inode)) {
@@ -3638,6 +3671,10 @@ void btrfs_evict_inode(struct inode *inode)
3638 BUG_ON(ret); 3671 BUG_ON(ret);
3639 } 3672 }
3640 3673
3674 if (!(root == root->fs_info->tree_root ||
3675 root->root_key.objectid == BTRFS_TREE_RELOC_OBJECTID))
3676 btrfs_return_ino(root, btrfs_ino(inode));
3677
3641 nr = trans->blocks_used; 3678 nr = trans->blocks_used;
3642 btrfs_end_transaction(trans, root); 3679 btrfs_end_transaction(trans, root);
3643 btrfs_btree_balance_dirty(root, nr); 3680 btrfs_btree_balance_dirty(root, nr);
@@ -3663,7 +3700,7 @@ static int btrfs_inode_by_name(struct inode *dir, struct dentry *dentry,
3663 path = btrfs_alloc_path(); 3700 path = btrfs_alloc_path();
3664 BUG_ON(!path); 3701 BUG_ON(!path);
3665 3702
3666 di = btrfs_lookup_dir_item(NULL, root, path, dir->i_ino, name, 3703 di = btrfs_lookup_dir_item(NULL, root, path, btrfs_ino(dir), name,
3667 namelen, 0); 3704 namelen, 0);
3668 if (IS_ERR(di)) 3705 if (IS_ERR(di))
3669 ret = PTR_ERR(di); 3706 ret = PTR_ERR(di);
@@ -3716,7 +3753,7 @@ static int fixup_tree_root_location(struct btrfs_root *root,
3716 3753
3717 leaf = path->nodes[0]; 3754 leaf = path->nodes[0];
3718 ref = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_root_ref); 3755 ref = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_root_ref);
3719 if (btrfs_root_ref_dirid(leaf, ref) != dir->i_ino || 3756 if (btrfs_root_ref_dirid(leaf, ref) != btrfs_ino(dir) ||
3720 btrfs_root_ref_name_len(leaf, ref) != dentry->d_name.len) 3757 btrfs_root_ref_name_len(leaf, ref) != dentry->d_name.len)
3721 goto out; 3758 goto out;
3722 3759
@@ -3755,6 +3792,7 @@ static void inode_tree_add(struct inode *inode)
3755 struct btrfs_inode *entry; 3792 struct btrfs_inode *entry;
3756 struct rb_node **p; 3793 struct rb_node **p;
3757 struct rb_node *parent; 3794 struct rb_node *parent;
3795 u64 ino = btrfs_ino(inode);
3758again: 3796again:
3759 p = &root->inode_tree.rb_node; 3797 p = &root->inode_tree.rb_node;
3760 parent = NULL; 3798 parent = NULL;
@@ -3767,9 +3805,9 @@ again:
3767 parent = *p; 3805 parent = *p;
3768 entry = rb_entry(parent, struct btrfs_inode, rb_node); 3806 entry = rb_entry(parent, struct btrfs_inode, rb_node);
3769 3807
3770 if (inode->i_ino < entry->vfs_inode.i_ino) 3808 if (ino < btrfs_ino(&entry->vfs_inode))
3771 p = &parent->rb_left; 3809 p = &parent->rb_left;
3772 else if (inode->i_ino > entry->vfs_inode.i_ino) 3810 else if (ino > btrfs_ino(&entry->vfs_inode))
3773 p = &parent->rb_right; 3811 p = &parent->rb_right;
3774 else { 3812 else {
3775 WARN_ON(!(entry->vfs_inode.i_state & 3813 WARN_ON(!(entry->vfs_inode.i_state &
@@ -3833,9 +3871,9 @@ again:
3833 prev = node; 3871 prev = node;
3834 entry = rb_entry(node, struct btrfs_inode, rb_node); 3872 entry = rb_entry(node, struct btrfs_inode, rb_node);
3835 3873
3836 if (objectid < entry->vfs_inode.i_ino) 3874 if (objectid < btrfs_ino(&entry->vfs_inode))
3837 node = node->rb_left; 3875 node = node->rb_left;
3838 else if (objectid > entry->vfs_inode.i_ino) 3876 else if (objectid > btrfs_ino(&entry->vfs_inode))
3839 node = node->rb_right; 3877 node = node->rb_right;
3840 else 3878 else
3841 break; 3879 break;
@@ -3843,7 +3881,7 @@ again:
3843 if (!node) { 3881 if (!node) {
3844 while (prev) { 3882 while (prev) {
3845 entry = rb_entry(prev, struct btrfs_inode, rb_node); 3883 entry = rb_entry(prev, struct btrfs_inode, rb_node);
3846 if (objectid <= entry->vfs_inode.i_ino) { 3884 if (objectid <= btrfs_ino(&entry->vfs_inode)) {
3847 node = prev; 3885 node = prev;
3848 break; 3886 break;
3849 } 3887 }
@@ -3852,7 +3890,7 @@ again:
3852 } 3890 }
3853 while (node) { 3891 while (node) {
3854 entry = rb_entry(node, struct btrfs_inode, rb_node); 3892 entry = rb_entry(node, struct btrfs_inode, rb_node);
3855 objectid = entry->vfs_inode.i_ino + 1; 3893 objectid = btrfs_ino(&entry->vfs_inode) + 1;
3856 inode = igrab(&entry->vfs_inode); 3894 inode = igrab(&entry->vfs_inode);
3857 if (inode) { 3895 if (inode) {
3858 spin_unlock(&root->inode_lock); 3896 spin_unlock(&root->inode_lock);
@@ -3890,7 +3928,7 @@ static int btrfs_init_locked_inode(struct inode *inode, void *p)
3890static int btrfs_find_actor(struct inode *inode, void *opaque) 3928static int btrfs_find_actor(struct inode *inode, void *opaque)
3891{ 3929{
3892 struct btrfs_iget_args *args = opaque; 3930 struct btrfs_iget_args *args = opaque;
3893 return args->ino == inode->i_ino && 3931 return args->ino == btrfs_ino(inode) &&
3894 args->root == BTRFS_I(inode)->root; 3932 args->root == BTRFS_I(inode)->root;
3895} 3933}
3896 3934
@@ -4035,7 +4073,7 @@ static struct dentry *btrfs_lookup(struct inode *dir, struct dentry *dentry,
4035 return d_splice_alias(inode, dentry); 4073 return d_splice_alias(inode, dentry);
4036} 4074}
4037 4075
4038static unsigned char btrfs_filetype_table[] = { 4076unsigned char btrfs_filetype_table[] = {
4039 DT_UNKNOWN, DT_REG, DT_DIR, DT_CHR, DT_BLK, DT_FIFO, DT_SOCK, DT_LNK 4077 DT_UNKNOWN, DT_REG, DT_DIR, DT_CHR, DT_BLK, DT_FIFO, DT_SOCK, DT_LNK
4040}; 4078};
4041 4079
@@ -4049,6 +4087,8 @@ static int btrfs_real_readdir(struct file *filp, void *dirent,
4049 struct btrfs_key key; 4087 struct btrfs_key key;
4050 struct btrfs_key found_key; 4088 struct btrfs_key found_key;
4051 struct btrfs_path *path; 4089 struct btrfs_path *path;
4090 struct list_head ins_list;
4091 struct list_head del_list;
4052 int ret; 4092 int ret;
4053 struct extent_buffer *leaf; 4093 struct extent_buffer *leaf;
4054 int slot; 4094 int slot;
@@ -4061,6 +4101,7 @@ static int btrfs_real_readdir(struct file *filp, void *dirent,
4061 char tmp_name[32]; 4101 char tmp_name[32];
4062 char *name_ptr; 4102 char *name_ptr;
4063 int name_len; 4103 int name_len;
4104 int is_curr = 0; /* filp->f_pos points to the current index? */
4064 4105
4065 /* FIXME, use a real flag for deciding about the key type */ 4106 /* FIXME, use a real flag for deciding about the key type */
4066 if (root->fs_info->tree_root == root) 4107 if (root->fs_info->tree_root == root)
@@ -4068,9 +4109,7 @@ static int btrfs_real_readdir(struct file *filp, void *dirent,
4068 4109
4069 /* special case for "." */ 4110 /* special case for "." */
4070 if (filp->f_pos == 0) { 4111 if (filp->f_pos == 0) {
4071 over = filldir(dirent, ".", 1, 4112 over = filldir(dirent, ".", 1, 1, btrfs_ino(inode), DT_DIR);
4072 1, inode->i_ino,
4073 DT_DIR);
4074 if (over) 4113 if (over)
4075 return 0; 4114 return 0;
4076 filp->f_pos = 1; 4115 filp->f_pos = 1;
@@ -4085,11 +4124,19 @@ static int btrfs_real_readdir(struct file *filp, void *dirent,
4085 filp->f_pos = 2; 4124 filp->f_pos = 2;
4086 } 4125 }
4087 path = btrfs_alloc_path(); 4126 path = btrfs_alloc_path();
4127 if (!path)
4128 return -ENOMEM;
4088 path->reada = 2; 4129 path->reada = 2;
4089 4130
4131 if (key_type == BTRFS_DIR_INDEX_KEY) {
4132 INIT_LIST_HEAD(&ins_list);
4133 INIT_LIST_HEAD(&del_list);
4134 btrfs_get_delayed_items(inode, &ins_list, &del_list);
4135 }
4136
4090 btrfs_set_key_type(&key, key_type); 4137 btrfs_set_key_type(&key, key_type);
4091 key.offset = filp->f_pos; 4138 key.offset = filp->f_pos;
4092 key.objectid = inode->i_ino; 4139 key.objectid = btrfs_ino(inode);
4093 4140
4094 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0); 4141 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
4095 if (ret < 0) 4142 if (ret < 0)
@@ -4116,8 +4163,13 @@ static int btrfs_real_readdir(struct file *filp, void *dirent,
4116 break; 4163 break;
4117 if (found_key.offset < filp->f_pos) 4164 if (found_key.offset < filp->f_pos)
4118 goto next; 4165 goto next;
4166 if (key_type == BTRFS_DIR_INDEX_KEY &&
4167 btrfs_should_delete_dir_index(&del_list,
4168 found_key.offset))
4169 goto next;
4119 4170
4120 filp->f_pos = found_key.offset; 4171 filp->f_pos = found_key.offset;
4172 is_curr = 1;
4121 4173
4122 di = btrfs_item_ptr(leaf, slot, struct btrfs_dir_item); 4174 di = btrfs_item_ptr(leaf, slot, struct btrfs_dir_item);
4123 di_cur = 0; 4175 di_cur = 0;
@@ -4172,6 +4224,15 @@ next:
4172 path->slots[0]++; 4224 path->slots[0]++;
4173 } 4225 }
4174 4226
4227 if (key_type == BTRFS_DIR_INDEX_KEY) {
4228 if (is_curr)
4229 filp->f_pos++;
4230 ret = btrfs_readdir_delayed_dir_index(filp, dirent, filldir,
4231 &ins_list);
4232 if (ret)
4233 goto nopos;
4234 }
4235
4175 /* Reached end of directory/root. Bump pos past the last item. */ 4236 /* Reached end of directory/root. Bump pos past the last item. */
4176 if (key_type == BTRFS_DIR_INDEX_KEY) 4237 if (key_type == BTRFS_DIR_INDEX_KEY)
4177 /* 4238 /*
@@ -4184,6 +4245,8 @@ next:
4184nopos: 4245nopos:
4185 ret = 0; 4246 ret = 0;
4186err: 4247err:
4248 if (key_type == BTRFS_DIR_INDEX_KEY)
4249 btrfs_put_delayed_items(&ins_list, &del_list);
4187 btrfs_free_path(path); 4250 btrfs_free_path(path);
4188 return ret; 4251 return ret;
4189} 4252}
@@ -4199,7 +4262,8 @@ int btrfs_write_inode(struct inode *inode, struct writeback_control *wbc)
4199 return 0; 4262 return 0;
4200 4263
4201 smp_mb(); 4264 smp_mb();
4202 nolock = (root->fs_info->closing && root == root->fs_info->tree_root); 4265 if (root->fs_info->closing && is_free_space_inode(root, inode))
4266 nolock = true;
4203 4267
4204 if (wbc->sync_mode == WB_SYNC_ALL) { 4268 if (wbc->sync_mode == WB_SYNC_ALL) {
4205 if (nolock) 4269 if (nolock)
@@ -4243,8 +4307,9 @@ void btrfs_dirty_inode(struct inode *inode)
4243 trans = btrfs_start_transaction(root, 1); 4307 trans = btrfs_start_transaction(root, 1);
4244 if (IS_ERR(trans)) { 4308 if (IS_ERR(trans)) {
4245 printk_ratelimited(KERN_ERR "btrfs: fail to " 4309 printk_ratelimited(KERN_ERR "btrfs: fail to "
4246 "dirty inode %lu error %ld\n", 4310 "dirty inode %llu error %ld\n",
4247 inode->i_ino, PTR_ERR(trans)); 4311 (unsigned long long)btrfs_ino(inode),
4312 PTR_ERR(trans));
4248 return; 4313 return;
4249 } 4314 }
4250 btrfs_set_trans_block_group(trans, inode); 4315 btrfs_set_trans_block_group(trans, inode);
@@ -4252,11 +4317,14 @@ void btrfs_dirty_inode(struct inode *inode)
4252 ret = btrfs_update_inode(trans, root, inode); 4317 ret = btrfs_update_inode(trans, root, inode);
4253 if (ret) { 4318 if (ret) {
4254 printk_ratelimited(KERN_ERR "btrfs: fail to " 4319 printk_ratelimited(KERN_ERR "btrfs: fail to "
4255 "dirty inode %lu error %d\n", 4320 "dirty inode %llu error %d\n",
4256 inode->i_ino, ret); 4321 (unsigned long long)btrfs_ino(inode),
4322 ret);
4257 } 4323 }
4258 } 4324 }
4259 btrfs_end_transaction(trans, root); 4325 btrfs_end_transaction(trans, root);
4326 if (BTRFS_I(inode)->delayed_node)
4327 btrfs_balance_delayed_items(root);
4260} 4328}
4261 4329
4262/* 4330/*
@@ -4272,7 +4340,7 @@ static int btrfs_set_inode_index_count(struct inode *inode)
4272 struct extent_buffer *leaf; 4340 struct extent_buffer *leaf;
4273 int ret; 4341 int ret;
4274 4342
4275 key.objectid = inode->i_ino; 4343 key.objectid = btrfs_ino(inode);
4276 btrfs_set_key_type(&key, BTRFS_DIR_INDEX_KEY); 4344 btrfs_set_key_type(&key, BTRFS_DIR_INDEX_KEY);
4277 key.offset = (u64)-1; 4345 key.offset = (u64)-1;
4278 4346
@@ -4304,7 +4372,7 @@ static int btrfs_set_inode_index_count(struct inode *inode)
4304 leaf = path->nodes[0]; 4372 leaf = path->nodes[0];
4305 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]); 4373 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
4306 4374
4307 if (found_key.objectid != inode->i_ino || 4375 if (found_key.objectid != btrfs_ino(inode) ||
4308 btrfs_key_type(&found_key) != BTRFS_DIR_INDEX_KEY) { 4376 btrfs_key_type(&found_key) != BTRFS_DIR_INDEX_KEY) {
4309 BTRFS_I(inode)->index_cnt = 2; 4377 BTRFS_I(inode)->index_cnt = 2;
4310 goto out; 4378 goto out;
@@ -4325,9 +4393,12 @@ int btrfs_set_inode_index(struct inode *dir, u64 *index)
4325 int ret = 0; 4393 int ret = 0;
4326 4394
4327 if (BTRFS_I(dir)->index_cnt == (u64)-1) { 4395 if (BTRFS_I(dir)->index_cnt == (u64)-1) {
4328 ret = btrfs_set_inode_index_count(dir); 4396 ret = btrfs_inode_delayed_dir_index_count(dir);
4329 if (ret) 4397 if (ret) {
4330 return ret; 4398 ret = btrfs_set_inode_index_count(dir);
4399 if (ret)
4400 return ret;
4401 }
4331 } 4402 }
4332 4403
4333 *index = BTRFS_I(dir)->index_cnt; 4404 *index = BTRFS_I(dir)->index_cnt;
@@ -4363,6 +4434,12 @@ static struct inode *btrfs_new_inode(struct btrfs_trans_handle *trans,
4363 return ERR_PTR(-ENOMEM); 4434 return ERR_PTR(-ENOMEM);
4364 } 4435 }
4365 4436
4437 /*
4438 * we have to initialize this early, so we can reclaim the inode
4439 * number if we fail afterwards in this function.
4440 */
4441 inode->i_ino = objectid;
4442
4366 if (dir) { 4443 if (dir) {
4367 trace_btrfs_inode_request(dir); 4444 trace_btrfs_inode_request(dir);
4368 4445
@@ -4408,7 +4485,6 @@ static struct inode *btrfs_new_inode(struct btrfs_trans_handle *trans,
4408 goto fail; 4485 goto fail;
4409 4486
4410 inode_init_owner(inode, dir, mode); 4487 inode_init_owner(inode, dir, mode);
4411 inode->i_ino = objectid;
4412 inode_set_bytes(inode, 0); 4488 inode_set_bytes(inode, 0);
4413 inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME; 4489 inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME;
4414 inode_item = btrfs_item_ptr(path->nodes[0], path->slots[0], 4490 inode_item = btrfs_item_ptr(path->nodes[0], path->slots[0],
@@ -4472,29 +4548,29 @@ int btrfs_add_link(struct btrfs_trans_handle *trans,
4472 int ret = 0; 4548 int ret = 0;
4473 struct btrfs_key key; 4549 struct btrfs_key key;
4474 struct btrfs_root *root = BTRFS_I(parent_inode)->root; 4550 struct btrfs_root *root = BTRFS_I(parent_inode)->root;
4551 u64 ino = btrfs_ino(inode);
4552 u64 parent_ino = btrfs_ino(parent_inode);
4475 4553
4476 if (unlikely(inode->i_ino == BTRFS_FIRST_FREE_OBJECTID)) { 4554 if (unlikely(ino == BTRFS_FIRST_FREE_OBJECTID)) {
4477 memcpy(&key, &BTRFS_I(inode)->root->root_key, sizeof(key)); 4555 memcpy(&key, &BTRFS_I(inode)->root->root_key, sizeof(key));
4478 } else { 4556 } else {
4479 key.objectid = inode->i_ino; 4557 key.objectid = ino;
4480 btrfs_set_key_type(&key, BTRFS_INODE_ITEM_KEY); 4558 btrfs_set_key_type(&key, BTRFS_INODE_ITEM_KEY);
4481 key.offset = 0; 4559 key.offset = 0;
4482 } 4560 }
4483 4561
4484 if (unlikely(inode->i_ino == BTRFS_FIRST_FREE_OBJECTID)) { 4562 if (unlikely(ino == BTRFS_FIRST_FREE_OBJECTID)) {
4485 ret = btrfs_add_root_ref(trans, root->fs_info->tree_root, 4563 ret = btrfs_add_root_ref(trans, root->fs_info->tree_root,
4486 key.objectid, root->root_key.objectid, 4564 key.objectid, root->root_key.objectid,
4487 parent_inode->i_ino, 4565 parent_ino, index, name, name_len);
4488 index, name, name_len);
4489 } else if (add_backref) { 4566 } else if (add_backref) {
4490 ret = btrfs_insert_inode_ref(trans, root, 4567 ret = btrfs_insert_inode_ref(trans, root, name, name_len, ino,
4491 name, name_len, inode->i_ino, 4568 parent_ino, index);
4492 parent_inode->i_ino, index);
4493 } 4569 }
4494 4570
4495 if (ret == 0) { 4571 if (ret == 0) {
4496 ret = btrfs_insert_dir_item(trans, root, name, name_len, 4572 ret = btrfs_insert_dir_item(trans, root, name, name_len,
4497 parent_inode->i_ino, &key, 4573 parent_inode, &key,
4498 btrfs_inode_type(inode), index); 4574 btrfs_inode_type(inode), index);
4499 BUG_ON(ret); 4575 BUG_ON(ret);
4500 4576
@@ -4537,10 +4613,6 @@ static int btrfs_mknod(struct inode *dir, struct dentry *dentry,
4537 if (!new_valid_dev(rdev)) 4613 if (!new_valid_dev(rdev))
4538 return -EINVAL; 4614 return -EINVAL;
4539 4615
4540 err = btrfs_find_free_objectid(NULL, root, dir->i_ino, &objectid);
4541 if (err)
4542 return err;
4543
4544 /* 4616 /*
4545 * 2 for inode item and ref 4617 * 2 for inode item and ref
4546 * 2 for dir items 4618 * 2 for dir items
@@ -4552,8 +4624,12 @@ static int btrfs_mknod(struct inode *dir, struct dentry *dentry,
4552 4624
4553 btrfs_set_trans_block_group(trans, dir); 4625 btrfs_set_trans_block_group(trans, dir);
4554 4626
4627 err = btrfs_find_free_ino(root, &objectid);
4628 if (err)
4629 goto out_unlock;
4630
4555 inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name, 4631 inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name,
4556 dentry->d_name.len, dir->i_ino, objectid, 4632 dentry->d_name.len, btrfs_ino(dir), objectid,
4557 BTRFS_I(dir)->block_group, mode, &index); 4633 BTRFS_I(dir)->block_group, mode, &index);
4558 if (IS_ERR(inode)) { 4634 if (IS_ERR(inode)) {
4559 err = PTR_ERR(inode); 4635 err = PTR_ERR(inode);
@@ -4600,9 +4676,6 @@ static int btrfs_create(struct inode *dir, struct dentry *dentry,
4600 u64 objectid; 4676 u64 objectid;
4601 u64 index = 0; 4677 u64 index = 0;
4602 4678
4603 err = btrfs_find_free_objectid(NULL, root, dir->i_ino, &objectid);
4604 if (err)
4605 return err;
4606 /* 4679 /*
4607 * 2 for inode item and ref 4680 * 2 for inode item and ref
4608 * 2 for dir items 4681 * 2 for dir items
@@ -4614,8 +4687,12 @@ static int btrfs_create(struct inode *dir, struct dentry *dentry,
4614 4687
4615 btrfs_set_trans_block_group(trans, dir); 4688 btrfs_set_trans_block_group(trans, dir);
4616 4689
4690 err = btrfs_find_free_ino(root, &objectid);
4691 if (err)
4692 goto out_unlock;
4693
4617 inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name, 4694 inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name,
4618 dentry->d_name.len, dir->i_ino, objectid, 4695 dentry->d_name.len, btrfs_ino(dir), objectid,
4619 BTRFS_I(dir)->block_group, mode, &index); 4696 BTRFS_I(dir)->block_group, mode, &index);
4620 if (IS_ERR(inode)) { 4697 if (IS_ERR(inode)) {
4621 err = PTR_ERR(inode); 4698 err = PTR_ERR(inode);
@@ -4726,10 +4803,6 @@ static int btrfs_mkdir(struct inode *dir, struct dentry *dentry, int mode)
4726 u64 index = 0; 4803 u64 index = 0;
4727 unsigned long nr = 1; 4804 unsigned long nr = 1;
4728 4805
4729 err = btrfs_find_free_objectid(NULL, root, dir->i_ino, &objectid);
4730 if (err)
4731 return err;
4732
4733 /* 4806 /*
4734 * 2 items for inode and ref 4807 * 2 items for inode and ref
4735 * 2 items for dir items 4808 * 2 items for dir items
@@ -4740,8 +4813,12 @@ static int btrfs_mkdir(struct inode *dir, struct dentry *dentry, int mode)
4740 return PTR_ERR(trans); 4813 return PTR_ERR(trans);
4741 btrfs_set_trans_block_group(trans, dir); 4814 btrfs_set_trans_block_group(trans, dir);
4742 4815
4816 err = btrfs_find_free_ino(root, &objectid);
4817 if (err)
4818 goto out_fail;
4819
4743 inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name, 4820 inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name,
4744 dentry->d_name.len, dir->i_ino, objectid, 4821 dentry->d_name.len, btrfs_ino(dir), objectid,
4745 BTRFS_I(dir)->block_group, S_IFDIR | mode, 4822 BTRFS_I(dir)->block_group, S_IFDIR | mode,
4746 &index); 4823 &index);
4747 if (IS_ERR(inode)) { 4824 if (IS_ERR(inode)) {
@@ -4864,7 +4941,7 @@ struct extent_map *btrfs_get_extent(struct inode *inode, struct page *page,
4864 u64 bytenr; 4941 u64 bytenr;
4865 u64 extent_start = 0; 4942 u64 extent_start = 0;
4866 u64 extent_end = 0; 4943 u64 extent_end = 0;
4867 u64 objectid = inode->i_ino; 4944 u64 objectid = btrfs_ino(inode);
4868 u32 found_type; 4945 u32 found_type;
4869 struct btrfs_path *path = NULL; 4946 struct btrfs_path *path = NULL;
4870 struct btrfs_root *root = BTRFS_I(inode)->root; 4947 struct btrfs_root *root = BTRFS_I(inode)->root;
@@ -5372,7 +5449,7 @@ static noinline int can_nocow_odirect(struct btrfs_trans_handle *trans,
5372 if (!path) 5449 if (!path)
5373 return -ENOMEM; 5450 return -ENOMEM;
5374 5451
5375 ret = btrfs_lookup_file_extent(trans, root, path, inode->i_ino, 5452 ret = btrfs_lookup_file_extent(trans, root, path, btrfs_ino(inode),
5376 offset, 0); 5453 offset, 0);
5377 if (ret < 0) 5454 if (ret < 0)
5378 goto out; 5455 goto out;
@@ -5389,7 +5466,7 @@ static noinline int can_nocow_odirect(struct btrfs_trans_handle *trans,
5389 ret = 0; 5466 ret = 0;
5390 leaf = path->nodes[0]; 5467 leaf = path->nodes[0];
5391 btrfs_item_key_to_cpu(leaf, &key, slot); 5468 btrfs_item_key_to_cpu(leaf, &key, slot);
5392 if (key.objectid != inode->i_ino || 5469 if (key.objectid != btrfs_ino(inode) ||
5393 key.type != BTRFS_EXTENT_DATA_KEY) { 5470 key.type != BTRFS_EXTENT_DATA_KEY) {
5394 /* not our file or wrong item type, must cow */ 5471 /* not our file or wrong item type, must cow */
5395 goto out; 5472 goto out;
@@ -5423,7 +5500,7 @@ static noinline int can_nocow_odirect(struct btrfs_trans_handle *trans,
5423 * look for other files referencing this extent, if we 5500 * look for other files referencing this extent, if we
5424 * find any we must cow 5501 * find any we must cow
5425 */ 5502 */
5426 if (btrfs_cross_ref_exist(trans, root, inode->i_ino, 5503 if (btrfs_cross_ref_exist(trans, root, btrfs_ino(inode),
5427 key.offset - backref_offset, disk_bytenr)) 5504 key.offset - backref_offset, disk_bytenr))
5428 goto out; 5505 goto out;
5429 5506
@@ -5613,9 +5690,10 @@ static void btrfs_endio_direct_read(struct bio *bio, int err)
5613 5690
5614 flush_dcache_page(bvec->bv_page); 5691 flush_dcache_page(bvec->bv_page);
5615 if (csum != *private) { 5692 if (csum != *private) {
5616 printk(KERN_ERR "btrfs csum failed ino %lu off" 5693 printk(KERN_ERR "btrfs csum failed ino %llu off"
5617 " %llu csum %u private %u\n", 5694 " %llu csum %u private %u\n",
5618 inode->i_ino, (unsigned long long)start, 5695 (unsigned long long)btrfs_ino(inode),
5696 (unsigned long long)start,
5619 csum, *private); 5697 csum, *private);
5620 err = -EIO; 5698 err = -EIO;
5621 } 5699 }
@@ -5762,9 +5840,9 @@ static void btrfs_end_dio_bio(struct bio *bio, int err)
5762 struct btrfs_dio_private *dip = bio->bi_private; 5840 struct btrfs_dio_private *dip = bio->bi_private;
5763 5841
5764 if (err) { 5842 if (err) {
5765 printk(KERN_ERR "btrfs direct IO failed ino %lu rw %lu " 5843 printk(KERN_ERR "btrfs direct IO failed ino %llu rw %lu "
5766 "sector %#Lx len %u err no %d\n", 5844 "sector %#Lx len %u err no %d\n",
5767 dip->inode->i_ino, bio->bi_rw, 5845 (unsigned long long)btrfs_ino(dip->inode), bio->bi_rw,
5768 (unsigned long long)bio->bi_sector, bio->bi_size, err); 5846 (unsigned long long)bio->bi_sector, bio->bi_size, err);
5769 dip->errors = 1; 5847 dip->errors = 1;
5770 5848
@@ -6607,6 +6685,8 @@ struct inode *btrfs_alloc_inode(struct super_block *sb)
6607 ei->dummy_inode = 0; 6685 ei->dummy_inode = 0;
6608 ei->force_compress = BTRFS_COMPRESS_NONE; 6686 ei->force_compress = BTRFS_COMPRESS_NONE;
6609 6687
6688 ei->delayed_node = NULL;
6689
6610 inode = &ei->vfs_inode; 6690 inode = &ei->vfs_inode;
6611 extent_map_tree_init(&ei->extent_tree); 6691 extent_map_tree_init(&ei->extent_tree);
6612 extent_io_tree_init(&ei->io_tree, &inode->i_data); 6692 extent_io_tree_init(&ei->io_tree, &inode->i_data);
@@ -6674,8 +6754,8 @@ void btrfs_destroy_inode(struct inode *inode)
6674 6754
6675 spin_lock(&root->orphan_lock); 6755 spin_lock(&root->orphan_lock);
6676 if (!list_empty(&BTRFS_I(inode)->i_orphan)) { 6756 if (!list_empty(&BTRFS_I(inode)->i_orphan)) {
6677 printk(KERN_INFO "BTRFS: inode %lu still on the orphan list\n", 6757 printk(KERN_INFO "BTRFS: inode %llu still on the orphan list\n",
6678 inode->i_ino); 6758 (unsigned long long)btrfs_ino(inode));
6679 list_del_init(&BTRFS_I(inode)->i_orphan); 6759 list_del_init(&BTRFS_I(inode)->i_orphan);
6680 } 6760 }
6681 spin_unlock(&root->orphan_lock); 6761 spin_unlock(&root->orphan_lock);
@@ -6697,6 +6777,7 @@ void btrfs_destroy_inode(struct inode *inode)
6697 inode_tree_del(inode); 6777 inode_tree_del(inode);
6698 btrfs_drop_extent_cache(inode, 0, (u64)-1, 0); 6778 btrfs_drop_extent_cache(inode, 0, (u64)-1, 0);
6699free: 6779free:
6780 btrfs_remove_delayed_node(inode);
6700 call_rcu(&inode->i_rcu, btrfs_i_callback); 6781 call_rcu(&inode->i_rcu, btrfs_i_callback);
6701} 6782}
6702 6783
@@ -6705,7 +6786,7 @@ int btrfs_drop_inode(struct inode *inode)
6705 struct btrfs_root *root = BTRFS_I(inode)->root; 6786 struct btrfs_root *root = BTRFS_I(inode)->root;
6706 6787
6707 if (btrfs_root_refs(&root->root_item) == 0 && 6788 if (btrfs_root_refs(&root->root_item) == 0 &&
6708 root != root->fs_info->tree_root) 6789 !is_free_space_inode(root, inode))
6709 return 1; 6790 return 1;
6710 else 6791 else
6711 return generic_drop_inode(inode); 6792 return generic_drop_inode(inode);
@@ -6808,38 +6889,39 @@ static int btrfs_rename(struct inode *old_dir, struct dentry *old_dentry,
6808 struct btrfs_trans_handle *trans; 6889 struct btrfs_trans_handle *trans;
6809 struct btrfs_root *root = BTRFS_I(old_dir)->root; 6890 struct btrfs_root *root = BTRFS_I(old_dir)->root;
6810 struct btrfs_root *dest = BTRFS_I(new_dir)->root; 6891 struct btrfs_root *dest = BTRFS_I(new_dir)->root;
6811 struct inode *newinode = new_dentry->d_inode; 6892 struct inode *new_inode = new_dentry->d_inode;
6812 struct inode *old_inode = old_dentry->d_inode; 6893 struct inode *old_inode = old_dentry->d_inode;
6813 struct timespec ctime = CURRENT_TIME; 6894 struct timespec ctime = CURRENT_TIME;
6814 u64 index = 0; 6895 u64 index = 0;
6815 u64 root_objectid; 6896 u64 root_objectid;
6816 int ret; 6897 int ret;
6898 u64 old_ino = btrfs_ino(old_inode);
6817 6899
6818 if (new_dir->i_ino == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID) 6900 if (btrfs_ino(new_dir) == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID)
6819 return -EPERM; 6901 return -EPERM;
6820 6902
6821 /* we only allow rename subvolume link between subvolumes */ 6903 /* we only allow rename subvolume link between subvolumes */
6822 if (old_inode->i_ino != BTRFS_FIRST_FREE_OBJECTID && root != dest) 6904 if (old_ino != BTRFS_FIRST_FREE_OBJECTID && root != dest)
6823 return -EXDEV; 6905 return -EXDEV;
6824 6906
6825 if (old_inode->i_ino == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID || 6907 if (old_ino == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID ||
6826 (newinode && newinode->i_ino == BTRFS_FIRST_FREE_OBJECTID)) 6908 (new_inode && btrfs_ino(new_inode) == BTRFS_FIRST_FREE_OBJECTID))
6827 return -ENOTEMPTY; 6909 return -ENOTEMPTY;
6828 6910
6829 if (S_ISDIR(old_inode->i_mode) && newinode && 6911 if (S_ISDIR(old_inode->i_mode) && new_inode &&
6830 newinode->i_size > BTRFS_EMPTY_DIR_SIZE) 6912 new_inode->i_size > BTRFS_EMPTY_DIR_SIZE)
6831 return -ENOTEMPTY; 6913 return -ENOTEMPTY;
6832 /* 6914 /*
6833 * we're using rename to replace one file with another. 6915 * we're using rename to replace one file with another.
6834 * and the replacement file is large. Start IO on it now so 6916 * and the replacement file is large. Start IO on it now so
6835 * we don't add too much work to the end of the transaction 6917 * we don't add too much work to the end of the transaction
6836 */ 6918 */
6837 if (newinode && S_ISREG(old_inode->i_mode) && newinode->i_size && 6919 if (new_inode && S_ISREG(old_inode->i_mode) && new_inode->i_size &&
6838 old_inode->i_size > BTRFS_ORDERED_OPERATIONS_FLUSH_LIMIT) 6920 old_inode->i_size > BTRFS_ORDERED_OPERATIONS_FLUSH_LIMIT)
6839 filemap_flush(old_inode->i_mapping); 6921 filemap_flush(old_inode->i_mapping);
6840 6922
6841 /* close the racy window with snapshot create/destroy ioctl */ 6923 /* close the racy window with snapshot create/destroy ioctl */
6842 if (old_inode->i_ino == BTRFS_FIRST_FREE_OBJECTID) 6924 if (old_ino == BTRFS_FIRST_FREE_OBJECTID)
6843 down_read(&root->fs_info->subvol_sem); 6925 down_read(&root->fs_info->subvol_sem);
6844 /* 6926 /*
6845 * We want to reserve the absolute worst case amount of items. So if 6927 * We want to reserve the absolute worst case amount of items. So if
@@ -6864,15 +6946,15 @@ static int btrfs_rename(struct inode *old_dir, struct dentry *old_dentry,
6864 if (ret) 6946 if (ret)
6865 goto out_fail; 6947 goto out_fail;
6866 6948
6867 if (unlikely(old_inode->i_ino == BTRFS_FIRST_FREE_OBJECTID)) { 6949 if (unlikely(old_ino == BTRFS_FIRST_FREE_OBJECTID)) {
6868 /* force full log commit if subvolume involved. */ 6950 /* force full log commit if subvolume involved. */
6869 root->fs_info->last_trans_log_full_commit = trans->transid; 6951 root->fs_info->last_trans_log_full_commit = trans->transid;
6870 } else { 6952 } else {
6871 ret = btrfs_insert_inode_ref(trans, dest, 6953 ret = btrfs_insert_inode_ref(trans, dest,
6872 new_dentry->d_name.name, 6954 new_dentry->d_name.name,
6873 new_dentry->d_name.len, 6955 new_dentry->d_name.len,
6874 old_inode->i_ino, 6956 old_ino,
6875 new_dir->i_ino, index); 6957 btrfs_ino(new_dir), index);
6876 if (ret) 6958 if (ret)
6877 goto out_fail; 6959 goto out_fail;
6878 /* 6960 /*
@@ -6888,10 +6970,8 @@ static int btrfs_rename(struct inode *old_dir, struct dentry *old_dentry,
6888 * make sure the inode gets flushed if it is replacing 6970 * make sure the inode gets flushed if it is replacing
6889 * something. 6971 * something.
6890 */ 6972 */
6891 if (newinode && newinode->i_size && 6973 if (new_inode && new_inode->i_size && S_ISREG(old_inode->i_mode))
6892 old_inode && S_ISREG(old_inode->i_mode)) {
6893 btrfs_add_ordered_operation(trans, root, old_inode); 6974 btrfs_add_ordered_operation(trans, root, old_inode);
6894 }
6895 6975
6896 old_dir->i_ctime = old_dir->i_mtime = ctime; 6976 old_dir->i_ctime = old_dir->i_mtime = ctime;
6897 new_dir->i_ctime = new_dir->i_mtime = ctime; 6977 new_dir->i_ctime = new_dir->i_mtime = ctime;
@@ -6900,7 +6980,7 @@ static int btrfs_rename(struct inode *old_dir, struct dentry *old_dentry,
6900 if (old_dentry->d_parent != new_dentry->d_parent) 6980 if (old_dentry->d_parent != new_dentry->d_parent)
6901 btrfs_record_unlink_dir(trans, old_dir, old_inode, 1); 6981 btrfs_record_unlink_dir(trans, old_dir, old_inode, 1);
6902 6982
6903 if (unlikely(old_inode->i_ino == BTRFS_FIRST_FREE_OBJECTID)) { 6983 if (unlikely(old_ino == BTRFS_FIRST_FREE_OBJECTID)) {
6904 root_objectid = BTRFS_I(old_inode)->root->root_key.objectid; 6984 root_objectid = BTRFS_I(old_inode)->root->root_key.objectid;
6905 ret = btrfs_unlink_subvol(trans, root, old_dir, root_objectid, 6985 ret = btrfs_unlink_subvol(trans, root, old_dir, root_objectid,
6906 old_dentry->d_name.name, 6986 old_dentry->d_name.name,
@@ -6915,16 +6995,16 @@ static int btrfs_rename(struct inode *old_dir, struct dentry *old_dentry,
6915 } 6995 }
6916 BUG_ON(ret); 6996 BUG_ON(ret);
6917 6997
6918 if (newinode) { 6998 if (new_inode) {
6919 newinode->i_ctime = CURRENT_TIME; 6999 new_inode->i_ctime = CURRENT_TIME;
6920 if (unlikely(newinode->i_ino == 7000 if (unlikely(btrfs_ino(new_inode) ==
6921 BTRFS_EMPTY_SUBVOL_DIR_OBJECTID)) { 7001 BTRFS_EMPTY_SUBVOL_DIR_OBJECTID)) {
6922 root_objectid = BTRFS_I(newinode)->location.objectid; 7002 root_objectid = BTRFS_I(new_inode)->location.objectid;
6923 ret = btrfs_unlink_subvol(trans, dest, new_dir, 7003 ret = btrfs_unlink_subvol(trans, dest, new_dir,
6924 root_objectid, 7004 root_objectid,
6925 new_dentry->d_name.name, 7005 new_dentry->d_name.name,
6926 new_dentry->d_name.len); 7006 new_dentry->d_name.len);
6927 BUG_ON(newinode->i_nlink == 0); 7007 BUG_ON(new_inode->i_nlink == 0);
6928 } else { 7008 } else {
6929 ret = btrfs_unlink_inode(trans, dest, new_dir, 7009 ret = btrfs_unlink_inode(trans, dest, new_dir,
6930 new_dentry->d_inode, 7010 new_dentry->d_inode,
@@ -6932,7 +7012,7 @@ static int btrfs_rename(struct inode *old_dir, struct dentry *old_dentry,
6932 new_dentry->d_name.len); 7012 new_dentry->d_name.len);
6933 } 7013 }
6934 BUG_ON(ret); 7014 BUG_ON(ret);
6935 if (newinode->i_nlink == 0) { 7015 if (new_inode->i_nlink == 0) {
6936 ret = btrfs_orphan_add(trans, new_dentry->d_inode); 7016 ret = btrfs_orphan_add(trans, new_dentry->d_inode);
6937 BUG_ON(ret); 7017 BUG_ON(ret);
6938 } 7018 }
@@ -6945,7 +7025,7 @@ static int btrfs_rename(struct inode *old_dir, struct dentry *old_dentry,
6945 new_dentry->d_name.len, 0, index); 7025 new_dentry->d_name.len, 0, index);
6946 BUG_ON(ret); 7026 BUG_ON(ret);
6947 7027
6948 if (old_inode->i_ino != BTRFS_FIRST_FREE_OBJECTID) { 7028 if (old_ino != BTRFS_FIRST_FREE_OBJECTID) {
6949 struct dentry *parent = dget_parent(new_dentry); 7029 struct dentry *parent = dget_parent(new_dentry);
6950 btrfs_log_new_name(trans, old_inode, old_dir, parent); 7030 btrfs_log_new_name(trans, old_inode, old_dir, parent);
6951 dput(parent); 7031 dput(parent);
@@ -6954,7 +7034,7 @@ static int btrfs_rename(struct inode *old_dir, struct dentry *old_dentry,
6954out_fail: 7034out_fail:
6955 btrfs_end_transaction_throttle(trans, root); 7035 btrfs_end_transaction_throttle(trans, root);
6956out_notrans: 7036out_notrans:
6957 if (old_inode->i_ino == BTRFS_FIRST_FREE_OBJECTID) 7037 if (old_ino == BTRFS_FIRST_FREE_OBJECTID)
6958 up_read(&root->fs_info->subvol_sem); 7038 up_read(&root->fs_info->subvol_sem);
6959 7039
6960 return ret; 7040 return ret;
@@ -7031,9 +7111,6 @@ static int btrfs_symlink(struct inode *dir, struct dentry *dentry,
7031 if (name_len > BTRFS_MAX_INLINE_DATA_SIZE(root)) 7111 if (name_len > BTRFS_MAX_INLINE_DATA_SIZE(root))
7032 return -ENAMETOOLONG; 7112 return -ENAMETOOLONG;
7033 7113
7034 err = btrfs_find_free_objectid(NULL, root, dir->i_ino, &objectid);
7035 if (err)
7036 return err;
7037 /* 7114 /*
7038 * 2 items for inode item and ref 7115 * 2 items for inode item and ref
7039 * 2 items for dir items 7116 * 2 items for dir items
@@ -7045,8 +7122,12 @@ static int btrfs_symlink(struct inode *dir, struct dentry *dentry,
7045 7122
7046 btrfs_set_trans_block_group(trans, dir); 7123 btrfs_set_trans_block_group(trans, dir);
7047 7124
7125 err = btrfs_find_free_ino(root, &objectid);
7126 if (err)
7127 goto out_unlock;
7128
7048 inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name, 7129 inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name,
7049 dentry->d_name.len, dir->i_ino, objectid, 7130 dentry->d_name.len, btrfs_ino(dir), objectid,
7050 BTRFS_I(dir)->block_group, S_IFLNK|S_IRWXUGO, 7131 BTRFS_I(dir)->block_group, S_IFLNK|S_IRWXUGO,
7051 &index); 7132 &index);
7052 if (IS_ERR(inode)) { 7133 if (IS_ERR(inode)) {
@@ -7078,7 +7159,7 @@ static int btrfs_symlink(struct inode *dir, struct dentry *dentry,
7078 7159
7079 path = btrfs_alloc_path(); 7160 path = btrfs_alloc_path();
7080 BUG_ON(!path); 7161 BUG_ON(!path);
7081 key.objectid = inode->i_ino; 7162 key.objectid = btrfs_ino(inode);
7082 key.offset = 0; 7163 key.offset = 0;
7083 btrfs_set_key_type(&key, BTRFS_EXTENT_DATA_KEY); 7164 btrfs_set_key_type(&key, BTRFS_EXTENT_DATA_KEY);
7084 datasize = btrfs_file_extent_calc_inline_size(name_len); 7165 datasize = btrfs_file_extent_calc_inline_size(name_len);