diff options
Diffstat (limited to 'fs/btrfs/inode.c')
-rw-r--r-- | fs/btrfs/inode.c | 145 |
1 files changed, 98 insertions, 47 deletions
diff --git a/fs/btrfs/inode.c b/fs/btrfs/inode.c index 13e6255182e3..15fceefbca0a 100644 --- a/fs/btrfs/inode.c +++ b/fs/btrfs/inode.c | |||
@@ -1061,7 +1061,8 @@ static noinline int run_delalloc_nocow(struct inode *inode, | |||
1061 | u64 ino = btrfs_ino(inode); | 1061 | u64 ino = btrfs_ino(inode); |
1062 | 1062 | ||
1063 | path = btrfs_alloc_path(); | 1063 | path = btrfs_alloc_path(); |
1064 | BUG_ON(!path); | 1064 | if (!path) |
1065 | return -ENOMEM; | ||
1065 | 1066 | ||
1066 | nolock = btrfs_is_free_space_inode(root, inode); | 1067 | nolock = btrfs_is_free_space_inode(root, inode); |
1067 | 1068 | ||
@@ -1282,17 +1283,16 @@ static int run_delalloc_range(struct inode *inode, struct page *locked_page, | |||
1282 | return ret; | 1283 | return ret; |
1283 | } | 1284 | } |
1284 | 1285 | ||
1285 | static int btrfs_split_extent_hook(struct inode *inode, | 1286 | static void btrfs_split_extent_hook(struct inode *inode, |
1286 | struct extent_state *orig, u64 split) | 1287 | struct extent_state *orig, u64 split) |
1287 | { | 1288 | { |
1288 | /* not delalloc, ignore it */ | 1289 | /* not delalloc, ignore it */ |
1289 | if (!(orig->state & EXTENT_DELALLOC)) | 1290 | if (!(orig->state & EXTENT_DELALLOC)) |
1290 | return 0; | 1291 | return; |
1291 | 1292 | ||
1292 | spin_lock(&BTRFS_I(inode)->lock); | 1293 | spin_lock(&BTRFS_I(inode)->lock); |
1293 | BTRFS_I(inode)->outstanding_extents++; | 1294 | BTRFS_I(inode)->outstanding_extents++; |
1294 | spin_unlock(&BTRFS_I(inode)->lock); | 1295 | spin_unlock(&BTRFS_I(inode)->lock); |
1295 | return 0; | ||
1296 | } | 1296 | } |
1297 | 1297 | ||
1298 | /* | 1298 | /* |
@@ -1301,18 +1301,17 @@ static int btrfs_split_extent_hook(struct inode *inode, | |||
1301 | * extents, such as when we are doing sequential writes, so we can properly | 1301 | * extents, such as when we are doing sequential writes, so we can properly |
1302 | * account for the metadata space we'll need. | 1302 | * account for the metadata space we'll need. |
1303 | */ | 1303 | */ |
1304 | static int btrfs_merge_extent_hook(struct inode *inode, | 1304 | static void btrfs_merge_extent_hook(struct inode *inode, |
1305 | struct extent_state *new, | 1305 | struct extent_state *new, |
1306 | struct extent_state *other) | 1306 | struct extent_state *other) |
1307 | { | 1307 | { |
1308 | /* not delalloc, ignore it */ | 1308 | /* not delalloc, ignore it */ |
1309 | if (!(other->state & EXTENT_DELALLOC)) | 1309 | if (!(other->state & EXTENT_DELALLOC)) |
1310 | return 0; | 1310 | return; |
1311 | 1311 | ||
1312 | spin_lock(&BTRFS_I(inode)->lock); | 1312 | spin_lock(&BTRFS_I(inode)->lock); |
1313 | BTRFS_I(inode)->outstanding_extents--; | 1313 | BTRFS_I(inode)->outstanding_extents--; |
1314 | spin_unlock(&BTRFS_I(inode)->lock); | 1314 | spin_unlock(&BTRFS_I(inode)->lock); |
1315 | return 0; | ||
1316 | } | 1315 | } |
1317 | 1316 | ||
1318 | /* | 1317 | /* |
@@ -1320,8 +1319,8 @@ static int btrfs_merge_extent_hook(struct inode *inode, | |||
1320 | * bytes in this file, and to maintain the list of inodes that | 1319 | * bytes in this file, and to maintain the list of inodes that |
1321 | * have pending delalloc work to be done. | 1320 | * have pending delalloc work to be done. |
1322 | */ | 1321 | */ |
1323 | static int btrfs_set_bit_hook(struct inode *inode, | 1322 | static void btrfs_set_bit_hook(struct inode *inode, |
1324 | struct extent_state *state, int *bits) | 1323 | struct extent_state *state, int *bits) |
1325 | { | 1324 | { |
1326 | 1325 | ||
1327 | /* | 1326 | /* |
@@ -1351,14 +1350,13 @@ static int btrfs_set_bit_hook(struct inode *inode, | |||
1351 | } | 1350 | } |
1352 | spin_unlock(&root->fs_info->delalloc_lock); | 1351 | spin_unlock(&root->fs_info->delalloc_lock); |
1353 | } | 1352 | } |
1354 | return 0; | ||
1355 | } | 1353 | } |
1356 | 1354 | ||
1357 | /* | 1355 | /* |
1358 | * extent_io.c clear_bit_hook, see set_bit_hook for why | 1356 | * extent_io.c clear_bit_hook, see set_bit_hook for why |
1359 | */ | 1357 | */ |
1360 | static int btrfs_clear_bit_hook(struct inode *inode, | 1358 | static void btrfs_clear_bit_hook(struct inode *inode, |
1361 | struct extent_state *state, int *bits) | 1359 | struct extent_state *state, int *bits) |
1362 | { | 1360 | { |
1363 | /* | 1361 | /* |
1364 | * set_bit and clear bit hooks normally require _irqsave/restore | 1362 | * set_bit and clear bit hooks normally require _irqsave/restore |
@@ -1395,7 +1393,6 @@ static int btrfs_clear_bit_hook(struct inode *inode, | |||
1395 | } | 1393 | } |
1396 | spin_unlock(&root->fs_info->delalloc_lock); | 1394 | spin_unlock(&root->fs_info->delalloc_lock); |
1397 | } | 1395 | } |
1398 | return 0; | ||
1399 | } | 1396 | } |
1400 | 1397 | ||
1401 | /* | 1398 | /* |
@@ -1645,7 +1642,8 @@ static int insert_reserved_file_extent(struct btrfs_trans_handle *trans, | |||
1645 | int ret; | 1642 | int ret; |
1646 | 1643 | ||
1647 | path = btrfs_alloc_path(); | 1644 | path = btrfs_alloc_path(); |
1648 | BUG_ON(!path); | 1645 | if (!path) |
1646 | return -ENOMEM; | ||
1649 | 1647 | ||
1650 | path->leave_spinning = 1; | 1648 | path->leave_spinning = 1; |
1651 | 1649 | ||
@@ -2215,7 +2213,8 @@ int btrfs_orphan_add(struct btrfs_trans_handle *trans, struct inode *inode) | |||
2215 | 2213 | ||
2216 | if (!root->orphan_block_rsv) { | 2214 | if (!root->orphan_block_rsv) { |
2217 | block_rsv = btrfs_alloc_block_rsv(root); | 2215 | block_rsv = btrfs_alloc_block_rsv(root); |
2218 | BUG_ON(!block_rsv); | 2216 | if (!block_rsv) |
2217 | return -ENOMEM; | ||
2219 | } | 2218 | } |
2220 | 2219 | ||
2221 | spin_lock(&root->orphan_lock); | 2220 | spin_lock(&root->orphan_lock); |
@@ -2517,7 +2516,9 @@ static void btrfs_read_locked_inode(struct inode *inode) | |||
2517 | filled = true; | 2516 | filled = true; |
2518 | 2517 | ||
2519 | path = btrfs_alloc_path(); | 2518 | path = btrfs_alloc_path(); |
2520 | BUG_ON(!path); | 2519 | if (!path) |
2520 | goto make_bad; | ||
2521 | |||
2521 | path->leave_spinning = 1; | 2522 | path->leave_spinning = 1; |
2522 | memcpy(&location, &BTRFS_I(inode)->location, sizeof(location)); | 2523 | memcpy(&location, &BTRFS_I(inode)->location, sizeof(location)); |
2523 | 2524 | ||
@@ -2998,13 +2999,16 @@ static int btrfs_unlink(struct inode *dir, struct dentry *dentry) | |||
2998 | 2999 | ||
2999 | ret = btrfs_unlink_inode(trans, root, dir, dentry->d_inode, | 3000 | ret = btrfs_unlink_inode(trans, root, dir, dentry->d_inode, |
3000 | dentry->d_name.name, dentry->d_name.len); | 3001 | dentry->d_name.name, dentry->d_name.len); |
3001 | BUG_ON(ret); | 3002 | if (ret) |
3003 | goto out; | ||
3002 | 3004 | ||
3003 | if (inode->i_nlink == 0) { | 3005 | if (inode->i_nlink == 0) { |
3004 | ret = btrfs_orphan_add(trans, inode); | 3006 | ret = btrfs_orphan_add(trans, inode); |
3005 | BUG_ON(ret); | 3007 | if (ret) |
3008 | goto out; | ||
3006 | } | 3009 | } |
3007 | 3010 | ||
3011 | out: | ||
3008 | nr = trans->blocks_used; | 3012 | nr = trans->blocks_used; |
3009 | __unlink_end_trans(trans, root); | 3013 | __unlink_end_trans(trans, root); |
3010 | btrfs_btree_balance_dirty(root, nr); | 3014 | btrfs_btree_balance_dirty(root, nr); |
@@ -3147,6 +3151,11 @@ int btrfs_truncate_inode_items(struct btrfs_trans_handle *trans, | |||
3147 | 3151 | ||
3148 | BUG_ON(new_size > 0 && min_type != BTRFS_EXTENT_DATA_KEY); | 3152 | BUG_ON(new_size > 0 && min_type != BTRFS_EXTENT_DATA_KEY); |
3149 | 3153 | ||
3154 | path = btrfs_alloc_path(); | ||
3155 | if (!path) | ||
3156 | return -ENOMEM; | ||
3157 | path->reada = -1; | ||
3158 | |||
3150 | if (root->ref_cows || root == root->fs_info->tree_root) | 3159 | if (root->ref_cows || root == root->fs_info->tree_root) |
3151 | btrfs_drop_extent_cache(inode, new_size & (~mask), (u64)-1, 0); | 3160 | btrfs_drop_extent_cache(inode, new_size & (~mask), (u64)-1, 0); |
3152 | 3161 | ||
@@ -3159,10 +3168,6 @@ int btrfs_truncate_inode_items(struct btrfs_trans_handle *trans, | |||
3159 | if (min_type == 0 && root == BTRFS_I(inode)->root) | 3168 | if (min_type == 0 && root == BTRFS_I(inode)->root) |
3160 | btrfs_kill_delayed_inode_items(inode); | 3169 | btrfs_kill_delayed_inode_items(inode); |
3161 | 3170 | ||
3162 | path = btrfs_alloc_path(); | ||
3163 | BUG_ON(!path); | ||
3164 | path->reada = -1; | ||
3165 | |||
3166 | key.objectid = ino; | 3171 | key.objectid = ino; |
3167 | key.offset = (u64)-1; | 3172 | key.offset = (u64)-1; |
3168 | key.type = (u8)-1; | 3173 | key.type = (u8)-1; |
@@ -3690,7 +3695,8 @@ static int btrfs_inode_by_name(struct inode *dir, struct dentry *dentry, | |||
3690 | int ret = 0; | 3695 | int ret = 0; |
3691 | 3696 | ||
3692 | path = btrfs_alloc_path(); | 3697 | path = btrfs_alloc_path(); |
3693 | BUG_ON(!path); | 3698 | if (!path) |
3699 | return -ENOMEM; | ||
3694 | 3700 | ||
3695 | di = btrfs_lookup_dir_item(NULL, root, path, btrfs_ino(dir), name, | 3701 | di = btrfs_lookup_dir_item(NULL, root, path, btrfs_ino(dir), name, |
3696 | namelen, 0); | 3702 | namelen, 0); |
@@ -3946,6 +3952,7 @@ struct inode *btrfs_iget(struct super_block *s, struct btrfs_key *location, | |||
3946 | struct btrfs_root *root, int *new) | 3952 | struct btrfs_root *root, int *new) |
3947 | { | 3953 | { |
3948 | struct inode *inode; | 3954 | struct inode *inode; |
3955 | int bad_inode = 0; | ||
3949 | 3956 | ||
3950 | inode = btrfs_iget_locked(s, location->objectid, root); | 3957 | inode = btrfs_iget_locked(s, location->objectid, root); |
3951 | if (!inode) | 3958 | if (!inode) |
@@ -3955,10 +3962,19 @@ struct inode *btrfs_iget(struct super_block *s, struct btrfs_key *location, | |||
3955 | BTRFS_I(inode)->root = root; | 3962 | BTRFS_I(inode)->root = root; |
3956 | memcpy(&BTRFS_I(inode)->location, location, sizeof(*location)); | 3963 | memcpy(&BTRFS_I(inode)->location, location, sizeof(*location)); |
3957 | btrfs_read_locked_inode(inode); | 3964 | btrfs_read_locked_inode(inode); |
3958 | inode_tree_add(inode); | 3965 | if (!is_bad_inode(inode)) { |
3959 | unlock_new_inode(inode); | 3966 | inode_tree_add(inode); |
3960 | if (new) | 3967 | unlock_new_inode(inode); |
3961 | *new = 1; | 3968 | if (new) |
3969 | *new = 1; | ||
3970 | } else { | ||
3971 | bad_inode = 1; | ||
3972 | } | ||
3973 | } | ||
3974 | |||
3975 | if (bad_inode) { | ||
3976 | iput(inode); | ||
3977 | inode = ERR_PTR(-ESTALE); | ||
3962 | } | 3978 | } |
3963 | 3979 | ||
3964 | return inode; | 3980 | return inode; |
@@ -3993,12 +4009,19 @@ struct inode *btrfs_lookup_dentry(struct inode *dir, struct dentry *dentry) | |||
3993 | struct btrfs_root *sub_root = root; | 4009 | struct btrfs_root *sub_root = root; |
3994 | struct btrfs_key location; | 4010 | struct btrfs_key location; |
3995 | int index; | 4011 | int index; |
3996 | int ret; | 4012 | int ret = 0; |
3997 | 4013 | ||
3998 | if (dentry->d_name.len > BTRFS_NAME_LEN) | 4014 | if (dentry->d_name.len > BTRFS_NAME_LEN) |
3999 | return ERR_PTR(-ENAMETOOLONG); | 4015 | return ERR_PTR(-ENAMETOOLONG); |
4000 | 4016 | ||
4001 | ret = btrfs_inode_by_name(dir, dentry, &location); | 4017 | if (unlikely(d_need_lookup(dentry))) { |
4018 | memcpy(&location, dentry->d_fsdata, sizeof(struct btrfs_key)); | ||
4019 | kfree(dentry->d_fsdata); | ||
4020 | dentry->d_fsdata = NULL; | ||
4021 | d_clear_need_lookup(dentry); | ||
4022 | } else { | ||
4023 | ret = btrfs_inode_by_name(dir, dentry, &location); | ||
4024 | } | ||
4002 | 4025 | ||
4003 | if (ret < 0) | 4026 | if (ret < 0) |
4004 | return ERR_PTR(ret); | 4027 | return ERR_PTR(ret); |
@@ -4053,6 +4076,12 @@ static int btrfs_dentry_delete(const struct dentry *dentry) | |||
4053 | return 0; | 4076 | return 0; |
4054 | } | 4077 | } |
4055 | 4078 | ||
4079 | static void btrfs_dentry_release(struct dentry *dentry) | ||
4080 | { | ||
4081 | if (dentry->d_fsdata) | ||
4082 | kfree(dentry->d_fsdata); | ||
4083 | } | ||
4084 | |||
4056 | static struct dentry *btrfs_lookup(struct inode *dir, struct dentry *dentry, | 4085 | static struct dentry *btrfs_lookup(struct inode *dir, struct dentry *dentry, |
4057 | struct nameidata *nd) | 4086 | struct nameidata *nd) |
4058 | { | 4087 | { |
@@ -4075,6 +4104,7 @@ static int btrfs_real_readdir(struct file *filp, void *dirent, | |||
4075 | struct btrfs_path *path; | 4104 | struct btrfs_path *path; |
4076 | struct list_head ins_list; | 4105 | struct list_head ins_list; |
4077 | struct list_head del_list; | 4106 | struct list_head del_list; |
4107 | struct qstr q; | ||
4078 | int ret; | 4108 | int ret; |
4079 | struct extent_buffer *leaf; | 4109 | struct extent_buffer *leaf; |
4080 | int slot; | 4110 | int slot; |
@@ -4164,6 +4194,7 @@ static int btrfs_real_readdir(struct file *filp, void *dirent, | |||
4164 | 4194 | ||
4165 | while (di_cur < di_total) { | 4195 | while (di_cur < di_total) { |
4166 | struct btrfs_key location; | 4196 | struct btrfs_key location; |
4197 | struct dentry *tmp; | ||
4167 | 4198 | ||
4168 | if (verify_dir_item(root, leaf, di)) | 4199 | if (verify_dir_item(root, leaf, di)) |
4169 | break; | 4200 | break; |
@@ -4184,6 +4215,33 @@ static int btrfs_real_readdir(struct file *filp, void *dirent, | |||
4184 | d_type = btrfs_filetype_table[btrfs_dir_type(leaf, di)]; | 4215 | d_type = btrfs_filetype_table[btrfs_dir_type(leaf, di)]; |
4185 | btrfs_dir_item_key_to_cpu(leaf, di, &location); | 4216 | btrfs_dir_item_key_to_cpu(leaf, di, &location); |
4186 | 4217 | ||
4218 | q.name = name_ptr; | ||
4219 | q.len = name_len; | ||
4220 | q.hash = full_name_hash(q.name, q.len); | ||
4221 | tmp = d_lookup(filp->f_dentry, &q); | ||
4222 | if (!tmp) { | ||
4223 | struct btrfs_key *newkey; | ||
4224 | |||
4225 | newkey = kzalloc(sizeof(struct btrfs_key), | ||
4226 | GFP_NOFS); | ||
4227 | if (!newkey) | ||
4228 | goto no_dentry; | ||
4229 | tmp = d_alloc(filp->f_dentry, &q); | ||
4230 | if (!tmp) { | ||
4231 | kfree(newkey); | ||
4232 | dput(tmp); | ||
4233 | goto no_dentry; | ||
4234 | } | ||
4235 | memcpy(newkey, &location, | ||
4236 | sizeof(struct btrfs_key)); | ||
4237 | tmp->d_fsdata = newkey; | ||
4238 | tmp->d_flags |= DCACHE_NEED_LOOKUP; | ||
4239 | d_rehash(tmp); | ||
4240 | dput(tmp); | ||
4241 | } else { | ||
4242 | dput(tmp); | ||
4243 | } | ||
4244 | no_dentry: | ||
4187 | /* is this a reference to our own snapshot? If so | 4245 | /* is this a reference to our own snapshot? If so |
4188 | * skip it | 4246 | * skip it |
4189 | */ | 4247 | */ |
@@ -4409,7 +4467,8 @@ static struct inode *btrfs_new_inode(struct btrfs_trans_handle *trans, | |||
4409 | int owner; | 4467 | int owner; |
4410 | 4468 | ||
4411 | path = btrfs_alloc_path(); | 4469 | path = btrfs_alloc_path(); |
4412 | BUG_ON(!path); | 4470 | if (!path) |
4471 | return ERR_PTR(-ENOMEM); | ||
4413 | 4472 | ||
4414 | inode = new_inode(root->fs_info->sb); | 4473 | inode = new_inode(root->fs_info->sb); |
4415 | if (!inode) { | 4474 | if (!inode) { |
@@ -6669,19 +6728,6 @@ int btrfs_create_subvol_root(struct btrfs_trans_handle *trans, | |||
6669 | return 0; | 6728 | return 0; |
6670 | } | 6729 | } |
6671 | 6730 | ||
6672 | /* helper function for file defrag and space balancing. This | ||
6673 | * forces readahead on a given range of bytes in an inode | ||
6674 | */ | ||
6675 | unsigned long btrfs_force_ra(struct address_space *mapping, | ||
6676 | struct file_ra_state *ra, struct file *file, | ||
6677 | pgoff_t offset, pgoff_t last_index) | ||
6678 | { | ||
6679 | pgoff_t req_size = last_index - offset + 1; | ||
6680 | |||
6681 | page_cache_sync_readahead(mapping, ra, file, offset, req_size); | ||
6682 | return offset + req_size; | ||
6683 | } | ||
6684 | |||
6685 | struct inode *btrfs_alloc_inode(struct super_block *sb) | 6731 | struct inode *btrfs_alloc_inode(struct super_block *sb) |
6686 | { | 6732 | { |
6687 | struct btrfs_inode *ei; | 6733 | struct btrfs_inode *ei; |
@@ -7164,7 +7210,11 @@ static int btrfs_symlink(struct inode *dir, struct dentry *dentry, | |||
7164 | goto out_unlock; | 7210 | goto out_unlock; |
7165 | 7211 | ||
7166 | path = btrfs_alloc_path(); | 7212 | path = btrfs_alloc_path(); |
7167 | BUG_ON(!path); | 7213 | if (!path) { |
7214 | err = -ENOMEM; | ||
7215 | drop_inode = 1; | ||
7216 | goto out_unlock; | ||
7217 | } | ||
7168 | key.objectid = btrfs_ino(inode); | 7218 | key.objectid = btrfs_ino(inode); |
7169 | key.offset = 0; | 7219 | key.offset = 0; |
7170 | btrfs_set_key_type(&key, BTRFS_EXTENT_DATA_KEY); | 7220 | btrfs_set_key_type(&key, BTRFS_EXTENT_DATA_KEY); |
@@ -7430,4 +7480,5 @@ static const struct inode_operations btrfs_symlink_inode_operations = { | |||
7430 | 7480 | ||
7431 | const struct dentry_operations btrfs_dentry_operations = { | 7481 | const struct dentry_operations btrfs_dentry_operations = { |
7432 | .d_delete = btrfs_dentry_delete, | 7482 | .d_delete = btrfs_dentry_delete, |
7483 | .d_release = btrfs_dentry_release, | ||
7433 | }; | 7484 | }; |