Skip to content

Commit 60263d5

Browse files
Christoph Hellwigdjwong
authored andcommitted
iomap: fall back to buffered writes for invalidation failures
Failing to invalid the page cache means data in incoherent, which is a very bad state for the system. Always fall back to buffered I/O through the page cache if we can't invalidate mappings. Signed-off-by: Christoph Hellwig <hch@lst.de> Acked-by: Dave Chinner <dchinner@redhat.com> Reviewed-by: Goldwyn Rodrigues <rgoldwyn@suse.com> Reviewed-by: Darrick J. Wong <darrick.wong@oracle.com> Signed-off-by: Darrick J. Wong <darrick.wong@oracle.com> Acked-by: Bob Peterson <rpeterso@redhat.com> Acked-by: Damien Le Moal <damien.lemoal@wdc.com> Reviewed-by: Theodore Ts'o <tytso@mit.edu> # for ext4 Reviewed-by: Andreas Gruenbacher <agruenba@redhat.com> # for gfs2 Reviewed-by: Ritesh Harjani <riteshh@linux.ibm.com>
1 parent 80e543a commit 60263d5

File tree

6 files changed

+23
-10
lines changed

6 files changed

+23
-10
lines changed

fs/ext4/file.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -544,6 +544,8 @@ static ssize_t ext4_dio_write_iter(struct kiocb *iocb, struct iov_iter *from)
544544
iomap_ops = &ext4_iomap_overwrite_ops;
545545
ret = iomap_dio_rw(iocb, from, iomap_ops, &ext4_dio_write_ops,
546546
is_sync_kiocb(iocb) || unaligned_io || extend);
547+
if (ret == -ENOTBLK)
548+
ret = 0;
547549

548550
if (extend)
549551
ret = ext4_handle_inode_extension(inode, offset, ret, count);

fs/gfs2/file.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -814,7 +814,8 @@ static ssize_t gfs2_file_direct_write(struct kiocb *iocb, struct iov_iter *from)
814814

815815
ret = iomap_dio_rw(iocb, from, &gfs2_iomap_ops, NULL,
816816
is_sync_kiocb(iocb));
817-
817+
if (ret == -ENOTBLK)
818+
ret = 0;
818819
out:
819820
gfs2_glock_dq(&gh);
820821
out_uninit:

fs/iomap/direct-io.c

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
#include <linux/backing-dev.h>
1111
#include <linux/uio.h>
1212
#include <linux/task_io_accounting_ops.h>
13+
#include "trace.h"
1314

1415
#include "../internal.h"
1516

@@ -401,6 +402,9 @@ iomap_dio_actor(struct inode *inode, loff_t pos, loff_t length,
401402
* can be mapped into multiple disjoint IOs and only a subset of the IOs issued
402403
* may be pure data writes. In that case, we still need to do a full data sync
403404
* completion.
405+
*
406+
* Returns -ENOTBLK In case of a page invalidation invalidation failure for
407+
* writes. The callers needs to fall back to buffered I/O in this case.
404408
*/
405409
ssize_t
406410
iomap_dio_rw(struct kiocb *iocb, struct iov_iter *iter,
@@ -478,13 +482,15 @@ iomap_dio_rw(struct kiocb *iocb, struct iov_iter *iter,
478482
if (iov_iter_rw(iter) == WRITE) {
479483
/*
480484
* Try to invalidate cache pages for the range we are writing.
481-
* If this invalidation fails, tough, the write will still work,
482-
* but racing two incompatible write paths is a pretty crazy
483-
* thing to do, so we don't support it 100%.
485+
* If this invalidation fails, let the caller fall back to
486+
* buffered I/O.
484487
*/
485488
if (invalidate_inode_pages2_range(mapping, pos >> PAGE_SHIFT,
486-
end >> PAGE_SHIFT))
487-
dio_warn_stale_pagecache(iocb->ki_filp);
489+
end >> PAGE_SHIFT)) {
490+
trace_iomap_dio_invalidate_fail(inode, pos, count);
491+
ret = -ENOTBLK;
492+
goto out_free_dio;
493+
}
488494

489495
if (!wait_for_completion && !inode->i_sb->s_dio_done_wq) {
490496
ret = sb_init_dio_done_wq(inode->i_sb);

fs/iomap/trace.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,7 @@ DEFINE_EVENT(iomap_range_class, name, \
7474
DEFINE_RANGE_EVENT(iomap_writepage);
7575
DEFINE_RANGE_EVENT(iomap_releasepage);
7676
DEFINE_RANGE_EVENT(iomap_invalidatepage);
77+
DEFINE_RANGE_EVENT(iomap_dio_invalidate_fail);
7778

7879
#define IOMAP_TYPE_STRINGS \
7980
{ IOMAP_HOLE, "HOLE" }, \

fs/xfs/xfs_file.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -553,8 +553,8 @@ xfs_file_dio_aio_write(
553553
xfs_iunlock(ip, iolock);
554554

555555
/*
556-
* No fallback to buffered IO on errors for XFS, direct IO will either
557-
* complete fully or fail.
556+
* No fallback to buffered IO after short writes for XFS, direct I/O
557+
* will either complete fully or return an error.
558558
*/
559559
ASSERT(ret < 0 || ret == count);
560560
return ret;

fs/zonefs/super.c

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -786,8 +786,11 @@ static ssize_t zonefs_file_write_iter(struct kiocb *iocb, struct iov_iter *from)
786786
if (iocb->ki_pos >= ZONEFS_I(inode)->i_max_size)
787787
return -EFBIG;
788788

789-
if (iocb->ki_flags & IOCB_DIRECT)
790-
return zonefs_file_dio_write(iocb, from);
789+
if (iocb->ki_flags & IOCB_DIRECT) {
790+
ssize_t ret = zonefs_file_dio_write(iocb, from);
791+
if (ret != -ENOTBLK)
792+
return ret;
793+
}
791794

792795
return zonefs_file_buffered_write(iocb, from);
793796
}

0 commit comments

Comments
 (0)