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.c929
1 files changed, 714 insertions, 215 deletions
diff --git a/fs/btrfs/inode.c b/fs/btrfs/inode.c
index 59cba180fe83..112e5aa85892 100644
--- a/fs/btrfs/inode.c
+++ b/fs/btrfs/inode.c
@@ -55,14 +55,14 @@ struct btrfs_iget_args {
55 struct btrfs_root *root; 55 struct btrfs_root *root;
56}; 56};
57 57
58static struct inode_operations btrfs_dir_inode_operations; 58static const struct inode_operations btrfs_dir_inode_operations;
59static struct inode_operations btrfs_symlink_inode_operations; 59static const struct inode_operations btrfs_symlink_inode_operations;
60static struct inode_operations btrfs_dir_ro_inode_operations; 60static const struct inode_operations btrfs_dir_ro_inode_operations;
61static struct inode_operations btrfs_special_inode_operations; 61static const struct inode_operations btrfs_special_inode_operations;
62static struct inode_operations btrfs_file_inode_operations; 62static const struct inode_operations btrfs_file_inode_operations;
63static struct address_space_operations btrfs_aops; 63static const struct address_space_operations btrfs_aops;
64static 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;
@@ -231,7 +231,8 @@ static noinline int cow_file_range_inline(struct btrfs_trans_handle *trans,
231 } 231 }
232 232
233 ret = btrfs_drop_extents(trans, root, inode, start, 233 ret = btrfs_drop_extents(trans, root, inode, start,
234 aligned_end, aligned_end, start, &hint_byte); 234 aligned_end, aligned_end, start,
235 &hint_byte, 1);
235 BUG_ON(ret); 236 BUG_ON(ret);
236 237
237 if (isize > actual_end) 238 if (isize > actual_end)
@@ -240,7 +241,7 @@ static noinline int cow_file_range_inline(struct btrfs_trans_handle *trans,
240 inline_len, compressed_size, 241 inline_len, compressed_size,
241 compressed_pages); 242 compressed_pages);
242 BUG_ON(ret); 243 BUG_ON(ret);
243 btrfs_drop_extent_cache(inode, start, aligned_end, 0); 244 btrfs_drop_extent_cache(inode, start, aligned_end - 1, 0);
244 return 0; 245 return 0;
245} 246}
246 247
@@ -425,7 +426,7 @@ again:
425 extent_clear_unlock_delalloc(inode, 426 extent_clear_unlock_delalloc(inode,
426 &BTRFS_I(inode)->io_tree, 427 &BTRFS_I(inode)->io_tree,
427 start, end, NULL, 1, 0, 428 start, end, NULL, 1, 0,
428 0, 1, 1, 1); 429 0, 1, 1, 1, 0);
429 ret = 0; 430 ret = 0;
430 goto free_pages_out; 431 goto free_pages_out;
431 } 432 }
@@ -611,9 +612,9 @@ static noinline int submit_compressed_extents(struct inode *inode,
611 set_bit(EXTENT_FLAG_COMPRESSED, &em->flags); 612 set_bit(EXTENT_FLAG_COMPRESSED, &em->flags);
612 613
613 while (1) { 614 while (1) {
614 spin_lock(&em_tree->lock); 615 write_lock(&em_tree->lock);
615 ret = add_extent_mapping(em_tree, em); 616 ret = add_extent_mapping(em_tree, em);
616 spin_unlock(&em_tree->lock); 617 write_unlock(&em_tree->lock);
617 if (ret != -EEXIST) { 618 if (ret != -EEXIST) {
618 free_extent_map(em); 619 free_extent_map(em);
619 break; 620 break;
@@ -640,7 +641,7 @@ static noinline int submit_compressed_extents(struct inode *inode,
640 async_extent->start, 641 async_extent->start,
641 async_extent->start + 642 async_extent->start +
642 async_extent->ram_size - 1, 643 async_extent->ram_size - 1,
643 NULL, 1, 1, 0, 1, 1, 0); 644 NULL, 1, 1, 0, 1, 1, 0, 0);
644 645
645 ret = btrfs_submit_compressed_write(inode, 646 ret = btrfs_submit_compressed_write(inode,
646 async_extent->start, 647 async_extent->start,
@@ -713,7 +714,7 @@ static noinline int cow_file_range(struct inode *inode,
713 extent_clear_unlock_delalloc(inode, 714 extent_clear_unlock_delalloc(inode,
714 &BTRFS_I(inode)->io_tree, 715 &BTRFS_I(inode)->io_tree,
715 start, end, NULL, 1, 1, 716 start, end, NULL, 1, 1,
716 1, 1, 1, 1); 717 1, 1, 1, 1, 0);
717 *nr_written = *nr_written + 718 *nr_written = *nr_written +
718 (end - start + PAGE_CACHE_SIZE) / PAGE_CACHE_SIZE; 719 (end - start + PAGE_CACHE_SIZE) / PAGE_CACHE_SIZE;
719 *page_started = 1; 720 *page_started = 1;
@@ -725,6 +726,15 @@ static noinline int cow_file_range(struct inode *inode,
725 BUG_ON(disk_num_bytes > 726 BUG_ON(disk_num_bytes >
726 btrfs_super_total_bytes(&root->fs_info->super_copy)); 727 btrfs_super_total_bytes(&root->fs_info->super_copy));
727 728
729
730 read_lock(&BTRFS_I(inode)->extent_tree.lock);
731 em = search_extent_mapping(&BTRFS_I(inode)->extent_tree,
732 start, num_bytes);
733 if (em) {
734 alloc_hint = em->block_start;
735 free_extent_map(em);
736 }
737 read_unlock(&BTRFS_I(inode)->extent_tree.lock);
728 btrfs_drop_extent_cache(inode, start, start + num_bytes - 1, 0); 738 btrfs_drop_extent_cache(inode, start, start + num_bytes - 1, 0);
729 739
730 while (disk_num_bytes > 0) { 740 while (disk_num_bytes > 0) {
@@ -737,7 +747,6 @@ static noinline int cow_file_range(struct inode *inode,
737 em = alloc_extent_map(GFP_NOFS); 747 em = alloc_extent_map(GFP_NOFS);
738 em->start = start; 748 em->start = start;
739 em->orig_start = em->start; 749 em->orig_start = em->start;
740
741 ram_size = ins.offset; 750 ram_size = ins.offset;
742 em->len = ins.offset; 751 em->len = ins.offset;
743 752
@@ -747,9 +756,9 @@ static noinline int cow_file_range(struct inode *inode,
747 set_bit(EXTENT_FLAG_PINNED, &em->flags); 756 set_bit(EXTENT_FLAG_PINNED, &em->flags);
748 757
749 while (1) { 758 while (1) {
750 spin_lock(&em_tree->lock); 759 write_lock(&em_tree->lock);
751 ret = add_extent_mapping(em_tree, em); 760 ret = add_extent_mapping(em_tree, em);
752 spin_unlock(&em_tree->lock); 761 write_unlock(&em_tree->lock);
753 if (ret != -EEXIST) { 762 if (ret != -EEXIST) {
754 free_extent_map(em); 763 free_extent_map(em);
755 break; 764 break;
@@ -776,11 +785,14 @@ static noinline int cow_file_range(struct inode *inode,
776 /* we're not doing compressed IO, don't unlock the first 785 /* we're not doing compressed IO, don't unlock the first
777 * page (which the caller expects to stay locked), don't 786 * page (which the caller expects to stay locked), don't
778 * clear any dirty bits and don't set any writeback bits 787 * clear any dirty bits and don't set any writeback bits
788 *
789 * Do set the Private2 bit so we know this page was properly
790 * setup for writepage
779 */ 791 */
780 extent_clear_unlock_delalloc(inode, &BTRFS_I(inode)->io_tree, 792 extent_clear_unlock_delalloc(inode, &BTRFS_I(inode)->io_tree,
781 start, start + ram_size - 1, 793 start, start + ram_size - 1,
782 locked_page, unlock, 1, 794 locked_page, unlock, 1,
783 1, 0, 0, 0); 795 1, 0, 0, 0, 1);
784 disk_num_bytes -= cur_alloc_size; 796 disk_num_bytes -= cur_alloc_size;
785 num_bytes -= cur_alloc_size; 797 num_bytes -= cur_alloc_size;
786 alloc_hint = ins.objectid + ins.offset; 798 alloc_hint = ins.objectid + ins.offset;
@@ -853,7 +865,7 @@ static int cow_file_range_async(struct inode *inode, struct page *locked_page,
853 int limit = 10 * 1024 * 1042; 865 int limit = 10 * 1024 * 1042;
854 866
855 clear_extent_bit(&BTRFS_I(inode)->io_tree, start, end, EXTENT_LOCKED | 867 clear_extent_bit(&BTRFS_I(inode)->io_tree, start, end, EXTENT_LOCKED |
856 EXTENT_DELALLOC, 1, 0, GFP_NOFS); 868 EXTENT_DELALLOC, 1, 0, NULL, GFP_NOFS);
857 while (start < end) { 869 while (start < end) {
858 async_cow = kmalloc(sizeof(*async_cow), GFP_NOFS); 870 async_cow = kmalloc(sizeof(*async_cow), GFP_NOFS);
859 async_cow->inode = inode; 871 async_cow->inode = inode;
@@ -1080,9 +1092,9 @@ out_check:
1080 em->bdev = root->fs_info->fs_devices->latest_bdev; 1092 em->bdev = root->fs_info->fs_devices->latest_bdev;
1081 set_bit(EXTENT_FLAG_PINNED, &em->flags); 1093 set_bit(EXTENT_FLAG_PINNED, &em->flags);
1082 while (1) { 1094 while (1) {
1083 spin_lock(&em_tree->lock); 1095 write_lock(&em_tree->lock);
1084 ret = add_extent_mapping(em_tree, em); 1096 ret = add_extent_mapping(em_tree, em);
1085 spin_unlock(&em_tree->lock); 1097 write_unlock(&em_tree->lock);
1086 if (ret != -EEXIST) { 1098 if (ret != -EEXIST) {
1087 free_extent_map(em); 1099 free_extent_map(em);
1088 break; 1100 break;
@@ -1101,7 +1113,7 @@ out_check:
1101 1113
1102 extent_clear_unlock_delalloc(inode, &BTRFS_I(inode)->io_tree, 1114 extent_clear_unlock_delalloc(inode, &BTRFS_I(inode)->io_tree,
1103 cur_offset, cur_offset + num_bytes - 1, 1115 cur_offset, cur_offset + num_bytes - 1,
1104 locked_page, 1, 1, 1, 0, 0, 0); 1116 locked_page, 1, 1, 1, 0, 0, 0, 1);
1105 cur_offset = extent_end; 1117 cur_offset = extent_end;
1106 if (cur_offset > end) 1118 if (cur_offset > end)
1107 break; 1119 break;
@@ -1147,6 +1159,83 @@ static int run_delalloc_range(struct inode *inode, struct page *locked_page,
1147 return ret; 1159 return ret;
1148} 1160}
1149 1161
1162static int btrfs_split_extent_hook(struct inode *inode,
1163 struct extent_state *orig, u64 split)
1164{
1165 struct btrfs_root *root = BTRFS_I(inode)->root;
1166 u64 size;
1167
1168 if (!(orig->state & EXTENT_DELALLOC))
1169 return 0;
1170
1171 size = orig->end - orig->start + 1;
1172 if (size > root->fs_info->max_extent) {
1173 u64 num_extents;
1174 u64 new_size;
1175
1176 new_size = orig->end - split + 1;
1177 num_extents = div64_u64(size + root->fs_info->max_extent - 1,
1178 root->fs_info->max_extent);
1179
1180 /*
1181 * if we break a large extent up then leave delalloc_extents be,
1182 * since we've already accounted for the large extent.
1183 */
1184 if (div64_u64(new_size + root->fs_info->max_extent - 1,
1185 root->fs_info->max_extent) < num_extents)
1186 return 0;
1187 }
1188
1189 BTRFS_I(inode)->delalloc_extents++;
1190
1191 return 0;
1192}
1193
1194/*
1195 * extent_io.c merge_extent_hook, used to track merged delayed allocation
1196 * extents so we can keep track of new extents that are just merged onto old
1197 * extents, such as when we are doing sequential writes, so we can properly
1198 * account for the metadata space we'll need.
1199 */
1200static int btrfs_merge_extent_hook(struct inode *inode,
1201 struct extent_state *new,
1202 struct extent_state *other)
1203{
1204 struct btrfs_root *root = BTRFS_I(inode)->root;
1205 u64 new_size, old_size;
1206 u64 num_extents;
1207
1208 /* not delalloc, ignore it */
1209 if (!(other->state & EXTENT_DELALLOC))
1210 return 0;
1211
1212 old_size = other->end - other->start + 1;
1213 if (new->start < other->start)
1214 new_size = other->end - new->start + 1;
1215 else
1216 new_size = new->end - other->start + 1;
1217
1218 /* we're not bigger than the max, unreserve the space and go */
1219 if (new_size <= root->fs_info->max_extent) {
1220 BTRFS_I(inode)->delalloc_extents--;
1221 return 0;
1222 }
1223
1224 /*
1225 * If we grew by another max_extent, just return, we want to keep that
1226 * reserved amount.
1227 */
1228 num_extents = div64_u64(old_size + root->fs_info->max_extent - 1,
1229 root->fs_info->max_extent);
1230 if (div64_u64(new_size + root->fs_info->max_extent - 1,
1231 root->fs_info->max_extent) > num_extents)
1232 return 0;
1233
1234 BTRFS_I(inode)->delalloc_extents--;
1235
1236 return 0;
1237}
1238
1150/* 1239/*
1151 * extent_io.c set_bit_hook, used to track delayed allocation 1240 * extent_io.c set_bit_hook, used to track delayed allocation
1152 * bytes in this file, and to maintain the list of inodes that 1241 * bytes in this file, and to maintain the list of inodes that
@@ -1155,6 +1244,7 @@ static int run_delalloc_range(struct inode *inode, struct page *locked_page,
1155static int btrfs_set_bit_hook(struct inode *inode, u64 start, u64 end, 1244static int btrfs_set_bit_hook(struct inode *inode, u64 start, u64 end,
1156 unsigned long old, unsigned long bits) 1245 unsigned long old, unsigned long bits)
1157{ 1246{
1247
1158 /* 1248 /*
1159 * set_bit and clear bit hooks normally require _irqsave/restore 1249 * set_bit and clear bit hooks normally require _irqsave/restore
1160 * but in this case, we are only testeing for the DELALLOC 1250 * but in this case, we are only testeing for the DELALLOC
@@ -1162,6 +1252,8 @@ static int btrfs_set_bit_hook(struct inode *inode, u64 start, u64 end,
1162 */ 1252 */
1163 if (!(old & EXTENT_DELALLOC) && (bits & EXTENT_DELALLOC)) { 1253 if (!(old & EXTENT_DELALLOC) && (bits & EXTENT_DELALLOC)) {
1164 struct btrfs_root *root = BTRFS_I(inode)->root; 1254 struct btrfs_root *root = BTRFS_I(inode)->root;
1255
1256 BTRFS_I(inode)->delalloc_extents++;
1165 btrfs_delalloc_reserve_space(root, inode, end - start + 1); 1257 btrfs_delalloc_reserve_space(root, inode, end - start + 1);
1166 spin_lock(&root->fs_info->delalloc_lock); 1258 spin_lock(&root->fs_info->delalloc_lock);
1167 BTRFS_I(inode)->delalloc_bytes += end - start + 1; 1259 BTRFS_I(inode)->delalloc_bytes += end - start + 1;
@@ -1178,22 +1270,27 @@ static int btrfs_set_bit_hook(struct inode *inode, u64 start, u64 end,
1178/* 1270/*
1179 * extent_io.c clear_bit_hook, see set_bit_hook for why 1271 * extent_io.c clear_bit_hook, see set_bit_hook for why
1180 */ 1272 */
1181static int btrfs_clear_bit_hook(struct inode *inode, u64 start, u64 end, 1273static int btrfs_clear_bit_hook(struct inode *inode,
1182 unsigned long old, unsigned long bits) 1274 struct extent_state *state, unsigned long bits)
1183{ 1275{
1184 /* 1276 /*
1185 * set_bit and clear bit hooks normally require _irqsave/restore 1277 * set_bit and clear bit hooks normally require _irqsave/restore
1186 * but in this case, we are only testeing for the DELALLOC 1278 * but in this case, we are only testeing for the DELALLOC
1187 * bit, which is only set or cleared with irqs on 1279 * bit, which is only set or cleared with irqs on
1188 */ 1280 */
1189 if ((old & EXTENT_DELALLOC) && (bits & EXTENT_DELALLOC)) { 1281 if ((state->state & EXTENT_DELALLOC) && (bits & EXTENT_DELALLOC)) {
1190 struct btrfs_root *root = BTRFS_I(inode)->root; 1282 struct btrfs_root *root = BTRFS_I(inode)->root;
1191 1283
1284 BTRFS_I(inode)->delalloc_extents--;
1285 btrfs_unreserve_metadata_for_delalloc(root, inode, 1);
1286
1192 spin_lock(&root->fs_info->delalloc_lock); 1287 spin_lock(&root->fs_info->delalloc_lock);
1193 if (end - start + 1 > root->fs_info->delalloc_bytes) { 1288 if (state->end - state->start + 1 >
1289 root->fs_info->delalloc_bytes) {
1194 printk(KERN_INFO "btrfs warning: delalloc account " 1290 printk(KERN_INFO "btrfs warning: delalloc account "
1195 "%llu %llu\n", 1291 "%llu %llu\n",
1196 (unsigned long long)end - start + 1, 1292 (unsigned long long)
1293 state->end - state->start + 1,
1197 (unsigned long long) 1294 (unsigned long long)
1198 root->fs_info->delalloc_bytes); 1295 root->fs_info->delalloc_bytes);
1199 btrfs_delalloc_free_space(root, inode, (u64)-1); 1296 btrfs_delalloc_free_space(root, inode, (u64)-1);
@@ -1201,9 +1298,12 @@ static int btrfs_clear_bit_hook(struct inode *inode, u64 start, u64 end,
1201 BTRFS_I(inode)->delalloc_bytes = 0; 1298 BTRFS_I(inode)->delalloc_bytes = 0;
1202 } else { 1299 } else {
1203 btrfs_delalloc_free_space(root, inode, 1300 btrfs_delalloc_free_space(root, inode,
1204 end - start + 1); 1301 state->end -
1205 root->fs_info->delalloc_bytes -= end - start + 1; 1302 state->start + 1);
1206 BTRFS_I(inode)->delalloc_bytes -= end - start + 1; 1303 root->fs_info->delalloc_bytes -= state->end -
1304 state->start + 1;
1305 BTRFS_I(inode)->delalloc_bytes -= state->end -
1306 state->start + 1;
1207 } 1307 }
1208 if (BTRFS_I(inode)->delalloc_bytes == 0 && 1308 if (BTRFS_I(inode)->delalloc_bytes == 0 &&
1209 !list_empty(&BTRFS_I(inode)->delalloc_inodes)) { 1309 !list_empty(&BTRFS_I(inode)->delalloc_inodes)) {
@@ -1374,10 +1474,8 @@ again:
1374 lock_extent(&BTRFS_I(inode)->io_tree, page_start, page_end, GFP_NOFS); 1474 lock_extent(&BTRFS_I(inode)->io_tree, page_start, page_end, GFP_NOFS);
1375 1475
1376 /* already ordered? We're done */ 1476 /* already ordered? We're done */
1377 if (test_range_bit(&BTRFS_I(inode)->io_tree, page_start, page_end, 1477 if (PagePrivate2(page))
1378 EXTENT_ORDERED, 0)) {
1379 goto out; 1478 goto out;
1380 }
1381 1479
1382 ordered = btrfs_lookup_ordered_extent(inode, page_start); 1480 ordered = btrfs_lookup_ordered_extent(inode, page_start);
1383 if (ordered) { 1481 if (ordered) {
@@ -1413,11 +1511,9 @@ static int btrfs_writepage_start_hook(struct page *page, u64 start, u64 end)
1413 struct inode *inode = page->mapping->host; 1511 struct inode *inode = page->mapping->host;
1414 struct btrfs_writepage_fixup *fixup; 1512 struct btrfs_writepage_fixup *fixup;
1415 struct btrfs_root *root = BTRFS_I(inode)->root; 1513 struct btrfs_root *root = BTRFS_I(inode)->root;
1416 int ret;
1417 1514
1418 ret = test_range_bit(&BTRFS_I(inode)->io_tree, start, end, 1515 /* this page is properly in the ordered list */
1419 EXTENT_ORDERED, 0); 1516 if (TestClearPagePrivate2(page))
1420 if (ret)
1421 return 0; 1517 return 0;
1422 1518
1423 if (PageChecked(page)) 1519 if (PageChecked(page))
@@ -1455,9 +1551,19 @@ static int insert_reserved_file_extent(struct btrfs_trans_handle *trans,
1455 BUG_ON(!path); 1551 BUG_ON(!path);
1456 1552
1457 path->leave_spinning = 1; 1553 path->leave_spinning = 1;
1554
1555 /*
1556 * we may be replacing one extent in the tree with another.
1557 * The new extent is pinned in the extent map, and we don't want
1558 * to drop it from the cache until it is completely in the btree.
1559 *
1560 * So, tell btrfs_drop_extents to leave this extent in the cache.
1561 * the caller is expected to unpin it and allow it to be merged
1562 * with the others.
1563 */
1458 ret = btrfs_drop_extents(trans, root, inode, file_pos, 1564 ret = btrfs_drop_extents(trans, root, inode, file_pos,
1459 file_pos + num_bytes, locked_end, 1565 file_pos + num_bytes, locked_end,
1460 file_pos, &hint); 1566 file_pos, &hint, 0);
1461 BUG_ON(ret); 1567 BUG_ON(ret);
1462 1568
1463 ins.objectid = inode->i_ino; 1569 ins.objectid = inode->i_ino;
@@ -1485,7 +1591,6 @@ static int insert_reserved_file_extent(struct btrfs_trans_handle *trans,
1485 btrfs_mark_buffer_dirty(leaf); 1591 btrfs_mark_buffer_dirty(leaf);
1486 1592
1487 inode_add_bytes(inode, num_bytes); 1593 inode_add_bytes(inode, num_bytes);
1488 btrfs_drop_extent_cache(inode, file_pos, file_pos + num_bytes - 1, 0);
1489 1594
1490 ins.objectid = disk_bytenr; 1595 ins.objectid = disk_bytenr;
1491 ins.offset = disk_num_bytes; 1596 ins.offset = disk_num_bytes;
@@ -1596,6 +1701,9 @@ static int btrfs_finish_ordered_io(struct inode *inode, u64 start, u64 end)
1596 ordered_extent->len, 1701 ordered_extent->len,
1597 compressed, 0, 0, 1702 compressed, 0, 0,
1598 BTRFS_FILE_EXTENT_REG); 1703 BTRFS_FILE_EXTENT_REG);
1704 unpin_extent_cache(&BTRFS_I(inode)->extent_tree,
1705 ordered_extent->file_offset,
1706 ordered_extent->len);
1599 BUG_ON(ret); 1707 BUG_ON(ret);
1600 } 1708 }
1601 unlock_extent(io_tree, ordered_extent->file_offset, 1709 unlock_extent(io_tree, ordered_extent->file_offset,
@@ -1623,6 +1731,7 @@ nocow:
1623static int btrfs_writepage_end_io_hook(struct page *page, u64 start, u64 end, 1731static int btrfs_writepage_end_io_hook(struct page *page, u64 start, u64 end,
1624 struct extent_state *state, int uptodate) 1732 struct extent_state *state, int uptodate)
1625{ 1733{
1734 ClearPagePrivate2(page);
1626 return btrfs_finish_ordered_io(page->mapping->host, start, end); 1735 return btrfs_finish_ordered_io(page->mapping->host, start, end);
1627} 1736}
1628 1737
@@ -1669,13 +1778,13 @@ static int btrfs_io_failed_hook(struct bio *failed_bio,
1669 failrec->last_mirror = 0; 1778 failrec->last_mirror = 0;
1670 failrec->bio_flags = 0; 1779 failrec->bio_flags = 0;
1671 1780
1672 spin_lock(&em_tree->lock); 1781 read_lock(&em_tree->lock);
1673 em = lookup_extent_mapping(em_tree, start, failrec->len); 1782 em = lookup_extent_mapping(em_tree, start, failrec->len);
1674 if (em->start > start || em->start + em->len < start) { 1783 if (em->start > start || em->start + em->len < start) {
1675 free_extent_map(em); 1784 free_extent_map(em);
1676 em = NULL; 1785 em = NULL;
1677 } 1786 }
1678 spin_unlock(&em_tree->lock); 1787 read_unlock(&em_tree->lock);
1679 1788
1680 if (!em || IS_ERR(em)) { 1789 if (!em || IS_ERR(em)) {
1681 kfree(failrec); 1790 kfree(failrec);
@@ -1794,7 +1903,7 @@ static int btrfs_readpage_end_io_hook(struct page *page, u64 start, u64 end,
1794 return 0; 1903 return 0;
1795 1904
1796 if (root->root_key.objectid == BTRFS_DATA_RELOC_TREE_OBJECTID && 1905 if (root->root_key.objectid == BTRFS_DATA_RELOC_TREE_OBJECTID &&
1797 test_range_bit(io_tree, start, end, EXTENT_NODATASUM, 1)) { 1906 test_range_bit(io_tree, start, end, EXTENT_NODATASUM, 1, NULL)) {
1798 clear_extent_bits(io_tree, start, end, EXTENT_NODATASUM, 1907 clear_extent_bits(io_tree, start, end, EXTENT_NODATASUM,
1799 GFP_NOFS); 1908 GFP_NOFS);
1800 return 0; 1909 return 0;
@@ -2352,6 +2461,69 @@ static int btrfs_unlink(struct inode *dir, struct dentry *dentry)
2352 return ret; 2461 return ret;
2353} 2462}
2354 2463
2464int btrfs_unlink_subvol(struct btrfs_trans_handle *trans,
2465 struct btrfs_root *root,
2466 struct inode *dir, u64 objectid,
2467 const char *name, int name_len)
2468{
2469 struct btrfs_path *path;
2470 struct extent_buffer *leaf;
2471 struct btrfs_dir_item *di;
2472 struct btrfs_key key;
2473 u64 index;
2474 int ret;
2475
2476 path = btrfs_alloc_path();
2477 if (!path)
2478 return -ENOMEM;
2479
2480 di = btrfs_lookup_dir_item(trans, root, path, dir->i_ino,
2481 name, name_len, -1);
2482 BUG_ON(!di || IS_ERR(di));
2483
2484 leaf = path->nodes[0];
2485 btrfs_dir_item_key_to_cpu(leaf, di, &key);
2486 WARN_ON(key.type != BTRFS_ROOT_ITEM_KEY || key.objectid != objectid);
2487 ret = btrfs_delete_one_dir_name(trans, root, path, di);
2488 BUG_ON(ret);
2489 btrfs_release_path(root, path);
2490
2491 ret = btrfs_del_root_ref(trans, root->fs_info->tree_root,
2492 objectid, root->root_key.objectid,
2493 dir->i_ino, &index, name, name_len);
2494 if (ret < 0) {
2495 BUG_ON(ret != -ENOENT);
2496 di = btrfs_search_dir_index_item(root, path, dir->i_ino,
2497 name, name_len);
2498 BUG_ON(!di || IS_ERR(di));
2499
2500 leaf = path->nodes[0];
2501 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
2502 btrfs_release_path(root, path);
2503 index = key.offset;
2504 }
2505
2506 di = btrfs_lookup_dir_index_item(trans, root, path, dir->i_ino,
2507 index, name, name_len, -1);
2508 BUG_ON(!di || IS_ERR(di));
2509
2510 leaf = path->nodes[0];
2511 btrfs_dir_item_key_to_cpu(leaf, di, &key);
2512 WARN_ON(key.type != BTRFS_ROOT_ITEM_KEY || key.objectid != objectid);
2513 ret = btrfs_delete_one_dir_name(trans, root, path, di);
2514 BUG_ON(ret);
2515 btrfs_release_path(root, path);
2516
2517 btrfs_i_size_write(dir, dir->i_size - name_len * 2);
2518 dir->i_mtime = dir->i_ctime = CURRENT_TIME;
2519 ret = btrfs_update_inode(trans, root, dir);
2520 BUG_ON(ret);
2521 dir->i_sb->s_dirt = 1;
2522
2523 btrfs_free_path(path);
2524 return 0;
2525}
2526
2355static int btrfs_rmdir(struct inode *dir, struct dentry *dentry) 2527static int btrfs_rmdir(struct inode *dir, struct dentry *dentry)
2356{ 2528{
2357 struct inode *inode = dentry->d_inode; 2529 struct inode *inode = dentry->d_inode;
@@ -2361,29 +2533,31 @@ static int btrfs_rmdir(struct inode *dir, struct dentry *dentry)
2361 struct btrfs_trans_handle *trans; 2533 struct btrfs_trans_handle *trans;
2362 unsigned long nr = 0; 2534 unsigned long nr = 0;
2363 2535
2364 /*
2365 * the FIRST_FREE_OBJECTID check makes sure we don't try to rmdir
2366 * the root of a subvolume or snapshot
2367 */
2368 if (inode->i_size > BTRFS_EMPTY_DIR_SIZE || 2536 if (inode->i_size > BTRFS_EMPTY_DIR_SIZE ||
2369 inode->i_ino == BTRFS_FIRST_FREE_OBJECTID) { 2537 inode->i_ino == BTRFS_FIRST_FREE_OBJECTID)
2370 return -ENOTEMPTY; 2538 return -ENOTEMPTY;
2371 }
2372 2539
2373 trans = btrfs_start_transaction(root, 1); 2540 trans = btrfs_start_transaction(root, 1);
2374 btrfs_set_trans_block_group(trans, dir); 2541 btrfs_set_trans_block_group(trans, dir);
2375 2542
2543 if (unlikely(inode->i_ino == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID)) {
2544 err = btrfs_unlink_subvol(trans, root, dir,
2545 BTRFS_I(inode)->location.objectid,
2546 dentry->d_name.name,
2547 dentry->d_name.len);
2548 goto out;
2549 }
2550
2376 err = btrfs_orphan_add(trans, inode); 2551 err = btrfs_orphan_add(trans, inode);
2377 if (err) 2552 if (err)
2378 goto fail_trans; 2553 goto out;
2379 2554
2380 /* now the directory is empty */ 2555 /* now the directory is empty */
2381 err = btrfs_unlink_inode(trans, root, dir, dentry->d_inode, 2556 err = btrfs_unlink_inode(trans, root, dir, dentry->d_inode,
2382 dentry->d_name.name, dentry->d_name.len); 2557 dentry->d_name.name, dentry->d_name.len);
2383 if (!err) 2558 if (!err)
2384 btrfs_i_size_write(inode, 0); 2559 btrfs_i_size_write(inode, 0);
2385 2560out:
2386fail_trans:
2387 nr = trans->blocks_used; 2561 nr = trans->blocks_used;
2388 ret = btrfs_end_transaction_throttle(trans, root); 2562 ret = btrfs_end_transaction_throttle(trans, root);
2389 btrfs_btree_balance_dirty(root, nr); 2563 btrfs_btree_balance_dirty(root, nr);
@@ -2864,7 +3038,12 @@ again:
2864 goto again; 3038 goto again;
2865 } 3039 }
2866 3040
2867 btrfs_set_extent_delalloc(inode, page_start, page_end); 3041 ret = btrfs_set_extent_delalloc(inode, page_start, page_end);
3042 if (ret) {
3043 unlock_extent(io_tree, page_start, page_end, GFP_NOFS);
3044 goto out_unlock;
3045 }
3046
2868 ret = 0; 3047 ret = 0;
2869 if (offset != PAGE_CACHE_SIZE) { 3048 if (offset != PAGE_CACHE_SIZE) {
2870 kaddr = kmap(page); 3049 kaddr = kmap(page);
@@ -2895,15 +3074,11 @@ int btrfs_cont_expand(struct inode *inode, loff_t size)
2895 u64 last_byte; 3074 u64 last_byte;
2896 u64 cur_offset; 3075 u64 cur_offset;
2897 u64 hole_size; 3076 u64 hole_size;
2898 int err; 3077 int err = 0;
2899 3078
2900 if (size <= hole_start) 3079 if (size <= hole_start)
2901 return 0; 3080 return 0;
2902 3081
2903 err = btrfs_check_metadata_free_space(root);
2904 if (err)
2905 return err;
2906
2907 btrfs_truncate_page(inode->i_mapping, inode->i_size); 3082 btrfs_truncate_page(inode->i_mapping, inode->i_size);
2908 3083
2909 while (1) { 3084 while (1) {
@@ -2935,15 +3110,21 @@ int btrfs_cont_expand(struct inode *inode, loff_t size)
2935 cur_offset, 3110 cur_offset,
2936 cur_offset + hole_size, 3111 cur_offset + hole_size,
2937 block_end, 3112 block_end,
2938 cur_offset, &hint_byte); 3113 cur_offset, &hint_byte, 1);
2939 if (err) 3114 if (err)
2940 break; 3115 break;
3116
3117 err = btrfs_reserve_metadata_space(root, 1);
3118 if (err)
3119 break;
3120
2941 err = btrfs_insert_file_extent(trans, root, 3121 err = btrfs_insert_file_extent(trans, root,
2942 inode->i_ino, cur_offset, 0, 3122 inode->i_ino, cur_offset, 0,
2943 0, hole_size, 0, hole_size, 3123 0, hole_size, 0, hole_size,
2944 0, 0, 0); 3124 0, 0, 0);
2945 btrfs_drop_extent_cache(inode, hole_start, 3125 btrfs_drop_extent_cache(inode, hole_start,
2946 last_byte - 1, 0); 3126 last_byte - 1, 0);
3127 btrfs_unreserve_metadata_space(root, 1);
2947 } 3128 }
2948 free_extent_map(em); 3129 free_extent_map(em);
2949 cur_offset = last_byte; 3130 cur_offset = last_byte;
@@ -3003,6 +3184,11 @@ void btrfs_delete_inode(struct inode *inode)
3003 } 3184 }
3004 btrfs_wait_ordered_range(inode, 0, (u64)-1); 3185 btrfs_wait_ordered_range(inode, 0, (u64)-1);
3005 3186
3187 if (inode->i_nlink > 0) {
3188 BUG_ON(btrfs_root_refs(&root->root_item) != 0);
3189 goto no_delete;
3190 }
3191
3006 btrfs_i_size_write(inode, 0); 3192 btrfs_i_size_write(inode, 0);
3007 trans = btrfs_join_transaction(root, 1); 3193 trans = btrfs_join_transaction(root, 1);
3008 3194
@@ -3070,29 +3256,67 @@ out_err:
3070 * is kind of like crossing a mount point. 3256 * is kind of like crossing a mount point.
3071 */ 3257 */
3072static int fixup_tree_root_location(struct btrfs_root *root, 3258static int fixup_tree_root_location(struct btrfs_root *root,
3073 struct btrfs_key *location, 3259 struct inode *dir,
3074 struct btrfs_root **sub_root, 3260 struct dentry *dentry,
3075 struct dentry *dentry) 3261 struct btrfs_key *location,
3262 struct btrfs_root **sub_root)
3076{ 3263{
3077 struct btrfs_root_item *ri; 3264 struct btrfs_path *path;
3265 struct btrfs_root *new_root;
3266 struct btrfs_root_ref *ref;
3267 struct extent_buffer *leaf;
3268 int ret;
3269 int err = 0;
3078 3270
3079 if (btrfs_key_type(location) != BTRFS_ROOT_ITEM_KEY) 3271 path = btrfs_alloc_path();
3080 return 0; 3272 if (!path) {
3081 if (location->objectid == BTRFS_ROOT_TREE_OBJECTID) 3273 err = -ENOMEM;
3082 return 0; 3274 goto out;
3275 }
3083 3276
3084 *sub_root = btrfs_read_fs_root(root->fs_info, location, 3277 err = -ENOENT;
3085 dentry->d_name.name, 3278 ret = btrfs_find_root_ref(root->fs_info->tree_root, path,
3086 dentry->d_name.len); 3279 BTRFS_I(dir)->root->root_key.objectid,
3087 if (IS_ERR(*sub_root)) 3280 location->objectid);
3088 return PTR_ERR(*sub_root); 3281 if (ret) {
3282 if (ret < 0)
3283 err = ret;
3284 goto out;
3285 }
3089 3286
3090 ri = &(*sub_root)->root_item; 3287 leaf = path->nodes[0];
3091 location->objectid = btrfs_root_dirid(ri); 3288 ref = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_root_ref);
3092 btrfs_set_key_type(location, BTRFS_INODE_ITEM_KEY); 3289 if (btrfs_root_ref_dirid(leaf, ref) != dir->i_ino ||
3093 location->offset = 0; 3290 btrfs_root_ref_name_len(leaf, ref) != dentry->d_name.len)
3291 goto out;
3094 3292
3095 return 0; 3293 ret = memcmp_extent_buffer(leaf, dentry->d_name.name,
3294 (unsigned long)(ref + 1),
3295 dentry->d_name.len);
3296 if (ret)
3297 goto out;
3298
3299 btrfs_release_path(root->fs_info->tree_root, path);
3300
3301 new_root = btrfs_read_fs_root_no_name(root->fs_info, location);
3302 if (IS_ERR(new_root)) {
3303 err = PTR_ERR(new_root);
3304 goto out;
3305 }
3306
3307 if (btrfs_root_refs(&new_root->root_item) == 0) {
3308 err = -ENOENT;
3309 goto out;
3310 }
3311
3312 *sub_root = new_root;
3313 location->objectid = btrfs_root_dirid(&new_root->root_item);
3314 location->type = BTRFS_INODE_ITEM_KEY;
3315 location->offset = 0;
3316 err = 0;
3317out:
3318 btrfs_free_path(path);
3319 return err;
3096} 3320}
3097 3321
3098static void inode_tree_add(struct inode *inode) 3322static void inode_tree_add(struct inode *inode)
@@ -3101,11 +3325,13 @@ static void inode_tree_add(struct inode *inode)
3101 struct btrfs_inode *entry; 3325 struct btrfs_inode *entry;
3102 struct rb_node **p; 3326 struct rb_node **p;
3103 struct rb_node *parent; 3327 struct rb_node *parent;
3104
3105again: 3328again:
3106 p = &root->inode_tree.rb_node; 3329 p = &root->inode_tree.rb_node;
3107 parent = NULL; 3330 parent = NULL;
3108 3331
3332 if (hlist_unhashed(&inode->i_hash))
3333 return;
3334
3109 spin_lock(&root->inode_lock); 3335 spin_lock(&root->inode_lock);
3110 while (*p) { 3336 while (*p) {
3111 parent = *p; 3337 parent = *p;
@@ -3132,13 +3358,87 @@ again:
3132static void inode_tree_del(struct inode *inode) 3358static void inode_tree_del(struct inode *inode)
3133{ 3359{
3134 struct btrfs_root *root = BTRFS_I(inode)->root; 3360 struct btrfs_root *root = BTRFS_I(inode)->root;
3361 int empty = 0;
3135 3362
3136 spin_lock(&root->inode_lock); 3363 spin_lock(&root->inode_lock);
3137 if (!RB_EMPTY_NODE(&BTRFS_I(inode)->rb_node)) { 3364 if (!RB_EMPTY_NODE(&BTRFS_I(inode)->rb_node)) {
3138 rb_erase(&BTRFS_I(inode)->rb_node, &root->inode_tree); 3365 rb_erase(&BTRFS_I(inode)->rb_node, &root->inode_tree);
3139 RB_CLEAR_NODE(&BTRFS_I(inode)->rb_node); 3366 RB_CLEAR_NODE(&BTRFS_I(inode)->rb_node);
3367 empty = RB_EMPTY_ROOT(&root->inode_tree);
3368 }
3369 spin_unlock(&root->inode_lock);
3370
3371 if (empty && btrfs_root_refs(&root->root_item) == 0) {
3372 synchronize_srcu(&root->fs_info->subvol_srcu);
3373 spin_lock(&root->inode_lock);
3374 empty = RB_EMPTY_ROOT(&root->inode_tree);
3375 spin_unlock(&root->inode_lock);
3376 if (empty)
3377 btrfs_add_dead_root(root);
3378 }
3379}
3380
3381int btrfs_invalidate_inodes(struct btrfs_root *root)
3382{
3383 struct rb_node *node;
3384 struct rb_node *prev;
3385 struct btrfs_inode *entry;
3386 struct inode *inode;
3387 u64 objectid = 0;
3388
3389 WARN_ON(btrfs_root_refs(&root->root_item) != 0);
3390
3391 spin_lock(&root->inode_lock);
3392again:
3393 node = root->inode_tree.rb_node;
3394 prev = NULL;
3395 while (node) {
3396 prev = node;
3397 entry = rb_entry(node, struct btrfs_inode, rb_node);
3398
3399 if (objectid < entry->vfs_inode.i_ino)
3400 node = node->rb_left;
3401 else if (objectid > entry->vfs_inode.i_ino)
3402 node = node->rb_right;
3403 else
3404 break;
3405 }
3406 if (!node) {
3407 while (prev) {
3408 entry = rb_entry(prev, struct btrfs_inode, rb_node);
3409 if (objectid <= entry->vfs_inode.i_ino) {
3410 node = prev;
3411 break;
3412 }
3413 prev = rb_next(prev);
3414 }
3415 }
3416 while (node) {
3417 entry = rb_entry(node, struct btrfs_inode, rb_node);
3418 objectid = entry->vfs_inode.i_ino + 1;
3419 inode = igrab(&entry->vfs_inode);
3420 if (inode) {
3421 spin_unlock(&root->inode_lock);
3422 if (atomic_read(&inode->i_count) > 1)
3423 d_prune_aliases(inode);
3424 /*
3425 * btrfs_drop_inode will remove it from
3426 * the inode cache when its usage count
3427 * hits zero.
3428 */
3429 iput(inode);
3430 cond_resched();
3431 spin_lock(&root->inode_lock);
3432 goto again;
3433 }
3434
3435 if (cond_resched_lock(&root->inode_lock))
3436 goto again;
3437
3438 node = rb_next(node);
3140 } 3439 }
3141 spin_unlock(&root->inode_lock); 3440 spin_unlock(&root->inode_lock);
3441 return 0;
3142} 3442}
3143 3443
3144static noinline void init_btrfs_i(struct inode *inode) 3444static noinline void init_btrfs_i(struct inode *inode)
@@ -3225,15 +3525,41 @@ struct inode *btrfs_iget(struct super_block *s, struct btrfs_key *location,
3225 return inode; 3525 return inode;
3226} 3526}
3227 3527
3528static struct inode *new_simple_dir(struct super_block *s,
3529 struct btrfs_key *key,
3530 struct btrfs_root *root)
3531{
3532 struct inode *inode = new_inode(s);
3533
3534 if (!inode)
3535 return ERR_PTR(-ENOMEM);
3536
3537 init_btrfs_i(inode);
3538
3539 BTRFS_I(inode)->root = root;
3540 memcpy(&BTRFS_I(inode)->location, key, sizeof(*key));
3541 BTRFS_I(inode)->dummy_inode = 1;
3542
3543 inode->i_ino = BTRFS_EMPTY_SUBVOL_DIR_OBJECTID;
3544 inode->i_op = &simple_dir_inode_operations;
3545 inode->i_fop = &simple_dir_operations;
3546 inode->i_mode = S_IFDIR | S_IRUGO | S_IWUSR | S_IXUGO;
3547 inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME;
3548
3549 return inode;
3550}
3551
3228struct inode *btrfs_lookup_dentry(struct inode *dir, struct dentry *dentry) 3552struct inode *btrfs_lookup_dentry(struct inode *dir, struct dentry *dentry)
3229{ 3553{
3230 struct inode *inode; 3554 struct inode *inode;
3231 struct btrfs_inode *bi = BTRFS_I(dir); 3555 struct btrfs_root *root = BTRFS_I(dir)->root;
3232 struct btrfs_root *root = bi->root;
3233 struct btrfs_root *sub_root = root; 3556 struct btrfs_root *sub_root = root;
3234 struct btrfs_key location; 3557 struct btrfs_key location;
3558 int index;
3235 int ret; 3559 int ret;
3236 3560
3561 dentry->d_op = &btrfs_dentry_operations;
3562
3237 if (dentry->d_name.len > BTRFS_NAME_LEN) 3563 if (dentry->d_name.len > BTRFS_NAME_LEN)
3238 return ERR_PTR(-ENAMETOOLONG); 3564 return ERR_PTR(-ENAMETOOLONG);
3239 3565
@@ -3242,29 +3568,50 @@ struct inode *btrfs_lookup_dentry(struct inode *dir, struct dentry *dentry)
3242 if (ret < 0) 3568 if (ret < 0)
3243 return ERR_PTR(ret); 3569 return ERR_PTR(ret);
3244 3570
3245 inode = NULL; 3571 if (location.objectid == 0)
3246 if (location.objectid) { 3572 return NULL;
3247 ret = fixup_tree_root_location(root, &location, &sub_root, 3573
3248 dentry); 3574 if (location.type == BTRFS_INODE_ITEM_KEY) {
3249 if (ret < 0) 3575 inode = btrfs_iget(dir->i_sb, &location, root);
3250 return ERR_PTR(ret); 3576 return inode;
3251 if (ret > 0) 3577 }
3252 return ERR_PTR(-ENOENT); 3578
3579 BUG_ON(location.type != BTRFS_ROOT_ITEM_KEY);
3580
3581 index = srcu_read_lock(&root->fs_info->subvol_srcu);
3582 ret = fixup_tree_root_location(root, dir, dentry,
3583 &location, &sub_root);
3584 if (ret < 0) {
3585 if (ret != -ENOENT)
3586 inode = ERR_PTR(ret);
3587 else
3588 inode = new_simple_dir(dir->i_sb, &location, sub_root);
3589 } else {
3253 inode = btrfs_iget(dir->i_sb, &location, sub_root); 3590 inode = btrfs_iget(dir->i_sb, &location, sub_root);
3254 if (IS_ERR(inode))
3255 return ERR_CAST(inode);
3256 } 3591 }
3592 srcu_read_unlock(&root->fs_info->subvol_srcu, index);
3593
3257 return inode; 3594 return inode;
3258} 3595}
3259 3596
3597static int btrfs_dentry_delete(struct dentry *dentry)
3598{
3599 struct btrfs_root *root;
3600
3601 if (!dentry->d_inode)
3602 return 0;
3603
3604 root = BTRFS_I(dentry->d_inode)->root;
3605 if (btrfs_root_refs(&root->root_item) == 0)
3606 return 1;
3607 return 0;
3608}
3609
3260static struct dentry *btrfs_lookup(struct inode *dir, struct dentry *dentry, 3610static struct dentry *btrfs_lookup(struct inode *dir, struct dentry *dentry,
3261 struct nameidata *nd) 3611 struct nameidata *nd)
3262{ 3612{
3263 struct inode *inode; 3613 struct inode *inode;
3264 3614
3265 if (dentry->d_name.len > BTRFS_NAME_LEN)
3266 return ERR_PTR(-ENAMETOOLONG);
3267
3268 inode = btrfs_lookup_dentry(dir, dentry); 3615 inode = btrfs_lookup_dentry(dir, dentry);
3269 if (IS_ERR(inode)) 3616 if (IS_ERR(inode))
3270 return ERR_CAST(inode); 3617 return ERR_CAST(inode);
@@ -3603,9 +3950,6 @@ static struct inode *btrfs_new_inode(struct btrfs_trans_handle *trans,
3603 if (ret != 0) 3950 if (ret != 0)
3604 goto fail; 3951 goto fail;
3605 3952
3606 if (objectid > root->highest_inode)
3607 root->highest_inode = objectid;
3608
3609 inode->i_uid = current_fsuid(); 3953 inode->i_uid = current_fsuid();
3610 3954
3611 if (dir && (dir->i_mode & S_ISGID)) { 3955 if (dir && (dir->i_mode & S_ISGID)) {
@@ -3673,26 +4017,35 @@ int btrfs_add_link(struct btrfs_trans_handle *trans,
3673 struct inode *parent_inode, struct inode *inode, 4017 struct inode *parent_inode, struct inode *inode,
3674 const char *name, int name_len, int add_backref, u64 index) 4018 const char *name, int name_len, int add_backref, u64 index)
3675{ 4019{
3676 int ret; 4020 int ret = 0;
3677 struct btrfs_key key; 4021 struct btrfs_key key;
3678 struct btrfs_root *root = BTRFS_I(parent_inode)->root; 4022 struct btrfs_root *root = BTRFS_I(parent_inode)->root;
3679 4023
3680 key.objectid = inode->i_ino; 4024 if (unlikely(inode->i_ino == BTRFS_FIRST_FREE_OBJECTID)) {
3681 btrfs_set_key_type(&key, BTRFS_INODE_ITEM_KEY); 4025 memcpy(&key, &BTRFS_I(inode)->root->root_key, sizeof(key));
3682 key.offset = 0; 4026 } else {
4027 key.objectid = inode->i_ino;
4028 btrfs_set_key_type(&key, BTRFS_INODE_ITEM_KEY);
4029 key.offset = 0;
4030 }
4031
4032 if (unlikely(inode->i_ino == BTRFS_FIRST_FREE_OBJECTID)) {
4033 ret = btrfs_add_root_ref(trans, root->fs_info->tree_root,
4034 key.objectid, root->root_key.objectid,
4035 parent_inode->i_ino,
4036 index, name, name_len);
4037 } else if (add_backref) {
4038 ret = btrfs_insert_inode_ref(trans, root,
4039 name, name_len, inode->i_ino,
4040 parent_inode->i_ino, index);
4041 }
3683 4042
3684 ret = btrfs_insert_dir_item(trans, root, name, name_len,
3685 parent_inode->i_ino,
3686 &key, btrfs_inode_type(inode),
3687 index);
3688 if (ret == 0) { 4043 if (ret == 0) {
3689 if (add_backref) { 4044 ret = btrfs_insert_dir_item(trans, root, name, name_len,
3690 ret = btrfs_insert_inode_ref(trans, root, 4045 parent_inode->i_ino, &key,
3691 name, name_len, 4046 btrfs_inode_type(inode), index);
3692 inode->i_ino, 4047 BUG_ON(ret);
3693 parent_inode->i_ino, 4048
3694 index);
3695 }
3696 btrfs_i_size_write(parent_inode, parent_inode->i_size + 4049 btrfs_i_size_write(parent_inode, parent_inode->i_size +
3697 name_len * 2); 4050 name_len * 2);
3698 parent_inode->i_mtime = parent_inode->i_ctime = CURRENT_TIME; 4051 parent_inode->i_mtime = parent_inode->i_ctime = CURRENT_TIME;
@@ -3732,11 +4085,18 @@ static int btrfs_mknod(struct inode *dir, struct dentry *dentry,
3732 if (!new_valid_dev(rdev)) 4085 if (!new_valid_dev(rdev))
3733 return -EINVAL; 4086 return -EINVAL;
3734 4087
3735 err = btrfs_check_metadata_free_space(root); 4088 /*
4089 * 2 for inode item and ref
4090 * 2 for dir items
4091 * 1 for xattr if selinux is on
4092 */
4093 err = btrfs_reserve_metadata_space(root, 5);
3736 if (err) 4094 if (err)
3737 goto fail; 4095 return err;
3738 4096
3739 trans = btrfs_start_transaction(root, 1); 4097 trans = btrfs_start_transaction(root, 1);
4098 if (!trans)
4099 goto fail;
3740 btrfs_set_trans_block_group(trans, dir); 4100 btrfs_set_trans_block_group(trans, dir);
3741 4101
3742 err = btrfs_find_free_objectid(trans, root, dir->i_ino, &objectid); 4102 err = btrfs_find_free_objectid(trans, root, dir->i_ino, &objectid);
@@ -3774,6 +4134,7 @@ out_unlock:
3774 nr = trans->blocks_used; 4134 nr = trans->blocks_used;
3775 btrfs_end_transaction_throttle(trans, root); 4135 btrfs_end_transaction_throttle(trans, root);
3776fail: 4136fail:
4137 btrfs_unreserve_metadata_space(root, 5);
3777 if (drop_inode) { 4138 if (drop_inode) {
3778 inode_dec_link_count(inode); 4139 inode_dec_link_count(inode);
3779 iput(inode); 4140 iput(inode);
@@ -3794,10 +4155,18 @@ static int btrfs_create(struct inode *dir, struct dentry *dentry,
3794 u64 objectid; 4155 u64 objectid;
3795 u64 index = 0; 4156 u64 index = 0;
3796 4157
3797 err = btrfs_check_metadata_free_space(root); 4158 /*
4159 * 2 for inode item and ref
4160 * 2 for dir items
4161 * 1 for xattr if selinux is on
4162 */
4163 err = btrfs_reserve_metadata_space(root, 5);
3798 if (err) 4164 if (err)
3799 goto fail; 4165 return err;
4166
3800 trans = btrfs_start_transaction(root, 1); 4167 trans = btrfs_start_transaction(root, 1);
4168 if (!trans)
4169 goto fail;
3801 btrfs_set_trans_block_group(trans, dir); 4170 btrfs_set_trans_block_group(trans, dir);
3802 4171
3803 err = btrfs_find_free_objectid(trans, root, dir->i_ino, &objectid); 4172 err = btrfs_find_free_objectid(trans, root, dir->i_ino, &objectid);
@@ -3838,6 +4207,7 @@ out_unlock:
3838 nr = trans->blocks_used; 4207 nr = trans->blocks_used;
3839 btrfs_end_transaction_throttle(trans, root); 4208 btrfs_end_transaction_throttle(trans, root);
3840fail: 4209fail:
4210 btrfs_unreserve_metadata_space(root, 5);
3841 if (drop_inode) { 4211 if (drop_inode) {
3842 inode_dec_link_count(inode); 4212 inode_dec_link_count(inode);
3843 iput(inode); 4213 iput(inode);
@@ -3860,10 +4230,16 @@ static int btrfs_link(struct dentry *old_dentry, struct inode *dir,
3860 if (inode->i_nlink == 0) 4230 if (inode->i_nlink == 0)
3861 return -ENOENT; 4231 return -ENOENT;
3862 4232
3863 btrfs_inc_nlink(inode); 4233 /*
3864 err = btrfs_check_metadata_free_space(root); 4234 * 1 item for inode ref
4235 * 2 items for dir items
4236 */
4237 err = btrfs_reserve_metadata_space(root, 3);
3865 if (err) 4238 if (err)
3866 goto fail; 4239 return err;
4240
4241 btrfs_inc_nlink(inode);
4242
3867 err = btrfs_set_inode_index(dir, &index); 4243 err = btrfs_set_inode_index(dir, &index);
3868 if (err) 4244 if (err)
3869 goto fail; 4245 goto fail;
@@ -3875,20 +4251,19 @@ static int btrfs_link(struct dentry *old_dentry, struct inode *dir,
3875 4251
3876 err = btrfs_add_nondir(trans, dentry, inode, 1, index); 4252 err = btrfs_add_nondir(trans, dentry, inode, 1, index);
3877 4253
3878 if (err) 4254 if (err) {
3879 drop_inode = 1;
3880
3881 btrfs_update_inode_block_group(trans, dir);
3882 err = btrfs_update_inode(trans, root, inode);
3883
3884 if (err)
3885 drop_inode = 1; 4255 drop_inode = 1;
4256 } else {
4257 btrfs_update_inode_block_group(trans, dir);
4258 err = btrfs_update_inode(trans, root, inode);
4259 BUG_ON(err);
4260 btrfs_log_new_name(trans, inode, NULL, dentry->d_parent);
4261 }
3886 4262
3887 nr = trans->blocks_used; 4263 nr = trans->blocks_used;
3888
3889 btrfs_log_new_name(trans, inode, NULL, dentry->d_parent);
3890 btrfs_end_transaction_throttle(trans, root); 4264 btrfs_end_transaction_throttle(trans, root);
3891fail: 4265fail:
4266 btrfs_unreserve_metadata_space(root, 3);
3892 if (drop_inode) { 4267 if (drop_inode) {
3893 inode_dec_link_count(inode); 4268 inode_dec_link_count(inode);
3894 iput(inode); 4269 iput(inode);
@@ -3908,17 +4283,21 @@ static int btrfs_mkdir(struct inode *dir, struct dentry *dentry, int mode)
3908 u64 index = 0; 4283 u64 index = 0;
3909 unsigned long nr = 1; 4284 unsigned long nr = 1;
3910 4285
3911 err = btrfs_check_metadata_free_space(root); 4286 /*
4287 * 2 items for inode and ref
4288 * 2 items for dir items
4289 * 1 for xattr if selinux is on
4290 */
4291 err = btrfs_reserve_metadata_space(root, 5);
3912 if (err) 4292 if (err)
3913 goto out_unlock; 4293 return err;
3914 4294
3915 trans = btrfs_start_transaction(root, 1); 4295 trans = btrfs_start_transaction(root, 1);
3916 btrfs_set_trans_block_group(trans, dir); 4296 if (!trans) {
3917 4297 err = -ENOMEM;
3918 if (IS_ERR(trans)) {
3919 err = PTR_ERR(trans);
3920 goto out_unlock; 4298 goto out_unlock;
3921 } 4299 }
4300 btrfs_set_trans_block_group(trans, dir);
3922 4301
3923 err = btrfs_find_free_objectid(trans, root, dir->i_ino, &objectid); 4302 err = btrfs_find_free_objectid(trans, root, dir->i_ino, &objectid);
3924 if (err) { 4303 if (err) {
@@ -3967,6 +4346,7 @@ out_fail:
3967 btrfs_end_transaction_throttle(trans, root); 4346 btrfs_end_transaction_throttle(trans, root);
3968 4347
3969out_unlock: 4348out_unlock:
4349 btrfs_unreserve_metadata_space(root, 5);
3970 if (drop_on_err) 4350 if (drop_on_err)
3971 iput(inode); 4351 iput(inode);
3972 btrfs_btree_balance_dirty(root, nr); 4352 btrfs_btree_balance_dirty(root, nr);
@@ -4064,11 +4444,11 @@ struct extent_map *btrfs_get_extent(struct inode *inode, struct page *page,
4064 int compressed; 4444 int compressed;
4065 4445
4066again: 4446again:
4067 spin_lock(&em_tree->lock); 4447 read_lock(&em_tree->lock);
4068 em = lookup_extent_mapping(em_tree, start, len); 4448 em = lookup_extent_mapping(em_tree, start, len);
4069 if (em) 4449 if (em)
4070 em->bdev = root->fs_info->fs_devices->latest_bdev; 4450 em->bdev = root->fs_info->fs_devices->latest_bdev;
4071 spin_unlock(&em_tree->lock); 4451 read_unlock(&em_tree->lock);
4072 4452
4073 if (em) { 4453 if (em) {
4074 if (em->start > start || em->start + em->len <= start) 4454 if (em->start > start || em->start + em->len <= start)
@@ -4215,6 +4595,11 @@ again:
4215 map = kmap(page); 4595 map = kmap(page);
4216 read_extent_buffer(leaf, map + pg_offset, ptr, 4596 read_extent_buffer(leaf, map + pg_offset, ptr,
4217 copy_size); 4597 copy_size);
4598 if (pg_offset + copy_size < PAGE_CACHE_SIZE) {
4599 memset(map + pg_offset + copy_size, 0,
4600 PAGE_CACHE_SIZE - pg_offset -
4601 copy_size);
4602 }
4218 kunmap(page); 4603 kunmap(page);
4219 } 4604 }
4220 flush_dcache_page(page); 4605 flush_dcache_page(page);
@@ -4259,7 +4644,7 @@ insert:
4259 } 4644 }
4260 4645
4261 err = 0; 4646 err = 0;
4262 spin_lock(&em_tree->lock); 4647 write_lock(&em_tree->lock);
4263 ret = add_extent_mapping(em_tree, em); 4648 ret = add_extent_mapping(em_tree, em);
4264 /* it is possible that someone inserted the extent into the tree 4649 /* it is possible that someone inserted the extent into the tree
4265 * while we had the lock dropped. It is also possible that 4650 * while we had the lock dropped. It is also possible that
@@ -4299,7 +4684,7 @@ insert:
4299 err = 0; 4684 err = 0;
4300 } 4685 }
4301 } 4686 }
4302 spin_unlock(&em_tree->lock); 4687 write_unlock(&em_tree->lock);
4303out: 4688out:
4304 if (path) 4689 if (path)
4305 btrfs_free_path(path); 4690 btrfs_free_path(path);
@@ -4398,13 +4783,21 @@ static void btrfs_invalidatepage(struct page *page, unsigned long offset)
4398 u64 page_start = page_offset(page); 4783 u64 page_start = page_offset(page);
4399 u64 page_end = page_start + PAGE_CACHE_SIZE - 1; 4784 u64 page_end = page_start + PAGE_CACHE_SIZE - 1;
4400 4785
4786
4787 /*
4788 * we have the page locked, so new writeback can't start,
4789 * and the dirty bit won't be cleared while we are here.
4790 *
4791 * Wait for IO on this page so that we can safely clear
4792 * the PagePrivate2 bit and do ordered accounting
4793 */
4401 wait_on_page_writeback(page); 4794 wait_on_page_writeback(page);
4795
4402 tree = &BTRFS_I(page->mapping->host)->io_tree; 4796 tree = &BTRFS_I(page->mapping->host)->io_tree;
4403 if (offset) { 4797 if (offset) {
4404 btrfs_releasepage(page, GFP_NOFS); 4798 btrfs_releasepage(page, GFP_NOFS);
4405 return; 4799 return;
4406 } 4800 }
4407
4408 lock_extent(tree, page_start, page_end, GFP_NOFS); 4801 lock_extent(tree, page_start, page_end, GFP_NOFS);
4409 ordered = btrfs_lookup_ordered_extent(page->mapping->host, 4802 ordered = btrfs_lookup_ordered_extent(page->mapping->host,
4410 page_offset(page)); 4803 page_offset(page));
@@ -4415,16 +4808,21 @@ static void btrfs_invalidatepage(struct page *page, unsigned long offset)
4415 */ 4808 */
4416 clear_extent_bit(tree, page_start, page_end, 4809 clear_extent_bit(tree, page_start, page_end,
4417 EXTENT_DIRTY | EXTENT_DELALLOC | 4810 EXTENT_DIRTY | EXTENT_DELALLOC |
4418 EXTENT_LOCKED, 1, 0, GFP_NOFS); 4811 EXTENT_LOCKED, 1, 0, NULL, GFP_NOFS);
4419 btrfs_finish_ordered_io(page->mapping->host, 4812 /*
4420 page_start, page_end); 4813 * whoever cleared the private bit is responsible
4814 * for the finish_ordered_io
4815 */
4816 if (TestClearPagePrivate2(page)) {
4817 btrfs_finish_ordered_io(page->mapping->host,
4818 page_start, page_end);
4819 }
4421 btrfs_put_ordered_extent(ordered); 4820 btrfs_put_ordered_extent(ordered);
4422 lock_extent(tree, page_start, page_end, GFP_NOFS); 4821 lock_extent(tree, page_start, page_end, GFP_NOFS);
4423 } 4822 }
4424 clear_extent_bit(tree, page_start, page_end, 4823 clear_extent_bit(tree, page_start, page_end,
4425 EXTENT_LOCKED | EXTENT_DIRTY | EXTENT_DELALLOC | 4824 EXTENT_LOCKED | EXTENT_DIRTY | EXTENT_DELALLOC,
4426 EXTENT_ORDERED, 4825 1, 1, NULL, GFP_NOFS);
4427 1, 1, GFP_NOFS);
4428 __btrfs_releasepage(page, GFP_NOFS); 4826 __btrfs_releasepage(page, GFP_NOFS);
4429 4827
4430 ClearPageChecked(page); 4828 ClearPageChecked(page);
@@ -4473,6 +4871,13 @@ int btrfs_page_mkwrite(struct vm_area_struct *vma, struct vm_fault *vmf)
4473 goto out; 4871 goto out;
4474 } 4872 }
4475 4873
4874 ret = btrfs_reserve_metadata_for_delalloc(root, inode, 1);
4875 if (ret) {
4876 btrfs_free_reserved_data_space(root, inode, PAGE_CACHE_SIZE);
4877 ret = VM_FAULT_SIGBUS;
4878 goto out;
4879 }
4880
4476 ret = VM_FAULT_NOPAGE; /* make the VM retry the fault */ 4881 ret = VM_FAULT_NOPAGE; /* make the VM retry the fault */
4477again: 4882again:
4478 lock_page(page); 4883 lock_page(page);
@@ -4504,7 +4909,23 @@ again:
4504 goto again; 4909 goto again;
4505 } 4910 }
4506 4911
4507 btrfs_set_extent_delalloc(inode, page_start, page_end); 4912 /*
4913 * XXX - page_mkwrite gets called every time the page is dirtied, even
4914 * if it was already dirty, so for space accounting reasons we need to
4915 * clear any delalloc bits for the range we are fixing to save. There
4916 * is probably a better way to do this, but for now keep consistent with
4917 * prepare_pages in the normal write path.
4918 */
4919 clear_extent_bits(&BTRFS_I(inode)->io_tree, page_start, page_end,
4920 EXTENT_DIRTY | EXTENT_DELALLOC, GFP_NOFS);
4921
4922 ret = btrfs_set_extent_delalloc(inode, page_start, page_end);
4923 if (ret) {
4924 unlock_extent(io_tree, page_start, page_end, GFP_NOFS);
4925 ret = VM_FAULT_SIGBUS;
4926 btrfs_free_reserved_data_space(root, inode, PAGE_CACHE_SIZE);
4927 goto out_unlock;
4928 }
4508 ret = 0; 4929 ret = 0;
4509 4930
4510 /* page is wholly or partially inside EOF */ 4931 /* page is wholly or partially inside EOF */
@@ -4521,11 +4942,15 @@ again:
4521 } 4942 }
4522 ClearPageChecked(page); 4943 ClearPageChecked(page);
4523 set_page_dirty(page); 4944 set_page_dirty(page);
4945 SetPageUptodate(page);
4524 4946
4525 BTRFS_I(inode)->last_trans = root->fs_info->generation + 1; 4947 BTRFS_I(inode)->last_trans = root->fs_info->generation + 1;
4526 unlock_extent(io_tree, page_start, page_end, GFP_NOFS); 4948 unlock_extent(io_tree, page_start, page_end, GFP_NOFS);
4527 4949
4528out_unlock: 4950out_unlock:
4951 btrfs_unreserve_metadata_for_delalloc(root, inode, 1);
4952 if (!ret)
4953 return VM_FAULT_LOCKED;
4529 unlock_page(page); 4954 unlock_page(page);
4530out: 4955out:
4531 return ret; 4956 return ret;
@@ -4594,11 +5019,11 @@ out:
4594 * create a new subvolume directory/inode (helper for the ioctl). 5019 * create a new subvolume directory/inode (helper for the ioctl).
4595 */ 5020 */
4596int btrfs_create_subvol_root(struct btrfs_trans_handle *trans, 5021int btrfs_create_subvol_root(struct btrfs_trans_handle *trans,
4597 struct btrfs_root *new_root, struct dentry *dentry, 5022 struct btrfs_root *new_root,
4598 u64 new_dirid, u64 alloc_hint) 5023 u64 new_dirid, u64 alloc_hint)
4599{ 5024{
4600 struct inode *inode; 5025 struct inode *inode;
4601 int error; 5026 int err;
4602 u64 index = 0; 5027 u64 index = 0;
4603 5028
4604 inode = btrfs_new_inode(trans, new_root, NULL, "..", 2, new_dirid, 5029 inode = btrfs_new_inode(trans, new_root, NULL, "..", 2, new_dirid,
@@ -4611,11 +5036,10 @@ int btrfs_create_subvol_root(struct btrfs_trans_handle *trans,
4611 inode->i_nlink = 1; 5036 inode->i_nlink = 1;
4612 btrfs_i_size_write(inode, 0); 5037 btrfs_i_size_write(inode, 0);
4613 5038
4614 error = btrfs_update_inode(trans, new_root, inode); 5039 err = btrfs_update_inode(trans, new_root, inode);
4615 if (error) 5040 BUG_ON(err);
4616 return error;
4617 5041
4618 d_instantiate(dentry, inode); 5042 iput(inode);
4619 return 0; 5043 return 0;
4620} 5044}
4621 5045
@@ -4641,6 +5065,8 @@ struct inode *btrfs_alloc_inode(struct super_block *sb)
4641 return NULL; 5065 return NULL;
4642 ei->last_trans = 0; 5066 ei->last_trans = 0;
4643 ei->logged_trans = 0; 5067 ei->logged_trans = 0;
5068 ei->delalloc_extents = 0;
5069 ei->delalloc_reserved_extents = 0;
4644 btrfs_ordered_inode_tree_init(&ei->ordered_tree); 5070 btrfs_ordered_inode_tree_init(&ei->ordered_tree);
4645 INIT_LIST_HEAD(&ei->i_orphan); 5071 INIT_LIST_HEAD(&ei->i_orphan);
4646 INIT_LIST_HEAD(&ei->ordered_operations); 5072 INIT_LIST_HEAD(&ei->ordered_operations);
@@ -4693,6 +5119,16 @@ void btrfs_destroy_inode(struct inode *inode)
4693 kmem_cache_free(btrfs_inode_cachep, BTRFS_I(inode)); 5119 kmem_cache_free(btrfs_inode_cachep, BTRFS_I(inode));
4694} 5120}
4695 5121
5122void btrfs_drop_inode(struct inode *inode)
5123{
5124 struct btrfs_root *root = BTRFS_I(inode)->root;
5125
5126 if (inode->i_nlink > 0 && btrfs_root_refs(&root->root_item) == 0)
5127 generic_delete_inode(inode);
5128 else
5129 generic_drop_inode(inode);
5130}
5131
4696static void init_once(void *foo) 5132static void init_once(void *foo)
4697{ 5133{
4698 struct btrfs_inode *ei = (struct btrfs_inode *) foo; 5134 struct btrfs_inode *ei = (struct btrfs_inode *) foo;
@@ -4761,31 +5197,37 @@ static int btrfs_rename(struct inode *old_dir, struct dentry *old_dentry,
4761{ 5197{
4762 struct btrfs_trans_handle *trans; 5198 struct btrfs_trans_handle *trans;
4763 struct btrfs_root *root = BTRFS_I(old_dir)->root; 5199 struct btrfs_root *root = BTRFS_I(old_dir)->root;
5200 struct btrfs_root *dest = BTRFS_I(new_dir)->root;
4764 struct inode *new_inode = new_dentry->d_inode; 5201 struct inode *new_inode = new_dentry->d_inode;
4765 struct inode *old_inode = old_dentry->d_inode; 5202 struct inode *old_inode = old_dentry->d_inode;
4766 struct timespec ctime = CURRENT_TIME; 5203 struct timespec ctime = CURRENT_TIME;
4767 u64 index = 0; 5204 u64 index = 0;
5205 u64 root_objectid;
4768 int ret; 5206 int ret;
4769 5207
4770 /* we're not allowed to rename between subvolumes */ 5208 if (new_dir->i_ino == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID)
4771 if (BTRFS_I(old_inode)->root->root_key.objectid != 5209 return -EPERM;
4772 BTRFS_I(new_dir)->root->root_key.objectid) 5210
5211 /* we only allow rename subvolume link between subvolumes */
5212 if (old_inode->i_ino != BTRFS_FIRST_FREE_OBJECTID && root != dest)
4773 return -EXDEV; 5213 return -EXDEV;
4774 5214
5215 if (old_inode->i_ino == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID ||
5216 (new_inode && new_inode->i_ino == BTRFS_FIRST_FREE_OBJECTID))
5217 return -ENOTEMPTY;
5218
4775 if (S_ISDIR(old_inode->i_mode) && new_inode && 5219 if (S_ISDIR(old_inode->i_mode) && new_inode &&
4776 new_inode->i_size > BTRFS_EMPTY_DIR_SIZE) { 5220 new_inode->i_size > BTRFS_EMPTY_DIR_SIZE)
4777 return -ENOTEMPTY; 5221 return -ENOTEMPTY;
4778 }
4779 5222
4780 /* to rename a snapshot or subvolume, we need to juggle the 5223 /*
4781 * backrefs. This isn't coded yet 5224 * 2 items for dir items
5225 * 1 item for orphan entry
5226 * 1 item for ref
4782 */ 5227 */
4783 if (old_inode->i_ino == BTRFS_FIRST_FREE_OBJECTID) 5228 ret = btrfs_reserve_metadata_space(root, 4);
4784 return -EXDEV;
4785
4786 ret = btrfs_check_metadata_free_space(root);
4787 if (ret) 5229 if (ret)
4788 goto out_unlock; 5230 return ret;
4789 5231
4790 /* 5232 /*
4791 * we're using rename to replace one file with another. 5233 * we're using rename to replace one file with another.
@@ -4796,8 +5238,40 @@ static int btrfs_rename(struct inode *old_dir, struct dentry *old_dentry,
4796 old_inode->i_size > BTRFS_ORDERED_OPERATIONS_FLUSH_LIMIT) 5238 old_inode->i_size > BTRFS_ORDERED_OPERATIONS_FLUSH_LIMIT)
4797 filemap_flush(old_inode->i_mapping); 5239 filemap_flush(old_inode->i_mapping);
4798 5240
5241 /* close the racy window with snapshot create/destroy ioctl */
5242 if (old_inode->i_ino == BTRFS_FIRST_FREE_OBJECTID)
5243 down_read(&root->fs_info->subvol_sem);
5244
4799 trans = btrfs_start_transaction(root, 1); 5245 trans = btrfs_start_transaction(root, 1);
5246 btrfs_set_trans_block_group(trans, new_dir);
5247
5248 if (dest != root)
5249 btrfs_record_root_in_trans(trans, dest);
5250
5251 ret = btrfs_set_inode_index(new_dir, &index);
5252 if (ret)
5253 goto out_fail;
4800 5254
5255 if (unlikely(old_inode->i_ino == BTRFS_FIRST_FREE_OBJECTID)) {
5256 /* force full log commit if subvolume involved. */
5257 root->fs_info->last_trans_log_full_commit = trans->transid;
5258 } else {
5259 ret = btrfs_insert_inode_ref(trans, dest,
5260 new_dentry->d_name.name,
5261 new_dentry->d_name.len,
5262 old_inode->i_ino,
5263 new_dir->i_ino, index);
5264 if (ret)
5265 goto out_fail;
5266 /*
5267 * this is an ugly little race, but the rename is required
5268 * to make sure that if we crash, the inode is either at the
5269 * old name or the new one. pinning the log transaction lets
5270 * us make sure we don't allow a log commit to come in after
5271 * we unlink the name but before we add the new name back in.
5272 */
5273 btrfs_pin_log_trans(root);
5274 }
4801 /* 5275 /*
4802 * make sure the inode gets flushed if it is replacing 5276 * make sure the inode gets flushed if it is replacing
4803 * something. 5277 * something.
@@ -4807,18 +5281,6 @@ static int btrfs_rename(struct inode *old_dir, struct dentry *old_dentry,
4807 btrfs_add_ordered_operation(trans, root, old_inode); 5281 btrfs_add_ordered_operation(trans, root, old_inode);
4808 } 5282 }
4809 5283
4810 /*
4811 * this is an ugly little race, but the rename is required to make
4812 * sure that if we crash, the inode is either at the old name
4813 * or the new one. pinning the log transaction lets us make sure
4814 * we don't allow a log commit to come in after we unlink the
4815 * name but before we add the new name back in.
4816 */
4817 btrfs_pin_log_trans(root);
4818
4819 btrfs_set_trans_block_group(trans, new_dir);
4820
4821 btrfs_inc_nlink(old_dentry->d_inode);
4822 old_dir->i_ctime = old_dir->i_mtime = ctime; 5284 old_dir->i_ctime = old_dir->i_mtime = ctime;
4823 new_dir->i_ctime = new_dir->i_mtime = ctime; 5285 new_dir->i_ctime = new_dir->i_mtime = ctime;
4824 old_inode->i_ctime = ctime; 5286 old_inode->i_ctime = ctime;
@@ -4826,47 +5288,60 @@ static int btrfs_rename(struct inode *old_dir, struct dentry *old_dentry,
4826 if (old_dentry->d_parent != new_dentry->d_parent) 5288 if (old_dentry->d_parent != new_dentry->d_parent)
4827 btrfs_record_unlink_dir(trans, old_dir, old_inode, 1); 5289 btrfs_record_unlink_dir(trans, old_dir, old_inode, 1);
4828 5290
4829 ret = btrfs_unlink_inode(trans, root, old_dir, old_dentry->d_inode, 5291 if (unlikely(old_inode->i_ino == BTRFS_FIRST_FREE_OBJECTID)) {
4830 old_dentry->d_name.name, 5292 root_objectid = BTRFS_I(old_inode)->root->root_key.objectid;
4831 old_dentry->d_name.len); 5293 ret = btrfs_unlink_subvol(trans, root, old_dir, root_objectid,
4832 if (ret) 5294 old_dentry->d_name.name,
4833 goto out_fail; 5295 old_dentry->d_name.len);
5296 } else {
5297 btrfs_inc_nlink(old_dentry->d_inode);
5298 ret = btrfs_unlink_inode(trans, root, old_dir,
5299 old_dentry->d_inode,
5300 old_dentry->d_name.name,
5301 old_dentry->d_name.len);
5302 }
5303 BUG_ON(ret);
4834 5304
4835 if (new_inode) { 5305 if (new_inode) {
4836 new_inode->i_ctime = CURRENT_TIME; 5306 new_inode->i_ctime = CURRENT_TIME;
4837 ret = btrfs_unlink_inode(trans, root, new_dir, 5307 if (unlikely(new_inode->i_ino ==
4838 new_dentry->d_inode, 5308 BTRFS_EMPTY_SUBVOL_DIR_OBJECTID)) {
4839 new_dentry->d_name.name, 5309 root_objectid = BTRFS_I(new_inode)->location.objectid;
4840 new_dentry->d_name.len); 5310 ret = btrfs_unlink_subvol(trans, dest, new_dir,
4841 if (ret) 5311 root_objectid,
4842 goto out_fail; 5312 new_dentry->d_name.name,
5313 new_dentry->d_name.len);
5314 BUG_ON(new_inode->i_nlink == 0);
5315 } else {
5316 ret = btrfs_unlink_inode(trans, dest, new_dir,
5317 new_dentry->d_inode,
5318 new_dentry->d_name.name,
5319 new_dentry->d_name.len);
5320 }
5321 BUG_ON(ret);
4843 if (new_inode->i_nlink == 0) { 5322 if (new_inode->i_nlink == 0) {
4844 ret = btrfs_orphan_add(trans, new_dentry->d_inode); 5323 ret = btrfs_orphan_add(trans, new_dentry->d_inode);
4845 if (ret) 5324 BUG_ON(ret);
4846 goto out_fail;
4847 } 5325 }
4848
4849 } 5326 }
4850 ret = btrfs_set_inode_index(new_dir, &index);
4851 if (ret)
4852 goto out_fail;
4853 5327
4854 ret = btrfs_add_link(trans, new_dentry->d_parent->d_inode, 5328 ret = btrfs_add_link(trans, new_dir, old_inode,
4855 old_inode, new_dentry->d_name.name, 5329 new_dentry->d_name.name,
4856 new_dentry->d_name.len, 1, index); 5330 new_dentry->d_name.len, 0, index);
4857 if (ret) 5331 BUG_ON(ret);
4858 goto out_fail;
4859 5332
4860 btrfs_log_new_name(trans, old_inode, old_dir, 5333 if (old_inode->i_ino != BTRFS_FIRST_FREE_OBJECTID) {
4861 new_dentry->d_parent); 5334 btrfs_log_new_name(trans, old_inode, old_dir,
5335 new_dentry->d_parent);
5336 btrfs_end_log_trans(root);
5337 }
4862out_fail: 5338out_fail:
4863
4864 /* this btrfs_end_log_trans just allows the current
4865 * log-sub transaction to complete
4866 */
4867 btrfs_end_log_trans(root);
4868 btrfs_end_transaction_throttle(trans, root); 5339 btrfs_end_transaction_throttle(trans, root);
4869out_unlock: 5340
5341 if (old_inode->i_ino == BTRFS_FIRST_FREE_OBJECTID)
5342 up_read(&root->fs_info->subvol_sem);
5343
5344 btrfs_unreserve_metadata_space(root, 4);
4870 return ret; 5345 return ret;
4871} 5346}
4872 5347
@@ -4938,11 +5413,18 @@ static int btrfs_symlink(struct inode *dir, struct dentry *dentry,
4938 if (name_len > BTRFS_MAX_INLINE_DATA_SIZE(root)) 5413 if (name_len > BTRFS_MAX_INLINE_DATA_SIZE(root))
4939 return -ENAMETOOLONG; 5414 return -ENAMETOOLONG;
4940 5415
4941 err = btrfs_check_metadata_free_space(root); 5416 /*
5417 * 2 items for inode item and ref
5418 * 2 items for dir items
5419 * 1 item for xattr if selinux is on
5420 */
5421 err = btrfs_reserve_metadata_space(root, 5);
4942 if (err) 5422 if (err)
4943 goto out_fail; 5423 return err;
4944 5424
4945 trans = btrfs_start_transaction(root, 1); 5425 trans = btrfs_start_transaction(root, 1);
5426 if (!trans)
5427 goto out_fail;
4946 btrfs_set_trans_block_group(trans, dir); 5428 btrfs_set_trans_block_group(trans, dir);
4947 5429
4948 err = btrfs_find_free_objectid(trans, root, dir->i_ino, &objectid); 5430 err = btrfs_find_free_objectid(trans, root, dir->i_ino, &objectid);
@@ -5023,6 +5505,7 @@ out_unlock:
5023 nr = trans->blocks_used; 5505 nr = trans->blocks_used;
5024 btrfs_end_transaction_throttle(trans, root); 5506 btrfs_end_transaction_throttle(trans, root);
5025out_fail: 5507out_fail:
5508 btrfs_unreserve_metadata_space(root, 5);
5026 if (drop_inode) { 5509 if (drop_inode) {
5027 inode_dec_link_count(inode); 5510 inode_dec_link_count(inode);
5028 iput(inode); 5511 iput(inode);
@@ -5044,6 +5527,11 @@ static int prealloc_file_range(struct btrfs_trans_handle *trans,
5044 5527
5045 while (num_bytes > 0) { 5528 while (num_bytes > 0) {
5046 alloc_size = min(num_bytes, root->fs_info->max_extent); 5529 alloc_size = min(num_bytes, root->fs_info->max_extent);
5530
5531 ret = btrfs_reserve_metadata_space(root, 1);
5532 if (ret)
5533 goto out;
5534
5047 ret = btrfs_reserve_extent(trans, root, alloc_size, 5535 ret = btrfs_reserve_extent(trans, root, alloc_size,
5048 root->sectorsize, 0, alloc_hint, 5536 root->sectorsize, 0, alloc_hint,
5049 (u64)-1, &ins, 1); 5537 (u64)-1, &ins, 1);
@@ -5058,9 +5546,12 @@ static int prealloc_file_range(struct btrfs_trans_handle *trans,
5058 0, 0, 0, 5546 0, 0, 0,
5059 BTRFS_FILE_EXTENT_PREALLOC); 5547 BTRFS_FILE_EXTENT_PREALLOC);
5060 BUG_ON(ret); 5548 BUG_ON(ret);
5549 btrfs_drop_extent_cache(inode, cur_offset,
5550 cur_offset + ins.offset -1, 0);
5061 num_bytes -= ins.offset; 5551 num_bytes -= ins.offset;
5062 cur_offset += ins.offset; 5552 cur_offset += ins.offset;
5063 alloc_hint = ins.objectid + ins.offset; 5553 alloc_hint = ins.objectid + ins.offset;
5554 btrfs_unreserve_metadata_space(root, 1);
5064 } 5555 }
5065out: 5556out:
5066 if (cur_offset > start) { 5557 if (cur_offset > start) {
@@ -5201,7 +5692,7 @@ static int btrfs_permission(struct inode *inode, int mask)
5201 return generic_permission(inode, mask, btrfs_check_acl); 5692 return generic_permission(inode, mask, btrfs_check_acl);
5202} 5693}
5203 5694
5204static struct inode_operations btrfs_dir_inode_operations = { 5695static const struct inode_operations btrfs_dir_inode_operations = {
5205 .getattr = btrfs_getattr, 5696 .getattr = btrfs_getattr,
5206 .lookup = btrfs_lookup, 5697 .lookup = btrfs_lookup,
5207 .create = btrfs_create, 5698 .create = btrfs_create,
@@ -5219,11 +5710,12 @@ static struct inode_operations btrfs_dir_inode_operations = {
5219 .removexattr = btrfs_removexattr, 5710 .removexattr = btrfs_removexattr,
5220 .permission = btrfs_permission, 5711 .permission = btrfs_permission,
5221}; 5712};
5222static struct inode_operations btrfs_dir_ro_inode_operations = { 5713static const struct inode_operations btrfs_dir_ro_inode_operations = {
5223 .lookup = btrfs_lookup, 5714 .lookup = btrfs_lookup,
5224 .permission = btrfs_permission, 5715 .permission = btrfs_permission,
5225}; 5716};
5226static struct file_operations btrfs_dir_file_operations = { 5717
5718static const struct file_operations btrfs_dir_file_operations = {
5227 .llseek = generic_file_llseek, 5719 .llseek = generic_file_llseek,
5228 .read = generic_read_dir, 5720 .read = generic_read_dir,
5229 .readdir = btrfs_real_readdir, 5721 .readdir = btrfs_real_readdir,
@@ -5245,6 +5737,8 @@ static struct extent_io_ops btrfs_extent_io_ops = {
5245 .readpage_io_failed_hook = btrfs_io_failed_hook, 5737 .readpage_io_failed_hook = btrfs_io_failed_hook,
5246 .set_bit_hook = btrfs_set_bit_hook, 5738 .set_bit_hook = btrfs_set_bit_hook,
5247 .clear_bit_hook = btrfs_clear_bit_hook, 5739 .clear_bit_hook = btrfs_clear_bit_hook,
5740 .merge_extent_hook = btrfs_merge_extent_hook,
5741 .split_extent_hook = btrfs_split_extent_hook,
5248}; 5742};
5249 5743
5250/* 5744/*
@@ -5259,7 +5753,7 @@ static struct extent_io_ops btrfs_extent_io_ops = {
5259 * 5753 *
5260 * For now we're avoiding this by dropping bmap. 5754 * For now we're avoiding this by dropping bmap.
5261 */ 5755 */
5262static struct address_space_operations btrfs_aops = { 5756static const struct address_space_operations btrfs_aops = {
5263 .readpage = btrfs_readpage, 5757 .readpage = btrfs_readpage,
5264 .writepage = btrfs_writepage, 5758 .writepage = btrfs_writepage,
5265 .writepages = btrfs_writepages, 5759 .writepages = btrfs_writepages,
@@ -5269,16 +5763,17 @@ static struct address_space_operations btrfs_aops = {
5269 .invalidatepage = btrfs_invalidatepage, 5763 .invalidatepage = btrfs_invalidatepage,
5270 .releasepage = btrfs_releasepage, 5764 .releasepage = btrfs_releasepage,
5271 .set_page_dirty = btrfs_set_page_dirty, 5765 .set_page_dirty = btrfs_set_page_dirty,
5766 .error_remove_page = generic_error_remove_page,
5272}; 5767};
5273 5768
5274static struct address_space_operations btrfs_symlink_aops = { 5769static const struct address_space_operations btrfs_symlink_aops = {
5275 .readpage = btrfs_readpage, 5770 .readpage = btrfs_readpage,
5276 .writepage = btrfs_writepage, 5771 .writepage = btrfs_writepage,
5277 .invalidatepage = btrfs_invalidatepage, 5772 .invalidatepage = btrfs_invalidatepage,
5278 .releasepage = btrfs_releasepage, 5773 .releasepage = btrfs_releasepage,
5279}; 5774};
5280 5775
5281static struct inode_operations btrfs_file_inode_operations = { 5776static const struct inode_operations btrfs_file_inode_operations = {
5282 .truncate = btrfs_truncate, 5777 .truncate = btrfs_truncate,
5283 .getattr = btrfs_getattr, 5778 .getattr = btrfs_getattr,
5284 .setattr = btrfs_setattr, 5779 .setattr = btrfs_setattr,
@@ -5290,7 +5785,7 @@ static struct inode_operations btrfs_file_inode_operations = {
5290 .fallocate = btrfs_fallocate, 5785 .fallocate = btrfs_fallocate,
5291 .fiemap = btrfs_fiemap, 5786 .fiemap = btrfs_fiemap,
5292}; 5787};
5293static struct inode_operations btrfs_special_inode_operations = { 5788static const struct inode_operations btrfs_special_inode_operations = {
5294 .getattr = btrfs_getattr, 5789 .getattr = btrfs_getattr,
5295 .setattr = btrfs_setattr, 5790 .setattr = btrfs_setattr,
5296 .permission = btrfs_permission, 5791 .permission = btrfs_permission,
@@ -5299,7 +5794,7 @@ static struct inode_operations btrfs_special_inode_operations = {
5299 .listxattr = btrfs_listxattr, 5794 .listxattr = btrfs_listxattr,
5300 .removexattr = btrfs_removexattr, 5795 .removexattr = btrfs_removexattr,
5301}; 5796};
5302static struct inode_operations btrfs_symlink_inode_operations = { 5797static const struct inode_operations btrfs_symlink_inode_operations = {
5303 .readlink = generic_readlink, 5798 .readlink = generic_readlink,
5304 .follow_link = page_follow_link_light, 5799 .follow_link = page_follow_link_light,
5305 .put_link = page_put_link, 5800 .put_link = page_put_link,
@@ -5309,3 +5804,7 @@ static struct inode_operations btrfs_symlink_inode_operations = {
5309 .listxattr = btrfs_listxattr, 5804 .listxattr = btrfs_listxattr,
5310 .removexattr = btrfs_removexattr, 5805 .removexattr = btrfs_removexattr,
5311}; 5806};
5807
5808struct dentry_operations btrfs_dentry_operations = {
5809 .d_delete = btrfs_dentry_delete,
5810};