Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit cb40e79a authored by Jaegeuk Kim's avatar Jaegeuk Kim
Browse files

f2fs: truncate preallocated blocks in error case



If write is failed, we must deallocate the blocks that we couldn't write.

Cc: stable@vger.kernel.org
Reviewed-by: default avatarChao Yu <yuchao0@huawei.com>
Signed-off-by: default avatarJaegeuk Kim <jaegeuk@kernel.org>
parent 61a1f920
Loading
Loading
Loading
Loading
+11 −2
Original line number Diff line number Diff line
@@ -2711,11 +2711,16 @@ static ssize_t f2fs_file_write_iter(struct kiocb *iocb, struct iov_iter *from)
	inode_lock(inode);
	ret = generic_write_checks(file, &pos, &count, S_ISBLK(inode->i_mode));
	if (!ret) {
		bool preallocated = false;
		size_t target_size = 0;
		int err;

		if (iov_iter_fault_in_readable(from, iov_iter_count(from)))
		if (iov_iter_fault_in_readable(from, iov_iter_count(from))) {
			set_inode_flag(inode, FI_NO_PREALLOC);

		} else {
			preallocated = true;
			target_size = pos + count;
		}
		err = f2fs_preallocate_blocks(inode, pos, count,
				file->f_flags & O_DIRECT);
		if (err) {
@@ -2728,6 +2733,10 @@ static ssize_t f2fs_file_write_iter(struct kiocb *iocb, struct iov_iter *from)
		blk_finish_plug(&plug);
		clear_inode_flag(inode, FI_NO_PREALLOC);

		/* if we couldn't write data, we should deallocate blocks. */
		if (preallocated && i_size_read(inode) < target_size)
			f2fs_truncate(inode);

		if (ret > 0)
			f2fs_update_iostat(F2FS_I_SB(inode), APP_WRITE_IO, ret);
	}