@@ -314,6 +314,7 @@ enum rw_hint {
#define IOCB_SYNC (1 << 5)
#define IOCB_WRITE (1 << 6)
#define IOCB_NOWAIT (1 << 7)
+#define IOCB_CACHED (1 << 8)
struct kiocb {
struct file *ki_filp;
@@ -2046,7 +2046,7 @@ static ssize_t generic_file_buffered_read(struct kiocb *iocb,
page = find_get_page(mapping, index);
if (!page) {
- if (iocb->ki_flags & IOCB_NOWAIT)
+ if (iocb->ki_flags & (IOCB_NOWAIT | IOCB_CACHED))
goto would_block;
page_cache_sync_readahead(mapping,
ra, filp,
@@ -2056,12 +2056,16 @@ static ssize_t generic_file_buffered_read(struct kiocb *iocb,
goto no_cached_page;
}
if (PageReadahead(page)) {
+ if (iocb->ki_flags & IOCB_CACHED) {
+ error = -ECANCELED;
+ goto out;
+ }
page_cache_async_readahead(mapping,
ra, filp, page,
index, last_index - index);
}
if (!PageUptodate(page)) {
- if (iocb->ki_flags & IOCB_NOWAIT) {
+ if (iocb->ki_flags & (IOCB_NOWAIT | IOCB_CACHED)) {
put_page(page);
goto would_block;
}
@@ -2266,6 +2270,13 @@ static ssize_t generic_file_buffered_read(struct kiocb *iocb,
*
* This is the "read_iter()" routine for all filesystems
* that can use the page cache directly.
+ *
+ * In the IOCB_NOWAIT flag in iocb->ki_flags indicates that -EAGAIN should be
+ * returned if completing the request would require I/O; this does not prevent
+ * readahead. The IOCB_CACHED flag indicates that -EAGAIN should be returned
+ * as under the IOCB_NOWAIT flag, and that -ECANCELED should be returned when
+ * readhead would be triggered.
+ *
* Return:
* * number of bytes copied, even for partial reads
* * negative error code if nothing was read
@@ -2286,7 +2297,7 @@ generic_file_read_iter(struct kiocb *iocb, struct iov_iter *iter)
loff_t size;
size = i_size_read(inode);
- if (iocb->ki_flags & IOCB_NOWAIT) {
+ if (iocb->ki_flags & (IOCB_NOWAIT | IOCB_CACHED)) {
if (filemap_range_has_page(mapping, iocb->ki_pos,
iocb->ki_pos + count - 1))
return -EAGAIN;
Add an IOCB_CACHED flag which indicates to generic_file_read_iter that it should only look at the page cache, without triggering any filesystem I/O for the actual request or for readahead. When filesystem I/O would be triggered, an error code should be returned instead. This allows the caller to perform a tentative read out of the page cache, and to retry the read after taking the necessary steps when the requested pages are not cached. When readahead would be triggered, we return -ECANCELED instead of -EAGAIN. This allows to distinguish attempted readheads from attempted reads (with IOCB_NOWAIT). Signed-off-by: Andreas Gruenbacher <agruenba@redhat.com> --- include/linux/fs.h | 1 + mm/filemap.c | 17 ++++++++++++++--- 2 files changed, 15 insertions(+), 3 deletions(-)