Skip to content

Commit 299537e

Browse files
riteshharjaniDarrick J. Wong
authored andcommitted
ext4: Do not fallback to buffered-io for DIO atomic write
atomic writes is currently only supported for single fsblock and only for direct-io. We should not return -ENOTBLK for atomic writes since we want the atomic write request to either complete fully or fail otherwise. Hence, we should never fallback to buffered-io in case of DIO atomic write requests. Let's also catch if this ever happens by adding some WARN_ON_ONCE before buffered-io handling for direct-io atomic writes. More details of the discussion [1]. While at it let's add an inline helper ext4_want_directio_fallback() which simplifies the logic checks and inherently fixes condition on when to return -ENOTBLK which otherwise was always returning true for any write or directio in ext4_iomap_end(). It was ok since ext4 only supports direct-io via iomap. [1]: https://lore.kernel.org/linux-xfs/[email protected]/T/#m9dbecc11bed713ed0d7a486432c56b105b555f04 Suggested-by: Darrick J. Wong <[email protected]> # inline helper Signed-off-by: Ritesh Harjani (IBM) <[email protected]> Reviewed-by: Darrick J. Wong <[email protected]> Signed-off-by: Darrick J. Wong <[email protected]> Reviewed-by: Jan Kara <[email protected]>
1 parent b7987a7 commit 299537e

File tree

2 files changed

+29
-5
lines changed

2 files changed

+29
-5
lines changed

fs/ext4/file.c

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -599,6 +599,13 @@ static ssize_t ext4_dio_write_iter(struct kiocb *iocb, struct iov_iter *from)
599599
ssize_t err;
600600
loff_t endbyte;
601601

602+
/*
603+
* There is no support for atomic writes on buffered-io yet,
604+
* we should never fallback to buffered-io for DIO atomic
605+
* writes.
606+
*/
607+
WARN_ON_ONCE(iocb->ki_flags & IOCB_ATOMIC);
608+
602609
offset = iocb->ki_pos;
603610
err = ext4_buffered_write_iter(iocb, from);
604611
if (err < 0)

fs/ext4/inode.c

Lines changed: 22 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3444,17 +3444,34 @@ static int ext4_iomap_overwrite_begin(struct inode *inode, loff_t offset,
34443444
return ret;
34453445
}
34463446

3447+
static inline bool ext4_want_directio_fallback(unsigned flags, ssize_t written)
3448+
{
3449+
/* must be a directio to fall back to buffered */
3450+
if ((flags & (IOMAP_WRITE | IOMAP_DIRECT)) !=
3451+
(IOMAP_WRITE | IOMAP_DIRECT))
3452+
return false;
3453+
3454+
/* atomic writes are all-or-nothing */
3455+
if (flags & IOMAP_ATOMIC)
3456+
return false;
3457+
3458+
/* can only try again if we wrote nothing */
3459+
return written == 0;
3460+
}
3461+
34473462
static int ext4_iomap_end(struct inode *inode, loff_t offset, loff_t length,
34483463
ssize_t written, unsigned flags, struct iomap *iomap)
34493464
{
34503465
/*
34513466
* Check to see whether an error occurred while writing out the data to
3452-
* the allocated blocks. If so, return the magic error code so that we
3453-
* fallback to buffered I/O and attempt to complete the remainder of
3454-
* the I/O. Any blocks that may have been allocated in preparation for
3455-
* the direct I/O will be reused during buffered I/O.
3467+
* the allocated blocks. If so, return the magic error code for
3468+
* non-atomic write so that we fallback to buffered I/O and attempt to
3469+
* complete the remainder of the I/O.
3470+
* For non-atomic writes, any blocks that may have been
3471+
* allocated in preparation for the direct I/O will be reused during
3472+
* buffered I/O. For atomic write, we never fallback to buffered-io.
34563473
*/
3457-
if (flags & (IOMAP_WRITE | IOMAP_DIRECT) && written == 0)
3474+
if (ext4_want_directio_fallback(flags, written))
34583475
return -ENOTBLK;
34593476

34603477
return 0;

0 commit comments

Comments
 (0)