Commit d018028055a21a28adef16b7f95422c426b46d60

Authored by Stephen Warren
Committed by Tom Rini
1 parent 50babaf852

fs: ext4: fix writing zero-length files

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>

Showing 1 changed file with 1 additions and 3 deletions Side-by-side Diff

fs/ext4/ext4_common.c
... ... @@ -1380,7 +1380,7 @@
1380 1380 unsigned int no_blks_reqd = 0;
1381 1381  
1382 1382 /* allocation of direct blocks */
1383   - for (i = 0; i < INDIRECT_BLOCKS; i++) {
  1383 + for (i = 0; total_remaining_blocks && i < INDIRECT_BLOCKS; i++) {
1384 1384 direct_blockno = ext4fs_get_new_blk_no();
1385 1385 if (direct_blockno == -1) {
1386 1386 printf("no block left to assign\n");
... ... @@ -1390,8 +1390,6 @@
1390 1390 debug("DB %ld: %u\n", direct_blockno, total_remaining_blocks);
1391 1391  
1392 1392 total_remaining_blocks--;
1393   - if (total_remaining_blocks == 0)
1394   - break;
1395 1393 }
1396 1394  
1397 1395 alloc_single_indirect_block(file_inode, &total_remaining_blocks,