diff options
Diffstat (limited to 'fs/btrfs/inode.c')
| -rw-r--r-- | fs/btrfs/inode.c | 442 |
1 files changed, 371 insertions, 71 deletions
diff --git a/fs/btrfs/inode.c b/fs/btrfs/inode.c index e9b76bcd1c12..b3ad168a0bfc 100644 --- a/fs/btrfs/inode.c +++ b/fs/btrfs/inode.c | |||
| @@ -62,7 +62,7 @@ static const struct inode_operations btrfs_special_inode_operations; | |||
| 62 | static const struct inode_operations btrfs_file_inode_operations; | 62 | static const struct inode_operations btrfs_file_inode_operations; |
| 63 | static const struct address_space_operations btrfs_aops; | 63 | static const struct address_space_operations btrfs_aops; |
| 64 | static const 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; |
| @@ -424,9 +424,12 @@ again: | |||
| 424 | * and free up our temp pages. | 424 | * and free up our temp pages. |
| 425 | */ | 425 | */ |
| 426 | extent_clear_unlock_delalloc(inode, | 426 | extent_clear_unlock_delalloc(inode, |
| 427 | &BTRFS_I(inode)->io_tree, | 427 | &BTRFS_I(inode)->io_tree, |
| 428 | start, end, NULL, 1, 0, | 428 | start, end, NULL, |
| 429 | 0, 1, 1, 1, 0); | 429 | EXTENT_CLEAR_UNLOCK_PAGE | EXTENT_CLEAR_DIRTY | |
| 430 | EXTENT_CLEAR_DELALLOC | | ||
| 431 | EXTENT_CLEAR_ACCOUNTING | | ||
| 432 | EXTENT_SET_WRITEBACK | EXTENT_END_WRITEBACK); | ||
| 430 | ret = 0; | 433 | ret = 0; |
| 431 | goto free_pages_out; | 434 | goto free_pages_out; |
| 432 | } | 435 | } |
| @@ -535,7 +538,7 @@ static noinline int submit_compressed_extents(struct inode *inode, | |||
| 535 | struct btrfs_root *root = BTRFS_I(inode)->root; | 538 | struct btrfs_root *root = BTRFS_I(inode)->root; |
| 536 | struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree; | 539 | struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree; |
| 537 | struct extent_io_tree *io_tree; | 540 | struct extent_io_tree *io_tree; |
| 538 | int ret; | 541 | int ret = 0; |
| 539 | 542 | ||
| 540 | if (list_empty(&async_cow->extents)) | 543 | if (list_empty(&async_cow->extents)) |
| 541 | return 0; | 544 | return 0; |
| @@ -549,6 +552,7 @@ static noinline int submit_compressed_extents(struct inode *inode, | |||
| 549 | 552 | ||
| 550 | io_tree = &BTRFS_I(inode)->io_tree; | 553 | io_tree = &BTRFS_I(inode)->io_tree; |
| 551 | 554 | ||
| 555 | retry: | ||
| 552 | /* did the compression code fall back to uncompressed IO? */ | 556 | /* did the compression code fall back to uncompressed IO? */ |
| 553 | if (!async_extent->pages) { | 557 | if (!async_extent->pages) { |
| 554 | int page_started = 0; | 558 | int page_started = 0; |
| @@ -559,11 +563,11 @@ static noinline int submit_compressed_extents(struct inode *inode, | |||
| 559 | async_extent->ram_size - 1, GFP_NOFS); | 563 | async_extent->ram_size - 1, GFP_NOFS); |
| 560 | 564 | ||
| 561 | /* allocate blocks */ | 565 | /* allocate blocks */ |
| 562 | cow_file_range(inode, async_cow->locked_page, | 566 | ret = cow_file_range(inode, async_cow->locked_page, |
| 563 | async_extent->start, | 567 | async_extent->start, |
| 564 | async_extent->start + | 568 | async_extent->start + |
| 565 | async_extent->ram_size - 1, | 569 | async_extent->ram_size - 1, |
| 566 | &page_started, &nr_written, 0); | 570 | &page_started, &nr_written, 0); |
| 567 | 571 | ||
| 568 | /* | 572 | /* |
| 569 | * if page_started, cow_file_range inserted an | 573 | * if page_started, cow_file_range inserted an |
| @@ -571,7 +575,7 @@ static noinline int submit_compressed_extents(struct inode *inode, | |||
| 571 | * and IO for us. Otherwise, we need to submit | 575 | * and IO for us. Otherwise, we need to submit |
| 572 | * all those pages down to the drive. | 576 | * all those pages down to the drive. |
| 573 | */ | 577 | */ |
| 574 | if (!page_started) | 578 | if (!page_started && !ret) |
| 575 | extent_write_locked_range(io_tree, | 579 | extent_write_locked_range(io_tree, |
| 576 | inode, async_extent->start, | 580 | inode, async_extent->start, |
| 577 | async_extent->start + | 581 | async_extent->start + |
| @@ -599,7 +603,21 @@ static noinline int submit_compressed_extents(struct inode *inode, | |||
| 599 | async_extent->compressed_size, | 603 | async_extent->compressed_size, |
| 600 | 0, alloc_hint, | 604 | 0, alloc_hint, |
| 601 | (u64)-1, &ins, 1); | 605 | (u64)-1, &ins, 1); |
| 602 | BUG_ON(ret); | 606 | if (ret) { |
| 607 | int i; | ||
| 608 | for (i = 0; i < async_extent->nr_pages; i++) { | ||
| 609 | WARN_ON(async_extent->pages[i]->mapping); | ||
| 610 | page_cache_release(async_extent->pages[i]); | ||
| 611 | } | ||
| 612 | kfree(async_extent->pages); | ||
| 613 | async_extent->nr_pages = 0; | ||
| 614 | async_extent->pages = NULL; | ||
| 615 | unlock_extent(io_tree, async_extent->start, | ||
| 616 | async_extent->start + | ||
| 617 | async_extent->ram_size - 1, GFP_NOFS); | ||
| 618 | goto retry; | ||
| 619 | } | ||
| 620 | |||
| 603 | em = alloc_extent_map(GFP_NOFS); | 621 | em = alloc_extent_map(GFP_NOFS); |
| 604 | em->start = async_extent->start; | 622 | em->start = async_extent->start; |
| 605 | em->len = async_extent->ram_size; | 623 | em->len = async_extent->ram_size; |
| @@ -637,11 +655,14 @@ static noinline int submit_compressed_extents(struct inode *inode, | |||
| 637 | * clear dirty, set writeback and unlock the pages. | 655 | * clear dirty, set writeback and unlock the pages. |
| 638 | */ | 656 | */ |
| 639 | extent_clear_unlock_delalloc(inode, | 657 | extent_clear_unlock_delalloc(inode, |
| 640 | &BTRFS_I(inode)->io_tree, | 658 | &BTRFS_I(inode)->io_tree, |
| 641 | async_extent->start, | 659 | async_extent->start, |
| 642 | async_extent->start + | 660 | async_extent->start + |
| 643 | async_extent->ram_size - 1, | 661 | async_extent->ram_size - 1, |
| 644 | NULL, 1, 1, 0, 1, 1, 0, 0); | 662 | NULL, EXTENT_CLEAR_UNLOCK_PAGE | |
| 663 | EXTENT_CLEAR_UNLOCK | | ||
| 664 | EXTENT_CLEAR_DELALLOC | | ||
| 665 | EXTENT_CLEAR_DIRTY | EXTENT_SET_WRITEBACK); | ||
| 645 | 666 | ||
| 646 | ret = btrfs_submit_compressed_write(inode, | 667 | ret = btrfs_submit_compressed_write(inode, |
| 647 | async_extent->start, | 668 | async_extent->start, |
| @@ -712,9 +733,15 @@ static noinline int cow_file_range(struct inode *inode, | |||
| 712 | start, end, 0, NULL); | 733 | start, end, 0, NULL); |
| 713 | if (ret == 0) { | 734 | if (ret == 0) { |
| 714 | extent_clear_unlock_delalloc(inode, | 735 | extent_clear_unlock_delalloc(inode, |
| 715 | &BTRFS_I(inode)->io_tree, | 736 | &BTRFS_I(inode)->io_tree, |
| 716 | start, end, NULL, 1, 1, | 737 | start, end, NULL, |
| 717 | 1, 1, 1, 1, 0); | 738 | EXTENT_CLEAR_UNLOCK_PAGE | |
| 739 | EXTENT_CLEAR_UNLOCK | | ||
| 740 | EXTENT_CLEAR_DELALLOC | | ||
| 741 | EXTENT_CLEAR_ACCOUNTING | | ||
| 742 | EXTENT_CLEAR_DIRTY | | ||
| 743 | EXTENT_SET_WRITEBACK | | ||
| 744 | EXTENT_END_WRITEBACK); | ||
| 718 | *nr_written = *nr_written + | 745 | *nr_written = *nr_written + |
| 719 | (end - start + PAGE_CACHE_SIZE) / PAGE_CACHE_SIZE; | 746 | (end - start + PAGE_CACHE_SIZE) / PAGE_CACHE_SIZE; |
| 720 | *page_started = 1; | 747 | *page_started = 1; |
| @@ -731,13 +758,29 @@ static noinline int cow_file_range(struct inode *inode, | |||
| 731 | em = search_extent_mapping(&BTRFS_I(inode)->extent_tree, | 758 | em = search_extent_mapping(&BTRFS_I(inode)->extent_tree, |
| 732 | start, num_bytes); | 759 | start, num_bytes); |
| 733 | if (em) { | 760 | if (em) { |
| 734 | alloc_hint = em->block_start; | 761 | /* |
| 735 | free_extent_map(em); | 762 | * if block start isn't an actual block number then find the |
| 763 | * first block in this inode and use that as a hint. If that | ||
| 764 | * block is also bogus then just don't worry about it. | ||
| 765 | */ | ||
| 766 | if (em->block_start >= EXTENT_MAP_LAST_BYTE) { | ||
| 767 | free_extent_map(em); | ||
| 768 | em = search_extent_mapping(em_tree, 0, 0); | ||
| 769 | if (em && em->block_start < EXTENT_MAP_LAST_BYTE) | ||
| 770 | alloc_hint = em->block_start; | ||
| 771 | if (em) | ||
| 772 | free_extent_map(em); | ||
| 773 | } else { | ||
| 774 | alloc_hint = em->block_start; | ||
| 775 | free_extent_map(em); | ||
| 776 | } | ||
| 736 | } | 777 | } |
| 737 | read_unlock(&BTRFS_I(inode)->extent_tree.lock); | 778 | read_unlock(&BTRFS_I(inode)->extent_tree.lock); |
| 738 | btrfs_drop_extent_cache(inode, start, start + num_bytes - 1, 0); | 779 | btrfs_drop_extent_cache(inode, start, start + num_bytes - 1, 0); |
| 739 | 780 | ||
| 740 | while (disk_num_bytes > 0) { | 781 | while (disk_num_bytes > 0) { |
| 782 | unsigned long op; | ||
| 783 | |||
| 741 | cur_alloc_size = min(disk_num_bytes, root->fs_info->max_extent); | 784 | cur_alloc_size = min(disk_num_bytes, root->fs_info->max_extent); |
| 742 | ret = btrfs_reserve_extent(trans, root, cur_alloc_size, | 785 | ret = btrfs_reserve_extent(trans, root, cur_alloc_size, |
| 743 | root->sectorsize, 0, alloc_hint, | 786 | root->sectorsize, 0, alloc_hint, |
| @@ -789,10 +832,13 @@ static noinline int cow_file_range(struct inode *inode, | |||
| 789 | * Do set the Private2 bit so we know this page was properly | 832 | * Do set the Private2 bit so we know this page was properly |
| 790 | * setup for writepage | 833 | * setup for writepage |
| 791 | */ | 834 | */ |
| 835 | op = unlock ? EXTENT_CLEAR_UNLOCK_PAGE : 0; | ||
| 836 | op |= EXTENT_CLEAR_UNLOCK | EXTENT_CLEAR_DELALLOC | | ||
| 837 | EXTENT_SET_PRIVATE2; | ||
| 838 | |||
| 792 | extent_clear_unlock_delalloc(inode, &BTRFS_I(inode)->io_tree, | 839 | extent_clear_unlock_delalloc(inode, &BTRFS_I(inode)->io_tree, |
| 793 | start, start + ram_size - 1, | 840 | start, start + ram_size - 1, |
| 794 | locked_page, unlock, 1, | 841 | locked_page, op); |
| 795 | 1, 0, 0, 0, 1); | ||
| 796 | disk_num_bytes -= cur_alloc_size; | 842 | disk_num_bytes -= cur_alloc_size; |
| 797 | num_bytes -= cur_alloc_size; | 843 | num_bytes -= cur_alloc_size; |
| 798 | alloc_hint = ins.objectid + ins.offset; | 844 | alloc_hint = ins.objectid + ins.offset; |
| @@ -864,8 +910,8 @@ static int cow_file_range_async(struct inode *inode, struct page *locked_page, | |||
| 864 | u64 cur_end; | 910 | u64 cur_end; |
| 865 | int limit = 10 * 1024 * 1042; | 911 | int limit = 10 * 1024 * 1042; |
| 866 | 912 | ||
| 867 | clear_extent_bit(&BTRFS_I(inode)->io_tree, start, end, EXTENT_LOCKED | | 913 | clear_extent_bit(&BTRFS_I(inode)->io_tree, start, end, EXTENT_LOCKED, |
| 868 | EXTENT_DELALLOC, 1, 0, NULL, GFP_NOFS); | 914 | 1, 0, NULL, GFP_NOFS); |
| 869 | while (start < end) { | 915 | while (start < end) { |
| 870 | async_cow = kmalloc(sizeof(*async_cow), GFP_NOFS); | 916 | async_cow = kmalloc(sizeof(*async_cow), GFP_NOFS); |
| 871 | async_cow->inode = inode; | 917 | async_cow->inode = inode; |
| @@ -1006,6 +1052,7 @@ next_slot: | |||
| 1006 | 1052 | ||
| 1007 | if (found_key.offset > cur_offset) { | 1053 | if (found_key.offset > cur_offset) { |
| 1008 | extent_end = found_key.offset; | 1054 | extent_end = found_key.offset; |
| 1055 | extent_type = 0; | ||
| 1009 | goto out_check; | 1056 | goto out_check; |
| 1010 | } | 1057 | } |
| 1011 | 1058 | ||
| @@ -1112,8 +1159,10 @@ out_check: | |||
| 1112 | BUG_ON(ret); | 1159 | BUG_ON(ret); |
| 1113 | 1160 | ||
| 1114 | extent_clear_unlock_delalloc(inode, &BTRFS_I(inode)->io_tree, | 1161 | extent_clear_unlock_delalloc(inode, &BTRFS_I(inode)->io_tree, |
| 1115 | cur_offset, cur_offset + num_bytes - 1, | 1162 | cur_offset, cur_offset + num_bytes - 1, |
| 1116 | locked_page, 1, 1, 1, 0, 0, 0, 1); | 1163 | locked_page, EXTENT_CLEAR_UNLOCK_PAGE | |
| 1164 | EXTENT_CLEAR_UNLOCK | EXTENT_CLEAR_DELALLOC | | ||
| 1165 | EXTENT_SET_PRIVATE2); | ||
| 1117 | cur_offset = extent_end; | 1166 | cur_offset = extent_end; |
| 1118 | if (cur_offset > end) | 1167 | if (cur_offset > end) |
| 1119 | break; | 1168 | break; |
| @@ -1159,6 +1208,89 @@ static int run_delalloc_range(struct inode *inode, struct page *locked_page, | |||
| 1159 | return ret; | 1208 | return ret; |
| 1160 | } | 1209 | } |
| 1161 | 1210 | ||
| 1211 | static int btrfs_split_extent_hook(struct inode *inode, | ||
| 1212 | struct extent_state *orig, u64 split) | ||
| 1213 | { | ||
| 1214 | struct btrfs_root *root = BTRFS_I(inode)->root; | ||
| 1215 | u64 size; | ||
| 1216 | |||
| 1217 | if (!(orig->state & EXTENT_DELALLOC)) | ||
| 1218 | return 0; | ||
| 1219 | |||
| 1220 | size = orig->end - orig->start + 1; | ||
| 1221 | if (size > root->fs_info->max_extent) { | ||
| 1222 | u64 num_extents; | ||
| 1223 | u64 new_size; | ||
| 1224 | |||
| 1225 | new_size = orig->end - split + 1; | ||
| 1226 | num_extents = div64_u64(size + root->fs_info->max_extent - 1, | ||
| 1227 | root->fs_info->max_extent); | ||
| 1228 | |||
| 1229 | /* | ||
| 1230 | * if we break a large extent up then leave oustanding_extents | ||
| 1231 | * be, since we've already accounted for the large extent. | ||
| 1232 | */ | ||
| 1233 | if (div64_u64(new_size + root->fs_info->max_extent - 1, | ||
| 1234 | root->fs_info->max_extent) < num_extents) | ||
| 1235 | return 0; | ||
| 1236 | } | ||
| 1237 | |||
| 1238 | spin_lock(&BTRFS_I(inode)->accounting_lock); | ||
| 1239 | BTRFS_I(inode)->outstanding_extents++; | ||
| 1240 | spin_unlock(&BTRFS_I(inode)->accounting_lock); | ||
| 1241 | |||
| 1242 | return 0; | ||
| 1243 | } | ||
| 1244 | |||
| 1245 | /* | ||
| 1246 | * extent_io.c merge_extent_hook, used to track merged delayed allocation | ||
| 1247 | * extents so we can keep track of new extents that are just merged onto old | ||
| 1248 | * extents, such as when we are doing sequential writes, so we can properly | ||
| 1249 | * account for the metadata space we'll need. | ||
| 1250 | */ | ||
| 1251 | static int btrfs_merge_extent_hook(struct inode *inode, | ||
| 1252 | struct extent_state *new, | ||
| 1253 | struct extent_state *other) | ||
| 1254 | { | ||
| 1255 | struct btrfs_root *root = BTRFS_I(inode)->root; | ||
| 1256 | u64 new_size, old_size; | ||
| 1257 | u64 num_extents; | ||
| 1258 | |||
| 1259 | /* not delalloc, ignore it */ | ||
| 1260 | if (!(other->state & EXTENT_DELALLOC)) | ||
| 1261 | return 0; | ||
| 1262 | |||
| 1263 | old_size = other->end - other->start + 1; | ||
| 1264 | if (new->start < other->start) | ||
| 1265 | new_size = other->end - new->start + 1; | ||
| 1266 | else | ||
| 1267 | new_size = new->end - other->start + 1; | ||
| 1268 | |||
| 1269 | /* we're not bigger than the max, unreserve the space and go */ | ||
| 1270 | if (new_size <= root->fs_info->max_extent) { | ||
| 1271 | spin_lock(&BTRFS_I(inode)->accounting_lock); | ||
| 1272 | BTRFS_I(inode)->outstanding_extents--; | ||
| 1273 | spin_unlock(&BTRFS_I(inode)->accounting_lock); | ||
| 1274 | return 0; | ||
| 1275 | } | ||
| 1276 | |||
| 1277 | /* | ||
| 1278 | * If we grew by another max_extent, just return, we want to keep that | ||
| 1279 | * reserved amount. | ||
| 1280 | */ | ||
| 1281 | num_extents = div64_u64(old_size + root->fs_info->max_extent - 1, | ||
| 1282 | root->fs_info->max_extent); | ||
| 1283 | if (div64_u64(new_size + root->fs_info->max_extent - 1, | ||
| 1284 | root->fs_info->max_extent) > num_extents) | ||
| 1285 | return 0; | ||
| 1286 | |||
| 1287 | spin_lock(&BTRFS_I(inode)->accounting_lock); | ||
| 1288 | BTRFS_I(inode)->outstanding_extents--; | ||
| 1289 | spin_unlock(&BTRFS_I(inode)->accounting_lock); | ||
| 1290 | |||
| 1291 | return 0; | ||
| 1292 | } | ||
| 1293 | |||
| 1162 | /* | 1294 | /* |
| 1163 | * extent_io.c set_bit_hook, used to track delayed allocation | 1295 | * extent_io.c set_bit_hook, used to track delayed allocation |
| 1164 | * bytes in this file, and to maintain the list of inodes that | 1296 | * bytes in this file, and to maintain the list of inodes that |
| @@ -1167,6 +1299,7 @@ static int run_delalloc_range(struct inode *inode, struct page *locked_page, | |||
| 1167 | static int btrfs_set_bit_hook(struct inode *inode, u64 start, u64 end, | 1299 | static int btrfs_set_bit_hook(struct inode *inode, u64 start, u64 end, |
| 1168 | unsigned long old, unsigned long bits) | 1300 | unsigned long old, unsigned long bits) |
| 1169 | { | 1301 | { |
| 1302 | |||
| 1170 | /* | 1303 | /* |
| 1171 | * set_bit and clear bit hooks normally require _irqsave/restore | 1304 | * set_bit and clear bit hooks normally require _irqsave/restore |
| 1172 | * but in this case, we are only testeing for the DELALLOC | 1305 | * but in this case, we are only testeing for the DELALLOC |
| @@ -1174,6 +1307,10 @@ static int btrfs_set_bit_hook(struct inode *inode, u64 start, u64 end, | |||
| 1174 | */ | 1307 | */ |
| 1175 | if (!(old & EXTENT_DELALLOC) && (bits & EXTENT_DELALLOC)) { | 1308 | if (!(old & EXTENT_DELALLOC) && (bits & EXTENT_DELALLOC)) { |
| 1176 | struct btrfs_root *root = BTRFS_I(inode)->root; | 1309 | struct btrfs_root *root = BTRFS_I(inode)->root; |
| 1310 | |||
| 1311 | spin_lock(&BTRFS_I(inode)->accounting_lock); | ||
| 1312 | BTRFS_I(inode)->outstanding_extents++; | ||
| 1313 | spin_unlock(&BTRFS_I(inode)->accounting_lock); | ||
| 1177 | btrfs_delalloc_reserve_space(root, inode, end - start + 1); | 1314 | btrfs_delalloc_reserve_space(root, inode, end - start + 1); |
| 1178 | spin_lock(&root->fs_info->delalloc_lock); | 1315 | spin_lock(&root->fs_info->delalloc_lock); |
| 1179 | BTRFS_I(inode)->delalloc_bytes += end - start + 1; | 1316 | BTRFS_I(inode)->delalloc_bytes += end - start + 1; |
| @@ -1190,22 +1327,31 @@ static int btrfs_set_bit_hook(struct inode *inode, u64 start, u64 end, | |||
| 1190 | /* | 1327 | /* |
| 1191 | * extent_io.c clear_bit_hook, see set_bit_hook for why | 1328 | * extent_io.c clear_bit_hook, see set_bit_hook for why |
| 1192 | */ | 1329 | */ |
| 1193 | static int btrfs_clear_bit_hook(struct inode *inode, u64 start, u64 end, | 1330 | static int btrfs_clear_bit_hook(struct inode *inode, |
| 1194 | unsigned long old, unsigned long bits) | 1331 | struct extent_state *state, unsigned long bits) |
| 1195 | { | 1332 | { |
| 1196 | /* | 1333 | /* |
| 1197 | * set_bit and clear bit hooks normally require _irqsave/restore | 1334 | * set_bit and clear bit hooks normally require _irqsave/restore |
| 1198 | * but in this case, we are only testeing for the DELALLOC | 1335 | * but in this case, we are only testeing for the DELALLOC |
| 1199 | * bit, which is only set or cleared with irqs on | 1336 | * bit, which is only set or cleared with irqs on |
| 1200 | */ | 1337 | */ |
| 1201 | if ((old & EXTENT_DELALLOC) && (bits & EXTENT_DELALLOC)) { | 1338 | if ((state->state & EXTENT_DELALLOC) && (bits & EXTENT_DELALLOC)) { |
| 1202 | struct btrfs_root *root = BTRFS_I(inode)->root; | 1339 | struct btrfs_root *root = BTRFS_I(inode)->root; |
| 1203 | 1340 | ||
| 1341 | if (bits & EXTENT_DO_ACCOUNTING) { | ||
| 1342 | spin_lock(&BTRFS_I(inode)->accounting_lock); | ||
| 1343 | BTRFS_I(inode)->outstanding_extents--; | ||
| 1344 | spin_unlock(&BTRFS_I(inode)->accounting_lock); | ||
| 1345 | btrfs_unreserve_metadata_for_delalloc(root, inode, 1); | ||
| 1346 | } | ||
| 1347 | |||
| 1204 | spin_lock(&root->fs_info->delalloc_lock); | 1348 | spin_lock(&root->fs_info->delalloc_lock); |
| 1205 | if (end - start + 1 > root->fs_info->delalloc_bytes) { | 1349 | if (state->end - state->start + 1 > |
| 1350 | root->fs_info->delalloc_bytes) { | ||
| 1206 | printk(KERN_INFO "btrfs warning: delalloc account " | 1351 | printk(KERN_INFO "btrfs warning: delalloc account " |
| 1207 | "%llu %llu\n", | 1352 | "%llu %llu\n", |
| 1208 | (unsigned long long)end - start + 1, | 1353 | (unsigned long long) |
| 1354 | state->end - state->start + 1, | ||
| 1209 | (unsigned long long) | 1355 | (unsigned long long) |
| 1210 | root->fs_info->delalloc_bytes); | 1356 | root->fs_info->delalloc_bytes); |
| 1211 | btrfs_delalloc_free_space(root, inode, (u64)-1); | 1357 | btrfs_delalloc_free_space(root, inode, (u64)-1); |
| @@ -1213,9 +1359,12 @@ static int btrfs_clear_bit_hook(struct inode *inode, u64 start, u64 end, | |||
| 1213 | BTRFS_I(inode)->delalloc_bytes = 0; | 1359 | BTRFS_I(inode)->delalloc_bytes = 0; |
| 1214 | } else { | 1360 | } else { |
| 1215 | btrfs_delalloc_free_space(root, inode, | 1361 | btrfs_delalloc_free_space(root, inode, |
| 1216 | end - start + 1); | 1362 | state->end - |
| 1217 | root->fs_info->delalloc_bytes -= end - start + 1; | 1363 | state->start + 1); |
| 1218 | BTRFS_I(inode)->delalloc_bytes -= end - start + 1; | 1364 | root->fs_info->delalloc_bytes -= state->end - |
| 1365 | state->start + 1; | ||
| 1366 | BTRFS_I(inode)->delalloc_bytes -= state->end - | ||
| 1367 | state->start + 1; | ||
| 1219 | } | 1368 | } |
| 1220 | if (BTRFS_I(inode)->delalloc_bytes == 0 && | 1369 | if (BTRFS_I(inode)->delalloc_bytes == 0 && |
| 1221 | !list_empty(&BTRFS_I(inode)->delalloc_inodes)) { | 1370 | !list_empty(&BTRFS_I(inode)->delalloc_inodes)) { |
| @@ -2354,7 +2503,19 @@ static int btrfs_unlink(struct inode *dir, struct dentry *dentry) | |||
| 2354 | 2503 | ||
| 2355 | root = BTRFS_I(dir)->root; | 2504 | root = BTRFS_I(dir)->root; |
| 2356 | 2505 | ||
| 2506 | /* | ||
| 2507 | * 5 items for unlink inode | ||
| 2508 | * 1 for orphan | ||
| 2509 | */ | ||
| 2510 | ret = btrfs_reserve_metadata_space(root, 6); | ||
| 2511 | if (ret) | ||
| 2512 | return ret; | ||
| 2513 | |||
| 2357 | trans = btrfs_start_transaction(root, 1); | 2514 | trans = btrfs_start_transaction(root, 1); |
| 2515 | if (IS_ERR(trans)) { | ||
| 2516 | btrfs_unreserve_metadata_space(root, 6); | ||
| 2517 | return PTR_ERR(trans); | ||
| 2518 | } | ||
| 2358 | 2519 | ||
| 2359 | btrfs_set_trans_block_group(trans, dir); | 2520 | btrfs_set_trans_block_group(trans, dir); |
| 2360 | 2521 | ||
| @@ -2369,6 +2530,7 @@ static int btrfs_unlink(struct inode *dir, struct dentry *dentry) | |||
| 2369 | nr = trans->blocks_used; | 2530 | nr = trans->blocks_used; |
| 2370 | 2531 | ||
| 2371 | btrfs_end_transaction_throttle(trans, root); | 2532 | btrfs_end_transaction_throttle(trans, root); |
| 2533 | btrfs_unreserve_metadata_space(root, 6); | ||
| 2372 | btrfs_btree_balance_dirty(root, nr); | 2534 | btrfs_btree_balance_dirty(root, nr); |
| 2373 | return ret; | 2535 | return ret; |
| 2374 | } | 2536 | } |
| @@ -2449,7 +2611,16 @@ static int btrfs_rmdir(struct inode *dir, struct dentry *dentry) | |||
| 2449 | inode->i_ino == BTRFS_FIRST_FREE_OBJECTID) | 2611 | inode->i_ino == BTRFS_FIRST_FREE_OBJECTID) |
| 2450 | return -ENOTEMPTY; | 2612 | return -ENOTEMPTY; |
| 2451 | 2613 | ||
| 2614 | ret = btrfs_reserve_metadata_space(root, 5); | ||
| 2615 | if (ret) | ||
| 2616 | return ret; | ||
| 2617 | |||
| 2452 | trans = btrfs_start_transaction(root, 1); | 2618 | trans = btrfs_start_transaction(root, 1); |
| 2619 | if (IS_ERR(trans)) { | ||
| 2620 | btrfs_unreserve_metadata_space(root, 5); | ||
| 2621 | return PTR_ERR(trans); | ||
| 2622 | } | ||
| 2623 | |||
| 2453 | btrfs_set_trans_block_group(trans, dir); | 2624 | btrfs_set_trans_block_group(trans, dir); |
| 2454 | 2625 | ||
| 2455 | if (unlikely(inode->i_ino == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID)) { | 2626 | if (unlikely(inode->i_ino == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID)) { |
| @@ -2472,6 +2643,7 @@ static int btrfs_rmdir(struct inode *dir, struct dentry *dentry) | |||
| 2472 | out: | 2643 | out: |
| 2473 | nr = trans->blocks_used; | 2644 | nr = trans->blocks_used; |
| 2474 | ret = btrfs_end_transaction_throttle(trans, root); | 2645 | ret = btrfs_end_transaction_throttle(trans, root); |
| 2646 | btrfs_unreserve_metadata_space(root, 5); | ||
| 2475 | btrfs_btree_balance_dirty(root, nr); | 2647 | btrfs_btree_balance_dirty(root, nr); |
| 2476 | 2648 | ||
| 2477 | if (ret && !err) | 2649 | if (ret && !err) |
| @@ -2912,12 +3084,22 @@ static int btrfs_truncate_page(struct address_space *mapping, loff_t from) | |||
| 2912 | 3084 | ||
| 2913 | if ((offset & (blocksize - 1)) == 0) | 3085 | if ((offset & (blocksize - 1)) == 0) |
| 2914 | goto out; | 3086 | goto out; |
| 3087 | ret = btrfs_check_data_free_space(root, inode, PAGE_CACHE_SIZE); | ||
| 3088 | if (ret) | ||
| 3089 | goto out; | ||
| 3090 | |||
| 3091 | ret = btrfs_reserve_metadata_for_delalloc(root, inode, 1); | ||
| 3092 | if (ret) | ||
| 3093 | goto out; | ||
| 2915 | 3094 | ||
| 2916 | ret = -ENOMEM; | 3095 | ret = -ENOMEM; |
| 2917 | again: | 3096 | again: |
| 2918 | page = grab_cache_page(mapping, index); | 3097 | page = grab_cache_page(mapping, index); |
| 2919 | if (!page) | 3098 | if (!page) { |
| 3099 | btrfs_free_reserved_data_space(root, inode, PAGE_CACHE_SIZE); | ||
| 3100 | btrfs_unreserve_metadata_for_delalloc(root, inode, 1); | ||
| 2920 | goto out; | 3101 | goto out; |
| 3102 | } | ||
| 2921 | 3103 | ||
| 2922 | page_start = page_offset(page); | 3104 | page_start = page_offset(page); |
| 2923 | page_end = page_start + PAGE_CACHE_SIZE - 1; | 3105 | page_end = page_start + PAGE_CACHE_SIZE - 1; |
| @@ -2950,7 +3132,16 @@ again: | |||
| 2950 | goto again; | 3132 | goto again; |
| 2951 | } | 3133 | } |
| 2952 | 3134 | ||
| 2953 | btrfs_set_extent_delalloc(inode, page_start, page_end); | 3135 | clear_extent_bits(&BTRFS_I(inode)->io_tree, page_start, page_end, |
| 3136 | EXTENT_DIRTY | EXTENT_DELALLOC | EXTENT_DO_ACCOUNTING, | ||
| 3137 | GFP_NOFS); | ||
| 3138 | |||
| 3139 | ret = btrfs_set_extent_delalloc(inode, page_start, page_end); | ||
| 3140 | if (ret) { | ||
| 3141 | unlock_extent(io_tree, page_start, page_end, GFP_NOFS); | ||
| 3142 | goto out_unlock; | ||
| 3143 | } | ||
| 3144 | |||
| 2954 | ret = 0; | 3145 | ret = 0; |
| 2955 | if (offset != PAGE_CACHE_SIZE) { | 3146 | if (offset != PAGE_CACHE_SIZE) { |
| 2956 | kaddr = kmap(page); | 3147 | kaddr = kmap(page); |
| @@ -2963,6 +3154,9 @@ again: | |||
| 2963 | unlock_extent(io_tree, page_start, page_end, GFP_NOFS); | 3154 | unlock_extent(io_tree, page_start, page_end, GFP_NOFS); |
| 2964 | 3155 | ||
| 2965 | out_unlock: | 3156 | out_unlock: |
| 3157 | if (ret) | ||
| 3158 | btrfs_free_reserved_data_space(root, inode, PAGE_CACHE_SIZE); | ||
| 3159 | btrfs_unreserve_metadata_for_delalloc(root, inode, 1); | ||
| 2966 | unlock_page(page); | 3160 | unlock_page(page); |
| 2967 | page_cache_release(page); | 3161 | page_cache_release(page); |
| 2968 | out: | 3162 | out: |
| @@ -2981,17 +3175,15 @@ int btrfs_cont_expand(struct inode *inode, loff_t size) | |||
| 2981 | u64 last_byte; | 3175 | u64 last_byte; |
| 2982 | u64 cur_offset; | 3176 | u64 cur_offset; |
| 2983 | u64 hole_size; | 3177 | u64 hole_size; |
| 2984 | int err; | 3178 | int err = 0; |
| 2985 | 3179 | ||
| 2986 | if (size <= hole_start) | 3180 | if (size <= hole_start) |
| 2987 | return 0; | 3181 | return 0; |
| 2988 | 3182 | ||
| 2989 | err = btrfs_check_metadata_free_space(root); | 3183 | err = btrfs_truncate_page(inode->i_mapping, inode->i_size); |
| 2990 | if (err) | 3184 | if (err) |
| 2991 | return err; | 3185 | return err; |
| 2992 | 3186 | ||
| 2993 | btrfs_truncate_page(inode->i_mapping, inode->i_size); | ||
| 2994 | |||
| 2995 | while (1) { | 3187 | while (1) { |
| 2996 | struct btrfs_ordered_extent *ordered; | 3188 | struct btrfs_ordered_extent *ordered; |
| 2997 | btrfs_wait_ordered_range(inode, hole_start, | 3189 | btrfs_wait_ordered_range(inode, hole_start, |
| @@ -3024,12 +3216,18 @@ int btrfs_cont_expand(struct inode *inode, loff_t size) | |||
| 3024 | cur_offset, &hint_byte, 1); | 3216 | cur_offset, &hint_byte, 1); |
| 3025 | if (err) | 3217 | if (err) |
| 3026 | break; | 3218 | break; |
| 3219 | |||
| 3220 | err = btrfs_reserve_metadata_space(root, 1); | ||
| 3221 | if (err) | ||
| 3222 | break; | ||
| 3223 | |||
| 3027 | err = btrfs_insert_file_extent(trans, root, | 3224 | err = btrfs_insert_file_extent(trans, root, |
| 3028 | inode->i_ino, cur_offset, 0, | 3225 | inode->i_ino, cur_offset, 0, |
| 3029 | 0, hole_size, 0, hole_size, | 3226 | 0, hole_size, 0, hole_size, |
| 3030 | 0, 0, 0); | 3227 | 0, 0, 0); |
| 3031 | btrfs_drop_extent_cache(inode, hole_start, | 3228 | btrfs_drop_extent_cache(inode, hole_start, |
| 3032 | last_byte - 1, 0); | 3229 | last_byte - 1, 0); |
| 3230 | btrfs_unreserve_metadata_space(root, 1); | ||
| 3033 | } | 3231 | } |
| 3034 | free_extent_map(em); | 3232 | free_extent_map(em); |
| 3035 | cur_offset = last_byte; | 3233 | cur_offset = last_byte; |
| @@ -3353,6 +3551,7 @@ static noinline void init_btrfs_i(struct inode *inode) | |||
| 3353 | bi->generation = 0; | 3551 | bi->generation = 0; |
| 3354 | bi->sequence = 0; | 3552 | bi->sequence = 0; |
| 3355 | bi->last_trans = 0; | 3553 | bi->last_trans = 0; |
| 3554 | bi->last_sub_trans = 0; | ||
| 3356 | bi->logged_trans = 0; | 3555 | bi->logged_trans = 0; |
| 3357 | bi->delalloc_bytes = 0; | 3556 | bi->delalloc_bytes = 0; |
| 3358 | bi->reserved_bytes = 0; | 3557 | bi->reserved_bytes = 0; |
| @@ -3503,12 +3702,14 @@ static int btrfs_dentry_delete(struct dentry *dentry) | |||
| 3503 | { | 3702 | { |
| 3504 | struct btrfs_root *root; | 3703 | struct btrfs_root *root; |
| 3505 | 3704 | ||
| 3506 | if (!dentry->d_inode) | 3705 | if (!dentry->d_inode && !IS_ROOT(dentry)) |
| 3507 | return 0; | 3706 | dentry = dentry->d_parent; |
| 3508 | 3707 | ||
| 3509 | root = BTRFS_I(dentry->d_inode)->root; | 3708 | if (dentry->d_inode) { |
| 3510 | if (btrfs_root_refs(&root->root_item) == 0) | 3709 | root = BTRFS_I(dentry->d_inode)->root; |
| 3511 | return 1; | 3710 | if (btrfs_root_refs(&root->root_item) == 0) |
| 3711 | return 1; | ||
| 3712 | } | ||
| 3512 | return 0; | 3713 | return 0; |
| 3513 | } | 3714 | } |
| 3514 | 3715 | ||
| @@ -3990,11 +4191,18 @@ static int btrfs_mknod(struct inode *dir, struct dentry *dentry, | |||
| 3990 | if (!new_valid_dev(rdev)) | 4191 | if (!new_valid_dev(rdev)) |
| 3991 | return -EINVAL; | 4192 | return -EINVAL; |
| 3992 | 4193 | ||
| 3993 | err = btrfs_check_metadata_free_space(root); | 4194 | /* |
| 4195 | * 2 for inode item and ref | ||
| 4196 | * 2 for dir items | ||
| 4197 | * 1 for xattr if selinux is on | ||
| 4198 | */ | ||
| 4199 | err = btrfs_reserve_metadata_space(root, 5); | ||
| 3994 | if (err) | 4200 | if (err) |
| 3995 | goto fail; | 4201 | return err; |
| 3996 | 4202 | ||
| 3997 | trans = btrfs_start_transaction(root, 1); | 4203 | trans = btrfs_start_transaction(root, 1); |
| 4204 | if (!trans) | ||
| 4205 | goto fail; | ||
| 3998 | btrfs_set_trans_block_group(trans, dir); | 4206 | btrfs_set_trans_block_group(trans, dir); |
| 3999 | 4207 | ||
| 4000 | err = btrfs_find_free_objectid(trans, root, dir->i_ino, &objectid); | 4208 | err = btrfs_find_free_objectid(trans, root, dir->i_ino, &objectid); |
| @@ -4032,6 +4240,7 @@ out_unlock: | |||
| 4032 | nr = trans->blocks_used; | 4240 | nr = trans->blocks_used; |
| 4033 | btrfs_end_transaction_throttle(trans, root); | 4241 | btrfs_end_transaction_throttle(trans, root); |
| 4034 | fail: | 4242 | fail: |
| 4243 | btrfs_unreserve_metadata_space(root, 5); | ||
| 4035 | if (drop_inode) { | 4244 | if (drop_inode) { |
| 4036 | inode_dec_link_count(inode); | 4245 | inode_dec_link_count(inode); |
| 4037 | iput(inode); | 4246 | iput(inode); |
| @@ -4052,10 +4261,18 @@ static int btrfs_create(struct inode *dir, struct dentry *dentry, | |||
| 4052 | u64 objectid; | 4261 | u64 objectid; |
| 4053 | u64 index = 0; | 4262 | u64 index = 0; |
| 4054 | 4263 | ||
| 4055 | err = btrfs_check_metadata_free_space(root); | 4264 | /* |
| 4265 | * 2 for inode item and ref | ||
| 4266 | * 2 for dir items | ||
| 4267 | * 1 for xattr if selinux is on | ||
| 4268 | */ | ||
| 4269 | err = btrfs_reserve_metadata_space(root, 5); | ||
| 4056 | if (err) | 4270 | if (err) |
| 4057 | goto fail; | 4271 | return err; |
| 4272 | |||
| 4058 | trans = btrfs_start_transaction(root, 1); | 4273 | trans = btrfs_start_transaction(root, 1); |
| 4274 | if (!trans) | ||
| 4275 | goto fail; | ||
| 4059 | btrfs_set_trans_block_group(trans, dir); | 4276 | btrfs_set_trans_block_group(trans, dir); |
| 4060 | 4277 | ||
| 4061 | err = btrfs_find_free_objectid(trans, root, dir->i_ino, &objectid); | 4278 | err = btrfs_find_free_objectid(trans, root, dir->i_ino, &objectid); |
| @@ -4096,6 +4313,7 @@ out_unlock: | |||
| 4096 | nr = trans->blocks_used; | 4313 | nr = trans->blocks_used; |
| 4097 | btrfs_end_transaction_throttle(trans, root); | 4314 | btrfs_end_transaction_throttle(trans, root); |
| 4098 | fail: | 4315 | fail: |
| 4316 | btrfs_unreserve_metadata_space(root, 5); | ||
| 4099 | if (drop_inode) { | 4317 | if (drop_inode) { |
| 4100 | inode_dec_link_count(inode); | 4318 | inode_dec_link_count(inode); |
| 4101 | iput(inode); | 4319 | iput(inode); |
| @@ -4118,10 +4336,16 @@ static int btrfs_link(struct dentry *old_dentry, struct inode *dir, | |||
| 4118 | if (inode->i_nlink == 0) | 4336 | if (inode->i_nlink == 0) |
| 4119 | return -ENOENT; | 4337 | return -ENOENT; |
| 4120 | 4338 | ||
| 4121 | btrfs_inc_nlink(inode); | 4339 | /* |
| 4122 | err = btrfs_check_metadata_free_space(root); | 4340 | * 1 item for inode ref |
| 4341 | * 2 items for dir items | ||
| 4342 | */ | ||
| 4343 | err = btrfs_reserve_metadata_space(root, 3); | ||
| 4123 | if (err) | 4344 | if (err) |
| 4124 | goto fail; | 4345 | return err; |
| 4346 | |||
| 4347 | btrfs_inc_nlink(inode); | ||
| 4348 | |||
| 4125 | err = btrfs_set_inode_index(dir, &index); | 4349 | err = btrfs_set_inode_index(dir, &index); |
| 4126 | if (err) | 4350 | if (err) |
| 4127 | goto fail; | 4351 | goto fail; |
| @@ -4145,6 +4369,7 @@ static int btrfs_link(struct dentry *old_dentry, struct inode *dir, | |||
| 4145 | nr = trans->blocks_used; | 4369 | nr = trans->blocks_used; |
| 4146 | btrfs_end_transaction_throttle(trans, root); | 4370 | btrfs_end_transaction_throttle(trans, root); |
| 4147 | fail: | 4371 | fail: |
| 4372 | btrfs_unreserve_metadata_space(root, 3); | ||
| 4148 | if (drop_inode) { | 4373 | if (drop_inode) { |
| 4149 | inode_dec_link_count(inode); | 4374 | inode_dec_link_count(inode); |
| 4150 | iput(inode); | 4375 | iput(inode); |
| @@ -4164,17 +4389,21 @@ static int btrfs_mkdir(struct inode *dir, struct dentry *dentry, int mode) | |||
| 4164 | u64 index = 0; | 4389 | u64 index = 0; |
| 4165 | unsigned long nr = 1; | 4390 | unsigned long nr = 1; |
| 4166 | 4391 | ||
| 4167 | err = btrfs_check_metadata_free_space(root); | 4392 | /* |
| 4393 | * 2 items for inode and ref | ||
| 4394 | * 2 items for dir items | ||
| 4395 | * 1 for xattr if selinux is on | ||
| 4396 | */ | ||
| 4397 | err = btrfs_reserve_metadata_space(root, 5); | ||
| 4168 | if (err) | 4398 | if (err) |
| 4169 | goto out_unlock; | 4399 | return err; |
| 4170 | 4400 | ||
| 4171 | trans = btrfs_start_transaction(root, 1); | 4401 | trans = btrfs_start_transaction(root, 1); |
| 4172 | btrfs_set_trans_block_group(trans, dir); | 4402 | if (!trans) { |
| 4173 | 4403 | err = -ENOMEM; | |
| 4174 | if (IS_ERR(trans)) { | ||
| 4175 | err = PTR_ERR(trans); | ||
| 4176 | goto out_unlock; | 4404 | goto out_unlock; |
| 4177 | } | 4405 | } |
| 4406 | btrfs_set_trans_block_group(trans, dir); | ||
| 4178 | 4407 | ||
| 4179 | err = btrfs_find_free_objectid(trans, root, dir->i_ino, &objectid); | 4408 | err = btrfs_find_free_objectid(trans, root, dir->i_ino, &objectid); |
| 4180 | if (err) { | 4409 | if (err) { |
| @@ -4223,6 +4452,7 @@ out_fail: | |||
| 4223 | btrfs_end_transaction_throttle(trans, root); | 4452 | btrfs_end_transaction_throttle(trans, root); |
| 4224 | 4453 | ||
| 4225 | out_unlock: | 4454 | out_unlock: |
| 4455 | btrfs_unreserve_metadata_space(root, 5); | ||
| 4226 | if (drop_on_err) | 4456 | if (drop_on_err) |
| 4227 | iput(inode); | 4457 | iput(inode); |
| 4228 | btrfs_btree_balance_dirty(root, nr); | 4458 | btrfs_btree_balance_dirty(root, nr); |
| @@ -4684,7 +4914,8 @@ static void btrfs_invalidatepage(struct page *page, unsigned long offset) | |||
| 4684 | */ | 4914 | */ |
| 4685 | clear_extent_bit(tree, page_start, page_end, | 4915 | clear_extent_bit(tree, page_start, page_end, |
| 4686 | EXTENT_DIRTY | EXTENT_DELALLOC | | 4916 | EXTENT_DIRTY | EXTENT_DELALLOC | |
| 4687 | EXTENT_LOCKED, 1, 0, NULL, GFP_NOFS); | 4917 | EXTENT_LOCKED | EXTENT_DO_ACCOUNTING, 1, 0, |
| 4918 | NULL, GFP_NOFS); | ||
| 4688 | /* | 4919 | /* |
| 4689 | * whoever cleared the private bit is responsible | 4920 | * whoever cleared the private bit is responsible |
| 4690 | * for the finish_ordered_io | 4921 | * for the finish_ordered_io |
| @@ -4697,8 +4928,8 @@ static void btrfs_invalidatepage(struct page *page, unsigned long offset) | |||
| 4697 | lock_extent(tree, page_start, page_end, GFP_NOFS); | 4928 | lock_extent(tree, page_start, page_end, GFP_NOFS); |
| 4698 | } | 4929 | } |
| 4699 | clear_extent_bit(tree, page_start, page_end, | 4930 | clear_extent_bit(tree, page_start, page_end, |
| 4700 | EXTENT_LOCKED | EXTENT_DIRTY | EXTENT_DELALLOC, | 4931 | EXTENT_LOCKED | EXTENT_DIRTY | EXTENT_DELALLOC | |
| 4701 | 1, 1, NULL, GFP_NOFS); | 4932 | EXTENT_DO_ACCOUNTING, 1, 1, NULL, GFP_NOFS); |
| 4702 | __btrfs_releasepage(page, GFP_NOFS); | 4933 | __btrfs_releasepage(page, GFP_NOFS); |
| 4703 | 4934 | ||
| 4704 | ClearPageChecked(page); | 4935 | ClearPageChecked(page); |
| @@ -4747,6 +4978,13 @@ int btrfs_page_mkwrite(struct vm_area_struct *vma, struct vm_fault *vmf) | |||
| 4747 | goto out; | 4978 | goto out; |
| 4748 | } | 4979 | } |
| 4749 | 4980 | ||
| 4981 | ret = btrfs_reserve_metadata_for_delalloc(root, inode, 1); | ||
| 4982 | if (ret) { | ||
| 4983 | btrfs_free_reserved_data_space(root, inode, PAGE_CACHE_SIZE); | ||
| 4984 | ret = VM_FAULT_SIGBUS; | ||
| 4985 | goto out; | ||
| 4986 | } | ||
| 4987 | |||
| 4750 | ret = VM_FAULT_NOPAGE; /* make the VM retry the fault */ | 4988 | ret = VM_FAULT_NOPAGE; /* make the VM retry the fault */ |
| 4751 | again: | 4989 | again: |
| 4752 | lock_page(page); | 4990 | lock_page(page); |
| @@ -4778,7 +5016,24 @@ again: | |||
| 4778 | goto again; | 5016 | goto again; |
| 4779 | } | 5017 | } |
| 4780 | 5018 | ||
| 4781 | btrfs_set_extent_delalloc(inode, page_start, page_end); | 5019 | /* |
| 5020 | * XXX - page_mkwrite gets called every time the page is dirtied, even | ||
| 5021 | * if it was already dirty, so for space accounting reasons we need to | ||
| 5022 | * clear any delalloc bits for the range we are fixing to save. There | ||
| 5023 | * is probably a better way to do this, but for now keep consistent with | ||
| 5024 | * prepare_pages in the normal write path. | ||
| 5025 | */ | ||
| 5026 | clear_extent_bits(&BTRFS_I(inode)->io_tree, page_start, page_end, | ||
| 5027 | EXTENT_DIRTY | EXTENT_DELALLOC | EXTENT_DO_ACCOUNTING, | ||
| 5028 | GFP_NOFS); | ||
| 5029 | |||
| 5030 | ret = btrfs_set_extent_delalloc(inode, page_start, page_end); | ||
| 5031 | if (ret) { | ||
| 5032 | unlock_extent(io_tree, page_start, page_end, GFP_NOFS); | ||
| 5033 | ret = VM_FAULT_SIGBUS; | ||
| 5034 | btrfs_free_reserved_data_space(root, inode, PAGE_CACHE_SIZE); | ||
| 5035 | goto out_unlock; | ||
| 5036 | } | ||
| 4782 | ret = 0; | 5037 | ret = 0; |
| 4783 | 5038 | ||
| 4784 | /* page is wholly or partially inside EOF */ | 5039 | /* page is wholly or partially inside EOF */ |
| @@ -4797,10 +5052,13 @@ again: | |||
| 4797 | set_page_dirty(page); | 5052 | set_page_dirty(page); |
| 4798 | SetPageUptodate(page); | 5053 | SetPageUptodate(page); |
| 4799 | 5054 | ||
| 4800 | BTRFS_I(inode)->last_trans = root->fs_info->generation + 1; | 5055 | BTRFS_I(inode)->last_trans = root->fs_info->generation; |
| 5056 | BTRFS_I(inode)->last_sub_trans = BTRFS_I(inode)->root->log_transid; | ||
| 5057 | |||
| 4801 | unlock_extent(io_tree, page_start, page_end, GFP_NOFS); | 5058 | unlock_extent(io_tree, page_start, page_end, GFP_NOFS); |
| 4802 | 5059 | ||
| 4803 | out_unlock: | 5060 | out_unlock: |
| 5061 | btrfs_unreserve_metadata_for_delalloc(root, inode, 1); | ||
| 4804 | if (!ret) | 5062 | if (!ret) |
| 4805 | return VM_FAULT_LOCKED; | 5063 | return VM_FAULT_LOCKED; |
| 4806 | unlock_page(page); | 5064 | unlock_page(page); |
| @@ -4821,7 +5079,9 @@ static void btrfs_truncate(struct inode *inode) | |||
| 4821 | if (IS_APPEND(inode) || IS_IMMUTABLE(inode)) | 5079 | if (IS_APPEND(inode) || IS_IMMUTABLE(inode)) |
| 4822 | return; | 5080 | return; |
| 4823 | 5081 | ||
| 4824 | btrfs_truncate_page(inode->i_mapping, inode->i_size); | 5082 | ret = btrfs_truncate_page(inode->i_mapping, inode->i_size); |
| 5083 | if (ret) | ||
| 5084 | return; | ||
| 4825 | btrfs_wait_ordered_range(inode, inode->i_size & (~mask), (u64)-1); | 5085 | btrfs_wait_ordered_range(inode, inode->i_size & (~mask), (u64)-1); |
| 4826 | 5086 | ||
| 4827 | trans = btrfs_start_transaction(root, 1); | 5087 | trans = btrfs_start_transaction(root, 1); |
| @@ -4916,7 +5176,12 @@ struct inode *btrfs_alloc_inode(struct super_block *sb) | |||
| 4916 | if (!ei) | 5176 | if (!ei) |
| 4917 | return NULL; | 5177 | return NULL; |
| 4918 | ei->last_trans = 0; | 5178 | ei->last_trans = 0; |
| 5179 | ei->last_sub_trans = 0; | ||
| 4919 | ei->logged_trans = 0; | 5180 | ei->logged_trans = 0; |
| 5181 | ei->outstanding_extents = 0; | ||
| 5182 | ei->reserved_extents = 0; | ||
| 5183 | ei->root = NULL; | ||
| 5184 | spin_lock_init(&ei->accounting_lock); | ||
| 4920 | btrfs_ordered_inode_tree_init(&ei->ordered_tree); | 5185 | btrfs_ordered_inode_tree_init(&ei->ordered_tree); |
| 4921 | INIT_LIST_HEAD(&ei->i_orphan); | 5186 | INIT_LIST_HEAD(&ei->i_orphan); |
| 4922 | INIT_LIST_HEAD(&ei->ordered_operations); | 5187 | INIT_LIST_HEAD(&ei->ordered_operations); |
| @@ -4932,6 +5197,14 @@ void btrfs_destroy_inode(struct inode *inode) | |||
| 4932 | WARN_ON(inode->i_data.nrpages); | 5197 | WARN_ON(inode->i_data.nrpages); |
| 4933 | 5198 | ||
| 4934 | /* | 5199 | /* |
| 5200 | * This can happen where we create an inode, but somebody else also | ||
| 5201 | * created the same inode and we need to destroy the one we already | ||
| 5202 | * created. | ||
| 5203 | */ | ||
| 5204 | if (!root) | ||
| 5205 | goto free; | ||
| 5206 | |||
| 5207 | /* | ||
| 4935 | * Make sure we're properly removed from the ordered operation | 5208 | * Make sure we're properly removed from the ordered operation |
| 4936 | * lists. | 5209 | * lists. |
| 4937 | */ | 5210 | */ |
| @@ -4966,6 +5239,7 @@ void btrfs_destroy_inode(struct inode *inode) | |||
| 4966 | } | 5239 | } |
| 4967 | inode_tree_del(inode); | 5240 | inode_tree_del(inode); |
| 4968 | btrfs_drop_extent_cache(inode, 0, (u64)-1, 0); | 5241 | btrfs_drop_extent_cache(inode, 0, (u64)-1, 0); |
| 5242 | free: | ||
| 4969 | kmem_cache_free(btrfs_inode_cachep, BTRFS_I(inode)); | 5243 | kmem_cache_free(btrfs_inode_cachep, BTRFS_I(inode)); |
| 4970 | } | 5244 | } |
| 4971 | 5245 | ||
| @@ -5070,7 +5344,15 @@ static int btrfs_rename(struct inode *old_dir, struct dentry *old_dentry, | |||
| 5070 | new_inode->i_size > BTRFS_EMPTY_DIR_SIZE) | 5344 | new_inode->i_size > BTRFS_EMPTY_DIR_SIZE) |
| 5071 | return -ENOTEMPTY; | 5345 | return -ENOTEMPTY; |
| 5072 | 5346 | ||
| 5073 | ret = btrfs_check_metadata_free_space(root); | 5347 | /* |
| 5348 | * We want to reserve the absolute worst case amount of items. So if | ||
| 5349 | * both inodes are subvols and we need to unlink them then that would | ||
| 5350 | * require 4 item modifications, but if they are both normal inodes it | ||
| 5351 | * would require 5 item modifications, so we'll assume their normal | ||
| 5352 | * inodes. So 5 * 2 is 10, plus 1 for the new link, so 11 total items | ||
| 5353 | * should cover the worst case number of items we'll modify. | ||
| 5354 | */ | ||
| 5355 | ret = btrfs_reserve_metadata_space(root, 11); | ||
| 5074 | if (ret) | 5356 | if (ret) |
| 5075 | return ret; | 5357 | return ret; |
| 5076 | 5358 | ||
| @@ -5185,6 +5467,8 @@ out_fail: | |||
| 5185 | 5467 | ||
| 5186 | if (old_inode->i_ino == BTRFS_FIRST_FREE_OBJECTID) | 5468 | if (old_inode->i_ino == BTRFS_FIRST_FREE_OBJECTID) |
| 5187 | up_read(&root->fs_info->subvol_sem); | 5469 | up_read(&root->fs_info->subvol_sem); |
| 5470 | |||
| 5471 | btrfs_unreserve_metadata_space(root, 11); | ||
| 5188 | return ret; | 5472 | return ret; |
| 5189 | } | 5473 | } |
| 5190 | 5474 | ||
| @@ -5256,11 +5540,18 @@ static int btrfs_symlink(struct inode *dir, struct dentry *dentry, | |||
| 5256 | if (name_len > BTRFS_MAX_INLINE_DATA_SIZE(root)) | 5540 | if (name_len > BTRFS_MAX_INLINE_DATA_SIZE(root)) |
| 5257 | return -ENAMETOOLONG; | 5541 | return -ENAMETOOLONG; |
| 5258 | 5542 | ||
| 5259 | err = btrfs_check_metadata_free_space(root); | 5543 | /* |
| 5544 | * 2 items for inode item and ref | ||
| 5545 | * 2 items for dir items | ||
| 5546 | * 1 item for xattr if selinux is on | ||
| 5547 | */ | ||
| 5548 | err = btrfs_reserve_metadata_space(root, 5); | ||
| 5260 | if (err) | 5549 | if (err) |
| 5261 | goto out_fail; | 5550 | return err; |
| 5262 | 5551 | ||
| 5263 | trans = btrfs_start_transaction(root, 1); | 5552 | trans = btrfs_start_transaction(root, 1); |
| 5553 | if (!trans) | ||
| 5554 | goto out_fail; | ||
| 5264 | btrfs_set_trans_block_group(trans, dir); | 5555 | btrfs_set_trans_block_group(trans, dir); |
| 5265 | 5556 | ||
| 5266 | err = btrfs_find_free_objectid(trans, root, dir->i_ino, &objectid); | 5557 | err = btrfs_find_free_objectid(trans, root, dir->i_ino, &objectid); |
| @@ -5341,6 +5632,7 @@ out_unlock: | |||
| 5341 | nr = trans->blocks_used; | 5632 | nr = trans->blocks_used; |
| 5342 | btrfs_end_transaction_throttle(trans, root); | 5633 | btrfs_end_transaction_throttle(trans, root); |
| 5343 | out_fail: | 5634 | out_fail: |
| 5635 | btrfs_unreserve_metadata_space(root, 5); | ||
| 5344 | if (drop_inode) { | 5636 | if (drop_inode) { |
| 5345 | inode_dec_link_count(inode); | 5637 | inode_dec_link_count(inode); |
| 5346 | iput(inode); | 5638 | iput(inode); |
| @@ -5362,6 +5654,11 @@ static int prealloc_file_range(struct btrfs_trans_handle *trans, | |||
| 5362 | 5654 | ||
| 5363 | while (num_bytes > 0) { | 5655 | while (num_bytes > 0) { |
| 5364 | alloc_size = min(num_bytes, root->fs_info->max_extent); | 5656 | alloc_size = min(num_bytes, root->fs_info->max_extent); |
| 5657 | |||
| 5658 | ret = btrfs_reserve_metadata_space(root, 1); | ||
| 5659 | if (ret) | ||
| 5660 | goto out; | ||
| 5661 | |||
| 5365 | ret = btrfs_reserve_extent(trans, root, alloc_size, | 5662 | ret = btrfs_reserve_extent(trans, root, alloc_size, |
| 5366 | root->sectorsize, 0, alloc_hint, | 5663 | root->sectorsize, 0, alloc_hint, |
| 5367 | (u64)-1, &ins, 1); | 5664 | (u64)-1, &ins, 1); |
| @@ -5381,6 +5678,7 @@ static int prealloc_file_range(struct btrfs_trans_handle *trans, | |||
| 5381 | num_bytes -= ins.offset; | 5678 | num_bytes -= ins.offset; |
| 5382 | cur_offset += ins.offset; | 5679 | cur_offset += ins.offset; |
| 5383 | alloc_hint = ins.objectid + ins.offset; | 5680 | alloc_hint = ins.objectid + ins.offset; |
| 5681 | btrfs_unreserve_metadata_space(root, 1); | ||
| 5384 | } | 5682 | } |
| 5385 | out: | 5683 | out: |
| 5386 | if (cur_offset > start) { | 5684 | if (cur_offset > start) { |
| @@ -5544,7 +5842,7 @@ static const struct inode_operations btrfs_dir_ro_inode_operations = { | |||
| 5544 | .permission = btrfs_permission, | 5842 | .permission = btrfs_permission, |
| 5545 | }; | 5843 | }; |
| 5546 | 5844 | ||
| 5547 | static struct file_operations btrfs_dir_file_operations = { | 5845 | static const struct file_operations btrfs_dir_file_operations = { |
| 5548 | .llseek = generic_file_llseek, | 5846 | .llseek = generic_file_llseek, |
| 5549 | .read = generic_read_dir, | 5847 | .read = generic_read_dir, |
| 5550 | .readdir = btrfs_real_readdir, | 5848 | .readdir = btrfs_real_readdir, |
| @@ -5566,6 +5864,8 @@ static struct extent_io_ops btrfs_extent_io_ops = { | |||
| 5566 | .readpage_io_failed_hook = btrfs_io_failed_hook, | 5864 | .readpage_io_failed_hook = btrfs_io_failed_hook, |
| 5567 | .set_bit_hook = btrfs_set_bit_hook, | 5865 | .set_bit_hook = btrfs_set_bit_hook, |
| 5568 | .clear_bit_hook = btrfs_clear_bit_hook, | 5866 | .clear_bit_hook = btrfs_clear_bit_hook, |
| 5867 | .merge_extent_hook = btrfs_merge_extent_hook, | ||
| 5868 | .split_extent_hook = btrfs_split_extent_hook, | ||
| 5569 | }; | 5869 | }; |
| 5570 | 5870 | ||
| 5571 | /* | 5871 | /* |
| @@ -5632,6 +5932,6 @@ static const struct inode_operations btrfs_symlink_inode_operations = { | |||
| 5632 | .removexattr = btrfs_removexattr, | 5932 | .removexattr = btrfs_removexattr, |
| 5633 | }; | 5933 | }; |
| 5634 | 5934 | ||
| 5635 | struct dentry_operations btrfs_dentry_operations = { | 5935 | const struct dentry_operations btrfs_dentry_operations = { |
| 5636 | .d_delete = btrfs_dentry_delete, | 5936 | .d_delete = btrfs_dentry_delete, |
| 5637 | }; | 5937 | }; |
