diff options
Diffstat (limited to 'fs/ext4/inode.c')
-rw-r--r-- | fs/ext4/inode.c | 360 |
1 files changed, 225 insertions, 135 deletions
diff --git a/fs/ext4/inode.c b/fs/ext4/inode.c index 5489703d9573..bb717cbb749c 100644 --- a/fs/ext4/inode.c +++ b/fs/ext4/inode.c | |||
@@ -105,7 +105,7 @@ int ext4_forget(handle_t *handle, int is_metadata, struct inode *inode, | |||
105 | */ | 105 | */ |
106 | static unsigned long blocks_for_truncate(struct inode *inode) | 106 | static unsigned long blocks_for_truncate(struct inode *inode) |
107 | { | 107 | { |
108 | unsigned long needed; | 108 | ext4_lblk_t needed; |
109 | 109 | ||
110 | needed = inode->i_blocks >> (inode->i_sb->s_blocksize_bits - 9); | 110 | needed = inode->i_blocks >> (inode->i_sb->s_blocksize_bits - 9); |
111 | 111 | ||
@@ -243,13 +243,6 @@ static inline void add_chain(Indirect *p, struct buffer_head *bh, __le32 *v) | |||
243 | p->bh = bh; | 243 | p->bh = bh; |
244 | } | 244 | } |
245 | 245 | ||
246 | static int verify_chain(Indirect *from, Indirect *to) | ||
247 | { | ||
248 | while (from <= to && from->key == *from->p) | ||
249 | from++; | ||
250 | return (from > to); | ||
251 | } | ||
252 | |||
253 | /** | 246 | /** |
254 | * ext4_block_to_path - parse the block number into array of offsets | 247 | * ext4_block_to_path - parse the block number into array of offsets |
255 | * @inode: inode in question (we are only interested in its superblock) | 248 | * @inode: inode in question (we are only interested in its superblock) |
@@ -282,7 +275,8 @@ static int verify_chain(Indirect *from, Indirect *to) | |||
282 | */ | 275 | */ |
283 | 276 | ||
284 | static int ext4_block_to_path(struct inode *inode, | 277 | static int ext4_block_to_path(struct inode *inode, |
285 | long i_block, int offsets[4], int *boundary) | 278 | ext4_lblk_t i_block, |
279 | ext4_lblk_t offsets[4], int *boundary) | ||
286 | { | 280 | { |
287 | int ptrs = EXT4_ADDR_PER_BLOCK(inode->i_sb); | 281 | int ptrs = EXT4_ADDR_PER_BLOCK(inode->i_sb); |
288 | int ptrs_bits = EXT4_ADDR_PER_BLOCK_BITS(inode->i_sb); | 282 | int ptrs_bits = EXT4_ADDR_PER_BLOCK_BITS(inode->i_sb); |
@@ -313,7 +307,10 @@ static int ext4_block_to_path(struct inode *inode, | |||
313 | offsets[n++] = i_block & (ptrs - 1); | 307 | offsets[n++] = i_block & (ptrs - 1); |
314 | final = ptrs; | 308 | final = ptrs; |
315 | } else { | 309 | } else { |
316 | ext4_warning(inode->i_sb, "ext4_block_to_path", "block > big"); | 310 | ext4_warning(inode->i_sb, "ext4_block_to_path", |
311 | "block %lu > max", | ||
312 | i_block + direct_blocks + | ||
313 | indirect_blocks + double_blocks); | ||
317 | } | 314 | } |
318 | if (boundary) | 315 | if (boundary) |
319 | *boundary = final - 1 - (i_block & (ptrs - 1)); | 316 | *boundary = final - 1 - (i_block & (ptrs - 1)); |
@@ -344,12 +341,14 @@ static int ext4_block_to_path(struct inode *inode, | |||
344 | * (pointer to last triple returned, *@err == 0) | 341 | * (pointer to last triple returned, *@err == 0) |
345 | * or when it gets an IO error reading an indirect block | 342 | * or when it gets an IO error reading an indirect block |
346 | * (ditto, *@err == -EIO) | 343 | * (ditto, *@err == -EIO) |
347 | * or when it notices that chain had been changed while it was reading | ||
348 | * (ditto, *@err == -EAGAIN) | ||
349 | * or when it reads all @depth-1 indirect blocks successfully and finds | 344 | * or when it reads all @depth-1 indirect blocks successfully and finds |
350 | * the whole chain, all way to the data (returns %NULL, *err == 0). | 345 | * the whole chain, all way to the data (returns %NULL, *err == 0). |
346 | * | ||
347 | * Need to be called with | ||
348 | * down_read(&EXT4_I(inode)->i_data_sem) | ||
351 | */ | 349 | */ |
352 | static Indirect *ext4_get_branch(struct inode *inode, int depth, int *offsets, | 350 | static Indirect *ext4_get_branch(struct inode *inode, int depth, |
351 | ext4_lblk_t *offsets, | ||
353 | Indirect chain[4], int *err) | 352 | Indirect chain[4], int *err) |
354 | { | 353 | { |
355 | struct super_block *sb = inode->i_sb; | 354 | struct super_block *sb = inode->i_sb; |
@@ -365,9 +364,6 @@ static Indirect *ext4_get_branch(struct inode *inode, int depth, int *offsets, | |||
365 | bh = sb_bread(sb, le32_to_cpu(p->key)); | 364 | bh = sb_bread(sb, le32_to_cpu(p->key)); |
366 | if (!bh) | 365 | if (!bh) |
367 | goto failure; | 366 | goto failure; |
368 | /* Reader: pointers */ | ||
369 | if (!verify_chain(chain, p)) | ||
370 | goto changed; | ||
371 | add_chain(++p, bh, (__le32*)bh->b_data + *++offsets); | 367 | add_chain(++p, bh, (__le32*)bh->b_data + *++offsets); |
372 | /* Reader: end */ | 368 | /* Reader: end */ |
373 | if (!p->key) | 369 | if (!p->key) |
@@ -375,10 +371,6 @@ static Indirect *ext4_get_branch(struct inode *inode, int depth, int *offsets, | |||
375 | } | 371 | } |
376 | return NULL; | 372 | return NULL; |
377 | 373 | ||
378 | changed: | ||
379 | brelse(bh); | ||
380 | *err = -EAGAIN; | ||
381 | goto no_block; | ||
382 | failure: | 374 | failure: |
383 | *err = -EIO; | 375 | *err = -EIO; |
384 | no_block: | 376 | no_block: |
@@ -445,7 +437,7 @@ static ext4_fsblk_t ext4_find_near(struct inode *inode, Indirect *ind) | |||
445 | * stores it in *@goal and returns zero. | 437 | * stores it in *@goal and returns zero. |
446 | */ | 438 | */ |
447 | 439 | ||
448 | static ext4_fsblk_t ext4_find_goal(struct inode *inode, long block, | 440 | static ext4_fsblk_t ext4_find_goal(struct inode *inode, ext4_lblk_t block, |
449 | Indirect chain[4], Indirect *partial) | 441 | Indirect chain[4], Indirect *partial) |
450 | { | 442 | { |
451 | struct ext4_block_alloc_info *block_i; | 443 | struct ext4_block_alloc_info *block_i; |
@@ -559,7 +551,7 @@ static int ext4_alloc_blocks(handle_t *handle, struct inode *inode, | |||
559 | return ret; | 551 | return ret; |
560 | failed_out: | 552 | failed_out: |
561 | for (i = 0; i <index; i++) | 553 | for (i = 0; i <index; i++) |
562 | ext4_free_blocks(handle, inode, new_blocks[i], 1); | 554 | ext4_free_blocks(handle, inode, new_blocks[i], 1, 0); |
563 | return ret; | 555 | return ret; |
564 | } | 556 | } |
565 | 557 | ||
@@ -590,7 +582,7 @@ failed_out: | |||
590 | */ | 582 | */ |
591 | static int ext4_alloc_branch(handle_t *handle, struct inode *inode, | 583 | static int ext4_alloc_branch(handle_t *handle, struct inode *inode, |
592 | int indirect_blks, int *blks, ext4_fsblk_t goal, | 584 | int indirect_blks, int *blks, ext4_fsblk_t goal, |
593 | int *offsets, Indirect *branch) | 585 | ext4_lblk_t *offsets, Indirect *branch) |
594 | { | 586 | { |
595 | int blocksize = inode->i_sb->s_blocksize; | 587 | int blocksize = inode->i_sb->s_blocksize; |
596 | int i, n = 0; | 588 | int i, n = 0; |
@@ -658,9 +650,9 @@ failed: | |||
658 | ext4_journal_forget(handle, branch[i].bh); | 650 | ext4_journal_forget(handle, branch[i].bh); |
659 | } | 651 | } |
660 | for (i = 0; i <indirect_blks; i++) | 652 | for (i = 0; i <indirect_blks; i++) |
661 | ext4_free_blocks(handle, inode, new_blocks[i], 1); | 653 | ext4_free_blocks(handle, inode, new_blocks[i], 1, 0); |
662 | 654 | ||
663 | ext4_free_blocks(handle, inode, new_blocks[i], num); | 655 | ext4_free_blocks(handle, inode, new_blocks[i], num, 0); |
664 | 656 | ||
665 | return err; | 657 | return err; |
666 | } | 658 | } |
@@ -680,7 +672,7 @@ failed: | |||
680 | * chain to new block and return 0. | 672 | * chain to new block and return 0. |
681 | */ | 673 | */ |
682 | static int ext4_splice_branch(handle_t *handle, struct inode *inode, | 674 | static int ext4_splice_branch(handle_t *handle, struct inode *inode, |
683 | long block, Indirect *where, int num, int blks) | 675 | ext4_lblk_t block, Indirect *where, int num, int blks) |
684 | { | 676 | { |
685 | int i; | 677 | int i; |
686 | int err = 0; | 678 | int err = 0; |
@@ -757,9 +749,10 @@ err_out: | |||
757 | for (i = 1; i <= num; i++) { | 749 | for (i = 1; i <= num; i++) { |
758 | BUFFER_TRACE(where[i].bh, "call jbd2_journal_forget"); | 750 | BUFFER_TRACE(where[i].bh, "call jbd2_journal_forget"); |
759 | ext4_journal_forget(handle, where[i].bh); | 751 | ext4_journal_forget(handle, where[i].bh); |
760 | ext4_free_blocks(handle,inode,le32_to_cpu(where[i-1].key),1); | 752 | ext4_free_blocks(handle, inode, |
753 | le32_to_cpu(where[i-1].key), 1, 0); | ||
761 | } | 754 | } |
762 | ext4_free_blocks(handle, inode, le32_to_cpu(where[num].key), blks); | 755 | ext4_free_blocks(handle, inode, le32_to_cpu(where[num].key), blks, 0); |
763 | 756 | ||
764 | return err; | 757 | return err; |
765 | } | 758 | } |
@@ -782,14 +775,19 @@ err_out: | |||
782 | * return > 0, # of blocks mapped or allocated. | 775 | * return > 0, # of blocks mapped or allocated. |
783 | * return = 0, if plain lookup failed. | 776 | * return = 0, if plain lookup failed. |
784 | * return < 0, error case. | 777 | * return < 0, error case. |
778 | * | ||
779 | * | ||
780 | * Need to be called with | ||
781 | * down_read(&EXT4_I(inode)->i_data_sem) if not allocating file system block | ||
782 | * (ie, create is zero). Otherwise down_write(&EXT4_I(inode)->i_data_sem) | ||
785 | */ | 783 | */ |
786 | int ext4_get_blocks_handle(handle_t *handle, struct inode *inode, | 784 | int ext4_get_blocks_handle(handle_t *handle, struct inode *inode, |
787 | sector_t iblock, unsigned long maxblocks, | 785 | ext4_lblk_t iblock, unsigned long maxblocks, |
788 | struct buffer_head *bh_result, | 786 | struct buffer_head *bh_result, |
789 | int create, int extend_disksize) | 787 | int create, int extend_disksize) |
790 | { | 788 | { |
791 | int err = -EIO; | 789 | int err = -EIO; |
792 | int offsets[4]; | 790 | ext4_lblk_t offsets[4]; |
793 | Indirect chain[4]; | 791 | Indirect chain[4]; |
794 | Indirect *partial; | 792 | Indirect *partial; |
795 | ext4_fsblk_t goal; | 793 | ext4_fsblk_t goal; |
@@ -803,7 +801,8 @@ int ext4_get_blocks_handle(handle_t *handle, struct inode *inode, | |||
803 | 801 | ||
804 | J_ASSERT(!(EXT4_I(inode)->i_flags & EXT4_EXTENTS_FL)); | 802 | J_ASSERT(!(EXT4_I(inode)->i_flags & EXT4_EXTENTS_FL)); |
805 | J_ASSERT(handle != NULL || create == 0); | 803 | J_ASSERT(handle != NULL || create == 0); |
806 | depth = ext4_block_to_path(inode,iblock,offsets,&blocks_to_boundary); | 804 | depth = ext4_block_to_path(inode, iblock, offsets, |
805 | &blocks_to_boundary); | ||
807 | 806 | ||
808 | if (depth == 0) | 807 | if (depth == 0) |
809 | goto out; | 808 | goto out; |
@@ -819,18 +818,6 @@ int ext4_get_blocks_handle(handle_t *handle, struct inode *inode, | |||
819 | while (count < maxblocks && count <= blocks_to_boundary) { | 818 | while (count < maxblocks && count <= blocks_to_boundary) { |
820 | ext4_fsblk_t blk; | 819 | ext4_fsblk_t blk; |
821 | 820 | ||
822 | if (!verify_chain(chain, partial)) { | ||
823 | /* | ||
824 | * Indirect block might be removed by | ||
825 | * truncate while we were reading it. | ||
826 | * Handling of that case: forget what we've | ||
827 | * got now. Flag the err as EAGAIN, so it | ||
828 | * will reread. | ||
829 | */ | ||
830 | err = -EAGAIN; | ||
831 | count = 0; | ||
832 | break; | ||
833 | } | ||
834 | blk = le32_to_cpu(*(chain[depth-1].p + count)); | 821 | blk = le32_to_cpu(*(chain[depth-1].p + count)); |
835 | 822 | ||
836 | if (blk == first_block + count) | 823 | if (blk == first_block + count) |
@@ -838,44 +825,13 @@ int ext4_get_blocks_handle(handle_t *handle, struct inode *inode, | |||
838 | else | 825 | else |
839 | break; | 826 | break; |
840 | } | 827 | } |
841 | if (err != -EAGAIN) | 828 | goto got_it; |
842 | goto got_it; | ||
843 | } | 829 | } |
844 | 830 | ||
845 | /* Next simple case - plain lookup or failed read of indirect block */ | 831 | /* Next simple case - plain lookup or failed read of indirect block */ |
846 | if (!create || err == -EIO) | 832 | if (!create || err == -EIO) |
847 | goto cleanup; | 833 | goto cleanup; |
848 | 834 | ||
849 | mutex_lock(&ei->truncate_mutex); | ||
850 | |||
851 | /* | ||
852 | * If the indirect block is missing while we are reading | ||
853 | * the chain(ext4_get_branch() returns -EAGAIN err), or | ||
854 | * if the chain has been changed after we grab the semaphore, | ||
855 | * (either because another process truncated this branch, or | ||
856 | * another get_block allocated this branch) re-grab the chain to see if | ||
857 | * the request block has been allocated or not. | ||
858 | * | ||
859 | * Since we already block the truncate/other get_block | ||
860 | * at this point, we will have the current copy of the chain when we | ||
861 | * splice the branch into the tree. | ||
862 | */ | ||
863 | if (err == -EAGAIN || !verify_chain(chain, partial)) { | ||
864 | while (partial > chain) { | ||
865 | brelse(partial->bh); | ||
866 | partial--; | ||
867 | } | ||
868 | partial = ext4_get_branch(inode, depth, offsets, chain, &err); | ||
869 | if (!partial) { | ||
870 | count++; | ||
871 | mutex_unlock(&ei->truncate_mutex); | ||
872 | if (err) | ||
873 | goto cleanup; | ||
874 | clear_buffer_new(bh_result); | ||
875 | goto got_it; | ||
876 | } | ||
877 | } | ||
878 | |||
879 | /* | 835 | /* |
880 | * Okay, we need to do block allocation. Lazily initialize the block | 836 | * Okay, we need to do block allocation. Lazily initialize the block |
881 | * allocation info here if necessary | 837 | * allocation info here if necessary |
@@ -911,13 +867,12 @@ int ext4_get_blocks_handle(handle_t *handle, struct inode *inode, | |||
911 | err = ext4_splice_branch(handle, inode, iblock, | 867 | err = ext4_splice_branch(handle, inode, iblock, |
912 | partial, indirect_blks, count); | 868 | partial, indirect_blks, count); |
913 | /* | 869 | /* |
914 | * i_disksize growing is protected by truncate_mutex. Don't forget to | 870 | * i_disksize growing is protected by i_data_sem. Don't forget to |
915 | * protect it if you're about to implement concurrent | 871 | * protect it if you're about to implement concurrent |
916 | * ext4_get_block() -bzzz | 872 | * ext4_get_block() -bzzz |
917 | */ | 873 | */ |
918 | if (!err && extend_disksize && inode->i_size > ei->i_disksize) | 874 | if (!err && extend_disksize && inode->i_size > ei->i_disksize) |
919 | ei->i_disksize = inode->i_size; | 875 | ei->i_disksize = inode->i_size; |
920 | mutex_unlock(&ei->truncate_mutex); | ||
921 | if (err) | 876 | if (err) |
922 | goto cleanup; | 877 | goto cleanup; |
923 | 878 | ||
@@ -942,6 +897,47 @@ out: | |||
942 | 897 | ||
943 | #define DIO_CREDITS (EXT4_RESERVE_TRANS_BLOCKS + 32) | 898 | #define DIO_CREDITS (EXT4_RESERVE_TRANS_BLOCKS + 32) |
944 | 899 | ||
900 | int ext4_get_blocks_wrap(handle_t *handle, struct inode *inode, sector_t block, | ||
901 | unsigned long max_blocks, struct buffer_head *bh, | ||
902 | int create, int extend_disksize) | ||
903 | { | ||
904 | int retval; | ||
905 | /* | ||
906 | * Try to see if we can get the block without requesting | ||
907 | * for new file system block. | ||
908 | */ | ||
909 | down_read((&EXT4_I(inode)->i_data_sem)); | ||
910 | if (EXT4_I(inode)->i_flags & EXT4_EXTENTS_FL) { | ||
911 | retval = ext4_ext_get_blocks(handle, inode, block, max_blocks, | ||
912 | bh, 0, 0); | ||
913 | } else { | ||
914 | retval = ext4_get_blocks_handle(handle, | ||
915 | inode, block, max_blocks, bh, 0, 0); | ||
916 | } | ||
917 | up_read((&EXT4_I(inode)->i_data_sem)); | ||
918 | if (!create || (retval > 0)) | ||
919 | return retval; | ||
920 | |||
921 | /* | ||
922 | * We need to allocate new blocks which will result | ||
923 | * in i_data update | ||
924 | */ | ||
925 | down_write((&EXT4_I(inode)->i_data_sem)); | ||
926 | /* | ||
927 | * We need to check for EXT4 here because migrate | ||
928 | * could have changed the inode type in between | ||
929 | */ | ||
930 | if (EXT4_I(inode)->i_flags & EXT4_EXTENTS_FL) { | ||
931 | retval = ext4_ext_get_blocks(handle, inode, block, max_blocks, | ||
932 | bh, create, extend_disksize); | ||
933 | } else { | ||
934 | retval = ext4_get_blocks_handle(handle, inode, block, | ||
935 | max_blocks, bh, create, extend_disksize); | ||
936 | } | ||
937 | up_write((&EXT4_I(inode)->i_data_sem)); | ||
938 | return retval; | ||
939 | } | ||
940 | |||
945 | static int ext4_get_block(struct inode *inode, sector_t iblock, | 941 | static int ext4_get_block(struct inode *inode, sector_t iblock, |
946 | struct buffer_head *bh_result, int create) | 942 | struct buffer_head *bh_result, int create) |
947 | { | 943 | { |
@@ -996,7 +992,7 @@ get_block: | |||
996 | * `handle' can be NULL if create is zero | 992 | * `handle' can be NULL if create is zero |
997 | */ | 993 | */ |
998 | struct buffer_head *ext4_getblk(handle_t *handle, struct inode *inode, | 994 | struct buffer_head *ext4_getblk(handle_t *handle, struct inode *inode, |
999 | long block, int create, int *errp) | 995 | ext4_lblk_t block, int create, int *errp) |
1000 | { | 996 | { |
1001 | struct buffer_head dummy; | 997 | struct buffer_head dummy; |
1002 | int fatal = 0, err; | 998 | int fatal = 0, err; |
@@ -1063,7 +1059,7 @@ err: | |||
1063 | } | 1059 | } |
1064 | 1060 | ||
1065 | struct buffer_head *ext4_bread(handle_t *handle, struct inode *inode, | 1061 | struct buffer_head *ext4_bread(handle_t *handle, struct inode *inode, |
1066 | int block, int create, int *err) | 1062 | ext4_lblk_t block, int create, int *err) |
1067 | { | 1063 | { |
1068 | struct buffer_head * bh; | 1064 | struct buffer_head * bh; |
1069 | 1065 | ||
@@ -1446,7 +1442,7 @@ static int jbd2_journal_dirty_data_fn(handle_t *handle, struct buffer_head *bh) | |||
1446 | * ext4_file_write() -> generic_file_write() -> __alloc_pages() -> ... | 1442 | * ext4_file_write() -> generic_file_write() -> __alloc_pages() -> ... |
1447 | * | 1443 | * |
1448 | * Same applies to ext4_get_block(). We will deadlock on various things like | 1444 | * Same applies to ext4_get_block(). We will deadlock on various things like |
1449 | * lock_journal and i_truncate_mutex. | 1445 | * lock_journal and i_data_sem |
1450 | * | 1446 | * |
1451 | * Setting PF_MEMALLOC here doesn't work - too many internal memory | 1447 | * Setting PF_MEMALLOC here doesn't work - too many internal memory |
1452 | * allocations fail. | 1448 | * allocations fail. |
@@ -1828,7 +1824,8 @@ int ext4_block_truncate_page(handle_t *handle, struct page *page, | |||
1828 | { | 1824 | { |
1829 | ext4_fsblk_t index = from >> PAGE_CACHE_SHIFT; | 1825 | ext4_fsblk_t index = from >> PAGE_CACHE_SHIFT; |
1830 | unsigned offset = from & (PAGE_CACHE_SIZE-1); | 1826 | unsigned offset = from & (PAGE_CACHE_SIZE-1); |
1831 | unsigned blocksize, iblock, length, pos; | 1827 | unsigned blocksize, length, pos; |
1828 | ext4_lblk_t iblock; | ||
1832 | struct inode *inode = mapping->host; | 1829 | struct inode *inode = mapping->host; |
1833 | struct buffer_head *bh; | 1830 | struct buffer_head *bh; |
1834 | int err = 0; | 1831 | int err = 0; |
@@ -1964,7 +1961,7 @@ static inline int all_zeroes(__le32 *p, __le32 *q) | |||
1964 | * (no partially truncated stuff there). */ | 1961 | * (no partially truncated stuff there). */ |
1965 | 1962 | ||
1966 | static Indirect *ext4_find_shared(struct inode *inode, int depth, | 1963 | static Indirect *ext4_find_shared(struct inode *inode, int depth, |
1967 | int offsets[4], Indirect chain[4], __le32 *top) | 1964 | ext4_lblk_t offsets[4], Indirect chain[4], __le32 *top) |
1968 | { | 1965 | { |
1969 | Indirect *partial, *p; | 1966 | Indirect *partial, *p; |
1970 | int k, err; | 1967 | int k, err; |
@@ -2048,15 +2045,15 @@ static void ext4_clear_blocks(handle_t *handle, struct inode *inode, | |||
2048 | for (p = first; p < last; p++) { | 2045 | for (p = first; p < last; p++) { |
2049 | u32 nr = le32_to_cpu(*p); | 2046 | u32 nr = le32_to_cpu(*p); |
2050 | if (nr) { | 2047 | if (nr) { |
2051 | struct buffer_head *bh; | 2048 | struct buffer_head *tbh; |
2052 | 2049 | ||
2053 | *p = 0; | 2050 | *p = 0; |
2054 | bh = sb_find_get_block(inode->i_sb, nr); | 2051 | tbh = sb_find_get_block(inode->i_sb, nr); |
2055 | ext4_forget(handle, 0, inode, bh, nr); | 2052 | ext4_forget(handle, 0, inode, tbh, nr); |
2056 | } | 2053 | } |
2057 | } | 2054 | } |
2058 | 2055 | ||
2059 | ext4_free_blocks(handle, inode, block_to_free, count); | 2056 | ext4_free_blocks(handle, inode, block_to_free, count, 0); |
2060 | } | 2057 | } |
2061 | 2058 | ||
2062 | /** | 2059 | /** |
@@ -2229,7 +2226,7 @@ static void ext4_free_branches(handle_t *handle, struct inode *inode, | |||
2229 | ext4_journal_test_restart(handle, inode); | 2226 | ext4_journal_test_restart(handle, inode); |
2230 | } | 2227 | } |
2231 | 2228 | ||
2232 | ext4_free_blocks(handle, inode, nr, 1); | 2229 | ext4_free_blocks(handle, inode, nr, 1, 1); |
2233 | 2230 | ||
2234 | if (parent_bh) { | 2231 | if (parent_bh) { |
2235 | /* | 2232 | /* |
@@ -2289,12 +2286,12 @@ void ext4_truncate(struct inode *inode) | |||
2289 | __le32 *i_data = ei->i_data; | 2286 | __le32 *i_data = ei->i_data; |
2290 | int addr_per_block = EXT4_ADDR_PER_BLOCK(inode->i_sb); | 2287 | int addr_per_block = EXT4_ADDR_PER_BLOCK(inode->i_sb); |
2291 | struct address_space *mapping = inode->i_mapping; | 2288 | struct address_space *mapping = inode->i_mapping; |
2292 | int offsets[4]; | 2289 | ext4_lblk_t offsets[4]; |
2293 | Indirect chain[4]; | 2290 | Indirect chain[4]; |
2294 | Indirect *partial; | 2291 | Indirect *partial; |
2295 | __le32 nr = 0; | 2292 | __le32 nr = 0; |
2296 | int n; | 2293 | int n; |
2297 | long last_block; | 2294 | ext4_lblk_t last_block; |
2298 | unsigned blocksize = inode->i_sb->s_blocksize; | 2295 | unsigned blocksize = inode->i_sb->s_blocksize; |
2299 | struct page *page; | 2296 | struct page *page; |
2300 | 2297 | ||
@@ -2320,8 +2317,10 @@ void ext4_truncate(struct inode *inode) | |||
2320 | return; | 2317 | return; |
2321 | } | 2318 | } |
2322 | 2319 | ||
2323 | if (EXT4_I(inode)->i_flags & EXT4_EXTENTS_FL) | 2320 | if (EXT4_I(inode)->i_flags & EXT4_EXTENTS_FL) { |
2324 | return ext4_ext_truncate(inode, page); | 2321 | ext4_ext_truncate(inode, page); |
2322 | return; | ||
2323 | } | ||
2325 | 2324 | ||
2326 | handle = start_transaction(inode); | 2325 | handle = start_transaction(inode); |
2327 | if (IS_ERR(handle)) { | 2326 | if (IS_ERR(handle)) { |
@@ -2369,7 +2368,7 @@ void ext4_truncate(struct inode *inode) | |||
2369 | * From here we block out all ext4_get_block() callers who want to | 2368 | * From here we block out all ext4_get_block() callers who want to |
2370 | * modify the block allocation tree. | 2369 | * modify the block allocation tree. |
2371 | */ | 2370 | */ |
2372 | mutex_lock(&ei->truncate_mutex); | 2371 | down_write(&ei->i_data_sem); |
2373 | 2372 | ||
2374 | if (n == 1) { /* direct blocks */ | 2373 | if (n == 1) { /* direct blocks */ |
2375 | ext4_free_data(handle, inode, NULL, i_data+offsets[0], | 2374 | ext4_free_data(handle, inode, NULL, i_data+offsets[0], |
@@ -2433,7 +2432,7 @@ do_indirects: | |||
2433 | 2432 | ||
2434 | ext4_discard_reservation(inode); | 2433 | ext4_discard_reservation(inode); |
2435 | 2434 | ||
2436 | mutex_unlock(&ei->truncate_mutex); | 2435 | up_write(&ei->i_data_sem); |
2437 | inode->i_mtime = inode->i_ctime = ext4_current_time(inode); | 2436 | inode->i_mtime = inode->i_ctime = ext4_current_time(inode); |
2438 | ext4_mark_inode_dirty(handle, inode); | 2437 | ext4_mark_inode_dirty(handle, inode); |
2439 | 2438 | ||
@@ -2460,7 +2459,8 @@ out_stop: | |||
2460 | static ext4_fsblk_t ext4_get_inode_block(struct super_block *sb, | 2459 | static ext4_fsblk_t ext4_get_inode_block(struct super_block *sb, |
2461 | unsigned long ino, struct ext4_iloc *iloc) | 2460 | unsigned long ino, struct ext4_iloc *iloc) |
2462 | { | 2461 | { |
2463 | unsigned long desc, group_desc, block_group; | 2462 | unsigned long desc, group_desc; |
2463 | ext4_group_t block_group; | ||
2464 | unsigned long offset; | 2464 | unsigned long offset; |
2465 | ext4_fsblk_t block; | 2465 | ext4_fsblk_t block; |
2466 | struct buffer_head *bh; | 2466 | struct buffer_head *bh; |
@@ -2547,7 +2547,7 @@ static int __ext4_get_inode_loc(struct inode *inode, | |||
2547 | struct ext4_group_desc *desc; | 2547 | struct ext4_group_desc *desc; |
2548 | int inodes_per_buffer; | 2548 | int inodes_per_buffer; |
2549 | int inode_offset, i; | 2549 | int inode_offset, i; |
2550 | int block_group; | 2550 | ext4_group_t block_group; |
2551 | int start; | 2551 | int start; |
2552 | 2552 | ||
2553 | block_group = (inode->i_ino - 1) / | 2553 | block_group = (inode->i_ino - 1) / |
@@ -2660,6 +2660,28 @@ void ext4_get_inode_flags(struct ext4_inode_info *ei) | |||
2660 | if (flags & S_DIRSYNC) | 2660 | if (flags & S_DIRSYNC) |
2661 | ei->i_flags |= EXT4_DIRSYNC_FL; | 2661 | ei->i_flags |= EXT4_DIRSYNC_FL; |
2662 | } | 2662 | } |
2663 | static blkcnt_t ext4_inode_blocks(struct ext4_inode *raw_inode, | ||
2664 | struct ext4_inode_info *ei) | ||
2665 | { | ||
2666 | blkcnt_t i_blocks ; | ||
2667 | struct inode *inode = &(ei->vfs_inode); | ||
2668 | struct super_block *sb = inode->i_sb; | ||
2669 | |||
2670 | if (EXT4_HAS_RO_COMPAT_FEATURE(sb, | ||
2671 | EXT4_FEATURE_RO_COMPAT_HUGE_FILE)) { | ||
2672 | /* we are using combined 48 bit field */ | ||
2673 | i_blocks = ((u64)le16_to_cpu(raw_inode->i_blocks_high)) << 32 | | ||
2674 | le32_to_cpu(raw_inode->i_blocks_lo); | ||
2675 | if (ei->i_flags & EXT4_HUGE_FILE_FL) { | ||
2676 | /* i_blocks represent file system block size */ | ||
2677 | return i_blocks << (inode->i_blkbits - 9); | ||
2678 | } else { | ||
2679 | return i_blocks; | ||
2680 | } | ||
2681 | } else { | ||
2682 | return le32_to_cpu(raw_inode->i_blocks_lo); | ||
2683 | } | ||
2684 | } | ||
2663 | 2685 | ||
2664 | void ext4_read_inode(struct inode * inode) | 2686 | void ext4_read_inode(struct inode * inode) |
2665 | { | 2687 | { |
@@ -2687,7 +2709,6 @@ void ext4_read_inode(struct inode * inode) | |||
2687 | inode->i_gid |= le16_to_cpu(raw_inode->i_gid_high) << 16; | 2709 | inode->i_gid |= le16_to_cpu(raw_inode->i_gid_high) << 16; |
2688 | } | 2710 | } |
2689 | inode->i_nlink = le16_to_cpu(raw_inode->i_links_count); | 2711 | inode->i_nlink = le16_to_cpu(raw_inode->i_links_count); |
2690 | inode->i_size = le32_to_cpu(raw_inode->i_size); | ||
2691 | 2712 | ||
2692 | ei->i_state = 0; | 2713 | ei->i_state = 0; |
2693 | ei->i_dir_start_lookup = 0; | 2714 | ei->i_dir_start_lookup = 0; |
@@ -2709,19 +2730,15 @@ void ext4_read_inode(struct inode * inode) | |||
2709 | * recovery code: that's fine, we're about to complete | 2730 | * recovery code: that's fine, we're about to complete |
2710 | * the process of deleting those. */ | 2731 | * the process of deleting those. */ |
2711 | } | 2732 | } |
2712 | inode->i_blocks = le32_to_cpu(raw_inode->i_blocks); | ||
2713 | ei->i_flags = le32_to_cpu(raw_inode->i_flags); | 2733 | ei->i_flags = le32_to_cpu(raw_inode->i_flags); |
2714 | ei->i_file_acl = le32_to_cpu(raw_inode->i_file_acl); | 2734 | inode->i_blocks = ext4_inode_blocks(raw_inode, ei); |
2735 | ei->i_file_acl = le32_to_cpu(raw_inode->i_file_acl_lo); | ||
2715 | if (EXT4_SB(inode->i_sb)->s_es->s_creator_os != | 2736 | if (EXT4_SB(inode->i_sb)->s_es->s_creator_os != |
2716 | cpu_to_le32(EXT4_OS_HURD)) | 2737 | cpu_to_le32(EXT4_OS_HURD)) { |
2717 | ei->i_file_acl |= | 2738 | ei->i_file_acl |= |
2718 | ((__u64)le16_to_cpu(raw_inode->i_file_acl_high)) << 32; | 2739 | ((__u64)le16_to_cpu(raw_inode->i_file_acl_high)) << 32; |
2719 | if (!S_ISREG(inode->i_mode)) { | ||
2720 | ei->i_dir_acl = le32_to_cpu(raw_inode->i_dir_acl); | ||
2721 | } else { | ||
2722 | inode->i_size |= | ||
2723 | ((__u64)le32_to_cpu(raw_inode->i_size_high)) << 32; | ||
2724 | } | 2740 | } |
2741 | inode->i_size = ext4_isize(raw_inode); | ||
2725 | ei->i_disksize = inode->i_size; | 2742 | ei->i_disksize = inode->i_size; |
2726 | inode->i_generation = le32_to_cpu(raw_inode->i_generation); | 2743 | inode->i_generation = le32_to_cpu(raw_inode->i_generation); |
2727 | ei->i_block_group = iloc.block_group; | 2744 | ei->i_block_group = iloc.block_group; |
@@ -2765,6 +2782,13 @@ void ext4_read_inode(struct inode * inode) | |||
2765 | EXT4_INODE_GET_XTIME(i_atime, inode, raw_inode); | 2782 | EXT4_INODE_GET_XTIME(i_atime, inode, raw_inode); |
2766 | EXT4_EINODE_GET_XTIME(i_crtime, ei, raw_inode); | 2783 | EXT4_EINODE_GET_XTIME(i_crtime, ei, raw_inode); |
2767 | 2784 | ||
2785 | inode->i_version = le32_to_cpu(raw_inode->i_disk_version); | ||
2786 | if (EXT4_INODE_SIZE(inode->i_sb) > EXT4_GOOD_OLD_INODE_SIZE) { | ||
2787 | if (EXT4_FITS_IN_INODE(raw_inode, ei, i_version_hi)) | ||
2788 | inode->i_version |= | ||
2789 | (__u64)(le32_to_cpu(raw_inode->i_version_hi)) << 32; | ||
2790 | } | ||
2791 | |||
2768 | if (S_ISREG(inode->i_mode)) { | 2792 | if (S_ISREG(inode->i_mode)) { |
2769 | inode->i_op = &ext4_file_inode_operations; | 2793 | inode->i_op = &ext4_file_inode_operations; |
2770 | inode->i_fop = &ext4_file_operations; | 2794 | inode->i_fop = &ext4_file_operations; |
@@ -2797,6 +2821,55 @@ bad_inode: | |||
2797 | return; | 2821 | return; |
2798 | } | 2822 | } |
2799 | 2823 | ||
2824 | static int ext4_inode_blocks_set(handle_t *handle, | ||
2825 | struct ext4_inode *raw_inode, | ||
2826 | struct ext4_inode_info *ei) | ||
2827 | { | ||
2828 | struct inode *inode = &(ei->vfs_inode); | ||
2829 | u64 i_blocks = inode->i_blocks; | ||
2830 | struct super_block *sb = inode->i_sb; | ||
2831 | int err = 0; | ||
2832 | |||
2833 | if (i_blocks <= ~0U) { | ||
2834 | /* | ||
2835 | * i_blocks can be represnted in a 32 bit variable | ||
2836 | * as multiple of 512 bytes | ||
2837 | */ | ||
2838 | raw_inode->i_blocks_lo = cpu_to_le32(i_blocks); | ||
2839 | raw_inode->i_blocks_high = 0; | ||
2840 | ei->i_flags &= ~EXT4_HUGE_FILE_FL; | ||
2841 | } else if (i_blocks <= 0xffffffffffffULL) { | ||
2842 | /* | ||
2843 | * i_blocks can be represented in a 48 bit variable | ||
2844 | * as multiple of 512 bytes | ||
2845 | */ | ||
2846 | err = ext4_update_rocompat_feature(handle, sb, | ||
2847 | EXT4_FEATURE_RO_COMPAT_HUGE_FILE); | ||
2848 | if (err) | ||
2849 | goto err_out; | ||
2850 | /* i_block is stored in the split 48 bit fields */ | ||
2851 | raw_inode->i_blocks_lo = cpu_to_le32(i_blocks); | ||
2852 | raw_inode->i_blocks_high = cpu_to_le16(i_blocks >> 32); | ||
2853 | ei->i_flags &= ~EXT4_HUGE_FILE_FL; | ||
2854 | } else { | ||
2855 | /* | ||
2856 | * i_blocks should be represented in a 48 bit variable | ||
2857 | * as multiple of file system block size | ||
2858 | */ | ||
2859 | err = ext4_update_rocompat_feature(handle, sb, | ||
2860 | EXT4_FEATURE_RO_COMPAT_HUGE_FILE); | ||
2861 | if (err) | ||
2862 | goto err_out; | ||
2863 | ei->i_flags |= EXT4_HUGE_FILE_FL; | ||
2864 | /* i_block is stored in file system block size */ | ||
2865 | i_blocks = i_blocks >> (inode->i_blkbits - 9); | ||
2866 | raw_inode->i_blocks_lo = cpu_to_le32(i_blocks); | ||
2867 | raw_inode->i_blocks_high = cpu_to_le16(i_blocks >> 32); | ||
2868 | } | ||
2869 | err_out: | ||
2870 | return err; | ||
2871 | } | ||
2872 | |||
2800 | /* | 2873 | /* |
2801 | * Post the struct inode info into an on-disk inode location in the | 2874 | * Post the struct inode info into an on-disk inode location in the |
2802 | * buffer-cache. This gobbles the caller's reference to the | 2875 | * buffer-cache. This gobbles the caller's reference to the |
@@ -2845,47 +2918,42 @@ static int ext4_do_update_inode(handle_t *handle, | |||
2845 | raw_inode->i_gid_high = 0; | 2918 | raw_inode->i_gid_high = 0; |
2846 | } | 2919 | } |
2847 | raw_inode->i_links_count = cpu_to_le16(inode->i_nlink); | 2920 | raw_inode->i_links_count = cpu_to_le16(inode->i_nlink); |
2848 | raw_inode->i_size = cpu_to_le32(ei->i_disksize); | ||
2849 | 2921 | ||
2850 | EXT4_INODE_SET_XTIME(i_ctime, inode, raw_inode); | 2922 | EXT4_INODE_SET_XTIME(i_ctime, inode, raw_inode); |
2851 | EXT4_INODE_SET_XTIME(i_mtime, inode, raw_inode); | 2923 | EXT4_INODE_SET_XTIME(i_mtime, inode, raw_inode); |
2852 | EXT4_INODE_SET_XTIME(i_atime, inode, raw_inode); | 2924 | EXT4_INODE_SET_XTIME(i_atime, inode, raw_inode); |
2853 | EXT4_EINODE_SET_XTIME(i_crtime, ei, raw_inode); | 2925 | EXT4_EINODE_SET_XTIME(i_crtime, ei, raw_inode); |
2854 | 2926 | ||
2855 | raw_inode->i_blocks = cpu_to_le32(inode->i_blocks); | 2927 | if (ext4_inode_blocks_set(handle, raw_inode, ei)) |
2928 | goto out_brelse; | ||
2856 | raw_inode->i_dtime = cpu_to_le32(ei->i_dtime); | 2929 | raw_inode->i_dtime = cpu_to_le32(ei->i_dtime); |
2857 | raw_inode->i_flags = cpu_to_le32(ei->i_flags); | 2930 | raw_inode->i_flags = cpu_to_le32(ei->i_flags); |
2858 | if (EXT4_SB(inode->i_sb)->s_es->s_creator_os != | 2931 | if (EXT4_SB(inode->i_sb)->s_es->s_creator_os != |
2859 | cpu_to_le32(EXT4_OS_HURD)) | 2932 | cpu_to_le32(EXT4_OS_HURD)) |
2860 | raw_inode->i_file_acl_high = | 2933 | raw_inode->i_file_acl_high = |
2861 | cpu_to_le16(ei->i_file_acl >> 32); | 2934 | cpu_to_le16(ei->i_file_acl >> 32); |
2862 | raw_inode->i_file_acl = cpu_to_le32(ei->i_file_acl); | 2935 | raw_inode->i_file_acl_lo = cpu_to_le32(ei->i_file_acl); |
2863 | if (!S_ISREG(inode->i_mode)) { | 2936 | ext4_isize_set(raw_inode, ei->i_disksize); |
2864 | raw_inode->i_dir_acl = cpu_to_le32(ei->i_dir_acl); | 2937 | if (ei->i_disksize > 0x7fffffffULL) { |
2865 | } else { | 2938 | struct super_block *sb = inode->i_sb; |
2866 | raw_inode->i_size_high = | 2939 | if (!EXT4_HAS_RO_COMPAT_FEATURE(sb, |
2867 | cpu_to_le32(ei->i_disksize >> 32); | 2940 | EXT4_FEATURE_RO_COMPAT_LARGE_FILE) || |
2868 | if (ei->i_disksize > 0x7fffffffULL) { | 2941 | EXT4_SB(sb)->s_es->s_rev_level == |
2869 | struct super_block *sb = inode->i_sb; | 2942 | cpu_to_le32(EXT4_GOOD_OLD_REV)) { |
2870 | if (!EXT4_HAS_RO_COMPAT_FEATURE(sb, | 2943 | /* If this is the first large file |
2871 | EXT4_FEATURE_RO_COMPAT_LARGE_FILE) || | 2944 | * created, add a flag to the superblock. |
2872 | EXT4_SB(sb)->s_es->s_rev_level == | 2945 | */ |
2873 | cpu_to_le32(EXT4_GOOD_OLD_REV)) { | 2946 | err = ext4_journal_get_write_access(handle, |
2874 | /* If this is the first large file | 2947 | EXT4_SB(sb)->s_sbh); |
2875 | * created, add a flag to the superblock. | 2948 | if (err) |
2876 | */ | 2949 | goto out_brelse; |
2877 | err = ext4_journal_get_write_access(handle, | 2950 | ext4_update_dynamic_rev(sb); |
2878 | EXT4_SB(sb)->s_sbh); | 2951 | EXT4_SET_RO_COMPAT_FEATURE(sb, |
2879 | if (err) | ||
2880 | goto out_brelse; | ||
2881 | ext4_update_dynamic_rev(sb); | ||
2882 | EXT4_SET_RO_COMPAT_FEATURE(sb, | ||
2883 | EXT4_FEATURE_RO_COMPAT_LARGE_FILE); | 2952 | EXT4_FEATURE_RO_COMPAT_LARGE_FILE); |
2884 | sb->s_dirt = 1; | 2953 | sb->s_dirt = 1; |
2885 | handle->h_sync = 1; | 2954 | handle->h_sync = 1; |
2886 | err = ext4_journal_dirty_metadata(handle, | 2955 | err = ext4_journal_dirty_metadata(handle, |
2887 | EXT4_SB(sb)->s_sbh); | 2956 | EXT4_SB(sb)->s_sbh); |
2888 | } | ||
2889 | } | 2957 | } |
2890 | } | 2958 | } |
2891 | raw_inode->i_generation = cpu_to_le32(inode->i_generation); | 2959 | raw_inode->i_generation = cpu_to_le32(inode->i_generation); |
@@ -2903,8 +2971,14 @@ static int ext4_do_update_inode(handle_t *handle, | |||
2903 | } else for (block = 0; block < EXT4_N_BLOCKS; block++) | 2971 | } else for (block = 0; block < EXT4_N_BLOCKS; block++) |
2904 | raw_inode->i_block[block] = ei->i_data[block]; | 2972 | raw_inode->i_block[block] = ei->i_data[block]; |
2905 | 2973 | ||
2906 | if (ei->i_extra_isize) | 2974 | raw_inode->i_disk_version = cpu_to_le32(inode->i_version); |
2975 | if (ei->i_extra_isize) { | ||
2976 | if (EXT4_FITS_IN_INODE(raw_inode, ei, i_version_hi)) | ||
2977 | raw_inode->i_version_hi = | ||
2978 | cpu_to_le32(inode->i_version >> 32); | ||
2907 | raw_inode->i_extra_isize = cpu_to_le16(ei->i_extra_isize); | 2979 | raw_inode->i_extra_isize = cpu_to_le16(ei->i_extra_isize); |
2980 | } | ||
2981 | |||
2908 | 2982 | ||
2909 | BUFFER_TRACE(bh, "call ext4_journal_dirty_metadata"); | 2983 | BUFFER_TRACE(bh, "call ext4_journal_dirty_metadata"); |
2910 | rc = ext4_journal_dirty_metadata(handle, bh); | 2984 | rc = ext4_journal_dirty_metadata(handle, bh); |
@@ -3024,6 +3098,17 @@ int ext4_setattr(struct dentry *dentry, struct iattr *attr) | |||
3024 | ext4_journal_stop(handle); | 3098 | ext4_journal_stop(handle); |
3025 | } | 3099 | } |
3026 | 3100 | ||
3101 | if (attr->ia_valid & ATTR_SIZE) { | ||
3102 | if (!(EXT4_I(inode)->i_flags & EXT4_EXTENTS_FL)) { | ||
3103 | struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb); | ||
3104 | |||
3105 | if (attr->ia_size > sbi->s_bitmap_maxbytes) { | ||
3106 | error = -EFBIG; | ||
3107 | goto err_out; | ||
3108 | } | ||
3109 | } | ||
3110 | } | ||
3111 | |||
3027 | if (S_ISREG(inode->i_mode) && | 3112 | if (S_ISREG(inode->i_mode) && |
3028 | attr->ia_valid & ATTR_SIZE && attr->ia_size < inode->i_size) { | 3113 | attr->ia_valid & ATTR_SIZE && attr->ia_size < inode->i_size) { |
3029 | handle_t *handle; | 3114 | handle_t *handle; |
@@ -3120,6 +3205,9 @@ int ext4_mark_iloc_dirty(handle_t *handle, | |||
3120 | { | 3205 | { |
3121 | int err = 0; | 3206 | int err = 0; |
3122 | 3207 | ||
3208 | if (test_opt(inode->i_sb, I_VERSION)) | ||
3209 | inode_inc_iversion(inode); | ||
3210 | |||
3123 | /* the do_update_inode consumes one bh->b_count */ | 3211 | /* the do_update_inode consumes one bh->b_count */ |
3124 | get_bh(iloc->bh); | 3212 | get_bh(iloc->bh); |
3125 | 3213 | ||
@@ -3158,8 +3246,10 @@ ext4_reserve_inode_write(handle_t *handle, struct inode *inode, | |||
3158 | * Expand an inode by new_extra_isize bytes. | 3246 | * Expand an inode by new_extra_isize bytes. |
3159 | * Returns 0 on success or negative error number on failure. | 3247 | * Returns 0 on success or negative error number on failure. |
3160 | */ | 3248 | */ |
3161 | int ext4_expand_extra_isize(struct inode *inode, unsigned int new_extra_isize, | 3249 | static int ext4_expand_extra_isize(struct inode *inode, |
3162 | struct ext4_iloc iloc, handle_t *handle) | 3250 | unsigned int new_extra_isize, |
3251 | struct ext4_iloc iloc, | ||
3252 | handle_t *handle) | ||
3163 | { | 3253 | { |
3164 | struct ext4_inode *raw_inode; | 3254 | struct ext4_inode *raw_inode; |
3165 | struct ext4_xattr_ibody_header *header; | 3255 | struct ext4_xattr_ibody_header *header; |