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.c328
1 files changed, 269 insertions, 59 deletions
diff --git a/fs/btrfs/inode.c b/fs/btrfs/inode.c
index e9b76bcd1c12..9e138b793dc7 100644
--- a/fs/btrfs/inode.c
+++ b/fs/btrfs/inode.c
@@ -62,7 +62,7 @@ static const struct inode_operations btrfs_special_inode_operations;
62static const struct inode_operations btrfs_file_inode_operations; 62static const struct inode_operations btrfs_file_inode_operations;
63static const struct address_space_operations btrfs_aops; 63static const struct address_space_operations btrfs_aops;
64static const struct address_space_operations btrfs_symlink_aops; 64static const struct address_space_operations btrfs_symlink_aops;
65static struct file_operations btrfs_dir_file_operations; 65static const struct file_operations btrfs_dir_file_operations;
66static struct extent_io_ops btrfs_extent_io_ops; 66static struct extent_io_ops btrfs_extent_io_ops;
67 67
68static struct kmem_cache *btrfs_inode_cachep; 68static struct kmem_cache *btrfs_inode_cachep;
@@ -424,9 +424,12 @@ again:
424 * and free up our temp pages. 424 * and free up our temp pages.
425 */ 425 */
426 extent_clear_unlock_delalloc(inode, 426 extent_clear_unlock_delalloc(inode,
427 &BTRFS_I(inode)->io_tree, 427 &BTRFS_I(inode)->io_tree,
428 start, end, NULL, 1, 0, 428 start, end, NULL,
429 0, 1, 1, 1, 0); 429 EXTENT_CLEAR_UNLOCK_PAGE | EXTENT_CLEAR_DIRTY |
430 EXTENT_CLEAR_DELALLOC |
431 EXTENT_CLEAR_ACCOUNTING |
432 EXTENT_SET_WRITEBACK | EXTENT_END_WRITEBACK);
430 ret = 0; 433 ret = 0;
431 goto free_pages_out; 434 goto free_pages_out;
432 } 435 }
@@ -637,11 +640,14 @@ static noinline int submit_compressed_extents(struct inode *inode,
637 * clear dirty, set writeback and unlock the pages. 640 * clear dirty, set writeback and unlock the pages.
638 */ 641 */
639 extent_clear_unlock_delalloc(inode, 642 extent_clear_unlock_delalloc(inode,
640 &BTRFS_I(inode)->io_tree, 643 &BTRFS_I(inode)->io_tree,
641 async_extent->start, 644 async_extent->start,
642 async_extent->start + 645 async_extent->start +
643 async_extent->ram_size - 1, 646 async_extent->ram_size - 1,
644 NULL, 1, 1, 0, 1, 1, 0, 0); 647 NULL, EXTENT_CLEAR_UNLOCK_PAGE |
648 EXTENT_CLEAR_UNLOCK |
649 EXTENT_CLEAR_DELALLOC |
650 EXTENT_CLEAR_DIRTY | EXTENT_SET_WRITEBACK);
645 651
646 ret = btrfs_submit_compressed_write(inode, 652 ret = btrfs_submit_compressed_write(inode,
647 async_extent->start, 653 async_extent->start,
@@ -712,9 +718,15 @@ static noinline int cow_file_range(struct inode *inode,
712 start, end, 0, NULL); 718 start, end, 0, NULL);
713 if (ret == 0) { 719 if (ret == 0) {
714 extent_clear_unlock_delalloc(inode, 720 extent_clear_unlock_delalloc(inode,
715 &BTRFS_I(inode)->io_tree, 721 &BTRFS_I(inode)->io_tree,
716 start, end, NULL, 1, 1, 722 start, end, NULL,
717 1, 1, 1, 1, 0); 723 EXTENT_CLEAR_UNLOCK_PAGE |
724 EXTENT_CLEAR_UNLOCK |
725 EXTENT_CLEAR_DELALLOC |
726 EXTENT_CLEAR_ACCOUNTING |
727 EXTENT_CLEAR_DIRTY |
728 EXTENT_SET_WRITEBACK |
729 EXTENT_END_WRITEBACK);
718 *nr_written = *nr_written + 730 *nr_written = *nr_written +
719 (end - start + PAGE_CACHE_SIZE) / PAGE_CACHE_SIZE; 731 (end - start + PAGE_CACHE_SIZE) / PAGE_CACHE_SIZE;
720 *page_started = 1; 732 *page_started = 1;
@@ -738,6 +750,8 @@ static noinline int cow_file_range(struct inode *inode,
738 btrfs_drop_extent_cache(inode, start, start + num_bytes - 1, 0); 750 btrfs_drop_extent_cache(inode, start, start + num_bytes - 1, 0);
739 751
740 while (disk_num_bytes > 0) { 752 while (disk_num_bytes > 0) {
753 unsigned long op;
754
741 cur_alloc_size = min(disk_num_bytes, root->fs_info->max_extent); 755 cur_alloc_size = min(disk_num_bytes, root->fs_info->max_extent);
742 ret = btrfs_reserve_extent(trans, root, cur_alloc_size, 756 ret = btrfs_reserve_extent(trans, root, cur_alloc_size,
743 root->sectorsize, 0, alloc_hint, 757 root->sectorsize, 0, alloc_hint,
@@ -789,10 +803,13 @@ static noinline int cow_file_range(struct inode *inode,
789 * Do set the Private2 bit so we know this page was properly 803 * Do set the Private2 bit so we know this page was properly
790 * setup for writepage 804 * setup for writepage
791 */ 805 */
806 op = unlock ? EXTENT_CLEAR_UNLOCK_PAGE : 0;
807 op |= EXTENT_CLEAR_UNLOCK | EXTENT_CLEAR_DELALLOC |
808 EXTENT_SET_PRIVATE2;
809
792 extent_clear_unlock_delalloc(inode, &BTRFS_I(inode)->io_tree, 810 extent_clear_unlock_delalloc(inode, &BTRFS_I(inode)->io_tree,
793 start, start + ram_size - 1, 811 start, start + ram_size - 1,
794 locked_page, unlock, 1, 812 locked_page, op);
795 1, 0, 0, 0, 1);
796 disk_num_bytes -= cur_alloc_size; 813 disk_num_bytes -= cur_alloc_size;
797 num_bytes -= cur_alloc_size; 814 num_bytes -= cur_alloc_size;
798 alloc_hint = ins.objectid + ins.offset; 815 alloc_hint = ins.objectid + ins.offset;
@@ -864,8 +881,8 @@ static int cow_file_range_async(struct inode *inode, struct page *locked_page,
864 u64 cur_end; 881 u64 cur_end;
865 int limit = 10 * 1024 * 1042; 882 int limit = 10 * 1024 * 1042;
866 883
867 clear_extent_bit(&BTRFS_I(inode)->io_tree, start, end, EXTENT_LOCKED | 884 clear_extent_bit(&BTRFS_I(inode)->io_tree, start, end, EXTENT_LOCKED,
868 EXTENT_DELALLOC, 1, 0, NULL, GFP_NOFS); 885 1, 0, NULL, GFP_NOFS);
869 while (start < end) { 886 while (start < end) {
870 async_cow = kmalloc(sizeof(*async_cow), GFP_NOFS); 887 async_cow = kmalloc(sizeof(*async_cow), GFP_NOFS);
871 async_cow->inode = inode; 888 async_cow->inode = inode;
@@ -1006,6 +1023,7 @@ next_slot:
1006 1023
1007 if (found_key.offset > cur_offset) { 1024 if (found_key.offset > cur_offset) {
1008 extent_end = found_key.offset; 1025 extent_end = found_key.offset;
1026 extent_type = 0;
1009 goto out_check; 1027 goto out_check;
1010 } 1028 }
1011 1029
@@ -1112,8 +1130,10 @@ out_check:
1112 BUG_ON(ret); 1130 BUG_ON(ret);
1113 1131
1114 extent_clear_unlock_delalloc(inode, &BTRFS_I(inode)->io_tree, 1132 extent_clear_unlock_delalloc(inode, &BTRFS_I(inode)->io_tree,
1115 cur_offset, cur_offset + num_bytes - 1, 1133 cur_offset, cur_offset + num_bytes - 1,
1116 locked_page, 1, 1, 1, 0, 0, 0, 1); 1134 locked_page, EXTENT_CLEAR_UNLOCK_PAGE |
1135 EXTENT_CLEAR_UNLOCK | EXTENT_CLEAR_DELALLOC |
1136 EXTENT_SET_PRIVATE2);
1117 cur_offset = extent_end; 1137 cur_offset = extent_end;
1118 if (cur_offset > end) 1138 if (cur_offset > end)
1119 break; 1139 break;
@@ -1159,6 +1179,89 @@ static int run_delalloc_range(struct inode *inode, struct page *locked_page,
1159 return ret; 1179 return ret;
1160} 1180}
1161 1181
1182static int btrfs_split_extent_hook(struct inode *inode,
1183 struct extent_state *orig, u64 split)
1184{
1185 struct btrfs_root *root = BTRFS_I(inode)->root;
1186 u64 size;
1187
1188 if (!(orig->state & EXTENT_DELALLOC))
1189 return 0;
1190
1191 size = orig->end - orig->start + 1;
1192 if (size > root->fs_info->max_extent) {
1193 u64 num_extents;
1194 u64 new_size;
1195
1196 new_size = orig->end - split + 1;
1197 num_extents = div64_u64(size + root->fs_info->max_extent - 1,
1198 root->fs_info->max_extent);
1199
1200 /*
1201 * if we break a large extent up then leave oustanding_extents
1202 * be, since we've already accounted for the large extent.
1203 */
1204 if (div64_u64(new_size + root->fs_info->max_extent - 1,
1205 root->fs_info->max_extent) < num_extents)
1206 return 0;
1207 }
1208
1209 spin_lock(&BTRFS_I(inode)->accounting_lock);
1210 BTRFS_I(inode)->outstanding_extents++;
1211 spin_unlock(&BTRFS_I(inode)->accounting_lock);
1212
1213 return 0;
1214}
1215
1216/*
1217 * extent_io.c merge_extent_hook, used to track merged delayed allocation
1218 * extents so we can keep track of new extents that are just merged onto old
1219 * extents, such as when we are doing sequential writes, so we can properly
1220 * account for the metadata space we'll need.
1221 */
1222static int btrfs_merge_extent_hook(struct inode *inode,
1223 struct extent_state *new,
1224 struct extent_state *other)
1225{
1226 struct btrfs_root *root = BTRFS_I(inode)->root;
1227 u64 new_size, old_size;
1228 u64 num_extents;
1229
1230 /* not delalloc, ignore it */
1231 if (!(other->state & EXTENT_DELALLOC))
1232 return 0;
1233
1234 old_size = other->end - other->start + 1;
1235 if (new->start < other->start)
1236 new_size = other->end - new->start + 1;
1237 else
1238 new_size = new->end - other->start + 1;
1239
1240 /* we're not bigger than the max, unreserve the space and go */
1241 if (new_size <= root->fs_info->max_extent) {
1242 spin_lock(&BTRFS_I(inode)->accounting_lock);
1243 BTRFS_I(inode)->outstanding_extents--;
1244 spin_unlock(&BTRFS_I(inode)->accounting_lock);
1245 return 0;
1246 }
1247
1248 /*
1249 * If we grew by another max_extent, just return, we want to keep that
1250 * reserved amount.
1251 */
1252 num_extents = div64_u64(old_size + root->fs_info->max_extent - 1,
1253 root->fs_info->max_extent);
1254 if (div64_u64(new_size + root->fs_info->max_extent - 1,
1255 root->fs_info->max_extent) > num_extents)
1256 return 0;
1257
1258 spin_lock(&BTRFS_I(inode)->accounting_lock);
1259 BTRFS_I(inode)->outstanding_extents--;
1260 spin_unlock(&BTRFS_I(inode)->accounting_lock);
1261
1262 return 0;
1263}
1264
1162/* 1265/*
1163 * extent_io.c set_bit_hook, used to track delayed allocation 1266 * extent_io.c set_bit_hook, used to track delayed allocation
1164 * bytes in this file, and to maintain the list of inodes that 1267 * bytes in this file, and to maintain the list of inodes that
@@ -1167,6 +1270,7 @@ static int run_delalloc_range(struct inode *inode, struct page *locked_page,
1167static int btrfs_set_bit_hook(struct inode *inode, u64 start, u64 end, 1270static int btrfs_set_bit_hook(struct inode *inode, u64 start, u64 end,
1168 unsigned long old, unsigned long bits) 1271 unsigned long old, unsigned long bits)
1169{ 1272{
1273
1170 /* 1274 /*
1171 * set_bit and clear bit hooks normally require _irqsave/restore 1275 * set_bit and clear bit hooks normally require _irqsave/restore
1172 * but in this case, we are only testeing for the DELALLOC 1276 * but in this case, we are only testeing for the DELALLOC
@@ -1174,6 +1278,10 @@ static int btrfs_set_bit_hook(struct inode *inode, u64 start, u64 end,
1174 */ 1278 */
1175 if (!(old & EXTENT_DELALLOC) && (bits & EXTENT_DELALLOC)) { 1279 if (!(old & EXTENT_DELALLOC) && (bits & EXTENT_DELALLOC)) {
1176 struct btrfs_root *root = BTRFS_I(inode)->root; 1280 struct btrfs_root *root = BTRFS_I(inode)->root;
1281
1282 spin_lock(&BTRFS_I(inode)->accounting_lock);
1283 BTRFS_I(inode)->outstanding_extents++;
1284 spin_unlock(&BTRFS_I(inode)->accounting_lock);
1177 btrfs_delalloc_reserve_space(root, inode, end - start + 1); 1285 btrfs_delalloc_reserve_space(root, inode, end - start + 1);
1178 spin_lock(&root->fs_info->delalloc_lock); 1286 spin_lock(&root->fs_info->delalloc_lock);
1179 BTRFS_I(inode)->delalloc_bytes += end - start + 1; 1287 BTRFS_I(inode)->delalloc_bytes += end - start + 1;
@@ -1190,22 +1298,31 @@ static int btrfs_set_bit_hook(struct inode *inode, u64 start, u64 end,
1190/* 1298/*
1191 * extent_io.c clear_bit_hook, see set_bit_hook for why 1299 * extent_io.c clear_bit_hook, see set_bit_hook for why
1192 */ 1300 */
1193static int btrfs_clear_bit_hook(struct inode *inode, u64 start, u64 end, 1301static int btrfs_clear_bit_hook(struct inode *inode,
1194 unsigned long old, unsigned long bits) 1302 struct extent_state *state, unsigned long bits)
1195{ 1303{
1196 /* 1304 /*
1197 * set_bit and clear bit hooks normally require _irqsave/restore 1305 * set_bit and clear bit hooks normally require _irqsave/restore
1198 * but in this case, we are only testeing for the DELALLOC 1306 * but in this case, we are only testeing for the DELALLOC
1199 * bit, which is only set or cleared with irqs on 1307 * bit, which is only set or cleared with irqs on
1200 */ 1308 */
1201 if ((old & EXTENT_DELALLOC) && (bits & EXTENT_DELALLOC)) { 1309 if ((state->state & EXTENT_DELALLOC) && (bits & EXTENT_DELALLOC)) {
1202 struct btrfs_root *root = BTRFS_I(inode)->root; 1310 struct btrfs_root *root = BTRFS_I(inode)->root;
1203 1311
1312 if (bits & EXTENT_DO_ACCOUNTING) {
1313 spin_lock(&BTRFS_I(inode)->accounting_lock);
1314 BTRFS_I(inode)->outstanding_extents--;
1315 spin_unlock(&BTRFS_I(inode)->accounting_lock);
1316 btrfs_unreserve_metadata_for_delalloc(root, inode, 1);
1317 }
1318
1204 spin_lock(&root->fs_info->delalloc_lock); 1319 spin_lock(&root->fs_info->delalloc_lock);
1205 if (end - start + 1 > root->fs_info->delalloc_bytes) { 1320 if (state->end - state->start + 1 >
1321 root->fs_info->delalloc_bytes) {
1206 printk(KERN_INFO "btrfs warning: delalloc account " 1322 printk(KERN_INFO "btrfs warning: delalloc account "
1207 "%llu %llu\n", 1323 "%llu %llu\n",
1208 (unsigned long long)end - start + 1, 1324 (unsigned long long)
1325 state->end - state->start + 1,
1209 (unsigned long long) 1326 (unsigned long long)
1210 root->fs_info->delalloc_bytes); 1327 root->fs_info->delalloc_bytes);
1211 btrfs_delalloc_free_space(root, inode, (u64)-1); 1328 btrfs_delalloc_free_space(root, inode, (u64)-1);
@@ -1213,9 +1330,12 @@ static int btrfs_clear_bit_hook(struct inode *inode, u64 start, u64 end,
1213 BTRFS_I(inode)->delalloc_bytes = 0; 1330 BTRFS_I(inode)->delalloc_bytes = 0;
1214 } else { 1331 } else {
1215 btrfs_delalloc_free_space(root, inode, 1332 btrfs_delalloc_free_space(root, inode,
1216 end - start + 1); 1333 state->end -
1217 root->fs_info->delalloc_bytes -= end - start + 1; 1334 state->start + 1);
1218 BTRFS_I(inode)->delalloc_bytes -= end - start + 1; 1335 root->fs_info->delalloc_bytes -= state->end -
1336 state->start + 1;
1337 BTRFS_I(inode)->delalloc_bytes -= state->end -
1338 state->start + 1;
1219 } 1339 }
1220 if (BTRFS_I(inode)->delalloc_bytes == 0 && 1340 if (BTRFS_I(inode)->delalloc_bytes == 0 &&
1221 !list_empty(&BTRFS_I(inode)->delalloc_inodes)) { 1341 !list_empty(&BTRFS_I(inode)->delalloc_inodes)) {
@@ -2950,7 +3070,12 @@ again:
2950 goto again; 3070 goto again;
2951 } 3071 }
2952 3072
2953 btrfs_set_extent_delalloc(inode, page_start, page_end); 3073 ret = btrfs_set_extent_delalloc(inode, page_start, page_end);
3074 if (ret) {
3075 unlock_extent(io_tree, page_start, page_end, GFP_NOFS);
3076 goto out_unlock;
3077 }
3078
2954 ret = 0; 3079 ret = 0;
2955 if (offset != PAGE_CACHE_SIZE) { 3080 if (offset != PAGE_CACHE_SIZE) {
2956 kaddr = kmap(page); 3081 kaddr = kmap(page);
@@ -2981,15 +3106,11 @@ int btrfs_cont_expand(struct inode *inode, loff_t size)
2981 u64 last_byte; 3106 u64 last_byte;
2982 u64 cur_offset; 3107 u64 cur_offset;
2983 u64 hole_size; 3108 u64 hole_size;
2984 int err; 3109 int err = 0;
2985 3110
2986 if (size <= hole_start) 3111 if (size <= hole_start)
2987 return 0; 3112 return 0;
2988 3113
2989 err = btrfs_check_metadata_free_space(root);
2990 if (err)
2991 return err;
2992
2993 btrfs_truncate_page(inode->i_mapping, inode->i_size); 3114 btrfs_truncate_page(inode->i_mapping, inode->i_size);
2994 3115
2995 while (1) { 3116 while (1) {
@@ -3024,12 +3145,18 @@ int btrfs_cont_expand(struct inode *inode, loff_t size)
3024 cur_offset, &hint_byte, 1); 3145 cur_offset, &hint_byte, 1);
3025 if (err) 3146 if (err)
3026 break; 3147 break;
3148
3149 err = btrfs_reserve_metadata_space(root, 1);
3150 if (err)
3151 break;
3152
3027 err = btrfs_insert_file_extent(trans, root, 3153 err = btrfs_insert_file_extent(trans, root,
3028 inode->i_ino, cur_offset, 0, 3154 inode->i_ino, cur_offset, 0,
3029 0, hole_size, 0, hole_size, 3155 0, hole_size, 0, hole_size,
3030 0, 0, 0); 3156 0, 0, 0);
3031 btrfs_drop_extent_cache(inode, hole_start, 3157 btrfs_drop_extent_cache(inode, hole_start,
3032 last_byte - 1, 0); 3158 last_byte - 1, 0);
3159 btrfs_unreserve_metadata_space(root, 1);
3033 } 3160 }
3034 free_extent_map(em); 3161 free_extent_map(em);
3035 cur_offset = last_byte; 3162 cur_offset = last_byte;
@@ -3503,12 +3630,14 @@ static int btrfs_dentry_delete(struct dentry *dentry)
3503{ 3630{
3504 struct btrfs_root *root; 3631 struct btrfs_root *root;
3505 3632
3506 if (!dentry->d_inode) 3633 if (!dentry->d_inode && !IS_ROOT(dentry))
3507 return 0; 3634 dentry = dentry->d_parent;
3508 3635
3509 root = BTRFS_I(dentry->d_inode)->root; 3636 if (dentry->d_inode) {
3510 if (btrfs_root_refs(&root->root_item) == 0) 3637 root = BTRFS_I(dentry->d_inode)->root;
3511 return 1; 3638 if (btrfs_root_refs(&root->root_item) == 0)
3639 return 1;
3640 }
3512 return 0; 3641 return 0;
3513} 3642}
3514 3643
@@ -3990,11 +4119,18 @@ static int btrfs_mknod(struct inode *dir, struct dentry *dentry,
3990 if (!new_valid_dev(rdev)) 4119 if (!new_valid_dev(rdev))
3991 return -EINVAL; 4120 return -EINVAL;
3992 4121
3993 err = btrfs_check_metadata_free_space(root); 4122 /*
4123 * 2 for inode item and ref
4124 * 2 for dir items
4125 * 1 for xattr if selinux is on
4126 */
4127 err = btrfs_reserve_metadata_space(root, 5);
3994 if (err) 4128 if (err)
3995 goto fail; 4129 return err;
3996 4130
3997 trans = btrfs_start_transaction(root, 1); 4131 trans = btrfs_start_transaction(root, 1);
4132 if (!trans)
4133 goto fail;
3998 btrfs_set_trans_block_group(trans, dir); 4134 btrfs_set_trans_block_group(trans, dir);
3999 4135
4000 err = btrfs_find_free_objectid(trans, root, dir->i_ino, &objectid); 4136 err = btrfs_find_free_objectid(trans, root, dir->i_ino, &objectid);
@@ -4032,6 +4168,7 @@ out_unlock:
4032 nr = trans->blocks_used; 4168 nr = trans->blocks_used;
4033 btrfs_end_transaction_throttle(trans, root); 4169 btrfs_end_transaction_throttle(trans, root);
4034fail: 4170fail:
4171 btrfs_unreserve_metadata_space(root, 5);
4035 if (drop_inode) { 4172 if (drop_inode) {
4036 inode_dec_link_count(inode); 4173 inode_dec_link_count(inode);
4037 iput(inode); 4174 iput(inode);
@@ -4052,10 +4189,18 @@ static int btrfs_create(struct inode *dir, struct dentry *dentry,
4052 u64 objectid; 4189 u64 objectid;
4053 u64 index = 0; 4190 u64 index = 0;
4054 4191
4055 err = btrfs_check_metadata_free_space(root); 4192 /*
4193 * 2 for inode item and ref
4194 * 2 for dir items
4195 * 1 for xattr if selinux is on
4196 */
4197 err = btrfs_reserve_metadata_space(root, 5);
4056 if (err) 4198 if (err)
4057 goto fail; 4199 return err;
4200
4058 trans = btrfs_start_transaction(root, 1); 4201 trans = btrfs_start_transaction(root, 1);
4202 if (!trans)
4203 goto fail;
4059 btrfs_set_trans_block_group(trans, dir); 4204 btrfs_set_trans_block_group(trans, dir);
4060 4205
4061 err = btrfs_find_free_objectid(trans, root, dir->i_ino, &objectid); 4206 err = btrfs_find_free_objectid(trans, root, dir->i_ino, &objectid);
@@ -4096,6 +4241,7 @@ out_unlock:
4096 nr = trans->blocks_used; 4241 nr = trans->blocks_used;
4097 btrfs_end_transaction_throttle(trans, root); 4242 btrfs_end_transaction_throttle(trans, root);
4098fail: 4243fail:
4244 btrfs_unreserve_metadata_space(root, 5);
4099 if (drop_inode) { 4245 if (drop_inode) {
4100 inode_dec_link_count(inode); 4246 inode_dec_link_count(inode);
4101 iput(inode); 4247 iput(inode);
@@ -4118,10 +4264,16 @@ static int btrfs_link(struct dentry *old_dentry, struct inode *dir,
4118 if (inode->i_nlink == 0) 4264 if (inode->i_nlink == 0)
4119 return -ENOENT; 4265 return -ENOENT;
4120 4266
4121 btrfs_inc_nlink(inode); 4267 /*
4122 err = btrfs_check_metadata_free_space(root); 4268 * 1 item for inode ref
4269 * 2 items for dir items
4270 */
4271 err = btrfs_reserve_metadata_space(root, 3);
4123 if (err) 4272 if (err)
4124 goto fail; 4273 return err;
4274
4275 btrfs_inc_nlink(inode);
4276
4125 err = btrfs_set_inode_index(dir, &index); 4277 err = btrfs_set_inode_index(dir, &index);
4126 if (err) 4278 if (err)
4127 goto fail; 4279 goto fail;
@@ -4145,6 +4297,7 @@ static int btrfs_link(struct dentry *old_dentry, struct inode *dir,
4145 nr = trans->blocks_used; 4297 nr = trans->blocks_used;
4146 btrfs_end_transaction_throttle(trans, root); 4298 btrfs_end_transaction_throttle(trans, root);
4147fail: 4299fail:
4300 btrfs_unreserve_metadata_space(root, 3);
4148 if (drop_inode) { 4301 if (drop_inode) {
4149 inode_dec_link_count(inode); 4302 inode_dec_link_count(inode);
4150 iput(inode); 4303 iput(inode);
@@ -4164,17 +4317,21 @@ static int btrfs_mkdir(struct inode *dir, struct dentry *dentry, int mode)
4164 u64 index = 0; 4317 u64 index = 0;
4165 unsigned long nr = 1; 4318 unsigned long nr = 1;
4166 4319
4167 err = btrfs_check_metadata_free_space(root); 4320 /*
4321 * 2 items for inode and ref
4322 * 2 items for dir items
4323 * 1 for xattr if selinux is on
4324 */
4325 err = btrfs_reserve_metadata_space(root, 5);
4168 if (err) 4326 if (err)
4169 goto out_unlock; 4327 return err;
4170 4328
4171 trans = btrfs_start_transaction(root, 1); 4329 trans = btrfs_start_transaction(root, 1);
4172 btrfs_set_trans_block_group(trans, dir); 4330 if (!trans) {
4173 4331 err = -ENOMEM;
4174 if (IS_ERR(trans)) {
4175 err = PTR_ERR(trans);
4176 goto out_unlock; 4332 goto out_unlock;
4177 } 4333 }
4334 btrfs_set_trans_block_group(trans, dir);
4178 4335
4179 err = btrfs_find_free_objectid(trans, root, dir->i_ino, &objectid); 4336 err = btrfs_find_free_objectid(trans, root, dir->i_ino, &objectid);
4180 if (err) { 4337 if (err) {
@@ -4223,6 +4380,7 @@ out_fail:
4223 btrfs_end_transaction_throttle(trans, root); 4380 btrfs_end_transaction_throttle(trans, root);
4224 4381
4225out_unlock: 4382out_unlock:
4383 btrfs_unreserve_metadata_space(root, 5);
4226 if (drop_on_err) 4384 if (drop_on_err)
4227 iput(inode); 4385 iput(inode);
4228 btrfs_btree_balance_dirty(root, nr); 4386 btrfs_btree_balance_dirty(root, nr);
@@ -4684,7 +4842,8 @@ static void btrfs_invalidatepage(struct page *page, unsigned long offset)
4684 */ 4842 */
4685 clear_extent_bit(tree, page_start, page_end, 4843 clear_extent_bit(tree, page_start, page_end,
4686 EXTENT_DIRTY | EXTENT_DELALLOC | 4844 EXTENT_DIRTY | EXTENT_DELALLOC |
4687 EXTENT_LOCKED, 1, 0, NULL, GFP_NOFS); 4845 EXTENT_LOCKED | EXTENT_DO_ACCOUNTING, 1, 0,
4846 NULL, GFP_NOFS);
4688 /* 4847 /*
4689 * whoever cleared the private bit is responsible 4848 * whoever cleared the private bit is responsible
4690 * for the finish_ordered_io 4849 * for the finish_ordered_io
@@ -4697,8 +4856,8 @@ static void btrfs_invalidatepage(struct page *page, unsigned long offset)
4697 lock_extent(tree, page_start, page_end, GFP_NOFS); 4856 lock_extent(tree, page_start, page_end, GFP_NOFS);
4698 } 4857 }
4699 clear_extent_bit(tree, page_start, page_end, 4858 clear_extent_bit(tree, page_start, page_end,
4700 EXTENT_LOCKED | EXTENT_DIRTY | EXTENT_DELALLOC, 4859 EXTENT_LOCKED | EXTENT_DIRTY | EXTENT_DELALLOC |
4701 1, 1, NULL, GFP_NOFS); 4860 EXTENT_DO_ACCOUNTING, 1, 1, NULL, GFP_NOFS);
4702 __btrfs_releasepage(page, GFP_NOFS); 4861 __btrfs_releasepage(page, GFP_NOFS);
4703 4862
4704 ClearPageChecked(page); 4863 ClearPageChecked(page);
@@ -4747,6 +4906,13 @@ int btrfs_page_mkwrite(struct vm_area_struct *vma, struct vm_fault *vmf)
4747 goto out; 4906 goto out;
4748 } 4907 }
4749 4908
4909 ret = btrfs_reserve_metadata_for_delalloc(root, inode, 1);
4910 if (ret) {
4911 btrfs_free_reserved_data_space(root, inode, PAGE_CACHE_SIZE);
4912 ret = VM_FAULT_SIGBUS;
4913 goto out;
4914 }
4915
4750 ret = VM_FAULT_NOPAGE; /* make the VM retry the fault */ 4916 ret = VM_FAULT_NOPAGE; /* make the VM retry the fault */
4751again: 4917again:
4752 lock_page(page); 4918 lock_page(page);
@@ -4778,7 +4944,24 @@ again:
4778 goto again; 4944 goto again;
4779 } 4945 }
4780 4946
4781 btrfs_set_extent_delalloc(inode, page_start, page_end); 4947 /*
4948 * XXX - page_mkwrite gets called every time the page is dirtied, even
4949 * if it was already dirty, so for space accounting reasons we need to
4950 * clear any delalloc bits for the range we are fixing to save. There
4951 * is probably a better way to do this, but for now keep consistent with
4952 * prepare_pages in the normal write path.
4953 */
4954 clear_extent_bits(&BTRFS_I(inode)->io_tree, page_start, page_end,
4955 EXTENT_DIRTY | EXTENT_DELALLOC | EXTENT_DO_ACCOUNTING,
4956 GFP_NOFS);
4957
4958 ret = btrfs_set_extent_delalloc(inode, page_start, page_end);
4959 if (ret) {
4960 unlock_extent(io_tree, page_start, page_end, GFP_NOFS);
4961 ret = VM_FAULT_SIGBUS;
4962 btrfs_free_reserved_data_space(root, inode, PAGE_CACHE_SIZE);
4963 goto out_unlock;
4964 }
4782 ret = 0; 4965 ret = 0;
4783 4966
4784 /* page is wholly or partially inside EOF */ 4967 /* page is wholly or partially inside EOF */
@@ -4801,6 +4984,7 @@ again:
4801 unlock_extent(io_tree, page_start, page_end, GFP_NOFS); 4984 unlock_extent(io_tree, page_start, page_end, GFP_NOFS);
4802 4985
4803out_unlock: 4986out_unlock:
4987 btrfs_unreserve_metadata_for_delalloc(root, inode, 1);
4804 if (!ret) 4988 if (!ret)
4805 return VM_FAULT_LOCKED; 4989 return VM_FAULT_LOCKED;
4806 unlock_page(page); 4990 unlock_page(page);
@@ -4917,6 +5101,9 @@ struct inode *btrfs_alloc_inode(struct super_block *sb)
4917 return NULL; 5101 return NULL;
4918 ei->last_trans = 0; 5102 ei->last_trans = 0;
4919 ei->logged_trans = 0; 5103 ei->logged_trans = 0;
5104 ei->outstanding_extents = 0;
5105 ei->reserved_extents = 0;
5106 spin_lock_init(&ei->accounting_lock);
4920 btrfs_ordered_inode_tree_init(&ei->ordered_tree); 5107 btrfs_ordered_inode_tree_init(&ei->ordered_tree);
4921 INIT_LIST_HEAD(&ei->i_orphan); 5108 INIT_LIST_HEAD(&ei->i_orphan);
4922 INIT_LIST_HEAD(&ei->ordered_operations); 5109 INIT_LIST_HEAD(&ei->ordered_operations);
@@ -5070,7 +5257,12 @@ static int btrfs_rename(struct inode *old_dir, struct dentry *old_dentry,
5070 new_inode->i_size > BTRFS_EMPTY_DIR_SIZE) 5257 new_inode->i_size > BTRFS_EMPTY_DIR_SIZE)
5071 return -ENOTEMPTY; 5258 return -ENOTEMPTY;
5072 5259
5073 ret = btrfs_check_metadata_free_space(root); 5260 /*
5261 * 2 items for dir items
5262 * 1 item for orphan entry
5263 * 1 item for ref
5264 */
5265 ret = btrfs_reserve_metadata_space(root, 4);
5074 if (ret) 5266 if (ret)
5075 return ret; 5267 return ret;
5076 5268
@@ -5185,6 +5377,8 @@ out_fail:
5185 5377
5186 if (old_inode->i_ino == BTRFS_FIRST_FREE_OBJECTID) 5378 if (old_inode->i_ino == BTRFS_FIRST_FREE_OBJECTID)
5187 up_read(&root->fs_info->subvol_sem); 5379 up_read(&root->fs_info->subvol_sem);
5380
5381 btrfs_unreserve_metadata_space(root, 4);
5188 return ret; 5382 return ret;
5189} 5383}
5190 5384
@@ -5256,11 +5450,18 @@ static int btrfs_symlink(struct inode *dir, struct dentry *dentry,
5256 if (name_len > BTRFS_MAX_INLINE_DATA_SIZE(root)) 5450 if (name_len > BTRFS_MAX_INLINE_DATA_SIZE(root))
5257 return -ENAMETOOLONG; 5451 return -ENAMETOOLONG;
5258 5452
5259 err = btrfs_check_metadata_free_space(root); 5453 /*
5454 * 2 items for inode item and ref
5455 * 2 items for dir items
5456 * 1 item for xattr if selinux is on
5457 */
5458 err = btrfs_reserve_metadata_space(root, 5);
5260 if (err) 5459 if (err)
5261 goto out_fail; 5460 return err;
5262 5461
5263 trans = btrfs_start_transaction(root, 1); 5462 trans = btrfs_start_transaction(root, 1);
5463 if (!trans)
5464 goto out_fail;
5264 btrfs_set_trans_block_group(trans, dir); 5465 btrfs_set_trans_block_group(trans, dir);
5265 5466
5266 err = btrfs_find_free_objectid(trans, root, dir->i_ino, &objectid); 5467 err = btrfs_find_free_objectid(trans, root, dir->i_ino, &objectid);
@@ -5341,6 +5542,7 @@ out_unlock:
5341 nr = trans->blocks_used; 5542 nr = trans->blocks_used;
5342 btrfs_end_transaction_throttle(trans, root); 5543 btrfs_end_transaction_throttle(trans, root);
5343out_fail: 5544out_fail:
5545 btrfs_unreserve_metadata_space(root, 5);
5344 if (drop_inode) { 5546 if (drop_inode) {
5345 inode_dec_link_count(inode); 5547 inode_dec_link_count(inode);
5346 iput(inode); 5548 iput(inode);
@@ -5362,6 +5564,11 @@ static int prealloc_file_range(struct btrfs_trans_handle *trans,
5362 5564
5363 while (num_bytes > 0) { 5565 while (num_bytes > 0) {
5364 alloc_size = min(num_bytes, root->fs_info->max_extent); 5566 alloc_size = min(num_bytes, root->fs_info->max_extent);
5567
5568 ret = btrfs_reserve_metadata_space(root, 1);
5569 if (ret)
5570 goto out;
5571
5365 ret = btrfs_reserve_extent(trans, root, alloc_size, 5572 ret = btrfs_reserve_extent(trans, root, alloc_size,
5366 root->sectorsize, 0, alloc_hint, 5573 root->sectorsize, 0, alloc_hint,
5367 (u64)-1, &ins, 1); 5574 (u64)-1, &ins, 1);
@@ -5381,6 +5588,7 @@ static int prealloc_file_range(struct btrfs_trans_handle *trans,
5381 num_bytes -= ins.offset; 5588 num_bytes -= ins.offset;
5382 cur_offset += ins.offset; 5589 cur_offset += ins.offset;
5383 alloc_hint = ins.objectid + ins.offset; 5590 alloc_hint = ins.objectid + ins.offset;
5591 btrfs_unreserve_metadata_space(root, 1);
5384 } 5592 }
5385out: 5593out:
5386 if (cur_offset > start) { 5594 if (cur_offset > start) {
@@ -5544,7 +5752,7 @@ static const struct inode_operations btrfs_dir_ro_inode_operations = {
5544 .permission = btrfs_permission, 5752 .permission = btrfs_permission,
5545}; 5753};
5546 5754
5547static struct file_operations btrfs_dir_file_operations = { 5755static const struct file_operations btrfs_dir_file_operations = {
5548 .llseek = generic_file_llseek, 5756 .llseek = generic_file_llseek,
5549 .read = generic_read_dir, 5757 .read = generic_read_dir,
5550 .readdir = btrfs_real_readdir, 5758 .readdir = btrfs_real_readdir,
@@ -5566,6 +5774,8 @@ static struct extent_io_ops btrfs_extent_io_ops = {
5566 .readpage_io_failed_hook = btrfs_io_failed_hook, 5774 .readpage_io_failed_hook = btrfs_io_failed_hook,
5567 .set_bit_hook = btrfs_set_bit_hook, 5775 .set_bit_hook = btrfs_set_bit_hook,
5568 .clear_bit_hook = btrfs_clear_bit_hook, 5776 .clear_bit_hook = btrfs_clear_bit_hook,
5777 .merge_extent_hook = btrfs_merge_extent_hook,
5778 .split_extent_hook = btrfs_split_extent_hook,
5569}; 5779};
5570 5780
5571/* 5781/*
@@ -5632,6 +5842,6 @@ static const struct inode_operations btrfs_symlink_inode_operations = {
5632 .removexattr = btrfs_removexattr, 5842 .removexattr = btrfs_removexattr,
5633}; 5843};
5634 5844
5635struct dentry_operations btrfs_dentry_operations = { 5845const struct dentry_operations btrfs_dentry_operations = {
5636 .d_delete = btrfs_dentry_delete, 5846 .d_delete = btrfs_dentry_delete,
5637}; 5847};