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.c218
1 files changed, 116 insertions, 102 deletions
diff --git a/fs/btrfs/inode.c b/fs/btrfs/inode.c
index 1c8b0190d031..59cba180fe83 100644
--- a/fs/btrfs/inode.c
+++ b/fs/btrfs/inode.c
@@ -26,7 +26,6 @@
26#include <linux/time.h> 26#include <linux/time.h>
27#include <linux/init.h> 27#include <linux/init.h>
28#include <linux/string.h> 28#include <linux/string.h>
29#include <linux/smp_lock.h>
30#include <linux/backing-dev.h> 29#include <linux/backing-dev.h>
31#include <linux/mpage.h> 30#include <linux/mpage.h>
32#include <linux/swap.h> 31#include <linux/swap.h>
@@ -48,7 +47,6 @@
48#include "ordered-data.h" 47#include "ordered-data.h"
49#include "xattr.h" 48#include "xattr.h"
50#include "tree-log.h" 49#include "tree-log.h"
51#include "ref-cache.h"
52#include "compression.h" 50#include "compression.h"
53#include "locking.h" 51#include "locking.h"
54 52
@@ -369,7 +367,7 @@ again:
369 * inode has not been flagged as nocompress. This flag can 367 * inode has not been flagged as nocompress. This flag can
370 * change at any time if we discover bad compression ratios. 368 * change at any time if we discover bad compression ratios.
371 */ 369 */
372 if (!btrfs_test_flag(inode, NOCOMPRESS) && 370 if (!(BTRFS_I(inode)->flags & BTRFS_INODE_NOCOMPRESS) &&
373 btrfs_test_opt(root, COMPRESS)) { 371 btrfs_test_opt(root, COMPRESS)) {
374 WARN_ON(pages); 372 WARN_ON(pages);
375 pages = kzalloc(sizeof(struct page *) * nr_pages, GFP_NOFS); 373 pages = kzalloc(sizeof(struct page *) * nr_pages, GFP_NOFS);
@@ -470,7 +468,7 @@ again:
470 nr_pages_ret = 0; 468 nr_pages_ret = 0;
471 469
472 /* flag the file so we don't compress in the future */ 470 /* flag the file so we don't compress in the future */
473 btrfs_set_flag(inode, NOCOMPRESS); 471 BTRFS_I(inode)->flags |= BTRFS_INODE_NOCOMPRESS;
474 } 472 }
475 if (will_compress) { 473 if (will_compress) {
476 *num_added += 1; 474 *num_added += 1;
@@ -863,7 +861,7 @@ static int cow_file_range_async(struct inode *inode, struct page *locked_page,
863 async_cow->locked_page = locked_page; 861 async_cow->locked_page = locked_page;
864 async_cow->start = start; 862 async_cow->start = start;
865 863
866 if (btrfs_test_flag(inode, NOCOMPRESS)) 864 if (BTRFS_I(inode)->flags & BTRFS_INODE_NOCOMPRESS)
867 cur_end = end; 865 cur_end = end;
868 else 866 else
869 cur_end = min(end, start + 512 * 1024 - 1); 867 cur_end = min(end, start + 512 * 1024 - 1);
@@ -944,6 +942,7 @@ static noinline int run_delalloc_nocow(struct inode *inode,
944 u64 cow_start; 942 u64 cow_start;
945 u64 cur_offset; 943 u64 cur_offset;
946 u64 extent_end; 944 u64 extent_end;
945 u64 extent_offset;
947 u64 disk_bytenr; 946 u64 disk_bytenr;
948 u64 num_bytes; 947 u64 num_bytes;
949 int extent_type; 948 int extent_type;
@@ -1005,6 +1004,7 @@ next_slot:
1005 if (extent_type == BTRFS_FILE_EXTENT_REG || 1004 if (extent_type == BTRFS_FILE_EXTENT_REG ||
1006 extent_type == BTRFS_FILE_EXTENT_PREALLOC) { 1005 extent_type == BTRFS_FILE_EXTENT_PREALLOC) {
1007 disk_bytenr = btrfs_file_extent_disk_bytenr(leaf, fi); 1006 disk_bytenr = btrfs_file_extent_disk_bytenr(leaf, fi);
1007 extent_offset = btrfs_file_extent_offset(leaf, fi);
1008 extent_end = found_key.offset + 1008 extent_end = found_key.offset +
1009 btrfs_file_extent_num_bytes(leaf, fi); 1009 btrfs_file_extent_num_bytes(leaf, fi);
1010 if (extent_end <= start) { 1010 if (extent_end <= start) {
@@ -1022,9 +1022,10 @@ next_slot:
1022 if (btrfs_extent_readonly(root, disk_bytenr)) 1022 if (btrfs_extent_readonly(root, disk_bytenr))
1023 goto out_check; 1023 goto out_check;
1024 if (btrfs_cross_ref_exist(trans, root, inode->i_ino, 1024 if (btrfs_cross_ref_exist(trans, root, inode->i_ino,
1025 disk_bytenr)) 1025 found_key.offset -
1026 extent_offset, disk_bytenr))
1026 goto out_check; 1027 goto out_check;
1027 disk_bytenr += btrfs_file_extent_offset(leaf, fi); 1028 disk_bytenr += extent_offset;
1028 disk_bytenr += cur_offset - found_key.offset; 1029 disk_bytenr += cur_offset - found_key.offset;
1029 num_bytes = min(end + 1, extent_end) - cur_offset; 1030 num_bytes = min(end + 1, extent_end) - cur_offset;
1030 /* 1031 /*
@@ -1131,10 +1132,10 @@ static int run_delalloc_range(struct inode *inode, struct page *locked_page,
1131 int ret; 1132 int ret;
1132 struct btrfs_root *root = BTRFS_I(inode)->root; 1133 struct btrfs_root *root = BTRFS_I(inode)->root;
1133 1134
1134 if (btrfs_test_flag(inode, NODATACOW)) 1135 if (BTRFS_I(inode)->flags & BTRFS_INODE_NODATACOW)
1135 ret = run_delalloc_nocow(inode, locked_page, start, end, 1136 ret = run_delalloc_nocow(inode, locked_page, start, end,
1136 page_started, 1, nr_written); 1137 page_started, 1, nr_written);
1137 else if (btrfs_test_flag(inode, PREALLOC)) 1138 else if (BTRFS_I(inode)->flags & BTRFS_INODE_PREALLOC)
1138 ret = run_delalloc_nocow(inode, locked_page, start, end, 1139 ret = run_delalloc_nocow(inode, locked_page, start, end,
1139 page_started, 0, nr_written); 1140 page_started, 0, nr_written);
1140 else if (!btrfs_test_opt(root, COMPRESS)) 1141 else if (!btrfs_test_opt(root, COMPRESS))
@@ -1288,7 +1289,7 @@ static int btrfs_submit_bio_hook(struct inode *inode, int rw, struct bio *bio,
1288 int ret = 0; 1289 int ret = 0;
1289 int skip_sum; 1290 int skip_sum;
1290 1291
1291 skip_sum = btrfs_test_flag(inode, NODATASUM); 1292 skip_sum = BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM;
1292 1293
1293 ret = btrfs_bio_wq_end_io(root->fs_info, bio, 0); 1294 ret = btrfs_bio_wq_end_io(root->fs_info, bio, 0);
1294 BUG_ON(ret); 1295 BUG_ON(ret);
@@ -1489,9 +1490,9 @@ static int insert_reserved_file_extent(struct btrfs_trans_handle *trans,
1489 ins.objectid = disk_bytenr; 1490 ins.objectid = disk_bytenr;
1490 ins.offset = disk_num_bytes; 1491 ins.offset = disk_num_bytes;
1491 ins.type = BTRFS_EXTENT_ITEM_KEY; 1492 ins.type = BTRFS_EXTENT_ITEM_KEY;
1492 ret = btrfs_alloc_reserved_extent(trans, root, leaf->start, 1493 ret = btrfs_alloc_reserved_file_extent(trans, root,
1493 root->root_key.objectid, 1494 root->root_key.objectid,
1494 trans->transid, inode->i_ino, &ins); 1495 inode->i_ino, file_pos, &ins);
1495 BUG_ON(ret); 1496 BUG_ON(ret);
1496 btrfs_free_path(path); 1497 btrfs_free_path(path);
1497 1498
@@ -1788,7 +1789,8 @@ static int btrfs_readpage_end_io_hook(struct page *page, u64 start, u64 end,
1788 ClearPageChecked(page); 1789 ClearPageChecked(page);
1789 goto good; 1790 goto good;
1790 } 1791 }
1791 if (btrfs_test_flag(inode, NODATASUM)) 1792
1793 if (BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM)
1792 return 0; 1794 return 0;
1793 1795
1794 if (root->root_key.objectid == BTRFS_DATA_RELOC_TREE_OBJECTID && 1796 if (root->root_key.objectid == BTRFS_DATA_RELOC_TREE_OBJECTID &&
@@ -1956,23 +1958,13 @@ void btrfs_orphan_cleanup(struct btrfs_root *root)
1956 * crossing root thing. we store the inode number in the 1958 * crossing root thing. we store the inode number in the
1957 * offset of the orphan item. 1959 * offset of the orphan item.
1958 */ 1960 */
1959 inode = btrfs_iget_locked(root->fs_info->sb, 1961 found_key.objectid = found_key.offset;
1960 found_key.offset, root); 1962 found_key.type = BTRFS_INODE_ITEM_KEY;
1961 if (!inode) 1963 found_key.offset = 0;
1964 inode = btrfs_iget(root->fs_info->sb, &found_key, root);
1965 if (IS_ERR(inode))
1962 break; 1966 break;
1963 1967
1964 if (inode->i_state & I_NEW) {
1965 BTRFS_I(inode)->root = root;
1966
1967 /* have to set the location manually */
1968 BTRFS_I(inode)->location.objectid = inode->i_ino;
1969 BTRFS_I(inode)->location.type = BTRFS_INODE_ITEM_KEY;
1970 BTRFS_I(inode)->location.offset = 0;
1971
1972 btrfs_read_locked_inode(inode);
1973 unlock_new_inode(inode);
1974 }
1975
1976 /* 1968 /*
1977 * add this inode to the orphan list so btrfs_orphan_del does 1969 * add this inode to the orphan list so btrfs_orphan_del does
1978 * the proper thing when we hit it 1970 * the proper thing when we hit it
@@ -2069,7 +2061,7 @@ static noinline int acls_after_inode_item(struct extent_buffer *leaf,
2069/* 2061/*
2070 * read an inode from the btree into the in-memory inode 2062 * read an inode from the btree into the in-memory inode
2071 */ 2063 */
2072void btrfs_read_locked_inode(struct inode *inode) 2064static void btrfs_read_locked_inode(struct inode *inode)
2073{ 2065{
2074 struct btrfs_path *path; 2066 struct btrfs_path *path;
2075 struct extent_buffer *leaf; 2067 struct extent_buffer *leaf;
@@ -2129,10 +2121,8 @@ void btrfs_read_locked_inode(struct inode *inode)
2129 * any xattrs or acls 2121 * any xattrs or acls
2130 */ 2122 */
2131 maybe_acls = acls_after_inode_item(leaf, path->slots[0], inode->i_ino); 2123 maybe_acls = acls_after_inode_item(leaf, path->slots[0], inode->i_ino);
2132 if (!maybe_acls) { 2124 if (!maybe_acls)
2133 BTRFS_I(inode)->i_acl = NULL; 2125 cache_no_acl(inode);
2134 BTRFS_I(inode)->i_default_acl = NULL;
2135 }
2136 2126
2137 BTRFS_I(inode)->block_group = btrfs_find_block_group(root, 0, 2127 BTRFS_I(inode)->block_group = btrfs_find_block_group(root, 0,
2138 alloc_group_block, 0); 2128 alloc_group_block, 0);
@@ -2164,6 +2154,8 @@ void btrfs_read_locked_inode(struct inode *inode)
2164 init_special_inode(inode, inode->i_mode, rdev); 2154 init_special_inode(inode, inode->i_mode, rdev);
2165 break; 2155 break;
2166 } 2156 }
2157
2158 btrfs_update_iflags(inode);
2167 return; 2159 return;
2168 2160
2169make_bad: 2161make_bad:
@@ -2327,7 +2319,6 @@ err:
2327 btrfs_update_inode(trans, root, dir); 2319 btrfs_update_inode(trans, root, dir);
2328 btrfs_drop_nlink(inode); 2320 btrfs_drop_nlink(inode);
2329 ret = btrfs_update_inode(trans, root, inode); 2321 ret = btrfs_update_inode(trans, root, inode);
2330 dir->i_sb->s_dirt = 1;
2331out: 2322out:
2332 return ret; 2323 return ret;
2333} 2324}
@@ -2599,9 +2590,8 @@ noinline int btrfs_truncate_inode_items(struct btrfs_trans_handle *trans,
2599 struct btrfs_file_extent_item *fi; 2590 struct btrfs_file_extent_item *fi;
2600 u64 extent_start = 0; 2591 u64 extent_start = 0;
2601 u64 extent_num_bytes = 0; 2592 u64 extent_num_bytes = 0;
2593 u64 extent_offset = 0;
2602 u64 item_end = 0; 2594 u64 item_end = 0;
2603 u64 root_gen = 0;
2604 u64 root_owner = 0;
2605 int found_extent; 2595 int found_extent;
2606 int del_item; 2596 int del_item;
2607 int pending_del_nr = 0; 2597 int pending_del_nr = 0;
@@ -2613,8 +2603,8 @@ noinline int btrfs_truncate_inode_items(struct btrfs_trans_handle *trans,
2613 if (root->ref_cows) 2603 if (root->ref_cows)
2614 btrfs_drop_extent_cache(inode, new_size & (~mask), (u64)-1, 0); 2604 btrfs_drop_extent_cache(inode, new_size & (~mask), (u64)-1, 0);
2615 path = btrfs_alloc_path(); 2605 path = btrfs_alloc_path();
2616 path->reada = -1;
2617 BUG_ON(!path); 2606 BUG_ON(!path);
2607 path->reada = -1;
2618 2608
2619 /* FIXME, add redo link to tree so we don't leak on crash */ 2609 /* FIXME, add redo link to tree so we don't leak on crash */
2620 key.objectid = inode->i_ino; 2610 key.objectid = inode->i_ino;
@@ -2716,6 +2706,9 @@ search_again:
2716 extent_num_bytes = 2706 extent_num_bytes =
2717 btrfs_file_extent_disk_num_bytes(leaf, 2707 btrfs_file_extent_disk_num_bytes(leaf,
2718 fi); 2708 fi);
2709 extent_offset = found_key.offset -
2710 btrfs_file_extent_offset(leaf, fi);
2711
2719 /* FIXME blocksize != 4096 */ 2712 /* FIXME blocksize != 4096 */
2720 num_dec = btrfs_file_extent_num_bytes(leaf, fi); 2713 num_dec = btrfs_file_extent_num_bytes(leaf, fi);
2721 if (extent_start != 0) { 2714 if (extent_start != 0) {
@@ -2723,8 +2716,6 @@ search_again:
2723 if (root->ref_cows) 2716 if (root->ref_cows)
2724 inode_sub_bytes(inode, num_dec); 2717 inode_sub_bytes(inode, num_dec);
2725 } 2718 }
2726 root_gen = btrfs_header_generation(leaf);
2727 root_owner = btrfs_header_owner(leaf);
2728 } 2719 }
2729 } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) { 2720 } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) {
2730 /* 2721 /*
@@ -2768,12 +2759,12 @@ delete:
2768 } else { 2759 } else {
2769 break; 2760 break;
2770 } 2761 }
2771 if (found_extent) { 2762 if (found_extent && root->ref_cows) {
2772 btrfs_set_path_blocking(path); 2763 btrfs_set_path_blocking(path);
2773 ret = btrfs_free_extent(trans, root, extent_start, 2764 ret = btrfs_free_extent(trans, root, extent_start,
2774 extent_num_bytes, 2765 extent_num_bytes, 0,
2775 leaf->start, root_owner, 2766 btrfs_header_owner(leaf),
2776 root_gen, inode->i_ino, 0); 2767 inode->i_ino, extent_offset);
2777 BUG_ON(ret); 2768 BUG_ON(ret);
2778 } 2769 }
2779next: 2770next:
@@ -2811,7 +2802,6 @@ error:
2811 pending_del_nr); 2802 pending_del_nr);
2812 } 2803 }
2813 btrfs_free_path(path); 2804 btrfs_free_path(path);
2814 inode->i_sb->s_dirt = 1;
2815 return ret; 2805 return ret;
2816} 2806}
2817 2807
@@ -3105,13 +3095,56 @@ static int fixup_tree_root_location(struct btrfs_root *root,
3105 return 0; 3095 return 0;
3106} 3096}
3107 3097
3098static void inode_tree_add(struct inode *inode)
3099{
3100 struct btrfs_root *root = BTRFS_I(inode)->root;
3101 struct btrfs_inode *entry;
3102 struct rb_node **p;
3103 struct rb_node *parent;
3104
3105again:
3106 p = &root->inode_tree.rb_node;
3107 parent = NULL;
3108
3109 spin_lock(&root->inode_lock);
3110 while (*p) {
3111 parent = *p;
3112 entry = rb_entry(parent, struct btrfs_inode, rb_node);
3113
3114 if (inode->i_ino < entry->vfs_inode.i_ino)
3115 p = &parent->rb_left;
3116 else if (inode->i_ino > entry->vfs_inode.i_ino)
3117 p = &parent->rb_right;
3118 else {
3119 WARN_ON(!(entry->vfs_inode.i_state &
3120 (I_WILL_FREE | I_FREEING | I_CLEAR)));
3121 rb_erase(parent, &root->inode_tree);
3122 RB_CLEAR_NODE(parent);
3123 spin_unlock(&root->inode_lock);
3124 goto again;
3125 }
3126 }
3127 rb_link_node(&BTRFS_I(inode)->rb_node, parent, p);
3128 rb_insert_color(&BTRFS_I(inode)->rb_node, &root->inode_tree);
3129 spin_unlock(&root->inode_lock);
3130}
3131
3132static void inode_tree_del(struct inode *inode)
3133{
3134 struct btrfs_root *root = BTRFS_I(inode)->root;
3135
3136 spin_lock(&root->inode_lock);
3137 if (!RB_EMPTY_NODE(&BTRFS_I(inode)->rb_node)) {
3138 rb_erase(&BTRFS_I(inode)->rb_node, &root->inode_tree);
3139 RB_CLEAR_NODE(&BTRFS_I(inode)->rb_node);
3140 }
3141 spin_unlock(&root->inode_lock);
3142}
3143
3108static noinline void init_btrfs_i(struct inode *inode) 3144static noinline void init_btrfs_i(struct inode *inode)
3109{ 3145{
3110 struct btrfs_inode *bi = BTRFS_I(inode); 3146 struct btrfs_inode *bi = BTRFS_I(inode);
3111 3147
3112 bi->i_acl = BTRFS_ACL_NOT_CACHED;
3113 bi->i_default_acl = BTRFS_ACL_NOT_CACHED;
3114
3115 bi->generation = 0; 3148 bi->generation = 0;
3116 bi->sequence = 0; 3149 bi->sequence = 0;
3117 bi->last_trans = 0; 3150 bi->last_trans = 0;
@@ -3130,6 +3163,7 @@ static noinline void init_btrfs_i(struct inode *inode)
3130 inode->i_mapping, GFP_NOFS); 3163 inode->i_mapping, GFP_NOFS);
3131 INIT_LIST_HEAD(&BTRFS_I(inode)->delalloc_inodes); 3164 INIT_LIST_HEAD(&BTRFS_I(inode)->delalloc_inodes);
3132 INIT_LIST_HEAD(&BTRFS_I(inode)->ordered_operations); 3165 INIT_LIST_HEAD(&BTRFS_I(inode)->ordered_operations);
3166 RB_CLEAR_NODE(&BTRFS_I(inode)->rb_node);
3133 btrfs_ordered_inode_tree_init(&BTRFS_I(inode)->ordered_tree); 3167 btrfs_ordered_inode_tree_init(&BTRFS_I(inode)->ordered_tree);
3134 mutex_init(&BTRFS_I(inode)->extent_mutex); 3168 mutex_init(&BTRFS_I(inode)->extent_mutex);
3135 mutex_init(&BTRFS_I(inode)->log_mutex); 3169 mutex_init(&BTRFS_I(inode)->log_mutex);
@@ -3152,26 +3186,9 @@ static int btrfs_find_actor(struct inode *inode, void *opaque)
3152 args->root == BTRFS_I(inode)->root; 3186 args->root == BTRFS_I(inode)->root;
3153} 3187}
3154 3188
3155struct inode *btrfs_ilookup(struct super_block *s, u64 objectid, 3189static struct inode *btrfs_iget_locked(struct super_block *s,
3156 struct btrfs_root *root, int wait) 3190 u64 objectid,
3157{ 3191 struct btrfs_root *root)
3158 struct inode *inode;
3159 struct btrfs_iget_args args;
3160 args.ino = objectid;
3161 args.root = root;
3162
3163 if (wait) {
3164 inode = ilookup5(s, objectid, btrfs_find_actor,
3165 (void *)&args);
3166 } else {
3167 inode = ilookup5_nowait(s, objectid, btrfs_find_actor,
3168 (void *)&args);
3169 }
3170 return inode;
3171}
3172
3173struct inode *btrfs_iget_locked(struct super_block *s, u64 objectid,
3174 struct btrfs_root *root)
3175{ 3192{
3176 struct inode *inode; 3193 struct inode *inode;
3177 struct btrfs_iget_args args; 3194 struct btrfs_iget_args args;
@@ -3188,24 +3205,21 @@ struct inode *btrfs_iget_locked(struct super_block *s, u64 objectid,
3188 * Returns in *is_new if the inode was read from disk 3205 * Returns in *is_new if the inode was read from disk
3189 */ 3206 */
3190struct inode *btrfs_iget(struct super_block *s, struct btrfs_key *location, 3207struct inode *btrfs_iget(struct super_block *s, struct btrfs_key *location,
3191 struct btrfs_root *root, int *is_new) 3208 struct btrfs_root *root)
3192{ 3209{
3193 struct inode *inode; 3210 struct inode *inode;
3194 3211
3195 inode = btrfs_iget_locked(s, location->objectid, root); 3212 inode = btrfs_iget_locked(s, location->objectid, root);
3196 if (!inode) 3213 if (!inode)
3197 return ERR_PTR(-EACCES); 3214 return ERR_PTR(-ENOMEM);
3198 3215
3199 if (inode->i_state & I_NEW) { 3216 if (inode->i_state & I_NEW) {
3200 BTRFS_I(inode)->root = root; 3217 BTRFS_I(inode)->root = root;
3201 memcpy(&BTRFS_I(inode)->location, location, sizeof(*location)); 3218 memcpy(&BTRFS_I(inode)->location, location, sizeof(*location));
3202 btrfs_read_locked_inode(inode); 3219 btrfs_read_locked_inode(inode);
3220
3221 inode_tree_add(inode);
3203 unlock_new_inode(inode); 3222 unlock_new_inode(inode);
3204 if (is_new)
3205 *is_new = 1;
3206 } else {
3207 if (is_new)
3208 *is_new = 0;
3209 } 3223 }
3210 3224
3211 return inode; 3225 return inode;
@@ -3218,7 +3232,7 @@ struct inode *btrfs_lookup_dentry(struct inode *dir, struct dentry *dentry)
3218 struct btrfs_root *root = bi->root; 3232 struct btrfs_root *root = bi->root;
3219 struct btrfs_root *sub_root = root; 3233 struct btrfs_root *sub_root = root;
3220 struct btrfs_key location; 3234 struct btrfs_key location;
3221 int ret, new; 3235 int ret;
3222 3236
3223 if (dentry->d_name.len > BTRFS_NAME_LEN) 3237 if (dentry->d_name.len > BTRFS_NAME_LEN)
3224 return ERR_PTR(-ENAMETOOLONG); 3238 return ERR_PTR(-ENAMETOOLONG);
@@ -3236,7 +3250,7 @@ struct inode *btrfs_lookup_dentry(struct inode *dir, struct dentry *dentry)
3236 return ERR_PTR(ret); 3250 return ERR_PTR(ret);
3237 if (ret > 0) 3251 if (ret > 0)
3238 return ERR_PTR(-ENOENT); 3252 return ERR_PTR(-ENOENT);
3239 inode = btrfs_iget(dir->i_sb, &location, sub_root, &new); 3253 inode = btrfs_iget(dir->i_sb, &location, sub_root);
3240 if (IS_ERR(inode)) 3254 if (IS_ERR(inode))
3241 return ERR_CAST(inode); 3255 return ERR_CAST(inode);
3242 } 3256 }
@@ -3572,12 +3586,6 @@ static struct inode *btrfs_new_inode(struct btrfs_trans_handle *trans,
3572 owner = 1; 3586 owner = 1;
3573 BTRFS_I(inode)->block_group = 3587 BTRFS_I(inode)->block_group =
3574 btrfs_find_block_group(root, 0, alloc_hint, owner); 3588 btrfs_find_block_group(root, 0, alloc_hint, owner);
3575 if ((mode & S_IFREG)) {
3576 if (btrfs_test_opt(root, NODATASUM))
3577 btrfs_set_flag(inode, NODATASUM);
3578 if (btrfs_test_opt(root, NODATACOW))
3579 btrfs_set_flag(inode, NODATACOW);
3580 }
3581 3589
3582 key[0].objectid = objectid; 3590 key[0].objectid = objectid;
3583 btrfs_set_key_type(&key[0], BTRFS_INODE_ITEM_KEY); 3591 btrfs_set_key_type(&key[0], BTRFS_INODE_ITEM_KEY);
@@ -3630,7 +3638,17 @@ static struct inode *btrfs_new_inode(struct btrfs_trans_handle *trans,
3630 location->offset = 0; 3638 location->offset = 0;
3631 btrfs_set_key_type(location, BTRFS_INODE_ITEM_KEY); 3639 btrfs_set_key_type(location, BTRFS_INODE_ITEM_KEY);
3632 3640
3641 btrfs_inherit_iflags(inode, dir);
3642
3643 if ((mode & S_IFREG)) {
3644 if (btrfs_test_opt(root, NODATASUM))
3645 BTRFS_I(inode)->flags |= BTRFS_INODE_NODATASUM;
3646 if (btrfs_test_opt(root, NODATACOW))
3647 BTRFS_I(inode)->flags |= BTRFS_INODE_NODATACOW;
3648 }
3649
3633 insert_inode_hash(inode); 3650 insert_inode_hash(inode);
3651 inode_tree_add(inode);
3634 return inode; 3652 return inode;
3635fail: 3653fail:
3636 if (dir) 3654 if (dir)
@@ -3750,7 +3768,6 @@ static int btrfs_mknod(struct inode *dir, struct dentry *dentry,
3750 init_special_inode(inode, inode->i_mode, rdev); 3768 init_special_inode(inode, inode->i_mode, rdev);
3751 btrfs_update_inode(trans, root, inode); 3769 btrfs_update_inode(trans, root, inode);
3752 } 3770 }
3753 dir->i_sb->s_dirt = 1;
3754 btrfs_update_inode_block_group(trans, inode); 3771 btrfs_update_inode_block_group(trans, inode);
3755 btrfs_update_inode_block_group(trans, dir); 3772 btrfs_update_inode_block_group(trans, dir);
3756out_unlock: 3773out_unlock:
@@ -3815,7 +3832,6 @@ static int btrfs_create(struct inode *dir, struct dentry *dentry,
3815 inode->i_op = &btrfs_file_inode_operations; 3832 inode->i_op = &btrfs_file_inode_operations;
3816 BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops; 3833 BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops;
3817 } 3834 }
3818 dir->i_sb->s_dirt = 1;
3819 btrfs_update_inode_block_group(trans, inode); 3835 btrfs_update_inode_block_group(trans, inode);
3820 btrfs_update_inode_block_group(trans, dir); 3836 btrfs_update_inode_block_group(trans, dir);
3821out_unlock: 3837out_unlock:
@@ -3862,7 +3878,6 @@ static int btrfs_link(struct dentry *old_dentry, struct inode *dir,
3862 if (err) 3878 if (err)
3863 drop_inode = 1; 3879 drop_inode = 1;
3864 3880
3865 dir->i_sb->s_dirt = 1;
3866 btrfs_update_inode_block_group(trans, dir); 3881 btrfs_update_inode_block_group(trans, dir);
3867 err = btrfs_update_inode(trans, root, inode); 3882 err = btrfs_update_inode(trans, root, inode);
3868 3883
@@ -3944,7 +3959,6 @@ static int btrfs_mkdir(struct inode *dir, struct dentry *dentry, int mode)
3944 3959
3945 d_instantiate(dentry, inode); 3960 d_instantiate(dentry, inode);
3946 drop_on_err = 0; 3961 drop_on_err = 0;
3947 dir->i_sb->s_dirt = 1;
3948 btrfs_update_inode_block_group(trans, inode); 3962 btrfs_update_inode_block_group(trans, inode);
3949 btrfs_update_inode_block_group(trans, dir); 3963 btrfs_update_inode_block_group(trans, dir);
3950 3964
@@ -4628,8 +4642,6 @@ struct inode *btrfs_alloc_inode(struct super_block *sb)
4628 ei->last_trans = 0; 4642 ei->last_trans = 0;
4629 ei->logged_trans = 0; 4643 ei->logged_trans = 0;
4630 btrfs_ordered_inode_tree_init(&ei->ordered_tree); 4644 btrfs_ordered_inode_tree_init(&ei->ordered_tree);
4631 ei->i_acl = BTRFS_ACL_NOT_CACHED;
4632 ei->i_default_acl = BTRFS_ACL_NOT_CACHED;
4633 INIT_LIST_HEAD(&ei->i_orphan); 4645 INIT_LIST_HEAD(&ei->i_orphan);
4634 INIT_LIST_HEAD(&ei->ordered_operations); 4646 INIT_LIST_HEAD(&ei->ordered_operations);
4635 return &ei->vfs_inode; 4647 return &ei->vfs_inode;
@@ -4643,13 +4655,6 @@ void btrfs_destroy_inode(struct inode *inode)
4643 WARN_ON(!list_empty(&inode->i_dentry)); 4655 WARN_ON(!list_empty(&inode->i_dentry));
4644 WARN_ON(inode->i_data.nrpages); 4656 WARN_ON(inode->i_data.nrpages);
4645 4657
4646 if (BTRFS_I(inode)->i_acl &&
4647 BTRFS_I(inode)->i_acl != BTRFS_ACL_NOT_CACHED)
4648 posix_acl_release(BTRFS_I(inode)->i_acl);
4649 if (BTRFS_I(inode)->i_default_acl &&
4650 BTRFS_I(inode)->i_default_acl != BTRFS_ACL_NOT_CACHED)
4651 posix_acl_release(BTRFS_I(inode)->i_default_acl);
4652
4653 /* 4658 /*
4654 * Make sure we're properly removed from the ordered operation 4659 * Make sure we're properly removed from the ordered operation
4655 * lists. 4660 * lists.
@@ -4683,6 +4688,7 @@ void btrfs_destroy_inode(struct inode *inode)
4683 btrfs_put_ordered_extent(ordered); 4688 btrfs_put_ordered_extent(ordered);
4684 } 4689 }
4685 } 4690 }
4691 inode_tree_del(inode);
4686 btrfs_drop_extent_cache(inode, 0, (u64)-1, 0); 4692 btrfs_drop_extent_cache(inode, 0, (u64)-1, 0);
4687 kmem_cache_free(btrfs_inode_cachep, BTRFS_I(inode)); 4693 kmem_cache_free(btrfs_inode_cachep, BTRFS_I(inode));
4688} 4694}
@@ -4786,8 +4792,7 @@ static int btrfs_rename(struct inode *old_dir, struct dentry *old_dentry,
4786 * and the replacement file is large. Start IO on it now so 4792 * and the replacement file is large. Start IO on it now so
4787 * we don't add too much work to the end of the transaction 4793 * we don't add too much work to the end of the transaction
4788 */ 4794 */
4789 if (new_inode && old_inode && S_ISREG(old_inode->i_mode) && 4795 if (new_inode && S_ISREG(old_inode->i_mode) && new_inode->i_size &&
4790 new_inode->i_size &&
4791 old_inode->i_size > BTRFS_ORDERED_OPERATIONS_FLUSH_LIMIT) 4796 old_inode->i_size > BTRFS_ORDERED_OPERATIONS_FLUSH_LIMIT)
4792 filemap_flush(old_inode->i_mapping); 4797 filemap_flush(old_inode->i_mapping);
4793 4798
@@ -4972,7 +4977,6 @@ static int btrfs_symlink(struct inode *dir, struct dentry *dentry,
4972 inode->i_op = &btrfs_file_inode_operations; 4977 inode->i_op = &btrfs_file_inode_operations;
4973 BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops; 4978 BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops;
4974 } 4979 }
4975 dir->i_sb->s_dirt = 1;
4976 btrfs_update_inode_block_group(trans, inode); 4980 btrfs_update_inode_block_group(trans, inode);
4977 btrfs_update_inode_block_group(trans, dir); 4981 btrfs_update_inode_block_group(trans, dir);
4978 if (drop_inode) 4982 if (drop_inode)
@@ -5061,7 +5065,7 @@ static int prealloc_file_range(struct btrfs_trans_handle *trans,
5061out: 5065out:
5062 if (cur_offset > start) { 5066 if (cur_offset > start) {
5063 inode->i_ctime = CURRENT_TIME; 5067 inode->i_ctime = CURRENT_TIME;
5064 btrfs_set_flag(inode, PREALLOC); 5068 BTRFS_I(inode)->flags |= BTRFS_INODE_PREALLOC;
5065 if (!(mode & FALLOC_FL_KEEP_SIZE) && 5069 if (!(mode & FALLOC_FL_KEEP_SIZE) &&
5066 cur_offset > i_size_read(inode)) 5070 cur_offset > i_size_read(inode))
5067 btrfs_i_size_write(inode, cur_offset); 5071 btrfs_i_size_write(inode, cur_offset);
@@ -5084,6 +5088,7 @@ static long btrfs_fallocate(struct inode *inode, int mode,
5084 u64 mask = BTRFS_I(inode)->root->sectorsize - 1; 5088 u64 mask = BTRFS_I(inode)->root->sectorsize - 1;
5085 struct extent_map *em; 5089 struct extent_map *em;
5086 struct btrfs_trans_handle *trans; 5090 struct btrfs_trans_handle *trans;
5091 struct btrfs_root *root;
5087 int ret; 5092 int ret;
5088 5093
5089 alloc_start = offset & ~mask; 5094 alloc_start = offset & ~mask;
@@ -5102,6 +5107,13 @@ static long btrfs_fallocate(struct inode *inode, int mode,
5102 goto out; 5107 goto out;
5103 } 5108 }
5104 5109
5110 root = BTRFS_I(inode)->root;
5111
5112 ret = btrfs_check_data_free_space(root, inode,
5113 alloc_end - alloc_start);
5114 if (ret)
5115 goto out;
5116
5105 locked_end = alloc_end - 1; 5117 locked_end = alloc_end - 1;
5106 while (1) { 5118 while (1) {
5107 struct btrfs_ordered_extent *ordered; 5119 struct btrfs_ordered_extent *ordered;
@@ -5109,7 +5121,7 @@ static long btrfs_fallocate(struct inode *inode, int mode,
5109 trans = btrfs_start_transaction(BTRFS_I(inode)->root, 1); 5121 trans = btrfs_start_transaction(BTRFS_I(inode)->root, 1);
5110 if (!trans) { 5122 if (!trans) {
5111 ret = -EIO; 5123 ret = -EIO;
5112 goto out; 5124 goto out_free;
5113 } 5125 }
5114 5126
5115 /* the extent lock is ordered inside the running 5127 /* the extent lock is ordered inside the running
@@ -5170,6 +5182,8 @@ static long btrfs_fallocate(struct inode *inode, int mode,
5170 GFP_NOFS); 5182 GFP_NOFS);
5171 5183
5172 btrfs_end_transaction(trans, BTRFS_I(inode)->root); 5184 btrfs_end_transaction(trans, BTRFS_I(inode)->root);
5185out_free:
5186 btrfs_free_reserved_data_space(root, inode, alloc_end - alloc_start);
5173out: 5187out:
5174 mutex_unlock(&inode->i_mutex); 5188 mutex_unlock(&inode->i_mutex);
5175 return ret; 5189 return ret;
@@ -5182,7 +5196,7 @@ static int btrfs_set_page_dirty(struct page *page)
5182 5196
5183static int btrfs_permission(struct inode *inode, int mask) 5197static int btrfs_permission(struct inode *inode, int mask)
5184{ 5198{
5185 if (btrfs_test_flag(inode, READONLY) && (mask & MAY_WRITE)) 5199 if ((BTRFS_I(inode)->flags & BTRFS_INODE_READONLY) && (mask & MAY_WRITE))
5186 return -EACCES; 5200 return -EACCES;
5187 return generic_permission(inode, mask, btrfs_check_acl); 5201 return generic_permission(inode, mask, btrfs_check_acl);
5188} 5202}