Skip to content

Commit 41da51b

Browse files
author
Andreas Gruenbacher
committed
fs: Add IOCB_NOIO flag for generic_file_read_iter
Add an IOCB_NOIO flag that indicates to generic_file_read_iter that it shouldn't trigger any filesystem I/O for the actual request or for readahead. This allows to do tentative reads out of the page cache as some filesystems allow, and to take the appropriate locks and retry the reads only if the requested pages are not cached. Signed-off-by: Andreas Gruenbacher <[email protected]>
1 parent dcb7fd8 commit 41da51b

File tree

2 files changed

+22
-2
lines changed

2 files changed

+22
-2
lines changed

include/linux/fs.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -315,6 +315,7 @@ enum rw_hint {
315315
#define IOCB_SYNC (1 << 5)
316316
#define IOCB_WRITE (1 << 6)
317317
#define IOCB_NOWAIT (1 << 7)
318+
#define IOCB_NOIO (1 << 9)
318319

319320
struct kiocb {
320321
struct file *ki_filp;

mm/filemap.c

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2028,7 +2028,7 @@ ssize_t generic_file_buffered_read(struct kiocb *iocb,
20282028

20292029
page = find_get_page(mapping, index);
20302030
if (!page) {
2031-
if (iocb->ki_flags & IOCB_NOWAIT)
2031+
if (iocb->ki_flags & (IOCB_NOWAIT | IOCB_NOIO))
20322032
goto would_block;
20332033
page_cache_sync_readahead(mapping,
20342034
ra, filp,
@@ -2038,6 +2038,10 @@ ssize_t generic_file_buffered_read(struct kiocb *iocb,
20382038
goto no_cached_page;
20392039
}
20402040
if (PageReadahead(page)) {
2041+
if (iocb->ki_flags & IOCB_NOIO) {
2042+
put_page(page);
2043+
goto out;
2044+
}
20412045
page_cache_async_readahead(mapping,
20422046
ra, filp, page,
20432047
index, last_index - index);
@@ -2160,6 +2164,11 @@ ssize_t generic_file_buffered_read(struct kiocb *iocb,
21602164
}
21612165

21622166
readpage:
2167+
if (iocb->ki_flags & IOCB_NOIO) {
2168+
unlock_page(page);
2169+
put_page(page);
2170+
goto would_block;
2171+
}
21632172
/*
21642173
* A previous I/O error may have been due to temporary
21652174
* failures, eg. multipath errors.
@@ -2249,9 +2258,19 @@ EXPORT_SYMBOL_GPL(generic_file_buffered_read);
22492258
*
22502259
* This is the "read_iter()" routine for all filesystems
22512260
* that can use the page cache directly.
2261+
*
2262+
* The IOCB_NOWAIT flag in iocb->ki_flags indicates that -EAGAIN shall
2263+
* be returned when no data can be read without waiting for I/O requests
2264+
* to complete; it doesn't prevent readahead.
2265+
*
2266+
* The IOCB_NOIO flag in iocb->ki_flags indicates that no new I/O
2267+
* requests shall be made for the read or for readahead. When no data
2268+
* can be read, -EAGAIN shall be returned. When readahead would be
2269+
* triggered, a partial, possibly empty read shall be returned.
2270+
*
22522271
* Return:
22532272
* * number of bytes copied, even for partial reads
2254-
* * negative error code if nothing was read
2273+
* * negative error code (or 0 if IOCB_NOIO) if nothing was read
22552274
*/
22562275
ssize_t
22572276
generic_file_read_iter(struct kiocb *iocb, struct iov_iter *iter)

0 commit comments

Comments
 (0)