3636#include "acl.h"
3737#include "truncate.h"
3838
39- static bool ext4_dio_supported (struct inode * inode )
39+ /*
40+ * Returns %true if the given DIO request should be attempted with DIO, or
41+ * %false if it should fall back to buffered I/O.
42+ *
43+ * DIO isn't well specified; when it's unsupported (either due to the request
44+ * being misaligned, or due to the file not supporting DIO at all), filesystems
45+ * either fall back to buffered I/O or return EINVAL. For files that don't use
46+ * any special features like encryption or verity, ext4 has traditionally
47+ * returned EINVAL for misaligned DIO. iomap_dio_rw() uses this convention too.
48+ * In this case, we should attempt the DIO, *not* fall back to buffered I/O.
49+ *
50+ * In contrast, in cases where DIO is unsupported due to ext4 features, ext4
51+ * traditionally falls back to buffered I/O.
52+ *
53+ * This function implements the traditional ext4 behavior in all these cases.
54+ */
55+ static bool ext4_should_use_dio (struct kiocb * iocb , struct iov_iter * iter )
4056{
41- if (IS_ENABLED (CONFIG_FS_ENCRYPTION ) && IS_ENCRYPTED (inode ))
42- return false;
43- if (fsverity_active (inode ))
44- return false;
45- if (ext4_should_journal_data (inode ))
46- return false;
47- if (ext4_has_inline_data (inode ))
57+ struct inode * inode = file_inode (iocb -> ki_filp );
58+ u32 dio_align = ext4_dio_alignment (inode );
59+
60+ if (dio_align == 0 )
4861 return false;
49- return true;
62+
63+ if (dio_align == 1 )
64+ return true;
65+
66+ return IS_ALIGNED (iocb -> ki_pos | iov_iter_alignment (iter ), dio_align );
5067}
5168
5269static ssize_t ext4_dio_read_iter (struct kiocb * iocb , struct iov_iter * to )
@@ -61,7 +78,7 @@ static ssize_t ext4_dio_read_iter(struct kiocb *iocb, struct iov_iter *to)
6178 inode_lock_shared (inode );
6279 }
6380
64- if (!ext4_dio_supported ( inode )) {
81+ if (!ext4_should_use_dio ( iocb , to )) {
6582 inode_unlock_shared (inode );
6683 /*
6784 * Fallback to buffered I/O if the operation being performed on
@@ -509,7 +526,7 @@ static ssize_t ext4_dio_write_iter(struct kiocb *iocb, struct iov_iter *from)
509526 }
510527
511528 /* Fallback to buffered I/O if the inode does not support direct I/O. */
512- if (!ext4_dio_supported ( inode )) {
529+ if (!ext4_should_use_dio ( iocb , from )) {
513530 if (ilock_shared )
514531 inode_unlock_shared (inode );
515532 else
0 commit comments