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.c282
1 files changed, 157 insertions, 125 deletions
diff --git a/fs/btrfs/inode.c b/fs/btrfs/inode.c
index 7cd8ab0ef04d..01438e9ba2e2 100644
--- a/fs/btrfs/inode.c
+++ b/fs/btrfs/inode.c
@@ -51,6 +51,7 @@
51#include "compression.h" 51#include "compression.h"
52#include "locking.h" 52#include "locking.h"
53#include "free-space-cache.h" 53#include "free-space-cache.h"
54#include "inode-map.h"
54 55
55struct btrfs_iget_args { 56struct btrfs_iget_args {
56 u64 ino; 57 u64 ino;
@@ -138,7 +139,7 @@ static noinline int insert_inline_extent(struct btrfs_trans_handle *trans,
138 path->leave_spinning = 1; 139 path->leave_spinning = 1;
139 btrfs_set_trans_block_group(trans, inode); 140 btrfs_set_trans_block_group(trans, inode);
140 141
141 key.objectid = inode->i_ino; 142 key.objectid = btrfs_ino(inode);
142 key.offset = start; 143 key.offset = start;
143 btrfs_set_key_type(&key, BTRFS_EXTENT_DATA_KEY); 144 btrfs_set_key_type(&key, BTRFS_EXTENT_DATA_KEY);
144 datasize = btrfs_file_extent_calc_inline_size(cur_size); 145 datasize = btrfs_file_extent_calc_inline_size(cur_size);
@@ -745,6 +746,15 @@ static u64 get_extent_allocation_hint(struct inode *inode, u64 start,
745 return alloc_hint; 746 return alloc_hint;
746} 747}
747 748
749static inline bool is_free_space_inode(struct btrfs_root *root,
750 struct inode *inode)
751{
752 if (root == root->fs_info->tree_root ||
753 BTRFS_I(inode)->location.objectid == BTRFS_FREE_INO_OBJECTID)
754 return true;
755 return false;
756}
757
748/* 758/*
749 * when extent_io.c finds a delayed allocation range in the file, 759 * when extent_io.c finds a delayed allocation range in the file,
750 * the call backs end up in this code. The basic idea is to 760 * the call backs end up in this code. The basic idea is to
@@ -777,7 +787,7 @@ static noinline int cow_file_range(struct inode *inode,
777 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree; 787 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
778 int ret = 0; 788 int ret = 0;
779 789
780 BUG_ON(root == root->fs_info->tree_root); 790 BUG_ON(is_free_space_inode(root, inode));
781 trans = btrfs_join_transaction(root, 1); 791 trans = btrfs_join_transaction(root, 1);
782 BUG_ON(IS_ERR(trans)); 792 BUG_ON(IS_ERR(trans));
783 btrfs_set_trans_block_group(trans, inode); 793 btrfs_set_trans_block_group(trans, inode);
@@ -1049,29 +1059,31 @@ static noinline int run_delalloc_nocow(struct inode *inode,
1049 int type; 1059 int type;
1050 int nocow; 1060 int nocow;
1051 int check_prev = 1; 1061 int check_prev = 1;
1052 bool nolock = false; 1062 bool nolock;
1063 u64 ino = btrfs_ino(inode);
1053 1064
1054 path = btrfs_alloc_path(); 1065 path = btrfs_alloc_path();
1055 BUG_ON(!path); 1066 BUG_ON(!path);
1056 if (root == root->fs_info->tree_root) { 1067
1057 nolock = true; 1068 nolock = is_free_space_inode(root, inode);
1069
1070 if (nolock)
1058 trans = btrfs_join_transaction_nolock(root, 1); 1071 trans = btrfs_join_transaction_nolock(root, 1);
1059 } else { 1072 else
1060 trans = btrfs_join_transaction(root, 1); 1073 trans = btrfs_join_transaction(root, 1);
1061 }
1062 BUG_ON(IS_ERR(trans)); 1074 BUG_ON(IS_ERR(trans));
1063 1075
1064 cow_start = (u64)-1; 1076 cow_start = (u64)-1;
1065 cur_offset = start; 1077 cur_offset = start;
1066 while (1) { 1078 while (1) {
1067 ret = btrfs_lookup_file_extent(trans, root, path, inode->i_ino, 1079 ret = btrfs_lookup_file_extent(trans, root, path, ino,
1068 cur_offset, 0); 1080 cur_offset, 0);
1069 BUG_ON(ret < 0); 1081 BUG_ON(ret < 0);
1070 if (ret > 0 && path->slots[0] > 0 && check_prev) { 1082 if (ret > 0 && path->slots[0] > 0 && check_prev) {
1071 leaf = path->nodes[0]; 1083 leaf = path->nodes[0];
1072 btrfs_item_key_to_cpu(leaf, &found_key, 1084 btrfs_item_key_to_cpu(leaf, &found_key,
1073 path->slots[0] - 1); 1085 path->slots[0] - 1);
1074 if (found_key.objectid == inode->i_ino && 1086 if (found_key.objectid == ino &&
1075 found_key.type == BTRFS_EXTENT_DATA_KEY) 1087 found_key.type == BTRFS_EXTENT_DATA_KEY)
1076 path->slots[0]--; 1088 path->slots[0]--;
1077 } 1089 }
@@ -1092,7 +1104,7 @@ next_slot:
1092 num_bytes = 0; 1104 num_bytes = 0;
1093 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]); 1105 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
1094 1106
1095 if (found_key.objectid > inode->i_ino || 1107 if (found_key.objectid > ino ||
1096 found_key.type > BTRFS_EXTENT_DATA_KEY || 1108 found_key.type > BTRFS_EXTENT_DATA_KEY ||
1097 found_key.offset > end) 1109 found_key.offset > end)
1098 break; 1110 break;
@@ -1127,7 +1139,7 @@ next_slot:
1127 goto out_check; 1139 goto out_check;
1128 if (btrfs_extent_readonly(root, disk_bytenr)) 1140 if (btrfs_extent_readonly(root, disk_bytenr))
1129 goto out_check; 1141 goto out_check;
1130 if (btrfs_cross_ref_exist(trans, root, inode->i_ino, 1142 if (btrfs_cross_ref_exist(trans, root, ino,
1131 found_key.offset - 1143 found_key.offset -
1132 extent_offset, disk_bytenr)) 1144 extent_offset, disk_bytenr))
1133 goto out_check; 1145 goto out_check;
@@ -1316,8 +1328,7 @@ static int btrfs_set_bit_hook(struct inode *inode,
1316 if (!(state->state & EXTENT_DELALLOC) && (*bits & EXTENT_DELALLOC)) { 1328 if (!(state->state & EXTENT_DELALLOC) && (*bits & EXTENT_DELALLOC)) {
1317 struct btrfs_root *root = BTRFS_I(inode)->root; 1329 struct btrfs_root *root = BTRFS_I(inode)->root;
1318 u64 len = state->end + 1 - state->start; 1330 u64 len = state->end + 1 - state->start;
1319 int do_list = (root->root_key.objectid != 1331 bool do_list = !is_free_space_inode(root, inode);
1320 BTRFS_ROOT_TREE_OBJECTID);
1321 1332
1322 if (*bits & EXTENT_FIRST_DELALLOC) 1333 if (*bits & EXTENT_FIRST_DELALLOC)
1323 *bits &= ~EXTENT_FIRST_DELALLOC; 1334 *bits &= ~EXTENT_FIRST_DELALLOC;
@@ -1350,8 +1361,7 @@ static int btrfs_clear_bit_hook(struct inode *inode,
1350 if ((state->state & EXTENT_DELALLOC) && (*bits & EXTENT_DELALLOC)) { 1361 if ((state->state & EXTENT_DELALLOC) && (*bits & EXTENT_DELALLOC)) {
1351 struct btrfs_root *root = BTRFS_I(inode)->root; 1362 struct btrfs_root *root = BTRFS_I(inode)->root;
1352 u64 len = state->end + 1 - state->start; 1363 u64 len = state->end + 1 - state->start;
1353 int do_list = (root->root_key.objectid != 1364 bool do_list = !is_free_space_inode(root, inode);
1354 BTRFS_ROOT_TREE_OBJECTID);
1355 1365
1356 if (*bits & EXTENT_FIRST_DELALLOC) 1366 if (*bits & EXTENT_FIRST_DELALLOC)
1357 *bits &= ~EXTENT_FIRST_DELALLOC; 1367 *bits &= ~EXTENT_FIRST_DELALLOC;
@@ -1458,7 +1468,7 @@ static int btrfs_submit_bio_hook(struct inode *inode, int rw, struct bio *bio,
1458 1468
1459 skip_sum = BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM; 1469 skip_sum = BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM;
1460 1470
1461 if (root == root->fs_info->tree_root) 1471 if (is_free_space_inode(root, inode))
1462 ret = btrfs_bio_wq_end_io(root->fs_info, bio, 2); 1472 ret = btrfs_bio_wq_end_io(root->fs_info, bio, 2);
1463 else 1473 else
1464 ret = btrfs_bio_wq_end_io(root->fs_info, bio, 0); 1474 ret = btrfs_bio_wq_end_io(root->fs_info, bio, 0);
@@ -1644,7 +1654,7 @@ static int insert_reserved_file_extent(struct btrfs_trans_handle *trans,
1644 &hint, 0); 1654 &hint, 0);
1645 BUG_ON(ret); 1655 BUG_ON(ret);
1646 1656
1647 ins.objectid = inode->i_ino; 1657 ins.objectid = btrfs_ino(inode);
1648 ins.offset = file_pos; 1658 ins.offset = file_pos;
1649 ins.type = BTRFS_EXTENT_DATA_KEY; 1659 ins.type = BTRFS_EXTENT_DATA_KEY;
1650 ret = btrfs_insert_empty_item(trans, root, path, &ins, sizeof(*fi)); 1660 ret = btrfs_insert_empty_item(trans, root, path, &ins, sizeof(*fi));
@@ -1675,7 +1685,7 @@ static int insert_reserved_file_extent(struct btrfs_trans_handle *trans,
1675 ins.type = BTRFS_EXTENT_ITEM_KEY; 1685 ins.type = BTRFS_EXTENT_ITEM_KEY;
1676 ret = btrfs_alloc_reserved_file_extent(trans, root, 1686 ret = btrfs_alloc_reserved_file_extent(trans, root,
1677 root->root_key.objectid, 1687 root->root_key.objectid,
1678 inode->i_ino, file_pos, &ins); 1688 btrfs_ino(inode), file_pos, &ins);
1679 BUG_ON(ret); 1689 BUG_ON(ret);
1680 btrfs_free_path(path); 1690 btrfs_free_path(path);
1681 1691
@@ -1701,7 +1711,7 @@ static int btrfs_finish_ordered_io(struct inode *inode, u64 start, u64 end)
1701 struct extent_state *cached_state = NULL; 1711 struct extent_state *cached_state = NULL;
1702 int compress_type = 0; 1712 int compress_type = 0;
1703 int ret; 1713 int ret;
1704 bool nolock = false; 1714 bool nolock;
1705 1715
1706 ret = btrfs_dec_test_ordered_pending(inode, &ordered_extent, start, 1716 ret = btrfs_dec_test_ordered_pending(inode, &ordered_extent, start,
1707 end - start + 1); 1717 end - start + 1);
@@ -1709,7 +1719,7 @@ static int btrfs_finish_ordered_io(struct inode *inode, u64 start, u64 end)
1709 return 0; 1719 return 0;
1710 BUG_ON(!ordered_extent); 1720 BUG_ON(!ordered_extent);
1711 1721
1712 nolock = (root == root->fs_info->tree_root); 1722 nolock = is_free_space_inode(root, inode);
1713 1723
1714 if (test_bit(BTRFS_ORDERED_NOCOW, &ordered_extent->flags)) { 1724 if (test_bit(BTRFS_ORDERED_NOCOW, &ordered_extent->flags)) {
1715 BUG_ON(!list_empty(&ordered_extent->list)); 1725 BUG_ON(!list_empty(&ordered_extent->list));
@@ -2005,8 +2015,9 @@ good:
2005 2015
2006zeroit: 2016zeroit:
2007 if (printk_ratelimit()) { 2017 if (printk_ratelimit()) {
2008 printk(KERN_INFO "btrfs csum failed ino %lu off %llu csum %u " 2018 printk(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 } 2023 }
@@ -2244,7 +2255,7 @@ int btrfs_orphan_add(struct btrfs_trans_handle *trans, struct inode *inode)
2244 2255
2245 /* insert an orphan item to track this unlinked/truncated file */ 2256 /* insert an orphan item to track this unlinked/truncated file */
2246 if (insert >= 1) { 2257 if (insert >= 1) {
2247 ret = btrfs_insert_orphan_item(trans, root, inode->i_ino); 2258 ret = btrfs_insert_orphan_item(trans, root, btrfs_ino(inode));
2248 BUG_ON(ret); 2259 BUG_ON(ret);
2249 } 2260 }
2250 2261
@@ -2281,7 +2292,7 @@ int btrfs_orphan_del(struct btrfs_trans_handle *trans, struct inode *inode)
2281 spin_unlock(&root->orphan_lock); 2292 spin_unlock(&root->orphan_lock);
2282 2293
2283 if (trans && delete_item) { 2294 if (trans && delete_item) {
2284 ret = btrfs_del_orphan_item(trans, root, inode->i_ino); 2295 ret = btrfs_del_orphan_item(trans, root, btrfs_ino(inode));
2285 BUG_ON(ret); 2296 BUG_ON(ret);
2286 } 2297 }
2287 2298
@@ -2543,7 +2554,8 @@ static void btrfs_read_locked_inode(struct inode *inode)
2543 * try to precache a NULL acl entry for files that don't have 2554 * try to precache a NULL acl entry for files that don't have
2544 * any xattrs or acls 2555 * any xattrs or acls
2545 */ 2556 */
2546 maybe_acls = acls_after_inode_item(leaf, path->slots[0], inode->i_ino); 2557 maybe_acls = acls_after_inode_item(leaf, path->slots[0],
2558 btrfs_ino(inode));
2547 if (!maybe_acls) 2559 if (!maybe_acls)
2548 cache_no_acl(inode); 2560 cache_no_acl(inode);
2549 2561
@@ -2689,6 +2701,8 @@ static int __btrfs_unlink_inode(struct btrfs_trans_handle *trans,
2689 struct btrfs_dir_item *di; 2701 struct btrfs_dir_item *di;
2690 struct btrfs_key key; 2702 struct btrfs_key key;
2691 u64 index; 2703 u64 index;
2704 u64 ino = btrfs_ino(inode);
2705 u64 dir_ino = btrfs_ino(dir);
2692 2706
2693 path = btrfs_alloc_path(); 2707 path = btrfs_alloc_path();
2694 if (!path) { 2708 if (!path) {
@@ -2697,7 +2711,7 @@ static int __btrfs_unlink_inode(struct btrfs_trans_handle *trans,
2697 } 2711 }
2698 2712
2699 path->leave_spinning = 1; 2713 path->leave_spinning = 1;
2700 di = btrfs_lookup_dir_item(trans, root, path, dir->i_ino, 2714 di = btrfs_lookup_dir_item(trans, root, path, dir_ino,
2701 name, name_len, -1); 2715 name, name_len, -1);
2702 if (IS_ERR(di)) { 2716 if (IS_ERR(di)) {
2703 ret = PTR_ERR(di); 2717 ret = PTR_ERR(di);
@@ -2714,17 +2728,16 @@ static int __btrfs_unlink_inode(struct btrfs_trans_handle *trans,
2714 goto err; 2728 goto err;
2715 btrfs_release_path(root, path); 2729 btrfs_release_path(root, path);
2716 2730
2717 ret = btrfs_del_inode_ref(trans, root, name, name_len, 2731 ret = btrfs_del_inode_ref(trans, root, name, name_len, ino,
2718 inode->i_ino, 2732 dir_ino, &index);
2719 dir->i_ino, &index);
2720 if (ret) { 2733 if (ret) {
2721 printk(KERN_INFO "btrfs failed to delete reference to %.*s, " 2734 printk(KERN_INFO "btrfs failed to delete reference to %.*s, "
2722 "inode %lu parent %lu\n", name_len, name, 2735 "inode %llu parent %llu\n", name_len, name,
2723 inode->i_ino, dir->i_ino); 2736 (unsigned long long)ino, (unsigned long long)dir_ino);
2724 goto err; 2737 goto err;
2725 } 2738 }
2726 2739
2727 di = btrfs_lookup_dir_index_item(trans, root, path, dir->i_ino, 2740 di = btrfs_lookup_dir_index_item(trans, root, path, dir_ino,
2728 index, name, name_len, -1); 2741 index, name, name_len, -1);
2729 if (IS_ERR(di)) { 2742 if (IS_ERR(di)) {
2730 ret = PTR_ERR(di); 2743 ret = PTR_ERR(di);
@@ -2738,7 +2751,7 @@ static int __btrfs_unlink_inode(struct btrfs_trans_handle *trans,
2738 btrfs_release_path(root, path); 2751 btrfs_release_path(root, path);
2739 2752
2740 ret = btrfs_del_inode_ref_in_log(trans, root, name, name_len, 2753 ret = btrfs_del_inode_ref_in_log(trans, root, name, name_len,
2741 inode, dir->i_ino); 2754 inode, dir_ino);
2742 BUG_ON(ret != 0 && ret != -ENOENT); 2755 BUG_ON(ret != 0 && ret != -ENOENT);
2743 2756
2744 ret = btrfs_del_dir_entries_in_log(trans, root, name, name_len, 2757 ret = btrfs_del_dir_entries_in_log(trans, root, name, name_len,
@@ -2816,12 +2829,14 @@ static struct btrfs_trans_handle *__unlink_start_trans(struct inode *dir,
2816 int check_link = 1; 2829 int check_link = 1;
2817 int err = -ENOSPC; 2830 int err = -ENOSPC;
2818 int ret; 2831 int ret;
2832 u64 ino = btrfs_ino(inode);
2833 u64 dir_ino = btrfs_ino(dir);
2819 2834
2820 trans = btrfs_start_transaction(root, 10); 2835 trans = btrfs_start_transaction(root, 10);
2821 if (!IS_ERR(trans) || PTR_ERR(trans) != -ENOSPC) 2836 if (!IS_ERR(trans) || PTR_ERR(trans) != -ENOSPC)
2822 return trans; 2837 return trans;
2823 2838
2824 if (inode->i_ino == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID) 2839 if (ino == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID)
2825 return ERR_PTR(-ENOSPC); 2840 return ERR_PTR(-ENOSPC);
2826 2841
2827 /* check if there is someone else holds reference */ 2842 /* check if there is someone else holds reference */
@@ -2880,7 +2895,7 @@ static struct btrfs_trans_handle *__unlink_start_trans(struct inode *dir,
2880 2895
2881 if (ret == 0 && S_ISREG(inode->i_mode)) { 2896 if (ret == 0 && S_ISREG(inode->i_mode)) {
2882 ret = btrfs_lookup_file_extent(trans, root, path, 2897 ret = btrfs_lookup_file_extent(trans, root, path,
2883 inode->i_ino, (u64)-1, 0); 2898 ino, (u64)-1, 0);
2884 if (ret < 0) { 2899 if (ret < 0) {
2885 err = ret; 2900 err = ret;
2886 goto out; 2901 goto out;
@@ -2896,7 +2911,7 @@ static struct btrfs_trans_handle *__unlink_start_trans(struct inode *dir,
2896 goto out; 2911 goto out;
2897 } 2912 }
2898 2913
2899 di = btrfs_lookup_dir_item(trans, root, path, dir->i_ino, 2914 di = btrfs_lookup_dir_item(trans, root, path, dir_ino,
2900 dentry->d_name.name, dentry->d_name.len, 0); 2915 dentry->d_name.name, dentry->d_name.len, 0);
2901 if (IS_ERR(di)) { 2916 if (IS_ERR(di)) {
2902 err = PTR_ERR(di); 2917 err = PTR_ERR(di);
@@ -2913,7 +2928,7 @@ static struct btrfs_trans_handle *__unlink_start_trans(struct inode *dir,
2913 2928
2914 ref = btrfs_lookup_inode_ref(trans, root, path, 2929 ref = btrfs_lookup_inode_ref(trans, root, path,
2915 dentry->d_name.name, dentry->d_name.len, 2930 dentry->d_name.name, dentry->d_name.len,
2916 inode->i_ino, dir->i_ino, 0); 2931 ino, dir_ino, 0);
2917 if (IS_ERR(ref)) { 2932 if (IS_ERR(ref)) {
2918 err = PTR_ERR(ref); 2933 err = PTR_ERR(ref);
2919 goto out; 2934 goto out;
@@ -2924,7 +2939,7 @@ static struct btrfs_trans_handle *__unlink_start_trans(struct inode *dir,
2924 index = btrfs_inode_ref_index(path->nodes[0], ref); 2939 index = btrfs_inode_ref_index(path->nodes[0], ref);
2925 btrfs_release_path(root, path); 2940 btrfs_release_path(root, path);
2926 2941
2927 di = btrfs_lookup_dir_index_item(trans, root, path, dir->i_ino, index, 2942 di = btrfs_lookup_dir_index_item(trans, root, path, dir_ino, index,
2928 dentry->d_name.name, dentry->d_name.len, 0); 2943 dentry->d_name.name, dentry->d_name.len, 0);
2929 if (IS_ERR(di)) { 2944 if (IS_ERR(di)) {
2930 err = PTR_ERR(di); 2945 err = PTR_ERR(di);
@@ -2999,12 +3014,13 @@ int btrfs_unlink_subvol(struct btrfs_trans_handle *trans,
2999 struct btrfs_key key; 3014 struct btrfs_key key;
3000 u64 index; 3015 u64 index;
3001 int ret; 3016 int ret;
3017 u64 dir_ino = btrfs_ino(dir);
3002 3018
3003 path = btrfs_alloc_path(); 3019 path = btrfs_alloc_path();
3004 if (!path) 3020 if (!path)
3005 return -ENOMEM; 3021 return -ENOMEM;
3006 3022
3007 di = btrfs_lookup_dir_item(trans, root, path, dir->i_ino, 3023 di = btrfs_lookup_dir_item(trans, root, path, dir_ino,
3008 name, name_len, -1); 3024 name, name_len, -1);
3009 BUG_ON(!di || IS_ERR(di)); 3025 BUG_ON(!di || IS_ERR(di));
3010 3026
@@ -3017,10 +3033,10 @@ int btrfs_unlink_subvol(struct btrfs_trans_handle *trans,
3017 3033
3018 ret = btrfs_del_root_ref(trans, root->fs_info->tree_root, 3034 ret = btrfs_del_root_ref(trans, root->fs_info->tree_root,
3019 objectid, root->root_key.objectid, 3035 objectid, root->root_key.objectid,
3020 dir->i_ino, &index, name, name_len); 3036 dir_ino, &index, name, name_len);
3021 if (ret < 0) { 3037 if (ret < 0) {
3022 BUG_ON(ret != -ENOENT); 3038 BUG_ON(ret != -ENOENT);
3023 di = btrfs_search_dir_index_item(root, path, dir->i_ino, 3039 di = btrfs_search_dir_index_item(root, path, dir_ino,
3024 name, name_len); 3040 name, name_len);
3025 BUG_ON(!di || IS_ERR(di)); 3041 BUG_ON(!di || IS_ERR(di));
3026 3042
@@ -3030,7 +3046,7 @@ int btrfs_unlink_subvol(struct btrfs_trans_handle *trans,
3030 index = key.offset; 3046 index = key.offset;
3031 } 3047 }
3032 3048
3033 di = btrfs_lookup_dir_index_item(trans, root, path, dir->i_ino, 3049 di = btrfs_lookup_dir_index_item(trans, root, path, dir_ino,
3034 index, name, name_len, -1); 3050 index, name, name_len, -1);
3035 BUG_ON(!di || IS_ERR(di)); 3051 BUG_ON(!di || IS_ERR(di));
3036 3052
@@ -3059,7 +3075,7 @@ static int btrfs_rmdir(struct inode *dir, struct dentry *dentry)
3059 unsigned long nr = 0; 3075 unsigned long nr = 0;
3060 3076
3061 if (inode->i_size > BTRFS_EMPTY_DIR_SIZE || 3077 if (inode->i_size > BTRFS_EMPTY_DIR_SIZE ||
3062 inode->i_ino == BTRFS_FIRST_FREE_OBJECTID) 3078 btrfs_ino(inode) == BTRFS_FIRST_FREE_OBJECTID)
3063 return -ENOTEMPTY; 3079 return -ENOTEMPTY;
3064 3080
3065 trans = __unlink_start_trans(dir, dentry); 3081 trans = __unlink_start_trans(dir, dentry);
@@ -3068,7 +3084,7 @@ static int btrfs_rmdir(struct inode *dir, struct dentry *dentry)
3068 3084
3069 btrfs_set_trans_block_group(trans, dir); 3085 btrfs_set_trans_block_group(trans, dir);
3070 3086
3071 if (unlikely(inode->i_ino == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID)) { 3087 if (unlikely(btrfs_ino(inode) == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID)) {
3072 err = btrfs_unlink_subvol(trans, root, dir, 3088 err = btrfs_unlink_subvol(trans, root, dir,
3073 BTRFS_I(inode)->location.objectid, 3089 BTRFS_I(inode)->location.objectid,
3074 dentry->d_name.name, 3090 dentry->d_name.name,
@@ -3300,6 +3316,7 @@ int btrfs_truncate_inode_items(struct btrfs_trans_handle *trans,
3300 int encoding; 3316 int encoding;
3301 int ret; 3317 int ret;
3302 int err = 0; 3318 int err = 0;
3319 u64 ino = btrfs_ino(inode);
3303 3320
3304 BUG_ON(new_size > 0 && min_type != BTRFS_EXTENT_DATA_KEY); 3321 BUG_ON(new_size > 0 && min_type != BTRFS_EXTENT_DATA_KEY);
3305 3322
@@ -3310,7 +3327,7 @@ int btrfs_truncate_inode_items(struct btrfs_trans_handle *trans,
3310 BUG_ON(!path); 3327 BUG_ON(!path);
3311 path->reada = -1; 3328 path->reada = -1;
3312 3329
3313 key.objectid = inode->i_ino; 3330 key.objectid = ino;
3314 key.offset = (u64)-1; 3331 key.offset = (u64)-1;
3315 key.type = (u8)-1; 3332 key.type = (u8)-1;
3316 3333
@@ -3338,7 +3355,7 @@ search_again:
3338 found_type = btrfs_key_type(&found_key); 3355 found_type = btrfs_key_type(&found_key);
3339 encoding = 0; 3356 encoding = 0;
3340 3357
3341 if (found_key.objectid != inode->i_ino) 3358 if (found_key.objectid != ino)
3342 break; 3359 break;
3343 3360
3344 if (found_type < min_type) 3361 if (found_type < min_type)
@@ -3457,7 +3474,7 @@ delete:
3457 ret = btrfs_free_extent(trans, root, extent_start, 3474 ret = btrfs_free_extent(trans, root, extent_start,
3458 extent_num_bytes, 0, 3475 extent_num_bytes, 0,
3459 btrfs_header_owner(leaf), 3476 btrfs_header_owner(leaf),
3460 inode->i_ino, extent_offset); 3477 ino, extent_offset);
3461 BUG_ON(ret); 3478 BUG_ON(ret);
3462 } 3479 }
3463 3480
@@ -3466,7 +3483,9 @@ delete:
3466 3483
3467 if (path->slots[0] == 0 || 3484 if (path->slots[0] == 0 ||
3468 path->slots[0] != pending_del_slot) { 3485 path->slots[0] != pending_del_slot) {
3469 if (root->ref_cows) { 3486 if (root->ref_cows &&
3487 BTRFS_I(inode)->location.objectid !=
3488 BTRFS_FREE_INO_OBJECTID) {
3470 err = -EAGAIN; 3489 err = -EAGAIN;
3471 goto out; 3490 goto out;
3472 } 3491 }
@@ -3656,7 +3675,7 @@ int btrfs_cont_expand(struct inode *inode, loff_t oldsize, loff_t size)
3656 break; 3675 break;
3657 3676
3658 err = btrfs_insert_file_extent(trans, root, 3677 err = btrfs_insert_file_extent(trans, root,
3659 inode->i_ino, cur_offset, 0, 3678 btrfs_ino(inode), cur_offset, 0,
3660 0, hole_size, 0, hole_size, 3679 0, hole_size, 0, hole_size,
3661 0, 0, 0); 3680 0, 0, 0);
3662 if (err) 3681 if (err)
@@ -3758,7 +3777,7 @@ void btrfs_evict_inode(struct inode *inode)
3758 3777
3759 truncate_inode_pages(&inode->i_data, 0); 3778 truncate_inode_pages(&inode->i_data, 0);
3760 if (inode->i_nlink && (btrfs_root_refs(&root->root_item) != 0 || 3779 if (inode->i_nlink && (btrfs_root_refs(&root->root_item) != 0 ||
3761 root == root->fs_info->tree_root)) 3780 is_free_space_inode(root, inode)))
3762 goto no_delete; 3781 goto no_delete;
3763 3782
3764 if (is_bad_inode(inode)) { 3783 if (is_bad_inode(inode)) {
@@ -3811,6 +3830,10 @@ void btrfs_evict_inode(struct inode *inode)
3811 BUG_ON(ret); 3830 BUG_ON(ret);
3812 } 3831 }
3813 3832
3833 if (!(root == root->fs_info->tree_root ||
3834 root->root_key.objectid == BTRFS_TREE_RELOC_OBJECTID))
3835 btrfs_return_ino(root, btrfs_ino(inode));
3836
3814 nr = trans->blocks_used; 3837 nr = trans->blocks_used;
3815 btrfs_end_transaction(trans, root); 3838 btrfs_end_transaction(trans, root);
3816 btrfs_btree_balance_dirty(root, nr); 3839 btrfs_btree_balance_dirty(root, nr);
@@ -3836,7 +3859,7 @@ static int btrfs_inode_by_name(struct inode *dir, struct dentry *dentry,
3836 path = btrfs_alloc_path(); 3859 path = btrfs_alloc_path();
3837 BUG_ON(!path); 3860 BUG_ON(!path);
3838 3861
3839 di = btrfs_lookup_dir_item(NULL, root, path, dir->i_ino, name, 3862 di = btrfs_lookup_dir_item(NULL, root, path, btrfs_ino(dir), name,
3840 namelen, 0); 3863 namelen, 0);
3841 if (IS_ERR(di)) 3864 if (IS_ERR(di))
3842 ret = PTR_ERR(di); 3865 ret = PTR_ERR(di);
@@ -3889,7 +3912,7 @@ static int fixup_tree_root_location(struct btrfs_root *root,
3889 3912
3890 leaf = path->nodes[0]; 3913 leaf = path->nodes[0];
3891 ref = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_root_ref); 3914 ref = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_root_ref);
3892 if (btrfs_root_ref_dirid(leaf, ref) != dir->i_ino || 3915 if (btrfs_root_ref_dirid(leaf, ref) != btrfs_ino(dir) ||
3893 btrfs_root_ref_name_len(leaf, ref) != dentry->d_name.len) 3916 btrfs_root_ref_name_len(leaf, ref) != dentry->d_name.len)
3894 goto out; 3917 goto out;
3895 3918
@@ -3928,6 +3951,7 @@ static void inode_tree_add(struct inode *inode)
3928 struct btrfs_inode *entry; 3951 struct btrfs_inode *entry;
3929 struct rb_node **p; 3952 struct rb_node **p;
3930 struct rb_node *parent; 3953 struct rb_node *parent;
3954 u64 ino = btrfs_ino(inode);
3931again: 3955again:
3932 p = &root->inode_tree.rb_node; 3956 p = &root->inode_tree.rb_node;
3933 parent = NULL; 3957 parent = NULL;
@@ -3940,9 +3964,9 @@ again:
3940 parent = *p; 3964 parent = *p;
3941 entry = rb_entry(parent, struct btrfs_inode, rb_node); 3965 entry = rb_entry(parent, struct btrfs_inode, rb_node);
3942 3966
3943 if (inode->i_ino < entry->vfs_inode.i_ino) 3967 if (ino < btrfs_ino(&entry->vfs_inode))
3944 p = &parent->rb_left; 3968 p = &parent->rb_left;
3945 else if (inode->i_ino > entry->vfs_inode.i_ino) 3969 else if (ino > btrfs_ino(&entry->vfs_inode))
3946 p = &parent->rb_right; 3970 p = &parent->rb_right;
3947 else { 3971 else {
3948 WARN_ON(!(entry->vfs_inode.i_state & 3972 WARN_ON(!(entry->vfs_inode.i_state &
@@ -4006,9 +4030,9 @@ again:
4006 prev = node; 4030 prev = node;
4007 entry = rb_entry(node, struct btrfs_inode, rb_node); 4031 entry = rb_entry(node, struct btrfs_inode, rb_node);
4008 4032
4009 if (objectid < entry->vfs_inode.i_ino) 4033 if (objectid < btrfs_ino(&entry->vfs_inode))
4010 node = node->rb_left; 4034 node = node->rb_left;
4011 else if (objectid > entry->vfs_inode.i_ino) 4035 else if (objectid > btrfs_ino(&entry->vfs_inode))
4012 node = node->rb_right; 4036 node = node->rb_right;
4013 else 4037 else
4014 break; 4038 break;
@@ -4016,7 +4040,7 @@ again:
4016 if (!node) { 4040 if (!node) {
4017 while (prev) { 4041 while (prev) {
4018 entry = rb_entry(prev, struct btrfs_inode, rb_node); 4042 entry = rb_entry(prev, struct btrfs_inode, rb_node);
4019 if (objectid <= entry->vfs_inode.i_ino) { 4043 if (objectid <= btrfs_ino(&entry->vfs_inode)) {
4020 node = prev; 4044 node = prev;
4021 break; 4045 break;
4022 } 4046 }
@@ -4025,7 +4049,7 @@ again:
4025 } 4049 }
4026 while (node) { 4050 while (node) {
4027 entry = rb_entry(node, struct btrfs_inode, rb_node); 4051 entry = rb_entry(node, struct btrfs_inode, rb_node);
4028 objectid = entry->vfs_inode.i_ino + 1; 4052 objectid = btrfs_ino(&entry->vfs_inode) + 1;
4029 inode = igrab(&entry->vfs_inode); 4053 inode = igrab(&entry->vfs_inode);
4030 if (inode) { 4054 if (inode) {
4031 spin_unlock(&root->inode_lock); 4055 spin_unlock(&root->inode_lock);
@@ -4063,7 +4087,7 @@ static int btrfs_init_locked_inode(struct inode *inode, void *p)
4063static int btrfs_find_actor(struct inode *inode, void *opaque) 4087static int btrfs_find_actor(struct inode *inode, void *opaque)
4064{ 4088{
4065 struct btrfs_iget_args *args = opaque; 4089 struct btrfs_iget_args *args = opaque;
4066 return args->ino == inode->i_ino && 4090 return args->ino == btrfs_ino(inode) &&
4067 args->root == BTRFS_I(inode)->root; 4091 args->root == BTRFS_I(inode)->root;
4068} 4092}
4069 4093
@@ -4241,9 +4265,7 @@ static int btrfs_real_readdir(struct file *filp, void *dirent,
4241 4265
4242 /* special case for "." */ 4266 /* special case for "." */
4243 if (filp->f_pos == 0) { 4267 if (filp->f_pos == 0) {
4244 over = filldir(dirent, ".", 1, 4268 over = filldir(dirent, ".", 1, 1, btrfs_ino(inode), DT_DIR);
4245 1, inode->i_ino,
4246 DT_DIR);
4247 if (over) 4269 if (over)
4248 return 0; 4270 return 0;
4249 filp->f_pos = 1; 4271 filp->f_pos = 1;
@@ -4262,7 +4284,7 @@ static int btrfs_real_readdir(struct file *filp, void *dirent,
4262 4284
4263 btrfs_set_key_type(&key, key_type); 4285 btrfs_set_key_type(&key, key_type);
4264 key.offset = filp->f_pos; 4286 key.offset = filp->f_pos;
4265 key.objectid = inode->i_ino; 4287 key.objectid = btrfs_ino(inode);
4266 4288
4267 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0); 4289 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
4268 if (ret < 0) 4290 if (ret < 0)
@@ -4372,7 +4394,8 @@ int btrfs_write_inode(struct inode *inode, struct writeback_control *wbc)
4372 return 0; 4394 return 0;
4373 4395
4374 smp_mb(); 4396 smp_mb();
4375 nolock = (root->fs_info->closing && root == root->fs_info->tree_root); 4397 if (root->fs_info->closing && is_free_space_inode(root, inode))
4398 nolock = true;
4376 4399
4377 if (wbc->sync_mode == WB_SYNC_ALL) { 4400 if (wbc->sync_mode == WB_SYNC_ALL) {
4378 if (nolock) 4401 if (nolock)
@@ -4417,8 +4440,9 @@ void btrfs_dirty_inode(struct inode *inode)
4417 if (IS_ERR(trans)) { 4440 if (IS_ERR(trans)) {
4418 if (printk_ratelimit()) { 4441 if (printk_ratelimit()) {
4419 printk(KERN_ERR "btrfs: fail to " 4442 printk(KERN_ERR "btrfs: fail to "
4420 "dirty inode %lu error %ld\n", 4443 "dirty inode %llu error %ld\n",
4421 inode->i_ino, PTR_ERR(trans)); 4444 (unsigned long long)btrfs_ino(inode),
4445 PTR_ERR(trans));
4422 } 4446 }
4423 return; 4447 return;
4424 } 4448 }
@@ -4428,8 +4452,9 @@ void btrfs_dirty_inode(struct inode *inode)
4428 if (ret) { 4452 if (ret) {
4429 if (printk_ratelimit()) { 4453 if (printk_ratelimit()) {
4430 printk(KERN_ERR "btrfs: fail to " 4454 printk(KERN_ERR "btrfs: fail to "
4431 "dirty inode %lu error %d\n", 4455 "dirty inode %llu error %d\n",
4432 inode->i_ino, ret); 4456 (unsigned long long)btrfs_ino(inode),
4457 ret);
4433 } 4458 }
4434 } 4459 }
4435 } 4460 }
@@ -4449,7 +4474,7 @@ static int btrfs_set_inode_index_count(struct inode *inode)
4449 struct extent_buffer *leaf; 4474 struct extent_buffer *leaf;
4450 int ret; 4475 int ret;
4451 4476
4452 key.objectid = inode->i_ino; 4477 key.objectid = btrfs_ino(inode);
4453 btrfs_set_key_type(&key, BTRFS_DIR_INDEX_KEY); 4478 btrfs_set_key_type(&key, BTRFS_DIR_INDEX_KEY);
4454 key.offset = (u64)-1; 4479 key.offset = (u64)-1;
4455 4480
@@ -4481,7 +4506,7 @@ static int btrfs_set_inode_index_count(struct inode *inode)
4481 leaf = path->nodes[0]; 4506 leaf = path->nodes[0];
4482 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]); 4507 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
4483 4508
4484 if (found_key.objectid != inode->i_ino || 4509 if (found_key.objectid != btrfs_ino(inode) ||
4485 btrfs_key_type(&found_key) != BTRFS_DIR_INDEX_KEY) { 4510 btrfs_key_type(&found_key) != BTRFS_DIR_INDEX_KEY) {
4486 BTRFS_I(inode)->index_cnt = 2; 4511 BTRFS_I(inode)->index_cnt = 2;
4487 goto out; 4512 goto out;
@@ -4540,6 +4565,12 @@ static struct inode *btrfs_new_inode(struct btrfs_trans_handle *trans,
4540 return ERR_PTR(-ENOMEM); 4565 return ERR_PTR(-ENOMEM);
4541 } 4566 }
4542 4567
4568 /*
4569 * we have to initialize this early, so we can reclaim the inode
4570 * number if we fail afterwards in this function.
4571 */
4572 inode->i_ino = objectid;
4573
4543 if (dir) { 4574 if (dir) {
4544 trace_btrfs_inode_request(dir); 4575 trace_btrfs_inode_request(dir);
4545 4576
@@ -4585,7 +4616,6 @@ static struct inode *btrfs_new_inode(struct btrfs_trans_handle *trans,
4585 goto fail; 4616 goto fail;
4586 4617
4587 inode_init_owner(inode, dir, mode); 4618 inode_init_owner(inode, dir, mode);
4588 inode->i_ino = objectid;
4589 inode_set_bytes(inode, 0); 4619 inode_set_bytes(inode, 0);
4590 inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME; 4620 inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME;
4591 inode_item = btrfs_item_ptr(path->nodes[0], path->slots[0], 4621 inode_item = btrfs_item_ptr(path->nodes[0], path->slots[0],
@@ -4649,29 +4679,29 @@ int btrfs_add_link(struct btrfs_trans_handle *trans,
4649 int ret = 0; 4679 int ret = 0;
4650 struct btrfs_key key; 4680 struct btrfs_key key;
4651 struct btrfs_root *root = BTRFS_I(parent_inode)->root; 4681 struct btrfs_root *root = BTRFS_I(parent_inode)->root;
4682 u64 ino = btrfs_ino(inode);
4683 u64 parent_ino = btrfs_ino(parent_inode);
4652 4684
4653 if (unlikely(inode->i_ino == BTRFS_FIRST_FREE_OBJECTID)) { 4685 if (unlikely(ino == BTRFS_FIRST_FREE_OBJECTID)) {
4654 memcpy(&key, &BTRFS_I(inode)->root->root_key, sizeof(key)); 4686 memcpy(&key, &BTRFS_I(inode)->root->root_key, sizeof(key));
4655 } else { 4687 } else {
4656 key.objectid = inode->i_ino; 4688 key.objectid = ino;
4657 btrfs_set_key_type(&key, BTRFS_INODE_ITEM_KEY); 4689 btrfs_set_key_type(&key, BTRFS_INODE_ITEM_KEY);
4658 key.offset = 0; 4690 key.offset = 0;
4659 } 4691 }
4660 4692
4661 if (unlikely(inode->i_ino == BTRFS_FIRST_FREE_OBJECTID)) { 4693 if (unlikely(ino == BTRFS_FIRST_FREE_OBJECTID)) {
4662 ret = btrfs_add_root_ref(trans, root->fs_info->tree_root, 4694 ret = btrfs_add_root_ref(trans, root->fs_info->tree_root,
4663 key.objectid, root->root_key.objectid, 4695 key.objectid, root->root_key.objectid,
4664 parent_inode->i_ino, 4696 parent_ino, index, name, name_len);
4665 index, name, name_len);
4666 } else if (add_backref) { 4697 } else if (add_backref) {
4667 ret = btrfs_insert_inode_ref(trans, root, 4698 ret = btrfs_insert_inode_ref(trans, root, name, name_len, ino,
4668 name, name_len, inode->i_ino, 4699 parent_ino, index);
4669 parent_inode->i_ino, index);
4670 } 4700 }
4671 4701
4672 if (ret == 0) { 4702 if (ret == 0) {
4673 ret = btrfs_insert_dir_item(trans, root, name, name_len, 4703 ret = btrfs_insert_dir_item(trans, root, name, name_len,
4674 parent_inode->i_ino, &key, 4704 parent_ino, &key,
4675 btrfs_inode_type(inode), index); 4705 btrfs_inode_type(inode), index);
4676 BUG_ON(ret); 4706 BUG_ON(ret);
4677 4707
@@ -4714,10 +4744,6 @@ static int btrfs_mknod(struct inode *dir, struct dentry *dentry,
4714 if (!new_valid_dev(rdev)) 4744 if (!new_valid_dev(rdev))
4715 return -EINVAL; 4745 return -EINVAL;
4716 4746
4717 err = btrfs_find_free_objectid(NULL, root, dir->i_ino, &objectid);
4718 if (err)
4719 return err;
4720
4721 /* 4747 /*
4722 * 2 for inode item and ref 4748 * 2 for inode item and ref
4723 * 2 for dir items 4749 * 2 for dir items
@@ -4729,8 +4755,12 @@ static int btrfs_mknod(struct inode *dir, struct dentry *dentry,
4729 4755
4730 btrfs_set_trans_block_group(trans, dir); 4756 btrfs_set_trans_block_group(trans, dir);
4731 4757
4758 err = btrfs_find_free_ino(root, &objectid);
4759 if (err)
4760 goto out_unlock;
4761
4732 inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name, 4762 inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name,
4733 dentry->d_name.len, dir->i_ino, objectid, 4763 dentry->d_name.len, btrfs_ino(dir), objectid,
4734 BTRFS_I(dir)->block_group, mode, &index); 4764 BTRFS_I(dir)->block_group, mode, &index);
4735 if (IS_ERR(inode)) { 4765 if (IS_ERR(inode)) {
4736 err = PTR_ERR(inode); 4766 err = PTR_ERR(inode);
@@ -4777,9 +4807,6 @@ static int btrfs_create(struct inode *dir, struct dentry *dentry,
4777 u64 objectid; 4807 u64 objectid;
4778 u64 index = 0; 4808 u64 index = 0;
4779 4809
4780 err = btrfs_find_free_objectid(NULL, root, dir->i_ino, &objectid);
4781 if (err)
4782 return err;
4783 /* 4810 /*
4784 * 2 for inode item and ref 4811 * 2 for inode item and ref
4785 * 2 for dir items 4812 * 2 for dir items
@@ -4791,8 +4818,12 @@ static int btrfs_create(struct inode *dir, struct dentry *dentry,
4791 4818
4792 btrfs_set_trans_block_group(trans, dir); 4819 btrfs_set_trans_block_group(trans, dir);
4793 4820
4821 err = btrfs_find_free_ino(root, &objectid);
4822 if (err)
4823 goto out_unlock;
4824
4794 inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name, 4825 inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name,
4795 dentry->d_name.len, dir->i_ino, objectid, 4826 dentry->d_name.len, btrfs_ino(dir), objectid,
4796 BTRFS_I(dir)->block_group, mode, &index); 4827 BTRFS_I(dir)->block_group, mode, &index);
4797 if (IS_ERR(inode)) { 4828 if (IS_ERR(inode)) {
4798 err = PTR_ERR(inode); 4829 err = PTR_ERR(inode);
@@ -4903,10 +4934,6 @@ static int btrfs_mkdir(struct inode *dir, struct dentry *dentry, int mode)
4903 u64 index = 0; 4934 u64 index = 0;
4904 unsigned long nr = 1; 4935 unsigned long nr = 1;
4905 4936
4906 err = btrfs_find_free_objectid(NULL, root, dir->i_ino, &objectid);
4907 if (err)
4908 return err;
4909
4910 /* 4937 /*
4911 * 2 items for inode and ref 4938 * 2 items for inode and ref
4912 * 2 items for dir items 4939 * 2 items for dir items
@@ -4917,8 +4944,12 @@ static int btrfs_mkdir(struct inode *dir, struct dentry *dentry, int mode)
4917 return PTR_ERR(trans); 4944 return PTR_ERR(trans);
4918 btrfs_set_trans_block_group(trans, dir); 4945 btrfs_set_trans_block_group(trans, dir);
4919 4946
4947 err = btrfs_find_free_ino(root, &objectid);
4948 if (err)
4949 goto out_fail;
4950
4920 inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name, 4951 inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name,
4921 dentry->d_name.len, dir->i_ino, objectid, 4952 dentry->d_name.len, btrfs_ino(dir), objectid,
4922 BTRFS_I(dir)->block_group, S_IFDIR | mode, 4953 BTRFS_I(dir)->block_group, S_IFDIR | mode,
4923 &index); 4954 &index);
4924 if (IS_ERR(inode)) { 4955 if (IS_ERR(inode)) {
@@ -5041,7 +5072,7 @@ struct extent_map *btrfs_get_extent(struct inode *inode, struct page *page,
5041 u64 bytenr; 5072 u64 bytenr;
5042 u64 extent_start = 0; 5073 u64 extent_start = 0;
5043 u64 extent_end = 0; 5074 u64 extent_end = 0;
5044 u64 objectid = inode->i_ino; 5075 u64 objectid = btrfs_ino(inode);
5045 u32 found_type; 5076 u32 found_type;
5046 struct btrfs_path *path = NULL; 5077 struct btrfs_path *path = NULL;
5047 struct btrfs_root *root = BTRFS_I(inode)->root; 5078 struct btrfs_root *root = BTRFS_I(inode)->root;
@@ -5549,7 +5580,7 @@ static noinline int can_nocow_odirect(struct btrfs_trans_handle *trans,
5549 if (!path) 5580 if (!path)
5550 return -ENOMEM; 5581 return -ENOMEM;
5551 5582
5552 ret = btrfs_lookup_file_extent(trans, root, path, inode->i_ino, 5583 ret = btrfs_lookup_file_extent(trans, root, path, btrfs_ino(inode),
5553 offset, 0); 5584 offset, 0);
5554 if (ret < 0) 5585 if (ret < 0)
5555 goto out; 5586 goto out;
@@ -5566,7 +5597,7 @@ static noinline int can_nocow_odirect(struct btrfs_trans_handle *trans,
5566 ret = 0; 5597 ret = 0;
5567 leaf = path->nodes[0]; 5598 leaf = path->nodes[0];
5568 btrfs_item_key_to_cpu(leaf, &key, slot); 5599 btrfs_item_key_to_cpu(leaf, &key, slot);
5569 if (key.objectid != inode->i_ino || 5600 if (key.objectid != btrfs_ino(inode) ||
5570 key.type != BTRFS_EXTENT_DATA_KEY) { 5601 key.type != BTRFS_EXTENT_DATA_KEY) {
5571 /* not our file or wrong item type, must cow */ 5602 /* not our file or wrong item type, must cow */
5572 goto out; 5603 goto out;
@@ -5600,7 +5631,7 @@ static noinline int can_nocow_odirect(struct btrfs_trans_handle *trans,
5600 * look for other files referencing this extent, if we 5631 * look for other files referencing this extent, if we
5601 * find any we must cow 5632 * find any we must cow
5602 */ 5633 */
5603 if (btrfs_cross_ref_exist(trans, root, inode->i_ino, 5634 if (btrfs_cross_ref_exist(trans, root, btrfs_ino(inode),
5604 key.offset - backref_offset, disk_bytenr)) 5635 key.offset - backref_offset, disk_bytenr))
5605 goto out; 5636 goto out;
5606 5637
@@ -5790,9 +5821,10 @@ static void btrfs_endio_direct_read(struct bio *bio, int err)
5790 5821
5791 flush_dcache_page(bvec->bv_page); 5822 flush_dcache_page(bvec->bv_page);
5792 if (csum != *private) { 5823 if (csum != *private) {
5793 printk(KERN_ERR "btrfs csum failed ino %lu off" 5824 printk(KERN_ERR "btrfs csum failed ino %llu off"
5794 " %llu csum %u private %u\n", 5825 " %llu csum %u private %u\n",
5795 inode->i_ino, (unsigned long long)start, 5826 (unsigned long long)btrfs_ino(inode),
5827 (unsigned long long)start,
5796 csum, *private); 5828 csum, *private);
5797 err = -EIO; 5829 err = -EIO;
5798 } 5830 }
@@ -5939,9 +5971,9 @@ static void btrfs_end_dio_bio(struct bio *bio, int err)
5939 struct btrfs_dio_private *dip = bio->bi_private; 5971 struct btrfs_dio_private *dip = bio->bi_private;
5940 5972
5941 if (err) { 5973 if (err) {
5942 printk(KERN_ERR "btrfs direct IO failed ino %lu rw %lu " 5974 printk(KERN_ERR "btrfs direct IO failed ino %llu rw %lu "
5943 "sector %#Lx len %u err no %d\n", 5975 "sector %#Lx len %u err no %d\n",
5944 dip->inode->i_ino, bio->bi_rw, 5976 (unsigned long long)btrfs_ino(dip->inode), bio->bi_rw,
5945 (unsigned long long)bio->bi_sector, bio->bi_size, err); 5977 (unsigned long long)bio->bi_sector, bio->bi_size, err);
5946 dip->errors = 1; 5978 dip->errors = 1;
5947 5979
@@ -6851,8 +6883,8 @@ void btrfs_destroy_inode(struct inode *inode)
6851 6883
6852 spin_lock(&root->orphan_lock); 6884 spin_lock(&root->orphan_lock);
6853 if (!list_empty(&BTRFS_I(inode)->i_orphan)) { 6885 if (!list_empty(&BTRFS_I(inode)->i_orphan)) {
6854 printk(KERN_INFO "BTRFS: inode %lu still on the orphan list\n", 6886 printk(KERN_INFO "BTRFS: inode %llu still on the orphan list\n",
6855 inode->i_ino); 6887 (unsigned long long)btrfs_ino(inode));
6856 list_del_init(&BTRFS_I(inode)->i_orphan); 6888 list_del_init(&BTRFS_I(inode)->i_orphan);
6857 } 6889 }
6858 spin_unlock(&root->orphan_lock); 6890 spin_unlock(&root->orphan_lock);
@@ -6882,7 +6914,7 @@ int btrfs_drop_inode(struct inode *inode)
6882 struct btrfs_root *root = BTRFS_I(inode)->root; 6914 struct btrfs_root *root = BTRFS_I(inode)->root;
6883 6915
6884 if (btrfs_root_refs(&root->root_item) == 0 && 6916 if (btrfs_root_refs(&root->root_item) == 0 &&
6885 root != root->fs_info->tree_root) 6917 !is_free_space_inode(root, inode))
6886 return 1; 6918 return 1;
6887 else 6919 else
6888 return generic_drop_inode(inode); 6920 return generic_drop_inode(inode);
@@ -6991,16 +7023,17 @@ static int btrfs_rename(struct inode *old_dir, struct dentry *old_dentry,
6991 u64 index = 0; 7023 u64 index = 0;
6992 u64 root_objectid; 7024 u64 root_objectid;
6993 int ret; 7025 int ret;
7026 u64 old_ino = btrfs_ino(old_inode);
6994 7027
6995 if (new_dir->i_ino == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID) 7028 if (btrfs_ino(new_dir) == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID)
6996 return -EPERM; 7029 return -EPERM;
6997 7030
6998 /* we only allow rename subvolume link between subvolumes */ 7031 /* we only allow rename subvolume link between subvolumes */
6999 if (old_inode->i_ino != BTRFS_FIRST_FREE_OBJECTID && root != dest) 7032 if (old_ino != BTRFS_FIRST_FREE_OBJECTID && root != dest)
7000 return -EXDEV; 7033 return -EXDEV;
7001 7034
7002 if (old_inode->i_ino == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID || 7035 if (old_ino == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID ||
7003 (new_inode && new_inode->i_ino == BTRFS_FIRST_FREE_OBJECTID)) 7036 (new_inode && btrfs_ino(new_inode) == BTRFS_FIRST_FREE_OBJECTID))
7004 return -ENOTEMPTY; 7037 return -ENOTEMPTY;
7005 7038
7006 if (S_ISDIR(old_inode->i_mode) && new_inode && 7039 if (S_ISDIR(old_inode->i_mode) && new_inode &&
@@ -7016,7 +7049,7 @@ static int btrfs_rename(struct inode *old_dir, struct dentry *old_dentry,
7016 filemap_flush(old_inode->i_mapping); 7049 filemap_flush(old_inode->i_mapping);
7017 7050
7018 /* close the racy window with snapshot create/destroy ioctl */ 7051 /* close the racy window with snapshot create/destroy ioctl */
7019 if (old_inode->i_ino == BTRFS_FIRST_FREE_OBJECTID) 7052 if (old_ino == BTRFS_FIRST_FREE_OBJECTID)
7020 down_read(&root->fs_info->subvol_sem); 7053 down_read(&root->fs_info->subvol_sem);
7021 /* 7054 /*
7022 * We want to reserve the absolute worst case amount of items. So if 7055 * We want to reserve the absolute worst case amount of items. So if
@@ -7041,15 +7074,15 @@ static int btrfs_rename(struct inode *old_dir, struct dentry *old_dentry,
7041 if (ret) 7074 if (ret)
7042 goto out_fail; 7075 goto out_fail;
7043 7076
7044 if (unlikely(old_inode->i_ino == BTRFS_FIRST_FREE_OBJECTID)) { 7077 if (unlikely(old_ino == BTRFS_FIRST_FREE_OBJECTID)) {
7045 /* force full log commit if subvolume involved. */ 7078 /* force full log commit if subvolume involved. */
7046 root->fs_info->last_trans_log_full_commit = trans->transid; 7079 root->fs_info->last_trans_log_full_commit = trans->transid;
7047 } else { 7080 } else {
7048 ret = btrfs_insert_inode_ref(trans, dest, 7081 ret = btrfs_insert_inode_ref(trans, dest,
7049 new_dentry->d_name.name, 7082 new_dentry->d_name.name,
7050 new_dentry->d_name.len, 7083 new_dentry->d_name.len,
7051 old_inode->i_ino, 7084 old_ino,
7052 new_dir->i_ino, index); 7085 btrfs_ino(new_dir), index);
7053 if (ret) 7086 if (ret)
7054 goto out_fail; 7087 goto out_fail;
7055 /* 7088 /*
@@ -7065,10 +7098,8 @@ static int btrfs_rename(struct inode *old_dir, struct dentry *old_dentry,
7065 * make sure the inode gets flushed if it is replacing 7098 * make sure the inode gets flushed if it is replacing
7066 * something. 7099 * something.
7067 */ 7100 */
7068 if (new_inode && new_inode->i_size && 7101 if (new_inode && new_inode->i_size && S_ISREG(old_inode->i_mode))
7069 old_inode && S_ISREG(old_inode->i_mode)) {
7070 btrfs_add_ordered_operation(trans, root, old_inode); 7102 btrfs_add_ordered_operation(trans, root, old_inode);
7071 }
7072 7103
7073 old_dir->i_ctime = old_dir->i_mtime = ctime; 7104 old_dir->i_ctime = old_dir->i_mtime = ctime;
7074 new_dir->i_ctime = new_dir->i_mtime = ctime; 7105 new_dir->i_ctime = new_dir->i_mtime = ctime;
@@ -7077,7 +7108,7 @@ static int btrfs_rename(struct inode *old_dir, struct dentry *old_dentry,
7077 if (old_dentry->d_parent != new_dentry->d_parent) 7108 if (old_dentry->d_parent != new_dentry->d_parent)
7078 btrfs_record_unlink_dir(trans, old_dir, old_inode, 1); 7109 btrfs_record_unlink_dir(trans, old_dir, old_inode, 1);
7079 7110
7080 if (unlikely(old_inode->i_ino == BTRFS_FIRST_FREE_OBJECTID)) { 7111 if (unlikely(old_ino == BTRFS_FIRST_FREE_OBJECTID)) {
7081 root_objectid = BTRFS_I(old_inode)->root->root_key.objectid; 7112 root_objectid = BTRFS_I(old_inode)->root->root_key.objectid;
7082 ret = btrfs_unlink_subvol(trans, root, old_dir, root_objectid, 7113 ret = btrfs_unlink_subvol(trans, root, old_dir, root_objectid,
7083 old_dentry->d_name.name, 7114 old_dentry->d_name.name,
@@ -7094,7 +7125,7 @@ static int btrfs_rename(struct inode *old_dir, struct dentry *old_dentry,
7094 7125
7095 if (new_inode) { 7126 if (new_inode) {
7096 new_inode->i_ctime = CURRENT_TIME; 7127 new_inode->i_ctime = CURRENT_TIME;
7097 if (unlikely(new_inode->i_ino == 7128 if (unlikely(btrfs_ino(new_inode) ==
7098 BTRFS_EMPTY_SUBVOL_DIR_OBJECTID)) { 7129 BTRFS_EMPTY_SUBVOL_DIR_OBJECTID)) {
7099 root_objectid = BTRFS_I(new_inode)->location.objectid; 7130 root_objectid = BTRFS_I(new_inode)->location.objectid;
7100 ret = btrfs_unlink_subvol(trans, dest, new_dir, 7131 ret = btrfs_unlink_subvol(trans, dest, new_dir,
@@ -7122,7 +7153,7 @@ static int btrfs_rename(struct inode *old_dir, struct dentry *old_dentry,
7122 new_dentry->d_name.len, 0, index); 7153 new_dentry->d_name.len, 0, index);
7123 BUG_ON(ret); 7154 BUG_ON(ret);
7124 7155
7125 if (old_inode->i_ino != BTRFS_FIRST_FREE_OBJECTID) { 7156 if (old_ino != BTRFS_FIRST_FREE_OBJECTID) {
7126 struct dentry *parent = dget_parent(new_dentry); 7157 struct dentry *parent = dget_parent(new_dentry);
7127 btrfs_log_new_name(trans, old_inode, old_dir, parent); 7158 btrfs_log_new_name(trans, old_inode, old_dir, parent);
7128 dput(parent); 7159 dput(parent);
@@ -7131,7 +7162,7 @@ static int btrfs_rename(struct inode *old_dir, struct dentry *old_dentry,
7131out_fail: 7162out_fail:
7132 btrfs_end_transaction_throttle(trans, root); 7163 btrfs_end_transaction_throttle(trans, root);
7133out_notrans: 7164out_notrans:
7134 if (old_inode->i_ino == BTRFS_FIRST_FREE_OBJECTID) 7165 if (old_ino == BTRFS_FIRST_FREE_OBJECTID)
7135 up_read(&root->fs_info->subvol_sem); 7166 up_read(&root->fs_info->subvol_sem);
7136 7167
7137 return ret; 7168 return ret;
@@ -7260,9 +7291,6 @@ static int btrfs_symlink(struct inode *dir, struct dentry *dentry,
7260 if (name_len > BTRFS_MAX_INLINE_DATA_SIZE(root)) 7291 if (name_len > BTRFS_MAX_INLINE_DATA_SIZE(root))
7261 return -ENAMETOOLONG; 7292 return -ENAMETOOLONG;
7262 7293
7263 err = btrfs_find_free_objectid(NULL, root, dir->i_ino, &objectid);
7264 if (err)
7265 return err;
7266 /* 7294 /*
7267 * 2 items for inode item and ref 7295 * 2 items for inode item and ref
7268 * 2 items for dir items 7296 * 2 items for dir items
@@ -7274,8 +7302,12 @@ static int btrfs_symlink(struct inode *dir, struct dentry *dentry,
7274 7302
7275 btrfs_set_trans_block_group(trans, dir); 7303 btrfs_set_trans_block_group(trans, dir);
7276 7304
7305 err = btrfs_find_free_ino(root, &objectid);
7306 if (err)
7307 goto out_unlock;
7308
7277 inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name, 7309 inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name,
7278 dentry->d_name.len, dir->i_ino, objectid, 7310 dentry->d_name.len, btrfs_ino(dir), objectid,
7279 BTRFS_I(dir)->block_group, S_IFLNK|S_IRWXUGO, 7311 BTRFS_I(dir)->block_group, S_IFLNK|S_IRWXUGO,
7280 &index); 7312 &index);
7281 if (IS_ERR(inode)) { 7313 if (IS_ERR(inode)) {
@@ -7307,7 +7339,7 @@ static int btrfs_symlink(struct inode *dir, struct dentry *dentry,
7307 7339
7308 path = btrfs_alloc_path(); 7340 path = btrfs_alloc_path();
7309 BUG_ON(!path); 7341 BUG_ON(!path);
7310 key.objectid = inode->i_ino; 7342 key.objectid = btrfs_ino(inode);
7311 key.offset = 0; 7343 key.offset = 0;
7312 btrfs_set_key_type(&key, BTRFS_EXTENT_DATA_KEY); 7344 btrfs_set_key_type(&key, BTRFS_EXTENT_DATA_KEY);
7313 datasize = btrfs_file_extent_calc_inline_size(name_len); 7345 datasize = btrfs_file_extent_calc_inline_size(name_len);