gfs2: fix O_SYNC write handling
In gfs2_file_write_iter, for direct writes, the error checking in the buffered write fallback case is incomplete. This can cause inode write errors to go undetected. Fix and clean up gfs2_file_write_iter along the way. Based on a proposed fix by Christoph Hellwig <hch@lst.de>. Fixes: 967bcc91b044 ("gfs2: iomap direct I/O support") Cc: stable@vger.kernel.org # v4.19+ Signed-off-by: Andreas Gruenbacher <agruenba@redhat.com>
This commit is contained in:
@ -847,7 +847,7 @@ static ssize_t gfs2_file_write_iter(struct kiocb *iocb, struct iov_iter *from)
|
|||||||
struct file *file = iocb->ki_filp;
|
struct file *file = iocb->ki_filp;
|
||||||
struct inode *inode = file_inode(file);
|
struct inode *inode = file_inode(file);
|
||||||
struct gfs2_inode *ip = GFS2_I(inode);
|
struct gfs2_inode *ip = GFS2_I(inode);
|
||||||
ssize_t written = 0, ret;
|
ssize_t ret;
|
||||||
|
|
||||||
ret = gfs2_rsqa_alloc(ip);
|
ret = gfs2_rsqa_alloc(ip);
|
||||||
if (ret)
|
if (ret)
|
||||||
@ -879,55 +879,46 @@ static ssize_t gfs2_file_write_iter(struct kiocb *iocb, struct iov_iter *from)
|
|||||||
|
|
||||||
if (iocb->ki_flags & IOCB_DIRECT) {
|
if (iocb->ki_flags & IOCB_DIRECT) {
|
||||||
struct address_space *mapping = file->f_mapping;
|
struct address_space *mapping = file->f_mapping;
|
||||||
loff_t pos, endbyte;
|
ssize_t buffered, ret2;
|
||||||
ssize_t buffered;
|
|
||||||
|
|
||||||
written = gfs2_file_direct_write(iocb, from);
|
ret = gfs2_file_direct_write(iocb, from);
|
||||||
if (written < 0 || !iov_iter_count(from))
|
if (ret < 0 || !iov_iter_count(from))
|
||||||
goto out_unlock;
|
goto out_unlock;
|
||||||
|
|
||||||
|
iocb->ki_flags |= IOCB_DSYNC;
|
||||||
current->backing_dev_info = inode_to_bdi(inode);
|
current->backing_dev_info = inode_to_bdi(inode);
|
||||||
ret = iomap_file_buffered_write(iocb, from, &gfs2_iomap_ops);
|
buffered = iomap_file_buffered_write(iocb, from, &gfs2_iomap_ops);
|
||||||
current->backing_dev_info = NULL;
|
current->backing_dev_info = NULL;
|
||||||
if (unlikely(ret < 0))
|
if (unlikely(buffered <= 0))
|
||||||
goto out_unlock;
|
goto out_unlock;
|
||||||
buffered = ret;
|
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* We need to ensure that the page cache pages are written to
|
* We need to ensure that the page cache pages are written to
|
||||||
* disk and invalidated to preserve the expected O_DIRECT
|
* disk and invalidated to preserve the expected O_DIRECT
|
||||||
* semantics.
|
* semantics. If the writeback or invalidate fails, only report
|
||||||
|
* the direct I/O range as we don't know if the buffered pages
|
||||||
|
* made it to disk.
|
||||||
*/
|
*/
|
||||||
pos = iocb->ki_pos;
|
iocb->ki_pos += buffered;
|
||||||
endbyte = pos + buffered - 1;
|
ret2 = generic_write_sync(iocb, buffered);
|
||||||
ret = filemap_write_and_wait_range(mapping, pos, endbyte);
|
invalidate_mapping_pages(mapping,
|
||||||
if (!ret) {
|
(iocb->ki_pos - buffered) >> PAGE_SHIFT,
|
||||||
iocb->ki_pos += buffered;
|
(iocb->ki_pos - 1) >> PAGE_SHIFT);
|
||||||
written += buffered;
|
if (!ret || ret2 > 0)
|
||||||
invalidate_mapping_pages(mapping,
|
ret += ret2;
|
||||||
pos >> PAGE_SHIFT,
|
|
||||||
endbyte >> PAGE_SHIFT);
|
|
||||||
} else {
|
|
||||||
/*
|
|
||||||
* We don't know how much we wrote, so just return
|
|
||||||
* the number of bytes which were direct-written
|
|
||||||
*/
|
|
||||||
}
|
|
||||||
} else {
|
} else {
|
||||||
current->backing_dev_info = inode_to_bdi(inode);
|
current->backing_dev_info = inode_to_bdi(inode);
|
||||||
ret = iomap_file_buffered_write(iocb, from, &gfs2_iomap_ops);
|
ret = iomap_file_buffered_write(iocb, from, &gfs2_iomap_ops);
|
||||||
current->backing_dev_info = NULL;
|
current->backing_dev_info = NULL;
|
||||||
if (likely(ret > 0))
|
if (likely(ret > 0)) {
|
||||||
iocb->ki_pos += ret;
|
iocb->ki_pos += ret;
|
||||||
|
ret = generic_write_sync(iocb, ret);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
out_unlock:
|
out_unlock:
|
||||||
inode_unlock(inode);
|
inode_unlock(inode);
|
||||||
if (likely(ret > 0)) {
|
return ret;
|
||||||
/* Handle various SYNC-type writes */
|
|
||||||
ret = generic_write_sync(iocb, ret);
|
|
||||||
}
|
|
||||||
return written ? written : ret;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
static int fallocate_chunk(struct inode *inode, loff_t offset, loff_t len,
|
static int fallocate_chunk(struct inode *inode, loff_t offset, loff_t len,
|
||||||
|
Reference in New Issue
Block a user