diff options
Diffstat (limited to 'fs/btrfs/inode.c')
-rw-r--r-- | fs/btrfs/inode.c | 1015 |
1 files changed, 788 insertions, 227 deletions
diff --git a/fs/btrfs/inode.c b/fs/btrfs/inode.c index 59cba180fe83..dae12dc7e159 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 | ||
58 | static struct inode_operations btrfs_dir_inode_operations; | 58 | static const struct inode_operations btrfs_dir_inode_operations; |
59 | static struct inode_operations btrfs_symlink_inode_operations; | 59 | static const struct inode_operations btrfs_symlink_inode_operations; |
60 | static struct inode_operations btrfs_dir_ro_inode_operations; | 60 | static const struct inode_operations btrfs_dir_ro_inode_operations; |
61 | static struct inode_operations btrfs_special_inode_operations; | 61 | static const struct inode_operations btrfs_special_inode_operations; |
62 | static struct inode_operations btrfs_file_inode_operations; | 62 | static const struct inode_operations btrfs_file_inode_operations; |
63 | static struct address_space_operations btrfs_aops; | 63 | static const struct address_space_operations btrfs_aops; |
64 | static struct address_space_operations btrfs_symlink_aops; | 64 | static const struct address_space_operations btrfs_symlink_aops; |
65 | static struct file_operations btrfs_dir_file_operations; | 65 | static const struct file_operations btrfs_dir_file_operations; |
66 | static struct extent_io_ops btrfs_extent_io_ops; | 66 | static struct extent_io_ops btrfs_extent_io_ops; |
67 | 67 | ||
68 | static struct kmem_cache *btrfs_inode_cachep; | 68 | static 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 | ||
@@ -423,9 +424,12 @@ again: | |||
423 | * and free up our temp pages. | 424 | * and free up our temp pages. |
424 | */ | 425 | */ |
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, |
428 | 0, 1, 1, 1); | 429 | EXTENT_CLEAR_UNLOCK_PAGE | EXTENT_CLEAR_DIRTY | |
430 | EXTENT_CLEAR_DELALLOC | | ||
431 | EXTENT_CLEAR_ACCOUNTING | | ||
432 | EXTENT_SET_WRITEBACK | EXTENT_END_WRITEBACK); | ||
429 | ret = 0; | 433 | ret = 0; |
430 | goto free_pages_out; | 434 | goto free_pages_out; |
431 | } | 435 | } |
@@ -611,9 +615,9 @@ static noinline int submit_compressed_extents(struct inode *inode, | |||
611 | set_bit(EXTENT_FLAG_COMPRESSED, &em->flags); | 615 | set_bit(EXTENT_FLAG_COMPRESSED, &em->flags); |
612 | 616 | ||
613 | while (1) { | 617 | while (1) { |
614 | spin_lock(&em_tree->lock); | 618 | write_lock(&em_tree->lock); |
615 | ret = add_extent_mapping(em_tree, em); | 619 | ret = add_extent_mapping(em_tree, em); |
616 | spin_unlock(&em_tree->lock); | 620 | write_unlock(&em_tree->lock); |
617 | if (ret != -EEXIST) { | 621 | if (ret != -EEXIST) { |
618 | free_extent_map(em); | 622 | free_extent_map(em); |
619 | break; | 623 | break; |
@@ -636,11 +640,14 @@ static noinline int submit_compressed_extents(struct inode *inode, | |||
636 | * clear dirty, set writeback and unlock the pages. | 640 | * clear dirty, set writeback and unlock the pages. |
637 | */ | 641 | */ |
638 | extent_clear_unlock_delalloc(inode, | 642 | extent_clear_unlock_delalloc(inode, |
639 | &BTRFS_I(inode)->io_tree, | 643 | &BTRFS_I(inode)->io_tree, |
640 | async_extent->start, | 644 | async_extent->start, |
641 | async_extent->start + | 645 | async_extent->start + |
642 | async_extent->ram_size - 1, | 646 | async_extent->ram_size - 1, |
643 | NULL, 1, 1, 0, 1, 1, 0); | 647 | NULL, EXTENT_CLEAR_UNLOCK_PAGE | |
648 | EXTENT_CLEAR_UNLOCK | | ||
649 | EXTENT_CLEAR_DELALLOC | | ||
650 | EXTENT_CLEAR_DIRTY | EXTENT_SET_WRITEBACK); | ||
644 | 651 | ||
645 | ret = btrfs_submit_compressed_write(inode, | 652 | ret = btrfs_submit_compressed_write(inode, |
646 | async_extent->start, | 653 | async_extent->start, |
@@ -711,9 +718,15 @@ static noinline int cow_file_range(struct inode *inode, | |||
711 | start, end, 0, NULL); | 718 | start, end, 0, NULL); |
712 | if (ret == 0) { | 719 | if (ret == 0) { |
713 | extent_clear_unlock_delalloc(inode, | 720 | extent_clear_unlock_delalloc(inode, |
714 | &BTRFS_I(inode)->io_tree, | 721 | &BTRFS_I(inode)->io_tree, |
715 | start, end, NULL, 1, 1, | 722 | start, end, NULL, |
716 | 1, 1, 1, 1); | 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); | ||
717 | *nr_written = *nr_written + | 730 | *nr_written = *nr_written + |
718 | (end - start + PAGE_CACHE_SIZE) / PAGE_CACHE_SIZE; | 731 | (end - start + PAGE_CACHE_SIZE) / PAGE_CACHE_SIZE; |
719 | *page_started = 1; | 732 | *page_started = 1; |
@@ -725,9 +738,20 @@ static noinline int cow_file_range(struct inode *inode, | |||
725 | BUG_ON(disk_num_bytes > | 738 | BUG_ON(disk_num_bytes > |
726 | btrfs_super_total_bytes(&root->fs_info->super_copy)); | 739 | btrfs_super_total_bytes(&root->fs_info->super_copy)); |
727 | 740 | ||
741 | |||
742 | read_lock(&BTRFS_I(inode)->extent_tree.lock); | ||
743 | em = search_extent_mapping(&BTRFS_I(inode)->extent_tree, | ||
744 | start, num_bytes); | ||
745 | if (em) { | ||
746 | alloc_hint = em->block_start; | ||
747 | free_extent_map(em); | ||
748 | } | ||
749 | read_unlock(&BTRFS_I(inode)->extent_tree.lock); | ||
728 | btrfs_drop_extent_cache(inode, start, start + num_bytes - 1, 0); | 750 | btrfs_drop_extent_cache(inode, start, start + num_bytes - 1, 0); |
729 | 751 | ||
730 | while (disk_num_bytes > 0) { | 752 | while (disk_num_bytes > 0) { |
753 | unsigned long op; | ||
754 | |||
731 | 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); |
732 | ret = btrfs_reserve_extent(trans, root, cur_alloc_size, | 756 | ret = btrfs_reserve_extent(trans, root, cur_alloc_size, |
733 | root->sectorsize, 0, alloc_hint, | 757 | root->sectorsize, 0, alloc_hint, |
@@ -737,7 +761,6 @@ static noinline int cow_file_range(struct inode *inode, | |||
737 | em = alloc_extent_map(GFP_NOFS); | 761 | em = alloc_extent_map(GFP_NOFS); |
738 | em->start = start; | 762 | em->start = start; |
739 | em->orig_start = em->start; | 763 | em->orig_start = em->start; |
740 | |||
741 | ram_size = ins.offset; | 764 | ram_size = ins.offset; |
742 | em->len = ins.offset; | 765 | em->len = ins.offset; |
743 | 766 | ||
@@ -747,9 +770,9 @@ static noinline int cow_file_range(struct inode *inode, | |||
747 | set_bit(EXTENT_FLAG_PINNED, &em->flags); | 770 | set_bit(EXTENT_FLAG_PINNED, &em->flags); |
748 | 771 | ||
749 | while (1) { | 772 | while (1) { |
750 | spin_lock(&em_tree->lock); | 773 | write_lock(&em_tree->lock); |
751 | ret = add_extent_mapping(em_tree, em); | 774 | ret = add_extent_mapping(em_tree, em); |
752 | spin_unlock(&em_tree->lock); | 775 | write_unlock(&em_tree->lock); |
753 | if (ret != -EEXIST) { | 776 | if (ret != -EEXIST) { |
754 | free_extent_map(em); | 777 | free_extent_map(em); |
755 | break; | 778 | break; |
@@ -776,11 +799,17 @@ static noinline int cow_file_range(struct inode *inode, | |||
776 | /* we're not doing compressed IO, don't unlock the first | 799 | /* we're not doing compressed IO, don't unlock the first |
777 | * page (which the caller expects to stay locked), don't | 800 | * page (which the caller expects to stay locked), don't |
778 | * clear any dirty bits and don't set any writeback bits | 801 | * clear any dirty bits and don't set any writeback bits |
802 | * | ||
803 | * Do set the Private2 bit so we know this page was properly | ||
804 | * setup for writepage | ||
779 | */ | 805 | */ |
806 | op = unlock ? EXTENT_CLEAR_UNLOCK_PAGE : 0; | ||
807 | op |= EXTENT_CLEAR_UNLOCK | EXTENT_CLEAR_DELALLOC | | ||
808 | EXTENT_SET_PRIVATE2; | ||
809 | |||
780 | extent_clear_unlock_delalloc(inode, &BTRFS_I(inode)->io_tree, | 810 | extent_clear_unlock_delalloc(inode, &BTRFS_I(inode)->io_tree, |
781 | start, start + ram_size - 1, | 811 | start, start + ram_size - 1, |
782 | locked_page, unlock, 1, | 812 | locked_page, op); |
783 | 1, 0, 0, 0); | ||
784 | disk_num_bytes -= cur_alloc_size; | 813 | disk_num_bytes -= cur_alloc_size; |
785 | num_bytes -= cur_alloc_size; | 814 | num_bytes -= cur_alloc_size; |
786 | alloc_hint = ins.objectid + ins.offset; | 815 | alloc_hint = ins.objectid + ins.offset; |
@@ -852,8 +881,8 @@ static int cow_file_range_async(struct inode *inode, struct page *locked_page, | |||
852 | u64 cur_end; | 881 | u64 cur_end; |
853 | int limit = 10 * 1024 * 1042; | 882 | int limit = 10 * 1024 * 1042; |
854 | 883 | ||
855 | 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, |
856 | EXTENT_DELALLOC, 1, 0, GFP_NOFS); | 885 | 1, 0, NULL, GFP_NOFS); |
857 | while (start < end) { | 886 | while (start < end) { |
858 | async_cow = kmalloc(sizeof(*async_cow), GFP_NOFS); | 887 | async_cow = kmalloc(sizeof(*async_cow), GFP_NOFS); |
859 | async_cow->inode = inode; | 888 | async_cow->inode = inode; |
@@ -994,6 +1023,7 @@ next_slot: | |||
994 | 1023 | ||
995 | if (found_key.offset > cur_offset) { | 1024 | if (found_key.offset > cur_offset) { |
996 | extent_end = found_key.offset; | 1025 | extent_end = found_key.offset; |
1026 | extent_type = 0; | ||
997 | goto out_check; | 1027 | goto out_check; |
998 | } | 1028 | } |
999 | 1029 | ||
@@ -1080,9 +1110,9 @@ out_check: | |||
1080 | em->bdev = root->fs_info->fs_devices->latest_bdev; | 1110 | em->bdev = root->fs_info->fs_devices->latest_bdev; |
1081 | set_bit(EXTENT_FLAG_PINNED, &em->flags); | 1111 | set_bit(EXTENT_FLAG_PINNED, &em->flags); |
1082 | while (1) { | 1112 | while (1) { |
1083 | spin_lock(&em_tree->lock); | 1113 | write_lock(&em_tree->lock); |
1084 | ret = add_extent_mapping(em_tree, em); | 1114 | ret = add_extent_mapping(em_tree, em); |
1085 | spin_unlock(&em_tree->lock); | 1115 | write_unlock(&em_tree->lock); |
1086 | if (ret != -EEXIST) { | 1116 | if (ret != -EEXIST) { |
1087 | free_extent_map(em); | 1117 | free_extent_map(em); |
1088 | break; | 1118 | break; |
@@ -1100,8 +1130,10 @@ out_check: | |||
1100 | BUG_ON(ret); | 1130 | BUG_ON(ret); |
1101 | 1131 | ||
1102 | extent_clear_unlock_delalloc(inode, &BTRFS_I(inode)->io_tree, | 1132 | extent_clear_unlock_delalloc(inode, &BTRFS_I(inode)->io_tree, |
1103 | cur_offset, cur_offset + num_bytes - 1, | 1133 | cur_offset, cur_offset + num_bytes - 1, |
1104 | locked_page, 1, 1, 1, 0, 0, 0); | 1134 | locked_page, EXTENT_CLEAR_UNLOCK_PAGE | |
1135 | EXTENT_CLEAR_UNLOCK | EXTENT_CLEAR_DELALLOC | | ||
1136 | EXTENT_SET_PRIVATE2); | ||
1105 | cur_offset = extent_end; | 1137 | cur_offset = extent_end; |
1106 | if (cur_offset > end) | 1138 | if (cur_offset > end) |
1107 | break; | 1139 | break; |
@@ -1147,6 +1179,89 @@ static int run_delalloc_range(struct inode *inode, struct page *locked_page, | |||
1147 | return ret; | 1179 | return ret; |
1148 | } | 1180 | } |
1149 | 1181 | ||
1182 | static 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 | */ | ||
1222 | static 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 | |||
1150 | /* | 1265 | /* |
1151 | * extent_io.c set_bit_hook, used to track delayed allocation | 1266 | * extent_io.c set_bit_hook, used to track delayed allocation |
1152 | * 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 |
@@ -1155,6 +1270,7 @@ static int run_delalloc_range(struct inode *inode, struct page *locked_page, | |||
1155 | static int btrfs_set_bit_hook(struct inode *inode, u64 start, u64 end, | 1270 | static int btrfs_set_bit_hook(struct inode *inode, u64 start, u64 end, |
1156 | unsigned long old, unsigned long bits) | 1271 | unsigned long old, unsigned long bits) |
1157 | { | 1272 | { |
1273 | |||
1158 | /* | 1274 | /* |
1159 | * set_bit and clear bit hooks normally require _irqsave/restore | 1275 | * set_bit and clear bit hooks normally require _irqsave/restore |
1160 | * but in this case, we are only testeing for the DELALLOC | 1276 | * but in this case, we are only testeing for the DELALLOC |
@@ -1162,6 +1278,10 @@ static int btrfs_set_bit_hook(struct inode *inode, u64 start, u64 end, | |||
1162 | */ | 1278 | */ |
1163 | if (!(old & EXTENT_DELALLOC) && (bits & EXTENT_DELALLOC)) { | 1279 | if (!(old & EXTENT_DELALLOC) && (bits & EXTENT_DELALLOC)) { |
1164 | 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); | ||
1165 | btrfs_delalloc_reserve_space(root, inode, end - start + 1); | 1285 | btrfs_delalloc_reserve_space(root, inode, end - start + 1); |
1166 | spin_lock(&root->fs_info->delalloc_lock); | 1286 | spin_lock(&root->fs_info->delalloc_lock); |
1167 | BTRFS_I(inode)->delalloc_bytes += end - start + 1; | 1287 | BTRFS_I(inode)->delalloc_bytes += end - start + 1; |
@@ -1178,22 +1298,31 @@ static int btrfs_set_bit_hook(struct inode *inode, u64 start, u64 end, | |||
1178 | /* | 1298 | /* |
1179 | * 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 |
1180 | */ | 1300 | */ |
1181 | static int btrfs_clear_bit_hook(struct inode *inode, u64 start, u64 end, | 1301 | static int btrfs_clear_bit_hook(struct inode *inode, |
1182 | unsigned long old, unsigned long bits) | 1302 | struct extent_state *state, unsigned long bits) |
1183 | { | 1303 | { |
1184 | /* | 1304 | /* |
1185 | * set_bit and clear bit hooks normally require _irqsave/restore | 1305 | * set_bit and clear bit hooks normally require _irqsave/restore |
1186 | * but in this case, we are only testeing for the DELALLOC | 1306 | * but in this case, we are only testeing for the DELALLOC |
1187 | * bit, which is only set or cleared with irqs on | 1307 | * bit, which is only set or cleared with irqs on |
1188 | */ | 1308 | */ |
1189 | if ((old & EXTENT_DELALLOC) && (bits & EXTENT_DELALLOC)) { | 1309 | if ((state->state & EXTENT_DELALLOC) && (bits & EXTENT_DELALLOC)) { |
1190 | struct btrfs_root *root = BTRFS_I(inode)->root; | 1310 | struct btrfs_root *root = BTRFS_I(inode)->root; |
1191 | 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 | |||
1192 | spin_lock(&root->fs_info->delalloc_lock); | 1319 | spin_lock(&root->fs_info->delalloc_lock); |
1193 | if (end - start + 1 > root->fs_info->delalloc_bytes) { | 1320 | if (state->end - state->start + 1 > |
1321 | root->fs_info->delalloc_bytes) { | ||
1194 | printk(KERN_INFO "btrfs warning: delalloc account " | 1322 | printk(KERN_INFO "btrfs warning: delalloc account " |
1195 | "%llu %llu\n", | 1323 | "%llu %llu\n", |
1196 | (unsigned long long)end - start + 1, | 1324 | (unsigned long long) |
1325 | state->end - state->start + 1, | ||
1197 | (unsigned long long) | 1326 | (unsigned long long) |
1198 | root->fs_info->delalloc_bytes); | 1327 | root->fs_info->delalloc_bytes); |
1199 | btrfs_delalloc_free_space(root, inode, (u64)-1); | 1328 | btrfs_delalloc_free_space(root, inode, (u64)-1); |
@@ -1201,9 +1330,12 @@ static int btrfs_clear_bit_hook(struct inode *inode, u64 start, u64 end, | |||
1201 | BTRFS_I(inode)->delalloc_bytes = 0; | 1330 | BTRFS_I(inode)->delalloc_bytes = 0; |
1202 | } else { | 1331 | } else { |
1203 | btrfs_delalloc_free_space(root, inode, | 1332 | btrfs_delalloc_free_space(root, inode, |
1204 | end - start + 1); | 1333 | state->end - |
1205 | root->fs_info->delalloc_bytes -= end - start + 1; | 1334 | state->start + 1); |
1206 | 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; | ||
1207 | } | 1339 | } |
1208 | if (BTRFS_I(inode)->delalloc_bytes == 0 && | 1340 | if (BTRFS_I(inode)->delalloc_bytes == 0 && |
1209 | !list_empty(&BTRFS_I(inode)->delalloc_inodes)) { | 1341 | !list_empty(&BTRFS_I(inode)->delalloc_inodes)) { |
@@ -1374,10 +1506,8 @@ again: | |||
1374 | lock_extent(&BTRFS_I(inode)->io_tree, page_start, page_end, GFP_NOFS); | 1506 | lock_extent(&BTRFS_I(inode)->io_tree, page_start, page_end, GFP_NOFS); |
1375 | 1507 | ||
1376 | /* already ordered? We're done */ | 1508 | /* already ordered? We're done */ |
1377 | if (test_range_bit(&BTRFS_I(inode)->io_tree, page_start, page_end, | 1509 | if (PagePrivate2(page)) |
1378 | EXTENT_ORDERED, 0)) { | ||
1379 | goto out; | 1510 | goto out; |
1380 | } | ||
1381 | 1511 | ||
1382 | ordered = btrfs_lookup_ordered_extent(inode, page_start); | 1512 | ordered = btrfs_lookup_ordered_extent(inode, page_start); |
1383 | if (ordered) { | 1513 | if (ordered) { |
@@ -1413,11 +1543,9 @@ static int btrfs_writepage_start_hook(struct page *page, u64 start, u64 end) | |||
1413 | struct inode *inode = page->mapping->host; | 1543 | struct inode *inode = page->mapping->host; |
1414 | struct btrfs_writepage_fixup *fixup; | 1544 | struct btrfs_writepage_fixup *fixup; |
1415 | struct btrfs_root *root = BTRFS_I(inode)->root; | 1545 | struct btrfs_root *root = BTRFS_I(inode)->root; |
1416 | int ret; | ||
1417 | 1546 | ||
1418 | ret = test_range_bit(&BTRFS_I(inode)->io_tree, start, end, | 1547 | /* this page is properly in the ordered list */ |
1419 | EXTENT_ORDERED, 0); | 1548 | if (TestClearPagePrivate2(page)) |
1420 | if (ret) | ||
1421 | return 0; | 1549 | return 0; |
1422 | 1550 | ||
1423 | if (PageChecked(page)) | 1551 | if (PageChecked(page)) |
@@ -1455,9 +1583,19 @@ static int insert_reserved_file_extent(struct btrfs_trans_handle *trans, | |||
1455 | BUG_ON(!path); | 1583 | BUG_ON(!path); |
1456 | 1584 | ||
1457 | path->leave_spinning = 1; | 1585 | path->leave_spinning = 1; |
1586 | |||
1587 | /* | ||
1588 | * we may be replacing one extent in the tree with another. | ||
1589 | * The new extent is pinned in the extent map, and we don't want | ||
1590 | * to drop it from the cache until it is completely in the btree. | ||
1591 | * | ||
1592 | * So, tell btrfs_drop_extents to leave this extent in the cache. | ||
1593 | * the caller is expected to unpin it and allow it to be merged | ||
1594 | * with the others. | ||
1595 | */ | ||
1458 | ret = btrfs_drop_extents(trans, root, inode, file_pos, | 1596 | ret = btrfs_drop_extents(trans, root, inode, file_pos, |
1459 | file_pos + num_bytes, locked_end, | 1597 | file_pos + num_bytes, locked_end, |
1460 | file_pos, &hint); | 1598 | file_pos, &hint, 0); |
1461 | BUG_ON(ret); | 1599 | BUG_ON(ret); |
1462 | 1600 | ||
1463 | ins.objectid = inode->i_ino; | 1601 | ins.objectid = inode->i_ino; |
@@ -1485,7 +1623,6 @@ static int insert_reserved_file_extent(struct btrfs_trans_handle *trans, | |||
1485 | btrfs_mark_buffer_dirty(leaf); | 1623 | btrfs_mark_buffer_dirty(leaf); |
1486 | 1624 | ||
1487 | inode_add_bytes(inode, num_bytes); | 1625 | inode_add_bytes(inode, num_bytes); |
1488 | btrfs_drop_extent_cache(inode, file_pos, file_pos + num_bytes - 1, 0); | ||
1489 | 1626 | ||
1490 | ins.objectid = disk_bytenr; | 1627 | ins.objectid = disk_bytenr; |
1491 | ins.offset = disk_num_bytes; | 1628 | ins.offset = disk_num_bytes; |
@@ -1596,6 +1733,9 @@ static int btrfs_finish_ordered_io(struct inode *inode, u64 start, u64 end) | |||
1596 | ordered_extent->len, | 1733 | ordered_extent->len, |
1597 | compressed, 0, 0, | 1734 | compressed, 0, 0, |
1598 | BTRFS_FILE_EXTENT_REG); | 1735 | BTRFS_FILE_EXTENT_REG); |
1736 | unpin_extent_cache(&BTRFS_I(inode)->extent_tree, | ||
1737 | ordered_extent->file_offset, | ||
1738 | ordered_extent->len); | ||
1599 | BUG_ON(ret); | 1739 | BUG_ON(ret); |
1600 | } | 1740 | } |
1601 | unlock_extent(io_tree, ordered_extent->file_offset, | 1741 | unlock_extent(io_tree, ordered_extent->file_offset, |
@@ -1623,6 +1763,7 @@ nocow: | |||
1623 | static int btrfs_writepage_end_io_hook(struct page *page, u64 start, u64 end, | 1763 | static int btrfs_writepage_end_io_hook(struct page *page, u64 start, u64 end, |
1624 | struct extent_state *state, int uptodate) | 1764 | struct extent_state *state, int uptodate) |
1625 | { | 1765 | { |
1766 | ClearPagePrivate2(page); | ||
1626 | return btrfs_finish_ordered_io(page->mapping->host, start, end); | 1767 | return btrfs_finish_ordered_io(page->mapping->host, start, end); |
1627 | } | 1768 | } |
1628 | 1769 | ||
@@ -1669,13 +1810,13 @@ static int btrfs_io_failed_hook(struct bio *failed_bio, | |||
1669 | failrec->last_mirror = 0; | 1810 | failrec->last_mirror = 0; |
1670 | failrec->bio_flags = 0; | 1811 | failrec->bio_flags = 0; |
1671 | 1812 | ||
1672 | spin_lock(&em_tree->lock); | 1813 | read_lock(&em_tree->lock); |
1673 | em = lookup_extent_mapping(em_tree, start, failrec->len); | 1814 | em = lookup_extent_mapping(em_tree, start, failrec->len); |
1674 | if (em->start > start || em->start + em->len < start) { | 1815 | if (em->start > start || em->start + em->len < start) { |
1675 | free_extent_map(em); | 1816 | free_extent_map(em); |
1676 | em = NULL; | 1817 | em = NULL; |
1677 | } | 1818 | } |
1678 | spin_unlock(&em_tree->lock); | 1819 | read_unlock(&em_tree->lock); |
1679 | 1820 | ||
1680 | if (!em || IS_ERR(em)) { | 1821 | if (!em || IS_ERR(em)) { |
1681 | kfree(failrec); | 1822 | kfree(failrec); |
@@ -1794,7 +1935,7 @@ static int btrfs_readpage_end_io_hook(struct page *page, u64 start, u64 end, | |||
1794 | return 0; | 1935 | return 0; |
1795 | 1936 | ||
1796 | if (root->root_key.objectid == BTRFS_DATA_RELOC_TREE_OBJECTID && | 1937 | if (root->root_key.objectid == BTRFS_DATA_RELOC_TREE_OBJECTID && |
1797 | test_range_bit(io_tree, start, end, EXTENT_NODATASUM, 1)) { | 1938 | test_range_bit(io_tree, start, end, EXTENT_NODATASUM, 1, NULL)) { |
1798 | clear_extent_bits(io_tree, start, end, EXTENT_NODATASUM, | 1939 | clear_extent_bits(io_tree, start, end, EXTENT_NODATASUM, |
1799 | GFP_NOFS); | 1940 | GFP_NOFS); |
1800 | return 0; | 1941 | return 0; |
@@ -2352,6 +2493,69 @@ static int btrfs_unlink(struct inode *dir, struct dentry *dentry) | |||
2352 | return ret; | 2493 | return ret; |
2353 | } | 2494 | } |
2354 | 2495 | ||
2496 | int btrfs_unlink_subvol(struct btrfs_trans_handle *trans, | ||
2497 | struct btrfs_root *root, | ||
2498 | struct inode *dir, u64 objectid, | ||
2499 | const char *name, int name_len) | ||
2500 | { | ||
2501 | struct btrfs_path *path; | ||
2502 | struct extent_buffer *leaf; | ||
2503 | struct btrfs_dir_item *di; | ||
2504 | struct btrfs_key key; | ||
2505 | u64 index; | ||
2506 | int ret; | ||
2507 | |||
2508 | path = btrfs_alloc_path(); | ||
2509 | if (!path) | ||
2510 | return -ENOMEM; | ||
2511 | |||
2512 | di = btrfs_lookup_dir_item(trans, root, path, dir->i_ino, | ||
2513 | name, name_len, -1); | ||
2514 | BUG_ON(!di || IS_ERR(di)); | ||
2515 | |||
2516 | leaf = path->nodes[0]; | ||
2517 | btrfs_dir_item_key_to_cpu(leaf, di, &key); | ||
2518 | WARN_ON(key.type != BTRFS_ROOT_ITEM_KEY || key.objectid != objectid); | ||
2519 | ret = btrfs_delete_one_dir_name(trans, root, path, di); | ||
2520 | BUG_ON(ret); | ||
2521 | btrfs_release_path(root, path); | ||
2522 | |||
2523 | ret = btrfs_del_root_ref(trans, root->fs_info->tree_root, | ||
2524 | objectid, root->root_key.objectid, | ||
2525 | dir->i_ino, &index, name, name_len); | ||
2526 | if (ret < 0) { | ||
2527 | BUG_ON(ret != -ENOENT); | ||
2528 | di = btrfs_search_dir_index_item(root, path, dir->i_ino, | ||
2529 | name, name_len); | ||
2530 | BUG_ON(!di || IS_ERR(di)); | ||
2531 | |||
2532 | leaf = path->nodes[0]; | ||
2533 | btrfs_item_key_to_cpu(leaf, &key, path->slots[0]); | ||
2534 | btrfs_release_path(root, path); | ||
2535 | index = key.offset; | ||
2536 | } | ||
2537 | |||
2538 | di = btrfs_lookup_dir_index_item(trans, root, path, dir->i_ino, | ||
2539 | index, name, name_len, -1); | ||
2540 | BUG_ON(!di || IS_ERR(di)); | ||
2541 | |||
2542 | leaf = path->nodes[0]; | ||
2543 | btrfs_dir_item_key_to_cpu(leaf, di, &key); | ||
2544 | WARN_ON(key.type != BTRFS_ROOT_ITEM_KEY || key.objectid != objectid); | ||
2545 | ret = btrfs_delete_one_dir_name(trans, root, path, di); | ||
2546 | BUG_ON(ret); | ||
2547 | btrfs_release_path(root, path); | ||
2548 | |||
2549 | btrfs_i_size_write(dir, dir->i_size - name_len * 2); | ||
2550 | dir->i_mtime = dir->i_ctime = CURRENT_TIME; | ||
2551 | ret = btrfs_update_inode(trans, root, dir); | ||
2552 | BUG_ON(ret); | ||
2553 | dir->i_sb->s_dirt = 1; | ||
2554 | |||
2555 | btrfs_free_path(path); | ||
2556 | return 0; | ||
2557 | } | ||
2558 | |||
2355 | static int btrfs_rmdir(struct inode *dir, struct dentry *dentry) | 2559 | static int btrfs_rmdir(struct inode *dir, struct dentry *dentry) |
2356 | { | 2560 | { |
2357 | struct inode *inode = dentry->d_inode; | 2561 | struct inode *inode = dentry->d_inode; |
@@ -2361,29 +2565,31 @@ static int btrfs_rmdir(struct inode *dir, struct dentry *dentry) | |||
2361 | struct btrfs_trans_handle *trans; | 2565 | struct btrfs_trans_handle *trans; |
2362 | unsigned long nr = 0; | 2566 | unsigned long nr = 0; |
2363 | 2567 | ||
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 || | 2568 | if (inode->i_size > BTRFS_EMPTY_DIR_SIZE || |
2369 | inode->i_ino == BTRFS_FIRST_FREE_OBJECTID) { | 2569 | inode->i_ino == BTRFS_FIRST_FREE_OBJECTID) |
2370 | return -ENOTEMPTY; | 2570 | return -ENOTEMPTY; |
2371 | } | ||
2372 | 2571 | ||
2373 | trans = btrfs_start_transaction(root, 1); | 2572 | trans = btrfs_start_transaction(root, 1); |
2374 | btrfs_set_trans_block_group(trans, dir); | 2573 | btrfs_set_trans_block_group(trans, dir); |
2375 | 2574 | ||
2575 | if (unlikely(inode->i_ino == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID)) { | ||
2576 | err = btrfs_unlink_subvol(trans, root, dir, | ||
2577 | BTRFS_I(inode)->location.objectid, | ||
2578 | dentry->d_name.name, | ||
2579 | dentry->d_name.len); | ||
2580 | goto out; | ||
2581 | } | ||
2582 | |||
2376 | err = btrfs_orphan_add(trans, inode); | 2583 | err = btrfs_orphan_add(trans, inode); |
2377 | if (err) | 2584 | if (err) |
2378 | goto fail_trans; | 2585 | goto out; |
2379 | 2586 | ||
2380 | /* now the directory is empty */ | 2587 | /* now the directory is empty */ |
2381 | err = btrfs_unlink_inode(trans, root, dir, dentry->d_inode, | 2588 | err = btrfs_unlink_inode(trans, root, dir, dentry->d_inode, |
2382 | dentry->d_name.name, dentry->d_name.len); | 2589 | dentry->d_name.name, dentry->d_name.len); |
2383 | if (!err) | 2590 | if (!err) |
2384 | btrfs_i_size_write(inode, 0); | 2591 | btrfs_i_size_write(inode, 0); |
2385 | 2592 | out: | |
2386 | fail_trans: | ||
2387 | nr = trans->blocks_used; | 2593 | nr = trans->blocks_used; |
2388 | ret = btrfs_end_transaction_throttle(trans, root); | 2594 | ret = btrfs_end_transaction_throttle(trans, root); |
2389 | btrfs_btree_balance_dirty(root, nr); | 2595 | btrfs_btree_balance_dirty(root, nr); |
@@ -2826,12 +3032,22 @@ static int btrfs_truncate_page(struct address_space *mapping, loff_t from) | |||
2826 | 3032 | ||
2827 | if ((offset & (blocksize - 1)) == 0) | 3033 | if ((offset & (blocksize - 1)) == 0) |
2828 | goto out; | 3034 | goto out; |
3035 | ret = btrfs_check_data_free_space(root, inode, PAGE_CACHE_SIZE); | ||
3036 | if (ret) | ||
3037 | goto out; | ||
3038 | |||
3039 | ret = btrfs_reserve_metadata_for_delalloc(root, inode, 1); | ||
3040 | if (ret) | ||
3041 | goto out; | ||
2829 | 3042 | ||
2830 | ret = -ENOMEM; | 3043 | ret = -ENOMEM; |
2831 | again: | 3044 | again: |
2832 | page = grab_cache_page(mapping, index); | 3045 | page = grab_cache_page(mapping, index); |
2833 | if (!page) | 3046 | if (!page) { |
3047 | btrfs_free_reserved_data_space(root, inode, PAGE_CACHE_SIZE); | ||
3048 | btrfs_unreserve_metadata_for_delalloc(root, inode, 1); | ||
2834 | goto out; | 3049 | goto out; |
3050 | } | ||
2835 | 3051 | ||
2836 | page_start = page_offset(page); | 3052 | page_start = page_offset(page); |
2837 | page_end = page_start + PAGE_CACHE_SIZE - 1; | 3053 | page_end = page_start + PAGE_CACHE_SIZE - 1; |
@@ -2864,7 +3080,16 @@ again: | |||
2864 | goto again; | 3080 | goto again; |
2865 | } | 3081 | } |
2866 | 3082 | ||
2867 | btrfs_set_extent_delalloc(inode, page_start, page_end); | 3083 | clear_extent_bits(&BTRFS_I(inode)->io_tree, page_start, page_end, |
3084 | EXTENT_DIRTY | EXTENT_DELALLOC | EXTENT_DO_ACCOUNTING, | ||
3085 | GFP_NOFS); | ||
3086 | |||
3087 | ret = btrfs_set_extent_delalloc(inode, page_start, page_end); | ||
3088 | if (ret) { | ||
3089 | unlock_extent(io_tree, page_start, page_end, GFP_NOFS); | ||
3090 | goto out_unlock; | ||
3091 | } | ||
3092 | |||
2868 | ret = 0; | 3093 | ret = 0; |
2869 | if (offset != PAGE_CACHE_SIZE) { | 3094 | if (offset != PAGE_CACHE_SIZE) { |
2870 | kaddr = kmap(page); | 3095 | kaddr = kmap(page); |
@@ -2877,6 +3102,9 @@ again: | |||
2877 | unlock_extent(io_tree, page_start, page_end, GFP_NOFS); | 3102 | unlock_extent(io_tree, page_start, page_end, GFP_NOFS); |
2878 | 3103 | ||
2879 | out_unlock: | 3104 | out_unlock: |
3105 | if (ret) | ||
3106 | btrfs_free_reserved_data_space(root, inode, PAGE_CACHE_SIZE); | ||
3107 | btrfs_unreserve_metadata_for_delalloc(root, inode, 1); | ||
2880 | unlock_page(page); | 3108 | unlock_page(page); |
2881 | page_cache_release(page); | 3109 | page_cache_release(page); |
2882 | out: | 3110 | out: |
@@ -2895,17 +3123,15 @@ int btrfs_cont_expand(struct inode *inode, loff_t size) | |||
2895 | u64 last_byte; | 3123 | u64 last_byte; |
2896 | u64 cur_offset; | 3124 | u64 cur_offset; |
2897 | u64 hole_size; | 3125 | u64 hole_size; |
2898 | int err; | 3126 | int err = 0; |
2899 | 3127 | ||
2900 | if (size <= hole_start) | 3128 | if (size <= hole_start) |
2901 | return 0; | 3129 | return 0; |
2902 | 3130 | ||
2903 | err = btrfs_check_metadata_free_space(root); | 3131 | err = btrfs_truncate_page(inode->i_mapping, inode->i_size); |
2904 | if (err) | 3132 | if (err) |
2905 | return err; | 3133 | return err; |
2906 | 3134 | ||
2907 | btrfs_truncate_page(inode->i_mapping, inode->i_size); | ||
2908 | |||
2909 | while (1) { | 3135 | while (1) { |
2910 | struct btrfs_ordered_extent *ordered; | 3136 | struct btrfs_ordered_extent *ordered; |
2911 | btrfs_wait_ordered_range(inode, hole_start, | 3137 | btrfs_wait_ordered_range(inode, hole_start, |
@@ -2935,15 +3161,21 @@ int btrfs_cont_expand(struct inode *inode, loff_t size) | |||
2935 | cur_offset, | 3161 | cur_offset, |
2936 | cur_offset + hole_size, | 3162 | cur_offset + hole_size, |
2937 | block_end, | 3163 | block_end, |
2938 | cur_offset, &hint_byte); | 3164 | cur_offset, &hint_byte, 1); |
3165 | if (err) | ||
3166 | break; | ||
3167 | |||
3168 | err = btrfs_reserve_metadata_space(root, 1); | ||
2939 | if (err) | 3169 | if (err) |
2940 | break; | 3170 | break; |
3171 | |||
2941 | err = btrfs_insert_file_extent(trans, root, | 3172 | err = btrfs_insert_file_extent(trans, root, |
2942 | inode->i_ino, cur_offset, 0, | 3173 | inode->i_ino, cur_offset, 0, |
2943 | 0, hole_size, 0, hole_size, | 3174 | 0, hole_size, 0, hole_size, |
2944 | 0, 0, 0); | 3175 | 0, 0, 0); |
2945 | btrfs_drop_extent_cache(inode, hole_start, | 3176 | btrfs_drop_extent_cache(inode, hole_start, |
2946 | last_byte - 1, 0); | 3177 | last_byte - 1, 0); |
3178 | btrfs_unreserve_metadata_space(root, 1); | ||
2947 | } | 3179 | } |
2948 | free_extent_map(em); | 3180 | free_extent_map(em); |
2949 | cur_offset = last_byte; | 3181 | cur_offset = last_byte; |
@@ -3003,6 +3235,11 @@ void btrfs_delete_inode(struct inode *inode) | |||
3003 | } | 3235 | } |
3004 | btrfs_wait_ordered_range(inode, 0, (u64)-1); | 3236 | btrfs_wait_ordered_range(inode, 0, (u64)-1); |
3005 | 3237 | ||
3238 | if (inode->i_nlink > 0) { | ||
3239 | BUG_ON(btrfs_root_refs(&root->root_item) != 0); | ||
3240 | goto no_delete; | ||
3241 | } | ||
3242 | |||
3006 | btrfs_i_size_write(inode, 0); | 3243 | btrfs_i_size_write(inode, 0); |
3007 | trans = btrfs_join_transaction(root, 1); | 3244 | trans = btrfs_join_transaction(root, 1); |
3008 | 3245 | ||
@@ -3070,29 +3307,67 @@ out_err: | |||
3070 | * is kind of like crossing a mount point. | 3307 | * is kind of like crossing a mount point. |
3071 | */ | 3308 | */ |
3072 | static int fixup_tree_root_location(struct btrfs_root *root, | 3309 | static int fixup_tree_root_location(struct btrfs_root *root, |
3073 | struct btrfs_key *location, | 3310 | struct inode *dir, |
3074 | struct btrfs_root **sub_root, | 3311 | struct dentry *dentry, |
3075 | struct dentry *dentry) | 3312 | struct btrfs_key *location, |
3313 | struct btrfs_root **sub_root) | ||
3076 | { | 3314 | { |
3077 | struct btrfs_root_item *ri; | 3315 | struct btrfs_path *path; |
3316 | struct btrfs_root *new_root; | ||
3317 | struct btrfs_root_ref *ref; | ||
3318 | struct extent_buffer *leaf; | ||
3319 | int ret; | ||
3320 | int err = 0; | ||
3078 | 3321 | ||
3079 | if (btrfs_key_type(location) != BTRFS_ROOT_ITEM_KEY) | 3322 | path = btrfs_alloc_path(); |
3080 | return 0; | 3323 | if (!path) { |
3081 | if (location->objectid == BTRFS_ROOT_TREE_OBJECTID) | 3324 | err = -ENOMEM; |
3082 | return 0; | 3325 | goto out; |
3326 | } | ||
3083 | 3327 | ||
3084 | *sub_root = btrfs_read_fs_root(root->fs_info, location, | 3328 | err = -ENOENT; |
3085 | dentry->d_name.name, | 3329 | ret = btrfs_find_root_ref(root->fs_info->tree_root, path, |
3086 | dentry->d_name.len); | 3330 | BTRFS_I(dir)->root->root_key.objectid, |
3087 | if (IS_ERR(*sub_root)) | 3331 | location->objectid); |
3088 | return PTR_ERR(*sub_root); | 3332 | if (ret) { |
3333 | if (ret < 0) | ||
3334 | err = ret; | ||
3335 | goto out; | ||
3336 | } | ||
3089 | 3337 | ||
3090 | ri = &(*sub_root)->root_item; | 3338 | leaf = path->nodes[0]; |
3091 | location->objectid = btrfs_root_dirid(ri); | 3339 | ref = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_root_ref); |
3092 | btrfs_set_key_type(location, BTRFS_INODE_ITEM_KEY); | 3340 | if (btrfs_root_ref_dirid(leaf, ref) != dir->i_ino || |
3093 | location->offset = 0; | 3341 | btrfs_root_ref_name_len(leaf, ref) != dentry->d_name.len) |
3342 | goto out; | ||
3094 | 3343 | ||
3095 | return 0; | 3344 | ret = memcmp_extent_buffer(leaf, dentry->d_name.name, |
3345 | (unsigned long)(ref + 1), | ||
3346 | dentry->d_name.len); | ||
3347 | if (ret) | ||
3348 | goto out; | ||
3349 | |||
3350 | btrfs_release_path(root->fs_info->tree_root, path); | ||
3351 | |||
3352 | new_root = btrfs_read_fs_root_no_name(root->fs_info, location); | ||
3353 | if (IS_ERR(new_root)) { | ||
3354 | err = PTR_ERR(new_root); | ||
3355 | goto out; | ||
3356 | } | ||
3357 | |||
3358 | if (btrfs_root_refs(&new_root->root_item) == 0) { | ||
3359 | err = -ENOENT; | ||
3360 | goto out; | ||
3361 | } | ||
3362 | |||
3363 | *sub_root = new_root; | ||
3364 | location->objectid = btrfs_root_dirid(&new_root->root_item); | ||
3365 | location->type = BTRFS_INODE_ITEM_KEY; | ||
3366 | location->offset = 0; | ||
3367 | err = 0; | ||
3368 | out: | ||
3369 | btrfs_free_path(path); | ||
3370 | return err; | ||
3096 | } | 3371 | } |
3097 | 3372 | ||
3098 | static void inode_tree_add(struct inode *inode) | 3373 | static void inode_tree_add(struct inode *inode) |
@@ -3101,11 +3376,13 @@ static void inode_tree_add(struct inode *inode) | |||
3101 | struct btrfs_inode *entry; | 3376 | struct btrfs_inode *entry; |
3102 | struct rb_node **p; | 3377 | struct rb_node **p; |
3103 | struct rb_node *parent; | 3378 | struct rb_node *parent; |
3104 | |||
3105 | again: | 3379 | again: |
3106 | p = &root->inode_tree.rb_node; | 3380 | p = &root->inode_tree.rb_node; |
3107 | parent = NULL; | 3381 | parent = NULL; |
3108 | 3382 | ||
3383 | if (hlist_unhashed(&inode->i_hash)) | ||
3384 | return; | ||
3385 | |||
3109 | spin_lock(&root->inode_lock); | 3386 | spin_lock(&root->inode_lock); |
3110 | while (*p) { | 3387 | while (*p) { |
3111 | parent = *p; | 3388 | parent = *p; |
@@ -3132,13 +3409,87 @@ again: | |||
3132 | static void inode_tree_del(struct inode *inode) | 3409 | static void inode_tree_del(struct inode *inode) |
3133 | { | 3410 | { |
3134 | struct btrfs_root *root = BTRFS_I(inode)->root; | 3411 | struct btrfs_root *root = BTRFS_I(inode)->root; |
3412 | int empty = 0; | ||
3135 | 3413 | ||
3136 | spin_lock(&root->inode_lock); | 3414 | spin_lock(&root->inode_lock); |
3137 | if (!RB_EMPTY_NODE(&BTRFS_I(inode)->rb_node)) { | 3415 | if (!RB_EMPTY_NODE(&BTRFS_I(inode)->rb_node)) { |
3138 | rb_erase(&BTRFS_I(inode)->rb_node, &root->inode_tree); | 3416 | rb_erase(&BTRFS_I(inode)->rb_node, &root->inode_tree); |
3139 | RB_CLEAR_NODE(&BTRFS_I(inode)->rb_node); | 3417 | RB_CLEAR_NODE(&BTRFS_I(inode)->rb_node); |
3418 | empty = RB_EMPTY_ROOT(&root->inode_tree); | ||
3140 | } | 3419 | } |
3141 | spin_unlock(&root->inode_lock); | 3420 | spin_unlock(&root->inode_lock); |
3421 | |||
3422 | if (empty && btrfs_root_refs(&root->root_item) == 0) { | ||
3423 | synchronize_srcu(&root->fs_info->subvol_srcu); | ||
3424 | spin_lock(&root->inode_lock); | ||
3425 | empty = RB_EMPTY_ROOT(&root->inode_tree); | ||
3426 | spin_unlock(&root->inode_lock); | ||
3427 | if (empty) | ||
3428 | btrfs_add_dead_root(root); | ||
3429 | } | ||
3430 | } | ||
3431 | |||
3432 | int btrfs_invalidate_inodes(struct btrfs_root *root) | ||
3433 | { | ||
3434 | struct rb_node *node; | ||
3435 | struct rb_node *prev; | ||
3436 | struct btrfs_inode *entry; | ||
3437 | struct inode *inode; | ||
3438 | u64 objectid = 0; | ||
3439 | |||
3440 | WARN_ON(btrfs_root_refs(&root->root_item) != 0); | ||
3441 | |||
3442 | spin_lock(&root->inode_lock); | ||
3443 | again: | ||
3444 | node = root->inode_tree.rb_node; | ||
3445 | prev = NULL; | ||
3446 | while (node) { | ||
3447 | prev = node; | ||
3448 | entry = rb_entry(node, struct btrfs_inode, rb_node); | ||
3449 | |||
3450 | if (objectid < entry->vfs_inode.i_ino) | ||
3451 | node = node->rb_left; | ||
3452 | else if (objectid > entry->vfs_inode.i_ino) | ||
3453 | node = node->rb_right; | ||
3454 | else | ||
3455 | break; | ||
3456 | } | ||
3457 | if (!node) { | ||
3458 | while (prev) { | ||
3459 | entry = rb_entry(prev, struct btrfs_inode, rb_node); | ||
3460 | if (objectid <= entry->vfs_inode.i_ino) { | ||
3461 | node = prev; | ||
3462 | break; | ||
3463 | } | ||
3464 | prev = rb_next(prev); | ||
3465 | } | ||
3466 | } | ||
3467 | while (node) { | ||
3468 | entry = rb_entry(node, struct btrfs_inode, rb_node); | ||
3469 | objectid = entry->vfs_inode.i_ino + 1; | ||
3470 | inode = igrab(&entry->vfs_inode); | ||
3471 | if (inode) { | ||
3472 | spin_unlock(&root->inode_lock); | ||
3473 | if (atomic_read(&inode->i_count) > 1) | ||
3474 | d_prune_aliases(inode); | ||
3475 | /* | ||
3476 | * btrfs_drop_inode will remove it from | ||
3477 | * the inode cache when its usage count | ||
3478 | * hits zero. | ||
3479 | */ | ||
3480 | iput(inode); | ||
3481 | cond_resched(); | ||
3482 | spin_lock(&root->inode_lock); | ||
3483 | goto again; | ||
3484 | } | ||
3485 | |||
3486 | if (cond_resched_lock(&root->inode_lock)) | ||
3487 | goto again; | ||
3488 | |||
3489 | node = rb_next(node); | ||
3490 | } | ||
3491 | spin_unlock(&root->inode_lock); | ||
3492 | return 0; | ||
3142 | } | 3493 | } |
3143 | 3494 | ||
3144 | static noinline void init_btrfs_i(struct inode *inode) | 3495 | static noinline void init_btrfs_i(struct inode *inode) |
@@ -3148,6 +3499,7 @@ static noinline void init_btrfs_i(struct inode *inode) | |||
3148 | bi->generation = 0; | 3499 | bi->generation = 0; |
3149 | bi->sequence = 0; | 3500 | bi->sequence = 0; |
3150 | bi->last_trans = 0; | 3501 | bi->last_trans = 0; |
3502 | bi->last_sub_trans = 0; | ||
3151 | bi->logged_trans = 0; | 3503 | bi->logged_trans = 0; |
3152 | bi->delalloc_bytes = 0; | 3504 | bi->delalloc_bytes = 0; |
3153 | bi->reserved_bytes = 0; | 3505 | bi->reserved_bytes = 0; |
@@ -3225,15 +3577,41 @@ struct inode *btrfs_iget(struct super_block *s, struct btrfs_key *location, | |||
3225 | return inode; | 3577 | return inode; |
3226 | } | 3578 | } |
3227 | 3579 | ||
3580 | static struct inode *new_simple_dir(struct super_block *s, | ||
3581 | struct btrfs_key *key, | ||
3582 | struct btrfs_root *root) | ||
3583 | { | ||
3584 | struct inode *inode = new_inode(s); | ||
3585 | |||
3586 | if (!inode) | ||
3587 | return ERR_PTR(-ENOMEM); | ||
3588 | |||
3589 | init_btrfs_i(inode); | ||
3590 | |||
3591 | BTRFS_I(inode)->root = root; | ||
3592 | memcpy(&BTRFS_I(inode)->location, key, sizeof(*key)); | ||
3593 | BTRFS_I(inode)->dummy_inode = 1; | ||
3594 | |||
3595 | inode->i_ino = BTRFS_EMPTY_SUBVOL_DIR_OBJECTID; | ||
3596 | inode->i_op = &simple_dir_inode_operations; | ||
3597 | inode->i_fop = &simple_dir_operations; | ||
3598 | inode->i_mode = S_IFDIR | S_IRUGO | S_IWUSR | S_IXUGO; | ||
3599 | inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME; | ||
3600 | |||
3601 | return inode; | ||
3602 | } | ||
3603 | |||
3228 | struct inode *btrfs_lookup_dentry(struct inode *dir, struct dentry *dentry) | 3604 | struct inode *btrfs_lookup_dentry(struct inode *dir, struct dentry *dentry) |
3229 | { | 3605 | { |
3230 | struct inode *inode; | 3606 | struct inode *inode; |
3231 | struct btrfs_inode *bi = BTRFS_I(dir); | 3607 | struct btrfs_root *root = BTRFS_I(dir)->root; |
3232 | struct btrfs_root *root = bi->root; | ||
3233 | struct btrfs_root *sub_root = root; | 3608 | struct btrfs_root *sub_root = root; |
3234 | struct btrfs_key location; | 3609 | struct btrfs_key location; |
3610 | int index; | ||
3235 | int ret; | 3611 | int ret; |
3236 | 3612 | ||
3613 | dentry->d_op = &btrfs_dentry_operations; | ||
3614 | |||
3237 | if (dentry->d_name.len > BTRFS_NAME_LEN) | 3615 | if (dentry->d_name.len > BTRFS_NAME_LEN) |
3238 | return ERR_PTR(-ENAMETOOLONG); | 3616 | return ERR_PTR(-ENAMETOOLONG); |
3239 | 3617 | ||
@@ -3242,29 +3620,52 @@ struct inode *btrfs_lookup_dentry(struct inode *dir, struct dentry *dentry) | |||
3242 | if (ret < 0) | 3620 | if (ret < 0) |
3243 | return ERR_PTR(ret); | 3621 | return ERR_PTR(ret); |
3244 | 3622 | ||
3245 | inode = NULL; | 3623 | if (location.objectid == 0) |
3246 | if (location.objectid) { | 3624 | return NULL; |
3247 | ret = fixup_tree_root_location(root, &location, &sub_root, | 3625 | |
3248 | dentry); | 3626 | if (location.type == BTRFS_INODE_ITEM_KEY) { |
3249 | if (ret < 0) | 3627 | inode = btrfs_iget(dir->i_sb, &location, root); |
3250 | return ERR_PTR(ret); | 3628 | return inode; |
3251 | if (ret > 0) | 3629 | } |
3252 | return ERR_PTR(-ENOENT); | 3630 | |
3631 | BUG_ON(location.type != BTRFS_ROOT_ITEM_KEY); | ||
3632 | |||
3633 | index = srcu_read_lock(&root->fs_info->subvol_srcu); | ||
3634 | ret = fixup_tree_root_location(root, dir, dentry, | ||
3635 | &location, &sub_root); | ||
3636 | if (ret < 0) { | ||
3637 | if (ret != -ENOENT) | ||
3638 | inode = ERR_PTR(ret); | ||
3639 | else | ||
3640 | inode = new_simple_dir(dir->i_sb, &location, sub_root); | ||
3641 | } else { | ||
3253 | inode = btrfs_iget(dir->i_sb, &location, sub_root); | 3642 | inode = btrfs_iget(dir->i_sb, &location, sub_root); |
3254 | if (IS_ERR(inode)) | ||
3255 | return ERR_CAST(inode); | ||
3256 | } | 3643 | } |
3644 | srcu_read_unlock(&root->fs_info->subvol_srcu, index); | ||
3645 | |||
3257 | return inode; | 3646 | return inode; |
3258 | } | 3647 | } |
3259 | 3648 | ||
3649 | static int btrfs_dentry_delete(struct dentry *dentry) | ||
3650 | { | ||
3651 | struct btrfs_root *root; | ||
3652 | |||
3653 | if (!dentry->d_inode && !IS_ROOT(dentry)) | ||
3654 | dentry = dentry->d_parent; | ||
3655 | |||
3656 | if (dentry->d_inode) { | ||
3657 | root = BTRFS_I(dentry->d_inode)->root; | ||
3658 | if (btrfs_root_refs(&root->root_item) == 0) | ||
3659 | return 1; | ||
3660 | } | ||
3661 | return 0; | ||
3662 | } | ||
3663 | |||
3260 | static struct dentry *btrfs_lookup(struct inode *dir, struct dentry *dentry, | 3664 | static struct dentry *btrfs_lookup(struct inode *dir, struct dentry *dentry, |
3261 | struct nameidata *nd) | 3665 | struct nameidata *nd) |
3262 | { | 3666 | { |
3263 | struct inode *inode; | 3667 | struct inode *inode; |
3264 | 3668 | ||
3265 | if (dentry->d_name.len > BTRFS_NAME_LEN) | ||
3266 | return ERR_PTR(-ENAMETOOLONG); | ||
3267 | |||
3268 | inode = btrfs_lookup_dentry(dir, dentry); | 3669 | inode = btrfs_lookup_dentry(dir, dentry); |
3269 | if (IS_ERR(inode)) | 3670 | if (IS_ERR(inode)) |
3270 | return ERR_CAST(inode); | 3671 | return ERR_CAST(inode); |
@@ -3603,9 +4004,6 @@ static struct inode *btrfs_new_inode(struct btrfs_trans_handle *trans, | |||
3603 | if (ret != 0) | 4004 | if (ret != 0) |
3604 | goto fail; | 4005 | goto fail; |
3605 | 4006 | ||
3606 | if (objectid > root->highest_inode) | ||
3607 | root->highest_inode = objectid; | ||
3608 | |||
3609 | inode->i_uid = current_fsuid(); | 4007 | inode->i_uid = current_fsuid(); |
3610 | 4008 | ||
3611 | if (dir && (dir->i_mode & S_ISGID)) { | 4009 | if (dir && (dir->i_mode & S_ISGID)) { |
@@ -3673,26 +4071,35 @@ int btrfs_add_link(struct btrfs_trans_handle *trans, | |||
3673 | struct inode *parent_inode, struct inode *inode, | 4071 | struct inode *parent_inode, struct inode *inode, |
3674 | const char *name, int name_len, int add_backref, u64 index) | 4072 | const char *name, int name_len, int add_backref, u64 index) |
3675 | { | 4073 | { |
3676 | int ret; | 4074 | int ret = 0; |
3677 | struct btrfs_key key; | 4075 | struct btrfs_key key; |
3678 | struct btrfs_root *root = BTRFS_I(parent_inode)->root; | 4076 | struct btrfs_root *root = BTRFS_I(parent_inode)->root; |
3679 | 4077 | ||
3680 | key.objectid = inode->i_ino; | 4078 | if (unlikely(inode->i_ino == BTRFS_FIRST_FREE_OBJECTID)) { |
3681 | btrfs_set_key_type(&key, BTRFS_INODE_ITEM_KEY); | 4079 | memcpy(&key, &BTRFS_I(inode)->root->root_key, sizeof(key)); |
3682 | key.offset = 0; | 4080 | } else { |
4081 | key.objectid = inode->i_ino; | ||
4082 | btrfs_set_key_type(&key, BTRFS_INODE_ITEM_KEY); | ||
4083 | key.offset = 0; | ||
4084 | } | ||
4085 | |||
4086 | if (unlikely(inode->i_ino == BTRFS_FIRST_FREE_OBJECTID)) { | ||
4087 | ret = btrfs_add_root_ref(trans, root->fs_info->tree_root, | ||
4088 | key.objectid, root->root_key.objectid, | ||
4089 | parent_inode->i_ino, | ||
4090 | index, name, name_len); | ||
4091 | } else if (add_backref) { | ||
4092 | ret = btrfs_insert_inode_ref(trans, root, | ||
4093 | name, name_len, inode->i_ino, | ||
4094 | parent_inode->i_ino, index); | ||
4095 | } | ||
3683 | 4096 | ||
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) { | 4097 | if (ret == 0) { |
3689 | if (add_backref) { | 4098 | ret = btrfs_insert_dir_item(trans, root, name, name_len, |
3690 | ret = btrfs_insert_inode_ref(trans, root, | 4099 | parent_inode->i_ino, &key, |
3691 | name, name_len, | 4100 | btrfs_inode_type(inode), index); |
3692 | inode->i_ino, | 4101 | BUG_ON(ret); |
3693 | parent_inode->i_ino, | 4102 | |
3694 | index); | ||
3695 | } | ||
3696 | btrfs_i_size_write(parent_inode, parent_inode->i_size + | 4103 | btrfs_i_size_write(parent_inode, parent_inode->i_size + |
3697 | name_len * 2); | 4104 | name_len * 2); |
3698 | parent_inode->i_mtime = parent_inode->i_ctime = CURRENT_TIME; | 4105 | parent_inode->i_mtime = parent_inode->i_ctime = CURRENT_TIME; |
@@ -3732,11 +4139,18 @@ static int btrfs_mknod(struct inode *dir, struct dentry *dentry, | |||
3732 | if (!new_valid_dev(rdev)) | 4139 | if (!new_valid_dev(rdev)) |
3733 | return -EINVAL; | 4140 | return -EINVAL; |
3734 | 4141 | ||
3735 | err = btrfs_check_metadata_free_space(root); | 4142 | /* |
4143 | * 2 for inode item and ref | ||
4144 | * 2 for dir items | ||
4145 | * 1 for xattr if selinux is on | ||
4146 | */ | ||
4147 | err = btrfs_reserve_metadata_space(root, 5); | ||
3736 | if (err) | 4148 | if (err) |
3737 | goto fail; | 4149 | return err; |
3738 | 4150 | ||
3739 | trans = btrfs_start_transaction(root, 1); | 4151 | trans = btrfs_start_transaction(root, 1); |
4152 | if (!trans) | ||
4153 | goto fail; | ||
3740 | btrfs_set_trans_block_group(trans, dir); | 4154 | btrfs_set_trans_block_group(trans, dir); |
3741 | 4155 | ||
3742 | err = btrfs_find_free_objectid(trans, root, dir->i_ino, &objectid); | 4156 | err = btrfs_find_free_objectid(trans, root, dir->i_ino, &objectid); |
@@ -3774,6 +4188,7 @@ out_unlock: | |||
3774 | nr = trans->blocks_used; | 4188 | nr = trans->blocks_used; |
3775 | btrfs_end_transaction_throttle(trans, root); | 4189 | btrfs_end_transaction_throttle(trans, root); |
3776 | fail: | 4190 | fail: |
4191 | btrfs_unreserve_metadata_space(root, 5); | ||
3777 | if (drop_inode) { | 4192 | if (drop_inode) { |
3778 | inode_dec_link_count(inode); | 4193 | inode_dec_link_count(inode); |
3779 | iput(inode); | 4194 | iput(inode); |
@@ -3794,10 +4209,18 @@ static int btrfs_create(struct inode *dir, struct dentry *dentry, | |||
3794 | u64 objectid; | 4209 | u64 objectid; |
3795 | u64 index = 0; | 4210 | u64 index = 0; |
3796 | 4211 | ||
3797 | err = btrfs_check_metadata_free_space(root); | 4212 | /* |
4213 | * 2 for inode item and ref | ||
4214 | * 2 for dir items | ||
4215 | * 1 for xattr if selinux is on | ||
4216 | */ | ||
4217 | err = btrfs_reserve_metadata_space(root, 5); | ||
3798 | if (err) | 4218 | if (err) |
3799 | goto fail; | 4219 | return err; |
4220 | |||
3800 | trans = btrfs_start_transaction(root, 1); | 4221 | trans = btrfs_start_transaction(root, 1); |
4222 | if (!trans) | ||
4223 | goto fail; | ||
3801 | btrfs_set_trans_block_group(trans, dir); | 4224 | btrfs_set_trans_block_group(trans, dir); |
3802 | 4225 | ||
3803 | err = btrfs_find_free_objectid(trans, root, dir->i_ino, &objectid); | 4226 | err = btrfs_find_free_objectid(trans, root, dir->i_ino, &objectid); |
@@ -3838,6 +4261,7 @@ out_unlock: | |||
3838 | nr = trans->blocks_used; | 4261 | nr = trans->blocks_used; |
3839 | btrfs_end_transaction_throttle(trans, root); | 4262 | btrfs_end_transaction_throttle(trans, root); |
3840 | fail: | 4263 | fail: |
4264 | btrfs_unreserve_metadata_space(root, 5); | ||
3841 | if (drop_inode) { | 4265 | if (drop_inode) { |
3842 | inode_dec_link_count(inode); | 4266 | inode_dec_link_count(inode); |
3843 | iput(inode); | 4267 | iput(inode); |
@@ -3860,10 +4284,16 @@ static int btrfs_link(struct dentry *old_dentry, struct inode *dir, | |||
3860 | if (inode->i_nlink == 0) | 4284 | if (inode->i_nlink == 0) |
3861 | return -ENOENT; | 4285 | return -ENOENT; |
3862 | 4286 | ||
3863 | btrfs_inc_nlink(inode); | 4287 | /* |
3864 | err = btrfs_check_metadata_free_space(root); | 4288 | * 1 item for inode ref |
4289 | * 2 items for dir items | ||
4290 | */ | ||
4291 | err = btrfs_reserve_metadata_space(root, 3); | ||
3865 | if (err) | 4292 | if (err) |
3866 | goto fail; | 4293 | return err; |
4294 | |||
4295 | btrfs_inc_nlink(inode); | ||
4296 | |||
3867 | err = btrfs_set_inode_index(dir, &index); | 4297 | err = btrfs_set_inode_index(dir, &index); |
3868 | if (err) | 4298 | if (err) |
3869 | goto fail; | 4299 | goto fail; |
@@ -3875,20 +4305,19 @@ static int btrfs_link(struct dentry *old_dentry, struct inode *dir, | |||
3875 | 4305 | ||
3876 | err = btrfs_add_nondir(trans, dentry, inode, 1, index); | 4306 | err = btrfs_add_nondir(trans, dentry, inode, 1, index); |
3877 | 4307 | ||
3878 | if (err) | 4308 | 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; | 4309 | drop_inode = 1; |
4310 | } else { | ||
4311 | btrfs_update_inode_block_group(trans, dir); | ||
4312 | err = btrfs_update_inode(trans, root, inode); | ||
4313 | BUG_ON(err); | ||
4314 | btrfs_log_new_name(trans, inode, NULL, dentry->d_parent); | ||
4315 | } | ||
3886 | 4316 | ||
3887 | nr = trans->blocks_used; | 4317 | nr = trans->blocks_used; |
3888 | |||
3889 | btrfs_log_new_name(trans, inode, NULL, dentry->d_parent); | ||
3890 | btrfs_end_transaction_throttle(trans, root); | 4318 | btrfs_end_transaction_throttle(trans, root); |
3891 | fail: | 4319 | fail: |
4320 | btrfs_unreserve_metadata_space(root, 3); | ||
3892 | if (drop_inode) { | 4321 | if (drop_inode) { |
3893 | inode_dec_link_count(inode); | 4322 | inode_dec_link_count(inode); |
3894 | iput(inode); | 4323 | iput(inode); |
@@ -3908,17 +4337,21 @@ static int btrfs_mkdir(struct inode *dir, struct dentry *dentry, int mode) | |||
3908 | u64 index = 0; | 4337 | u64 index = 0; |
3909 | unsigned long nr = 1; | 4338 | unsigned long nr = 1; |
3910 | 4339 | ||
3911 | err = btrfs_check_metadata_free_space(root); | 4340 | /* |
4341 | * 2 items for inode and ref | ||
4342 | * 2 items for dir items | ||
4343 | * 1 for xattr if selinux is on | ||
4344 | */ | ||
4345 | err = btrfs_reserve_metadata_space(root, 5); | ||
3912 | if (err) | 4346 | if (err) |
3913 | goto out_unlock; | 4347 | return err; |
3914 | 4348 | ||
3915 | trans = btrfs_start_transaction(root, 1); | 4349 | trans = btrfs_start_transaction(root, 1); |
3916 | btrfs_set_trans_block_group(trans, dir); | 4350 | if (!trans) { |
3917 | 4351 | err = -ENOMEM; | |
3918 | if (IS_ERR(trans)) { | ||
3919 | err = PTR_ERR(trans); | ||
3920 | goto out_unlock; | 4352 | goto out_unlock; |
3921 | } | 4353 | } |
4354 | btrfs_set_trans_block_group(trans, dir); | ||
3922 | 4355 | ||
3923 | err = btrfs_find_free_objectid(trans, root, dir->i_ino, &objectid); | 4356 | err = btrfs_find_free_objectid(trans, root, dir->i_ino, &objectid); |
3924 | if (err) { | 4357 | if (err) { |
@@ -3967,6 +4400,7 @@ out_fail: | |||
3967 | btrfs_end_transaction_throttle(trans, root); | 4400 | btrfs_end_transaction_throttle(trans, root); |
3968 | 4401 | ||
3969 | out_unlock: | 4402 | out_unlock: |
4403 | btrfs_unreserve_metadata_space(root, 5); | ||
3970 | if (drop_on_err) | 4404 | if (drop_on_err) |
3971 | iput(inode); | 4405 | iput(inode); |
3972 | btrfs_btree_balance_dirty(root, nr); | 4406 | btrfs_btree_balance_dirty(root, nr); |
@@ -4064,11 +4498,11 @@ struct extent_map *btrfs_get_extent(struct inode *inode, struct page *page, | |||
4064 | int compressed; | 4498 | int compressed; |
4065 | 4499 | ||
4066 | again: | 4500 | again: |
4067 | spin_lock(&em_tree->lock); | 4501 | read_lock(&em_tree->lock); |
4068 | em = lookup_extent_mapping(em_tree, start, len); | 4502 | em = lookup_extent_mapping(em_tree, start, len); |
4069 | if (em) | 4503 | if (em) |
4070 | em->bdev = root->fs_info->fs_devices->latest_bdev; | 4504 | em->bdev = root->fs_info->fs_devices->latest_bdev; |
4071 | spin_unlock(&em_tree->lock); | 4505 | read_unlock(&em_tree->lock); |
4072 | 4506 | ||
4073 | if (em) { | 4507 | if (em) { |
4074 | if (em->start > start || em->start + em->len <= start) | 4508 | if (em->start > start || em->start + em->len <= start) |
@@ -4215,6 +4649,11 @@ again: | |||
4215 | map = kmap(page); | 4649 | map = kmap(page); |
4216 | read_extent_buffer(leaf, map + pg_offset, ptr, | 4650 | read_extent_buffer(leaf, map + pg_offset, ptr, |
4217 | copy_size); | 4651 | copy_size); |
4652 | if (pg_offset + copy_size < PAGE_CACHE_SIZE) { | ||
4653 | memset(map + pg_offset + copy_size, 0, | ||
4654 | PAGE_CACHE_SIZE - pg_offset - | ||
4655 | copy_size); | ||
4656 | } | ||
4218 | kunmap(page); | 4657 | kunmap(page); |
4219 | } | 4658 | } |
4220 | flush_dcache_page(page); | 4659 | flush_dcache_page(page); |
@@ -4259,7 +4698,7 @@ insert: | |||
4259 | } | 4698 | } |
4260 | 4699 | ||
4261 | err = 0; | 4700 | err = 0; |
4262 | spin_lock(&em_tree->lock); | 4701 | write_lock(&em_tree->lock); |
4263 | ret = add_extent_mapping(em_tree, em); | 4702 | ret = add_extent_mapping(em_tree, em); |
4264 | /* it is possible that someone inserted the extent into the tree | 4703 | /* it is possible that someone inserted the extent into the tree |
4265 | * while we had the lock dropped. It is also possible that | 4704 | * while we had the lock dropped. It is also possible that |
@@ -4299,7 +4738,7 @@ insert: | |||
4299 | err = 0; | 4738 | err = 0; |
4300 | } | 4739 | } |
4301 | } | 4740 | } |
4302 | spin_unlock(&em_tree->lock); | 4741 | write_unlock(&em_tree->lock); |
4303 | out: | 4742 | out: |
4304 | if (path) | 4743 | if (path) |
4305 | btrfs_free_path(path); | 4744 | btrfs_free_path(path); |
@@ -4398,13 +4837,21 @@ static void btrfs_invalidatepage(struct page *page, unsigned long offset) | |||
4398 | u64 page_start = page_offset(page); | 4837 | u64 page_start = page_offset(page); |
4399 | u64 page_end = page_start + PAGE_CACHE_SIZE - 1; | 4838 | u64 page_end = page_start + PAGE_CACHE_SIZE - 1; |
4400 | 4839 | ||
4840 | |||
4841 | /* | ||
4842 | * we have the page locked, so new writeback can't start, | ||
4843 | * and the dirty bit won't be cleared while we are here. | ||
4844 | * | ||
4845 | * Wait for IO on this page so that we can safely clear | ||
4846 | * the PagePrivate2 bit and do ordered accounting | ||
4847 | */ | ||
4401 | wait_on_page_writeback(page); | 4848 | wait_on_page_writeback(page); |
4849 | |||
4402 | tree = &BTRFS_I(page->mapping->host)->io_tree; | 4850 | tree = &BTRFS_I(page->mapping->host)->io_tree; |
4403 | if (offset) { | 4851 | if (offset) { |
4404 | btrfs_releasepage(page, GFP_NOFS); | 4852 | btrfs_releasepage(page, GFP_NOFS); |
4405 | return; | 4853 | return; |
4406 | } | 4854 | } |
4407 | |||
4408 | lock_extent(tree, page_start, page_end, GFP_NOFS); | 4855 | lock_extent(tree, page_start, page_end, GFP_NOFS); |
4409 | ordered = btrfs_lookup_ordered_extent(page->mapping->host, | 4856 | ordered = btrfs_lookup_ordered_extent(page->mapping->host, |
4410 | page_offset(page)); | 4857 | page_offset(page)); |
@@ -4415,16 +4862,22 @@ static void btrfs_invalidatepage(struct page *page, unsigned long offset) | |||
4415 | */ | 4862 | */ |
4416 | clear_extent_bit(tree, page_start, page_end, | 4863 | clear_extent_bit(tree, page_start, page_end, |
4417 | EXTENT_DIRTY | EXTENT_DELALLOC | | 4864 | EXTENT_DIRTY | EXTENT_DELALLOC | |
4418 | EXTENT_LOCKED, 1, 0, GFP_NOFS); | 4865 | EXTENT_LOCKED | EXTENT_DO_ACCOUNTING, 1, 0, |
4419 | btrfs_finish_ordered_io(page->mapping->host, | 4866 | NULL, GFP_NOFS); |
4420 | page_start, page_end); | 4867 | /* |
4868 | * whoever cleared the private bit is responsible | ||
4869 | * for the finish_ordered_io | ||
4870 | */ | ||
4871 | if (TestClearPagePrivate2(page)) { | ||
4872 | btrfs_finish_ordered_io(page->mapping->host, | ||
4873 | page_start, page_end); | ||
4874 | } | ||
4421 | btrfs_put_ordered_extent(ordered); | 4875 | btrfs_put_ordered_extent(ordered); |
4422 | lock_extent(tree, page_start, page_end, GFP_NOFS); | 4876 | lock_extent(tree, page_start, page_end, GFP_NOFS); |
4423 | } | 4877 | } |
4424 | clear_extent_bit(tree, page_start, page_end, | 4878 | clear_extent_bit(tree, page_start, page_end, |
4425 | EXTENT_LOCKED | EXTENT_DIRTY | EXTENT_DELALLOC | | 4879 | EXTENT_LOCKED | EXTENT_DIRTY | EXTENT_DELALLOC | |
4426 | EXTENT_ORDERED, | 4880 | EXTENT_DO_ACCOUNTING, 1, 1, NULL, GFP_NOFS); |
4427 | 1, 1, GFP_NOFS); | ||
4428 | __btrfs_releasepage(page, GFP_NOFS); | 4881 | __btrfs_releasepage(page, GFP_NOFS); |
4429 | 4882 | ||
4430 | ClearPageChecked(page); | 4883 | ClearPageChecked(page); |
@@ -4473,6 +4926,13 @@ int btrfs_page_mkwrite(struct vm_area_struct *vma, struct vm_fault *vmf) | |||
4473 | goto out; | 4926 | goto out; |
4474 | } | 4927 | } |
4475 | 4928 | ||
4929 | ret = btrfs_reserve_metadata_for_delalloc(root, inode, 1); | ||
4930 | if (ret) { | ||
4931 | btrfs_free_reserved_data_space(root, inode, PAGE_CACHE_SIZE); | ||
4932 | ret = VM_FAULT_SIGBUS; | ||
4933 | goto out; | ||
4934 | } | ||
4935 | |||
4476 | ret = VM_FAULT_NOPAGE; /* make the VM retry the fault */ | 4936 | ret = VM_FAULT_NOPAGE; /* make the VM retry the fault */ |
4477 | again: | 4937 | again: |
4478 | lock_page(page); | 4938 | lock_page(page); |
@@ -4504,7 +4964,24 @@ again: | |||
4504 | goto again; | 4964 | goto again; |
4505 | } | 4965 | } |
4506 | 4966 | ||
4507 | btrfs_set_extent_delalloc(inode, page_start, page_end); | 4967 | /* |
4968 | * XXX - page_mkwrite gets called every time the page is dirtied, even | ||
4969 | * if it was already dirty, so for space accounting reasons we need to | ||
4970 | * clear any delalloc bits for the range we are fixing to save. There | ||
4971 | * is probably a better way to do this, but for now keep consistent with | ||
4972 | * prepare_pages in the normal write path. | ||
4973 | */ | ||
4974 | clear_extent_bits(&BTRFS_I(inode)->io_tree, page_start, page_end, | ||
4975 | EXTENT_DIRTY | EXTENT_DELALLOC | EXTENT_DO_ACCOUNTING, | ||
4976 | GFP_NOFS); | ||
4977 | |||
4978 | ret = btrfs_set_extent_delalloc(inode, page_start, page_end); | ||
4979 | if (ret) { | ||
4980 | unlock_extent(io_tree, page_start, page_end, GFP_NOFS); | ||
4981 | ret = VM_FAULT_SIGBUS; | ||
4982 | btrfs_free_reserved_data_space(root, inode, PAGE_CACHE_SIZE); | ||
4983 | goto out_unlock; | ||
4984 | } | ||
4508 | ret = 0; | 4985 | ret = 0; |
4509 | 4986 | ||
4510 | /* page is wholly or partially inside EOF */ | 4987 | /* page is wholly or partially inside EOF */ |
@@ -4521,11 +4998,17 @@ again: | |||
4521 | } | 4998 | } |
4522 | ClearPageChecked(page); | 4999 | ClearPageChecked(page); |
4523 | set_page_dirty(page); | 5000 | set_page_dirty(page); |
5001 | SetPageUptodate(page); | ||
5002 | |||
5003 | BTRFS_I(inode)->last_trans = root->fs_info->generation; | ||
5004 | BTRFS_I(inode)->last_sub_trans = BTRFS_I(inode)->root->log_transid; | ||
4524 | 5005 | ||
4525 | BTRFS_I(inode)->last_trans = root->fs_info->generation + 1; | ||
4526 | unlock_extent(io_tree, page_start, page_end, GFP_NOFS); | 5006 | unlock_extent(io_tree, page_start, page_end, GFP_NOFS); |
4527 | 5007 | ||
4528 | out_unlock: | 5008 | out_unlock: |
5009 | btrfs_unreserve_metadata_for_delalloc(root, inode, 1); | ||
5010 | if (!ret) | ||
5011 | return VM_FAULT_LOCKED; | ||
4529 | unlock_page(page); | 5012 | unlock_page(page); |
4530 | out: | 5013 | out: |
4531 | return ret; | 5014 | return ret; |
@@ -4544,7 +5027,9 @@ static void btrfs_truncate(struct inode *inode) | |||
4544 | if (IS_APPEND(inode) || IS_IMMUTABLE(inode)) | 5027 | if (IS_APPEND(inode) || IS_IMMUTABLE(inode)) |
4545 | return; | 5028 | return; |
4546 | 5029 | ||
4547 | btrfs_truncate_page(inode->i_mapping, inode->i_size); | 5030 | ret = btrfs_truncate_page(inode->i_mapping, inode->i_size); |
5031 | if (ret) | ||
5032 | return; | ||
4548 | btrfs_wait_ordered_range(inode, inode->i_size & (~mask), (u64)-1); | 5033 | btrfs_wait_ordered_range(inode, inode->i_size & (~mask), (u64)-1); |
4549 | 5034 | ||
4550 | trans = btrfs_start_transaction(root, 1); | 5035 | trans = btrfs_start_transaction(root, 1); |
@@ -4594,11 +5079,11 @@ out: | |||
4594 | * create a new subvolume directory/inode (helper for the ioctl). | 5079 | * create a new subvolume directory/inode (helper for the ioctl). |
4595 | */ | 5080 | */ |
4596 | int btrfs_create_subvol_root(struct btrfs_trans_handle *trans, | 5081 | int btrfs_create_subvol_root(struct btrfs_trans_handle *trans, |
4597 | struct btrfs_root *new_root, struct dentry *dentry, | 5082 | struct btrfs_root *new_root, |
4598 | u64 new_dirid, u64 alloc_hint) | 5083 | u64 new_dirid, u64 alloc_hint) |
4599 | { | 5084 | { |
4600 | struct inode *inode; | 5085 | struct inode *inode; |
4601 | int error; | 5086 | int err; |
4602 | u64 index = 0; | 5087 | u64 index = 0; |
4603 | 5088 | ||
4604 | inode = btrfs_new_inode(trans, new_root, NULL, "..", 2, new_dirid, | 5089 | inode = btrfs_new_inode(trans, new_root, NULL, "..", 2, new_dirid, |
@@ -4611,11 +5096,10 @@ int btrfs_create_subvol_root(struct btrfs_trans_handle *trans, | |||
4611 | inode->i_nlink = 1; | 5096 | inode->i_nlink = 1; |
4612 | btrfs_i_size_write(inode, 0); | 5097 | btrfs_i_size_write(inode, 0); |
4613 | 5098 | ||
4614 | error = btrfs_update_inode(trans, new_root, inode); | 5099 | err = btrfs_update_inode(trans, new_root, inode); |
4615 | if (error) | 5100 | BUG_ON(err); |
4616 | return error; | ||
4617 | 5101 | ||
4618 | d_instantiate(dentry, inode); | 5102 | iput(inode); |
4619 | return 0; | 5103 | return 0; |
4620 | } | 5104 | } |
4621 | 5105 | ||
@@ -4640,7 +5124,11 @@ struct inode *btrfs_alloc_inode(struct super_block *sb) | |||
4640 | if (!ei) | 5124 | if (!ei) |
4641 | return NULL; | 5125 | return NULL; |
4642 | ei->last_trans = 0; | 5126 | ei->last_trans = 0; |
5127 | ei->last_sub_trans = 0; | ||
4643 | ei->logged_trans = 0; | 5128 | ei->logged_trans = 0; |
5129 | ei->outstanding_extents = 0; | ||
5130 | ei->reserved_extents = 0; | ||
5131 | spin_lock_init(&ei->accounting_lock); | ||
4644 | btrfs_ordered_inode_tree_init(&ei->ordered_tree); | 5132 | btrfs_ordered_inode_tree_init(&ei->ordered_tree); |
4645 | INIT_LIST_HEAD(&ei->i_orphan); | 5133 | INIT_LIST_HEAD(&ei->i_orphan); |
4646 | INIT_LIST_HEAD(&ei->ordered_operations); | 5134 | INIT_LIST_HEAD(&ei->ordered_operations); |
@@ -4693,6 +5181,16 @@ void btrfs_destroy_inode(struct inode *inode) | |||
4693 | kmem_cache_free(btrfs_inode_cachep, BTRFS_I(inode)); | 5181 | kmem_cache_free(btrfs_inode_cachep, BTRFS_I(inode)); |
4694 | } | 5182 | } |
4695 | 5183 | ||
5184 | void btrfs_drop_inode(struct inode *inode) | ||
5185 | { | ||
5186 | struct btrfs_root *root = BTRFS_I(inode)->root; | ||
5187 | |||
5188 | if (inode->i_nlink > 0 && btrfs_root_refs(&root->root_item) == 0) | ||
5189 | generic_delete_inode(inode); | ||
5190 | else | ||
5191 | generic_drop_inode(inode); | ||
5192 | } | ||
5193 | |||
4696 | static void init_once(void *foo) | 5194 | static void init_once(void *foo) |
4697 | { | 5195 | { |
4698 | struct btrfs_inode *ei = (struct btrfs_inode *) foo; | 5196 | struct btrfs_inode *ei = (struct btrfs_inode *) foo; |
@@ -4761,31 +5259,37 @@ static int btrfs_rename(struct inode *old_dir, struct dentry *old_dentry, | |||
4761 | { | 5259 | { |
4762 | struct btrfs_trans_handle *trans; | 5260 | struct btrfs_trans_handle *trans; |
4763 | struct btrfs_root *root = BTRFS_I(old_dir)->root; | 5261 | struct btrfs_root *root = BTRFS_I(old_dir)->root; |
5262 | struct btrfs_root *dest = BTRFS_I(new_dir)->root; | ||
4764 | struct inode *new_inode = new_dentry->d_inode; | 5263 | struct inode *new_inode = new_dentry->d_inode; |
4765 | struct inode *old_inode = old_dentry->d_inode; | 5264 | struct inode *old_inode = old_dentry->d_inode; |
4766 | struct timespec ctime = CURRENT_TIME; | 5265 | struct timespec ctime = CURRENT_TIME; |
4767 | u64 index = 0; | 5266 | u64 index = 0; |
5267 | u64 root_objectid; | ||
4768 | int ret; | 5268 | int ret; |
4769 | 5269 | ||
4770 | /* we're not allowed to rename between subvolumes */ | 5270 | if (new_dir->i_ino == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID) |
4771 | if (BTRFS_I(old_inode)->root->root_key.objectid != | 5271 | return -EPERM; |
4772 | BTRFS_I(new_dir)->root->root_key.objectid) | 5272 | |
5273 | /* we only allow rename subvolume link between subvolumes */ | ||
5274 | if (old_inode->i_ino != BTRFS_FIRST_FREE_OBJECTID && root != dest) | ||
4773 | return -EXDEV; | 5275 | return -EXDEV; |
4774 | 5276 | ||
5277 | if (old_inode->i_ino == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID || | ||
5278 | (new_inode && new_inode->i_ino == BTRFS_FIRST_FREE_OBJECTID)) | ||
5279 | return -ENOTEMPTY; | ||
5280 | |||
4775 | if (S_ISDIR(old_inode->i_mode) && new_inode && | 5281 | if (S_ISDIR(old_inode->i_mode) && new_inode && |
4776 | new_inode->i_size > BTRFS_EMPTY_DIR_SIZE) { | 5282 | new_inode->i_size > BTRFS_EMPTY_DIR_SIZE) |
4777 | return -ENOTEMPTY; | 5283 | return -ENOTEMPTY; |
4778 | } | ||
4779 | 5284 | ||
4780 | /* to rename a snapshot or subvolume, we need to juggle the | 5285 | /* |
4781 | * backrefs. This isn't coded yet | 5286 | * 2 items for dir items |
5287 | * 1 item for orphan entry | ||
5288 | * 1 item for ref | ||
4782 | */ | 5289 | */ |
4783 | if (old_inode->i_ino == BTRFS_FIRST_FREE_OBJECTID) | 5290 | ret = btrfs_reserve_metadata_space(root, 4); |
4784 | return -EXDEV; | ||
4785 | |||
4786 | ret = btrfs_check_metadata_free_space(root); | ||
4787 | if (ret) | 5291 | if (ret) |
4788 | goto out_unlock; | 5292 | return ret; |
4789 | 5293 | ||
4790 | /* | 5294 | /* |
4791 | * we're using rename to replace one file with another. | 5295 | * we're using rename to replace one file with another. |
@@ -4796,8 +5300,40 @@ static int btrfs_rename(struct inode *old_dir, struct dentry *old_dentry, | |||
4796 | old_inode->i_size > BTRFS_ORDERED_OPERATIONS_FLUSH_LIMIT) | 5300 | old_inode->i_size > BTRFS_ORDERED_OPERATIONS_FLUSH_LIMIT) |
4797 | filemap_flush(old_inode->i_mapping); | 5301 | filemap_flush(old_inode->i_mapping); |
4798 | 5302 | ||
5303 | /* close the racy window with snapshot create/destroy ioctl */ | ||
5304 | if (old_inode->i_ino == BTRFS_FIRST_FREE_OBJECTID) | ||
5305 | down_read(&root->fs_info->subvol_sem); | ||
5306 | |||
4799 | trans = btrfs_start_transaction(root, 1); | 5307 | trans = btrfs_start_transaction(root, 1); |
5308 | btrfs_set_trans_block_group(trans, new_dir); | ||
5309 | |||
5310 | if (dest != root) | ||
5311 | btrfs_record_root_in_trans(trans, dest); | ||
4800 | 5312 | ||
5313 | ret = btrfs_set_inode_index(new_dir, &index); | ||
5314 | if (ret) | ||
5315 | goto out_fail; | ||
5316 | |||
5317 | if (unlikely(old_inode->i_ino == BTRFS_FIRST_FREE_OBJECTID)) { | ||
5318 | /* force full log commit if subvolume involved. */ | ||
5319 | root->fs_info->last_trans_log_full_commit = trans->transid; | ||
5320 | } else { | ||
5321 | ret = btrfs_insert_inode_ref(trans, dest, | ||
5322 | new_dentry->d_name.name, | ||
5323 | new_dentry->d_name.len, | ||
5324 | old_inode->i_ino, | ||
5325 | new_dir->i_ino, index); | ||
5326 | if (ret) | ||
5327 | goto out_fail; | ||
5328 | /* | ||
5329 | * this is an ugly little race, but the rename is required | ||
5330 | * to make sure that if we crash, the inode is either at the | ||
5331 | * old name or the new one. pinning the log transaction lets | ||
5332 | * us make sure we don't allow a log commit to come in after | ||
5333 | * we unlink the name but before we add the new name back in. | ||
5334 | */ | ||
5335 | btrfs_pin_log_trans(root); | ||
5336 | } | ||
4801 | /* | 5337 | /* |
4802 | * make sure the inode gets flushed if it is replacing | 5338 | * make sure the inode gets flushed if it is replacing |
4803 | * something. | 5339 | * something. |
@@ -4807,18 +5343,6 @@ static int btrfs_rename(struct inode *old_dir, struct dentry *old_dentry, | |||
4807 | btrfs_add_ordered_operation(trans, root, old_inode); | 5343 | btrfs_add_ordered_operation(trans, root, old_inode); |
4808 | } | 5344 | } |
4809 | 5345 | ||
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; | 5346 | old_dir->i_ctime = old_dir->i_mtime = ctime; |
4823 | new_dir->i_ctime = new_dir->i_mtime = ctime; | 5347 | new_dir->i_ctime = new_dir->i_mtime = ctime; |
4824 | old_inode->i_ctime = ctime; | 5348 | old_inode->i_ctime = ctime; |
@@ -4826,47 +5350,60 @@ static int btrfs_rename(struct inode *old_dir, struct dentry *old_dentry, | |||
4826 | if (old_dentry->d_parent != new_dentry->d_parent) | 5350 | if (old_dentry->d_parent != new_dentry->d_parent) |
4827 | btrfs_record_unlink_dir(trans, old_dir, old_inode, 1); | 5351 | btrfs_record_unlink_dir(trans, old_dir, old_inode, 1); |
4828 | 5352 | ||
4829 | ret = btrfs_unlink_inode(trans, root, old_dir, old_dentry->d_inode, | 5353 | if (unlikely(old_inode->i_ino == BTRFS_FIRST_FREE_OBJECTID)) { |
4830 | old_dentry->d_name.name, | 5354 | root_objectid = BTRFS_I(old_inode)->root->root_key.objectid; |
4831 | old_dentry->d_name.len); | 5355 | ret = btrfs_unlink_subvol(trans, root, old_dir, root_objectid, |
4832 | if (ret) | 5356 | old_dentry->d_name.name, |
4833 | goto out_fail; | 5357 | old_dentry->d_name.len); |
5358 | } else { | ||
5359 | btrfs_inc_nlink(old_dentry->d_inode); | ||
5360 | ret = btrfs_unlink_inode(trans, root, old_dir, | ||
5361 | old_dentry->d_inode, | ||
5362 | old_dentry->d_name.name, | ||
5363 | old_dentry->d_name.len); | ||
5364 | } | ||
5365 | BUG_ON(ret); | ||
4834 | 5366 | ||
4835 | if (new_inode) { | 5367 | if (new_inode) { |
4836 | new_inode->i_ctime = CURRENT_TIME; | 5368 | new_inode->i_ctime = CURRENT_TIME; |
4837 | ret = btrfs_unlink_inode(trans, root, new_dir, | 5369 | if (unlikely(new_inode->i_ino == |
4838 | new_dentry->d_inode, | 5370 | BTRFS_EMPTY_SUBVOL_DIR_OBJECTID)) { |
4839 | new_dentry->d_name.name, | 5371 | root_objectid = BTRFS_I(new_inode)->location.objectid; |
4840 | new_dentry->d_name.len); | 5372 | ret = btrfs_unlink_subvol(trans, dest, new_dir, |
4841 | if (ret) | 5373 | root_objectid, |
4842 | goto out_fail; | 5374 | new_dentry->d_name.name, |
5375 | new_dentry->d_name.len); | ||
5376 | BUG_ON(new_inode->i_nlink == 0); | ||
5377 | } else { | ||
5378 | ret = btrfs_unlink_inode(trans, dest, new_dir, | ||
5379 | new_dentry->d_inode, | ||
5380 | new_dentry->d_name.name, | ||
5381 | new_dentry->d_name.len); | ||
5382 | } | ||
5383 | BUG_ON(ret); | ||
4843 | if (new_inode->i_nlink == 0) { | 5384 | if (new_inode->i_nlink == 0) { |
4844 | ret = btrfs_orphan_add(trans, new_dentry->d_inode); | 5385 | ret = btrfs_orphan_add(trans, new_dentry->d_inode); |
4845 | if (ret) | 5386 | BUG_ON(ret); |
4846 | goto out_fail; | ||
4847 | } | 5387 | } |
4848 | |||
4849 | } | 5388 | } |
4850 | ret = btrfs_set_inode_index(new_dir, &index); | ||
4851 | if (ret) | ||
4852 | goto out_fail; | ||
4853 | 5389 | ||
4854 | ret = btrfs_add_link(trans, new_dentry->d_parent->d_inode, | 5390 | ret = btrfs_add_link(trans, new_dir, old_inode, |
4855 | old_inode, new_dentry->d_name.name, | 5391 | new_dentry->d_name.name, |
4856 | new_dentry->d_name.len, 1, index); | 5392 | new_dentry->d_name.len, 0, index); |
4857 | if (ret) | 5393 | BUG_ON(ret); |
4858 | goto out_fail; | ||
4859 | 5394 | ||
4860 | btrfs_log_new_name(trans, old_inode, old_dir, | 5395 | if (old_inode->i_ino != BTRFS_FIRST_FREE_OBJECTID) { |
4861 | new_dentry->d_parent); | 5396 | btrfs_log_new_name(trans, old_inode, old_dir, |
5397 | new_dentry->d_parent); | ||
5398 | btrfs_end_log_trans(root); | ||
5399 | } | ||
4862 | out_fail: | 5400 | out_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); | 5401 | btrfs_end_transaction_throttle(trans, root); |
4869 | out_unlock: | 5402 | |
5403 | if (old_inode->i_ino == BTRFS_FIRST_FREE_OBJECTID) | ||
5404 | up_read(&root->fs_info->subvol_sem); | ||
5405 | |||
5406 | btrfs_unreserve_metadata_space(root, 4); | ||
4870 | return ret; | 5407 | return ret; |
4871 | } | 5408 | } |
4872 | 5409 | ||
@@ -4938,11 +5475,18 @@ static int btrfs_symlink(struct inode *dir, struct dentry *dentry, | |||
4938 | if (name_len > BTRFS_MAX_INLINE_DATA_SIZE(root)) | 5475 | if (name_len > BTRFS_MAX_INLINE_DATA_SIZE(root)) |
4939 | return -ENAMETOOLONG; | 5476 | return -ENAMETOOLONG; |
4940 | 5477 | ||
4941 | err = btrfs_check_metadata_free_space(root); | 5478 | /* |
5479 | * 2 items for inode item and ref | ||
5480 | * 2 items for dir items | ||
5481 | * 1 item for xattr if selinux is on | ||
5482 | */ | ||
5483 | err = btrfs_reserve_metadata_space(root, 5); | ||
4942 | if (err) | 5484 | if (err) |
4943 | goto out_fail; | 5485 | return err; |
4944 | 5486 | ||
4945 | trans = btrfs_start_transaction(root, 1); | 5487 | trans = btrfs_start_transaction(root, 1); |
5488 | if (!trans) | ||
5489 | goto out_fail; | ||
4946 | btrfs_set_trans_block_group(trans, dir); | 5490 | btrfs_set_trans_block_group(trans, dir); |
4947 | 5491 | ||
4948 | err = btrfs_find_free_objectid(trans, root, dir->i_ino, &objectid); | 5492 | err = btrfs_find_free_objectid(trans, root, dir->i_ino, &objectid); |
@@ -5023,6 +5567,7 @@ out_unlock: | |||
5023 | nr = trans->blocks_used; | 5567 | nr = trans->blocks_used; |
5024 | btrfs_end_transaction_throttle(trans, root); | 5568 | btrfs_end_transaction_throttle(trans, root); |
5025 | out_fail: | 5569 | out_fail: |
5570 | btrfs_unreserve_metadata_space(root, 5); | ||
5026 | if (drop_inode) { | 5571 | if (drop_inode) { |
5027 | inode_dec_link_count(inode); | 5572 | inode_dec_link_count(inode); |
5028 | iput(inode); | 5573 | iput(inode); |
@@ -5044,6 +5589,11 @@ static int prealloc_file_range(struct btrfs_trans_handle *trans, | |||
5044 | 5589 | ||
5045 | while (num_bytes > 0) { | 5590 | while (num_bytes > 0) { |
5046 | alloc_size = min(num_bytes, root->fs_info->max_extent); | 5591 | alloc_size = min(num_bytes, root->fs_info->max_extent); |
5592 | |||
5593 | ret = btrfs_reserve_metadata_space(root, 1); | ||
5594 | if (ret) | ||
5595 | goto out; | ||
5596 | |||
5047 | ret = btrfs_reserve_extent(trans, root, alloc_size, | 5597 | ret = btrfs_reserve_extent(trans, root, alloc_size, |
5048 | root->sectorsize, 0, alloc_hint, | 5598 | root->sectorsize, 0, alloc_hint, |
5049 | (u64)-1, &ins, 1); | 5599 | (u64)-1, &ins, 1); |
@@ -5058,9 +5608,12 @@ static int prealloc_file_range(struct btrfs_trans_handle *trans, | |||
5058 | 0, 0, 0, | 5608 | 0, 0, 0, |
5059 | BTRFS_FILE_EXTENT_PREALLOC); | 5609 | BTRFS_FILE_EXTENT_PREALLOC); |
5060 | BUG_ON(ret); | 5610 | BUG_ON(ret); |
5611 | btrfs_drop_extent_cache(inode, cur_offset, | ||
5612 | cur_offset + ins.offset -1, 0); | ||
5061 | num_bytes -= ins.offset; | 5613 | num_bytes -= ins.offset; |
5062 | cur_offset += ins.offset; | 5614 | cur_offset += ins.offset; |
5063 | alloc_hint = ins.objectid + ins.offset; | 5615 | alloc_hint = ins.objectid + ins.offset; |
5616 | btrfs_unreserve_metadata_space(root, 1); | ||
5064 | } | 5617 | } |
5065 | out: | 5618 | out: |
5066 | if (cur_offset > start) { | 5619 | if (cur_offset > start) { |
@@ -5201,7 +5754,7 @@ static int btrfs_permission(struct inode *inode, int mask) | |||
5201 | return generic_permission(inode, mask, btrfs_check_acl); | 5754 | return generic_permission(inode, mask, btrfs_check_acl); |
5202 | } | 5755 | } |
5203 | 5756 | ||
5204 | static struct inode_operations btrfs_dir_inode_operations = { | 5757 | static const struct inode_operations btrfs_dir_inode_operations = { |
5205 | .getattr = btrfs_getattr, | 5758 | .getattr = btrfs_getattr, |
5206 | .lookup = btrfs_lookup, | 5759 | .lookup = btrfs_lookup, |
5207 | .create = btrfs_create, | 5760 | .create = btrfs_create, |
@@ -5219,11 +5772,12 @@ static struct inode_operations btrfs_dir_inode_operations = { | |||
5219 | .removexattr = btrfs_removexattr, | 5772 | .removexattr = btrfs_removexattr, |
5220 | .permission = btrfs_permission, | 5773 | .permission = btrfs_permission, |
5221 | }; | 5774 | }; |
5222 | static struct inode_operations btrfs_dir_ro_inode_operations = { | 5775 | static const struct inode_operations btrfs_dir_ro_inode_operations = { |
5223 | .lookup = btrfs_lookup, | 5776 | .lookup = btrfs_lookup, |
5224 | .permission = btrfs_permission, | 5777 | .permission = btrfs_permission, |
5225 | }; | 5778 | }; |
5226 | static struct file_operations btrfs_dir_file_operations = { | 5779 | |
5780 | static const struct file_operations btrfs_dir_file_operations = { | ||
5227 | .llseek = generic_file_llseek, | 5781 | .llseek = generic_file_llseek, |
5228 | .read = generic_read_dir, | 5782 | .read = generic_read_dir, |
5229 | .readdir = btrfs_real_readdir, | 5783 | .readdir = btrfs_real_readdir, |
@@ -5245,6 +5799,8 @@ static struct extent_io_ops btrfs_extent_io_ops = { | |||
5245 | .readpage_io_failed_hook = btrfs_io_failed_hook, | 5799 | .readpage_io_failed_hook = btrfs_io_failed_hook, |
5246 | .set_bit_hook = btrfs_set_bit_hook, | 5800 | .set_bit_hook = btrfs_set_bit_hook, |
5247 | .clear_bit_hook = btrfs_clear_bit_hook, | 5801 | .clear_bit_hook = btrfs_clear_bit_hook, |
5802 | .merge_extent_hook = btrfs_merge_extent_hook, | ||
5803 | .split_extent_hook = btrfs_split_extent_hook, | ||
5248 | }; | 5804 | }; |
5249 | 5805 | ||
5250 | /* | 5806 | /* |
@@ -5259,7 +5815,7 @@ static struct extent_io_ops btrfs_extent_io_ops = { | |||
5259 | * | 5815 | * |
5260 | * For now we're avoiding this by dropping bmap. | 5816 | * For now we're avoiding this by dropping bmap. |
5261 | */ | 5817 | */ |
5262 | static struct address_space_operations btrfs_aops = { | 5818 | static const struct address_space_operations btrfs_aops = { |
5263 | .readpage = btrfs_readpage, | 5819 | .readpage = btrfs_readpage, |
5264 | .writepage = btrfs_writepage, | 5820 | .writepage = btrfs_writepage, |
5265 | .writepages = btrfs_writepages, | 5821 | .writepages = btrfs_writepages, |
@@ -5269,16 +5825,17 @@ static struct address_space_operations btrfs_aops = { | |||
5269 | .invalidatepage = btrfs_invalidatepage, | 5825 | .invalidatepage = btrfs_invalidatepage, |
5270 | .releasepage = btrfs_releasepage, | 5826 | .releasepage = btrfs_releasepage, |
5271 | .set_page_dirty = btrfs_set_page_dirty, | 5827 | .set_page_dirty = btrfs_set_page_dirty, |
5828 | .error_remove_page = generic_error_remove_page, | ||
5272 | }; | 5829 | }; |
5273 | 5830 | ||
5274 | static struct address_space_operations btrfs_symlink_aops = { | 5831 | static const struct address_space_operations btrfs_symlink_aops = { |
5275 | .readpage = btrfs_readpage, | 5832 | .readpage = btrfs_readpage, |
5276 | .writepage = btrfs_writepage, | 5833 | .writepage = btrfs_writepage, |
5277 | .invalidatepage = btrfs_invalidatepage, | 5834 | .invalidatepage = btrfs_invalidatepage, |
5278 | .releasepage = btrfs_releasepage, | 5835 | .releasepage = btrfs_releasepage, |
5279 | }; | 5836 | }; |
5280 | 5837 | ||
5281 | static struct inode_operations btrfs_file_inode_operations = { | 5838 | static const struct inode_operations btrfs_file_inode_operations = { |
5282 | .truncate = btrfs_truncate, | 5839 | .truncate = btrfs_truncate, |
5283 | .getattr = btrfs_getattr, | 5840 | .getattr = btrfs_getattr, |
5284 | .setattr = btrfs_setattr, | 5841 | .setattr = btrfs_setattr, |
@@ -5290,7 +5847,7 @@ static struct inode_operations btrfs_file_inode_operations = { | |||
5290 | .fallocate = btrfs_fallocate, | 5847 | .fallocate = btrfs_fallocate, |
5291 | .fiemap = btrfs_fiemap, | 5848 | .fiemap = btrfs_fiemap, |
5292 | }; | 5849 | }; |
5293 | static struct inode_operations btrfs_special_inode_operations = { | 5850 | static const struct inode_operations btrfs_special_inode_operations = { |
5294 | .getattr = btrfs_getattr, | 5851 | .getattr = btrfs_getattr, |
5295 | .setattr = btrfs_setattr, | 5852 | .setattr = btrfs_setattr, |
5296 | .permission = btrfs_permission, | 5853 | .permission = btrfs_permission, |
@@ -5299,7 +5856,7 @@ static struct inode_operations btrfs_special_inode_operations = { | |||
5299 | .listxattr = btrfs_listxattr, | 5856 | .listxattr = btrfs_listxattr, |
5300 | .removexattr = btrfs_removexattr, | 5857 | .removexattr = btrfs_removexattr, |
5301 | }; | 5858 | }; |
5302 | static struct inode_operations btrfs_symlink_inode_operations = { | 5859 | static const struct inode_operations btrfs_symlink_inode_operations = { |
5303 | .readlink = generic_readlink, | 5860 | .readlink = generic_readlink, |
5304 | .follow_link = page_follow_link_light, | 5861 | .follow_link = page_follow_link_light, |
5305 | .put_link = page_put_link, | 5862 | .put_link = page_put_link, |
@@ -5309,3 +5866,7 @@ static struct inode_operations btrfs_symlink_inode_operations = { | |||
5309 | .listxattr = btrfs_listxattr, | 5866 | .listxattr = btrfs_listxattr, |
5310 | .removexattr = btrfs_removexattr, | 5867 | .removexattr = btrfs_removexattr, |
5311 | }; | 5868 | }; |
5869 | |||
5870 | const struct dentry_operations btrfs_dentry_operations = { | ||
5871 | .d_delete = btrfs_dentry_delete, | ||
5872 | }; | ||