fs: ext4: fix writing zero-length files
authorStephen Warren <swarren@nvidia.com>
Wed, 11 Jun 2014 18:46:16 +0000 (12:46 -0600)
committerChanho Park <chanho61.park@samsung.com>
Thu, 6 Aug 2015 04:35:19 +0000 (13:35 +0900)
ext4fs_allocate_blocks() always allocates at least one block for a file.
If the file size is zero, this causes total_remaining_blocks to
underflow, which then causes an apparent hang while 2^32 blocks are
allocated.

To solve this, check that total_remaining_blocks is non-zero as part of
the loop condition (i.e. before each loop) rather than at the end of
the loop.

Signed-off-by: Stephen Warren <swarren@nvidia.com>
fs/ext4/ext4_common.c

index 5cc03bebcc96537a7bbe43f67560bd31e76ff61f..018eb4fda4325a65649ae7f9e0fd95a69862b66b 100644 (file)
@@ -1392,7 +1392,7 @@ void ext4fs_allocate_blocks(struct ext2_inode *file_inode,
        unsigned int no_blks_reqd = 0;
 
        /* allocation of direct blocks */
-       for (i = 0; i < INDIRECT_BLOCKS; i++) {
+       for (i = 0; total_remaining_blocks && i < INDIRECT_BLOCKS; i++) {
                direct_blockno = ext4fs_get_new_blk_no();
                if (direct_blockno == -1) {
                        printf("no block left to assign\n");
@@ -1402,8 +1402,6 @@ void ext4fs_allocate_blocks(struct ext2_inode *file_inode,
                debug("DB %ld: %u\n", direct_blockno, total_remaining_blocks);
 
                total_remaining_blocks--;
-               if (total_remaining_blocks == 0)
-                       break;
        }
 
        alloc_single_indirect_block(file_inode, &total_remaining_blocks,