Commit c402a9a9 authored by Christoph Hellwig's avatar Christoph Hellwig Committed by Andrew Morton

filemap: add a kiocb_invalidate_post_direct_write helper

Add a helper to invalidate page cache after a dio write.

Link: https://lkml.kernel.org/r/20230601145904.1385409-7-hch@lst.deSigned-off-by: default avatarChristoph Hellwig <hch@lst.de>
Reviewed-by: default avatarDamien Le Moal <dlemoal@kernel.org>
Reviewed-by: default avatarHannes Reinecke <hare@suse.de>
Acked-by: default avatarDarrick J. Wong <djwong@kernel.org>
Cc: Al Viro <viro@zeniv.linux.org.uk>
Cc: Andreas Gruenbacher <agruenba@redhat.com>
Cc: Anna Schumaker <anna@kernel.org>
Cc: Chao Yu <chao@kernel.org>
Cc: Christian Brauner <brauner@kernel.org>
Cc: Ilya Dryomov <idryomov@gmail.com>
Cc: Jaegeuk Kim <jaegeuk@kernel.org>
Cc: Jens Axboe <axboe@kernel.dk>
Cc: Johannes Thumshirn <johannes.thumshirn@wdc.com>
Cc: Matthew Wilcox <willy@infradead.org>
Cc: Miklos Szeredi <miklos@szeredi.hu>
Cc: Miklos Szeredi <mszeredi@redhat.com>
Cc: Theodore Ts'o <tytso@mit.edu>
Cc: Trond Myklebust <trond.myklebust@hammerspace.com>
Cc: Xiubo Li <xiubli@redhat.com>
Signed-off-by: default avatarAndrew Morton <akpm@linux-foundation.org>
parent e003f74a
...@@ -285,14 +285,8 @@ static ssize_t dio_complete(struct dio *dio, ssize_t ret, unsigned int flags) ...@@ -285,14 +285,8 @@ static ssize_t dio_complete(struct dio *dio, ssize_t ret, unsigned int flags)
* zeros from unwritten extents. * zeros from unwritten extents.
*/ */
if (flags & DIO_COMPLETE_INVALIDATE && if (flags & DIO_COMPLETE_INVALIDATE &&
ret > 0 && dio_op == REQ_OP_WRITE && ret > 0 && dio_op == REQ_OP_WRITE)
dio->inode->i_mapping->nrpages) { kiocb_invalidate_post_direct_write(dio->iocb, ret);
err = invalidate_inode_pages2_range(dio->inode->i_mapping,
offset >> PAGE_SHIFT,
(offset + ret - 1) >> PAGE_SHIFT);
if (err)
dio_warn_stale_pagecache(dio->iocb->ki_filp);
}
inode_dio_end(dio->inode); inode_dio_end(dio->inode);
......
...@@ -81,7 +81,6 @@ ssize_t iomap_dio_complete(struct iomap_dio *dio) ...@@ -81,7 +81,6 @@ ssize_t iomap_dio_complete(struct iomap_dio *dio)
{ {
const struct iomap_dio_ops *dops = dio->dops; const struct iomap_dio_ops *dops = dio->dops;
struct kiocb *iocb = dio->iocb; struct kiocb *iocb = dio->iocb;
struct inode *inode = file_inode(iocb->ki_filp);
loff_t offset = iocb->ki_pos; loff_t offset = iocb->ki_pos;
ssize_t ret = dio->error; ssize_t ret = dio->error;
...@@ -108,15 +107,8 @@ ssize_t iomap_dio_complete(struct iomap_dio *dio) ...@@ -108,15 +107,8 @@ ssize_t iomap_dio_complete(struct iomap_dio *dio)
* ->end_io() when necessary, otherwise a racing buffer read would cache * ->end_io() when necessary, otherwise a racing buffer read would cache
* zeros from unwritten extents. * zeros from unwritten extents.
*/ */
if (!dio->error && dio->size && if (!dio->error && dio->size && (dio->flags & IOMAP_DIO_WRITE))
(dio->flags & IOMAP_DIO_WRITE) && inode->i_mapping->nrpages) { kiocb_invalidate_post_direct_write(iocb, dio->size);
int err;
err = invalidate_inode_pages2_range(inode->i_mapping,
offset >> PAGE_SHIFT,
(offset + dio->size - 1) >> PAGE_SHIFT);
if (err)
dio_warn_stale_pagecache(iocb->ki_filp);
}
inode_dio_end(file_inode(iocb->ki_filp)); inode_dio_end(file_inode(iocb->ki_filp));
......
...@@ -2843,11 +2843,6 @@ static inline void inode_dio_end(struct inode *inode) ...@@ -2843,11 +2843,6 @@ static inline void inode_dio_end(struct inode *inode)
wake_up_bit(&inode->i_state, __I_DIO_WAKEUP); wake_up_bit(&inode->i_state, __I_DIO_WAKEUP);
} }
/*
* Warn about a page cache invalidation failure diring a direct I/O write.
*/
void dio_warn_stale_pagecache(struct file *filp);
extern void inode_set_flags(struct inode *inode, unsigned int flags, extern void inode_set_flags(struct inode *inode, unsigned int flags,
unsigned int mask); unsigned int mask);
......
...@@ -31,6 +31,7 @@ int invalidate_inode_pages2(struct address_space *mapping); ...@@ -31,6 +31,7 @@ int invalidate_inode_pages2(struct address_space *mapping);
int invalidate_inode_pages2_range(struct address_space *mapping, int invalidate_inode_pages2_range(struct address_space *mapping,
pgoff_t start, pgoff_t end); pgoff_t start, pgoff_t end);
int kiocb_invalidate_pages(struct kiocb *iocb, size_t count); int kiocb_invalidate_pages(struct kiocb *iocb, size_t count);
void kiocb_invalidate_post_direct_write(struct kiocb *iocb, size_t count);
int write_inode_now(struct inode *, int sync); int write_inode_now(struct inode *, int sync);
int filemap_fdatawrite(struct address_space *); int filemap_fdatawrite(struct address_space *);
......
...@@ -3789,7 +3789,7 @@ EXPORT_SYMBOL(read_cache_page_gfp); ...@@ -3789,7 +3789,7 @@ EXPORT_SYMBOL(read_cache_page_gfp);
/* /*
* Warn about a page cache invalidation failure during a direct I/O write. * Warn about a page cache invalidation failure during a direct I/O write.
*/ */
void dio_warn_stale_pagecache(struct file *filp) static void dio_warn_stale_pagecache(struct file *filp)
{ {
static DEFINE_RATELIMIT_STATE(_rs, 86400 * HZ, DEFAULT_RATELIMIT_BURST); static DEFINE_RATELIMIT_STATE(_rs, 86400 * HZ, DEFAULT_RATELIMIT_BURST);
char pathname[128]; char pathname[128];
...@@ -3806,19 +3806,23 @@ void dio_warn_stale_pagecache(struct file *filp) ...@@ -3806,19 +3806,23 @@ void dio_warn_stale_pagecache(struct file *filp)
} }
} }
void kiocb_invalidate_post_direct_write(struct kiocb *iocb, size_t count)
{
struct address_space *mapping = iocb->ki_filp->f_mapping;
if (mapping->nrpages &&
invalidate_inode_pages2_range(mapping,
iocb->ki_pos >> PAGE_SHIFT,
(iocb->ki_pos + count - 1) >> PAGE_SHIFT))
dio_warn_stale_pagecache(iocb->ki_filp);
}
ssize_t ssize_t
generic_file_direct_write(struct kiocb *iocb, struct iov_iter *from) generic_file_direct_write(struct kiocb *iocb, struct iov_iter *from)
{ {
struct file *file = iocb->ki_filp; struct address_space *mapping = iocb->ki_filp->f_mapping;
struct address_space *mapping = file->f_mapping; size_t write_len = iov_iter_count(from);
struct inode *inode = mapping->host;
loff_t pos = iocb->ki_pos;
ssize_t written; ssize_t written;
size_t write_len;
pgoff_t end;
write_len = iov_iter_count(from);
end = (pos + write_len - 1) >> PAGE_SHIFT;
/* /*
* If a page can not be invalidated, return 0 to fall back * If a page can not be invalidated, return 0 to fall back
...@@ -3828,7 +3832,7 @@ generic_file_direct_write(struct kiocb *iocb, struct iov_iter *from) ...@@ -3828,7 +3832,7 @@ generic_file_direct_write(struct kiocb *iocb, struct iov_iter *from)
if (written) { if (written) {
if (written == -EBUSY) if (written == -EBUSY)
return 0; return 0;
goto out; return written;
} }
written = mapping->a_ops->direct_IO(iocb, from); written = mapping->a_ops->direct_IO(iocb, from);
...@@ -3850,11 +3854,11 @@ generic_file_direct_write(struct kiocb *iocb, struct iov_iter *from) ...@@ -3850,11 +3854,11 @@ generic_file_direct_write(struct kiocb *iocb, struct iov_iter *from)
* *
* Skip invalidation for async writes or if mapping has no pages. * Skip invalidation for async writes or if mapping has no pages.
*/ */
if (written > 0 && mapping->nrpages &&
invalidate_inode_pages2_range(mapping, pos >> PAGE_SHIFT, end))
dio_warn_stale_pagecache(file);
if (written > 0) { if (written > 0) {
struct inode *inode = mapping->host;
loff_t pos = iocb->ki_pos;
kiocb_invalidate_post_direct_write(iocb, written);
pos += written; pos += written;
write_len -= written; write_len -= written;
if (pos > i_size_read(inode) && !S_ISBLK(inode->i_mode)) { if (pos > i_size_read(inode) && !S_ISBLK(inode->i_mode)) {
...@@ -3865,7 +3869,6 @@ generic_file_direct_write(struct kiocb *iocb, struct iov_iter *from) ...@@ -3865,7 +3869,6 @@ generic_file_direct_write(struct kiocb *iocb, struct iov_iter *from)
} }
if (written != -EIOCBQUEUED) if (written != -EIOCBQUEUED)
iov_iter_revert(from, write_len - iov_iter_count(from)); iov_iter_revert(from, write_len - iov_iter_count(from));
out:
return written; return written;
} }
EXPORT_SYMBOL(generic_file_direct_write); EXPORT_SYMBOL(generic_file_direct_write);
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment