fs: return if direct I/O will trigger writeback
authorGoldwyn Rodrigues <rgoldwyn@suse.com>
Tue, 20 Jun 2017 12:05:44 +0000 (07:05 -0500)
committerJens Axboe <axboe@kernel.dk>
Tue, 20 Jun 2017 13:12:03 +0000 (07:12 -0600)
Find out if the I/O will trigger a wait due to writeback. If yes,
return -EAGAIN.

Return -EINVAL for buffered AIO: there are multiple causes of
delay such as page locks, dirty throttling logic, page loading
from disk etc. which cannot be taken care of.

Reviewed-by: Christoph Hellwig <hch@lst.de>
Reviewed-by: Jan Kara <jack@suse.cz>
Signed-off-by: Goldwyn Rodrigues <rgoldwyn@suse.com>
Signed-off-by: Jens Axboe <axboe@kernel.dk>
mm/filemap.c

index 9b39a2390b9e40169c2af7019f59953c4ddecee5..742034e56100fde2772566b14e30a8afdaaebe78 100644 (file)
@@ -2070,10 +2070,17 @@ generic_file_read_iter(struct kiocb *iocb, struct iov_iter *iter)
                loff_t size;
 
                size = i_size_read(inode);
-               retval = filemap_write_and_wait_range(mapping, iocb->ki_pos,
-                                       iocb->ki_pos + count - 1);
-               if (retval < 0)
-                       goto out;
+               if (iocb->ki_flags & IOCB_NOWAIT) {
+                       if (filemap_range_has_page(mapping, iocb->ki_pos,
+                                                  iocb->ki_pos + count - 1))
+                               return -EAGAIN;
+               } else {
+                       retval = filemap_write_and_wait_range(mapping,
+                                               iocb->ki_pos,
+                                               iocb->ki_pos + count - 1);
+                       if (retval < 0)
+                               goto out;
+               }
 
                file_accessed(file);
 
@@ -2674,6 +2681,9 @@ inline ssize_t generic_write_checks(struct kiocb *iocb, struct iov_iter *from)
 
        pos = iocb->ki_pos;
 
+       if ((iocb->ki_flags & IOCB_NOWAIT) && !(iocb->ki_flags & IOCB_DIRECT))
+               return -EINVAL;
+
        if (limit != RLIM_INFINITY) {
                if (iocb->ki_pos >= limit) {
                        send_sig(SIGXFSZ, current, 0);
@@ -2742,9 +2752,17 @@ generic_file_direct_write(struct kiocb *iocb, struct iov_iter *from)
        write_len = iov_iter_count(from);
        end = (pos + write_len - 1) >> PAGE_SHIFT;
 
-       written = filemap_write_and_wait_range(mapping, pos, pos + write_len - 1);
-       if (written)
-               goto out;
+       if (iocb->ki_flags & IOCB_NOWAIT) {
+               /* If there are pages to writeback, return */
+               if (filemap_range_has_page(inode->i_mapping, pos,
+                                          pos + iov_iter_count(from)))
+                       return -EAGAIN;
+       } else {
+               written = filemap_write_and_wait_range(mapping, pos,
+                                                       pos + write_len - 1);
+               if (written)
+                       goto out;
+       }
 
        /*
         * After a write we want buffered reads to be sure to go to disk to get