Whamcloud - gitweb
libext2fs: fail fileio write if we can't allocate a block
authorDarrick J. Wong <darrick.wong@oracle.com>
Thu, 12 Dec 2013 18:19:46 +0000 (13:19 -0500)
committerTheodore Ts'o <tytso@mit.edu>
Thu, 12 Dec 2013 18:19:48 +0000 (13:19 -0500)
If we're using ext2fs_file_write() to write to a hole in a file,
ensure that we can actually allocate the block before updating i_size.
In other words, don't update i_size and don't return success if we hit
an error while allocating space.

Signed-off-by: Darrick J. Wong <darrick.wong@oracle.com>
Signed-off-by: "Theodore Ts'o" <tytso@mit.edu>
lib/ext2fs/fileio.c

index d092e65..03bdf86 100644 (file)
@@ -297,6 +297,20 @@ errcode_t ext2fs_file_write(ext2_file_t file, const void *buf,
                if (retval)
                        goto fail;
 
+               /*
+                * OK, the physical block hasn't been allocated yet.
+                * Allocate it.
+                */
+               if (!file->physblock) {
+                       retval = ext2fs_bmap2(fs, file->ino, &file->inode,
+                                             BMAP_BUFFER,
+                                             file->ino ? BMAP_ALLOC : 0,
+                                             file->blockno, 0,
+                                             &file->physblock);
+                       if (retval)
+                               goto fail;
+               }
+
                file->flags |= EXT2_FILE_BUF_DIRTY;
                memcpy(file->buf+start, ptr, c);
                file->pos += c;