Commit 55ce2f64 authored by Zhang Yi's avatar Zhang Yi Committed by Theodore Ts'o
Browse files

ext4: correct the error path of ext4_write_inline_data_end()



Current error path of ext4_write_inline_data_end() is not correct.

Firstly, it should pass out the error value if ext4_get_inode_loc()
return fail, or else it could trigger infinite loop if we inject error
here. And then it's better to add inode to orphan list if it return fail
in ext4_journal_stop(), otherwise we could not restore inline xattr
entry after power failure. Finally, we need to reset the 'ret' value if
ext4_write_inline_data_end() return success in ext4_write_end() and
ext4_journalled_write_end(), otherwise we could not get the error return
value of ext4_journal_stop().

Signed-off-by: default avatarZhang Yi <yi.zhang@huawei.com>
Reviewed-by: default avatarJan Kara <jack@suse.cz>
Signed-off-by: default avatarTheodore Ts'o <tytso@mit.edu>
Link: https://lore.kernel.org/r/20210716122024.1105856-3-yi.zhang@huawei.com
parent 4df031ff
Loading
Loading
Loading
Loading
+5 −10
Original line number Original line Diff line number Diff line
@@ -733,25 +733,20 @@ int ext4_write_inline_data_end(struct inode *inode, loff_t pos, unsigned len,
	void *kaddr;
	void *kaddr;
	struct ext4_iloc iloc;
	struct ext4_iloc iloc;


	if (unlikely(copied < len)) {
	if (unlikely(copied < len) && !PageUptodate(page))
		if (!PageUptodate(page)) {
		return 0;
			copied = 0;
			goto out;
		}
	}


	ret = ext4_get_inode_loc(inode, &iloc);
	ret = ext4_get_inode_loc(inode, &iloc);
	if (ret) {
	if (ret) {
		ext4_std_error(inode->i_sb, ret);
		ext4_std_error(inode->i_sb, ret);
		copied = 0;
		return ret;
		goto out;
	}
	}


	ext4_write_lock_xattr(inode, &no_expand);
	ext4_write_lock_xattr(inode, &no_expand);
	BUG_ON(!ext4_has_inline_data(inode));
	BUG_ON(!ext4_has_inline_data(inode));


	kaddr = kmap_atomic(page);
	kaddr = kmap_atomic(page);
	ext4_write_inline_data(inode, &iloc, kaddr, pos, len);
	ext4_write_inline_data(inode, &iloc, kaddr, pos, copied);
	kunmap_atomic(kaddr);
	kunmap_atomic(kaddr);
	SetPageUptodate(page);
	SetPageUptodate(page);
	/* clear page dirty so that writepages wouldn't work for us. */
	/* clear page dirty so that writepages wouldn't work for us. */
@@ -760,7 +755,7 @@ int ext4_write_inline_data_end(struct inode *inode, loff_t pos, unsigned len,
	ext4_write_unlock_xattr(inode, &no_expand);
	ext4_write_unlock_xattr(inode, &no_expand);
	brelse(iloc.bh);
	brelse(iloc.bh);
	mark_inode_dirty(inode);
	mark_inode_dirty(inode);
out:

	return copied;
	return copied;
}
}


+5 −2
Original line number Original line Diff line number Diff line
@@ -1295,6 +1295,7 @@ static int ext4_write_end(struct file *file,
			goto errout;
			goto errout;
		}
		}
		copied = ret;
		copied = ret;
		ret = 0;
	} else
	} else
		copied = block_write_end(file, mapping, pos,
		copied = block_write_end(file, mapping, pos,
					 len, copied, page, fsdata);
					 len, copied, page, fsdata);
@@ -1321,13 +1322,14 @@ static int ext4_write_end(struct file *file,
	if (i_size_changed || inline_data)
	if (i_size_changed || inline_data)
		ret = ext4_mark_inode_dirty(handle, inode);
		ret = ext4_mark_inode_dirty(handle, inode);


errout:
	if (pos + len > inode->i_size && !verity && ext4_can_truncate(inode))
	if (pos + len > inode->i_size && !verity && ext4_can_truncate(inode))
		/* if we have allocated more blocks and copied
		/* if we have allocated more blocks and copied
		 * less. We will have blocks allocated outside
		 * less. We will have blocks allocated outside
		 * inode->i_size. So truncate them
		 * inode->i_size. So truncate them
		 */
		 */
		ext4_orphan_add(handle, inode);
		ext4_orphan_add(handle, inode);
errout:

	ret2 = ext4_journal_stop(handle);
	ret2 = ext4_journal_stop(handle);
	if (!ret)
	if (!ret)
		ret = ret2;
		ret = ret2;
@@ -1410,6 +1412,7 @@ static int ext4_journalled_write_end(struct file *file,
			goto errout;
			goto errout;
		}
		}
		copied = ret;
		copied = ret;
		ret = 0;
	} else if (unlikely(copied < len) && !PageUptodate(page)) {
	} else if (unlikely(copied < len) && !PageUptodate(page)) {
		copied = 0;
		copied = 0;
		ext4_journalled_zero_new_buffers(handle, page, from, to);
		ext4_journalled_zero_new_buffers(handle, page, from, to);
@@ -1439,6 +1442,7 @@ static int ext4_journalled_write_end(struct file *file,
			ret = ret2;
			ret = ret2;
	}
	}


errout:
	if (pos + len > inode->i_size && !verity && ext4_can_truncate(inode))
	if (pos + len > inode->i_size && !verity && ext4_can_truncate(inode))
		/* if we have allocated more blocks and copied
		/* if we have allocated more blocks and copied
		 * less. We will have blocks allocated outside
		 * less. We will have blocks allocated outside
@@ -1446,7 +1450,6 @@ static int ext4_journalled_write_end(struct file *file,
		 */
		 */
		ext4_orphan_add(handle, inode);
		ext4_orphan_add(handle, inode);


errout:
	ret2 = ext4_journal_stop(handle);
	ret2 = ext4_journal_stop(handle);
	if (!ret)
	if (!ret)
		ret = ret2;
		ret = ret2;