ext4: support STATX_DIOALIGN
authorEric Biggers <ebiggers@google.com>
Sat, 27 Aug 2022 06:58:47 +0000 (23:58 -0700)
committerEric Biggers <ebiggers@google.com>
Mon, 12 Sep 2022 00:47:12 +0000 (19:47 -0500)
Add support for STATX_DIOALIGN to ext4, so that direct I/O alignment
restrictions are exposed to userspace in a generic way.

Acked-by: Theodore Ts'o <tytso@mit.edu>
Signed-off-by: Eric Biggers <ebiggers@google.com>
Link: https://lore.kernel.org/r/20220827065851.135710-5-ebiggers@kernel.org
fs/ext4/ext4.h
fs/ext4/file.c
fs/ext4/inode.c

index 9bca5565547baeb0bc5e77e94082b2b5d81b4a75..e6674504ca2abec016dcc8faa54d141982f22e79 100644 (file)
@@ -2979,6 +2979,7 @@ extern struct inode *__ext4_iget(struct super_block *sb, unsigned long ino,
 extern int  ext4_write_inode(struct inode *, struct writeback_control *);
 extern int  ext4_setattr(struct user_namespace *, struct dentry *,
                         struct iattr *);
+extern u32  ext4_dio_alignment(struct inode *inode);
 extern int  ext4_getattr(struct user_namespace *, const struct path *,
                         struct kstat *, u32, unsigned int);
 extern void ext4_evict_inode(struct inode *);
index 26d7426208970d7dd27e542b06fe42ce89a8b92f..8bb1c35fd6dd5a6e7a918dcc3c7f41d03cb62c83 100644 (file)
 #include "acl.h"
 #include "truncate.h"
 
-static bool ext4_dio_supported(struct kiocb *iocb, struct iov_iter *iter)
+/*
+ * Returns %true if the given DIO request should be attempted with DIO, or
+ * %false if it should fall back to buffered I/O.
+ *
+ * DIO isn't well specified; when it's unsupported (either due to the request
+ * being misaligned, or due to the file not supporting DIO at all), filesystems
+ * either fall back to buffered I/O or return EINVAL.  For files that don't use
+ * any special features like encryption or verity, ext4 has traditionally
+ * returned EINVAL for misaligned DIO.  iomap_dio_rw() uses this convention too.
+ * In this case, we should attempt the DIO, *not* fall back to buffered I/O.
+ *
+ * In contrast, in cases where DIO is unsupported due to ext4 features, ext4
+ * traditionally falls back to buffered I/O.
+ *
+ * This function implements the traditional ext4 behavior in all these cases.
+ */
+static bool ext4_should_use_dio(struct kiocb *iocb, struct iov_iter *iter)
 {
        struct inode *inode = file_inode(iocb->ki_filp);
+       u32 dio_align = ext4_dio_alignment(inode);
 
-       if (IS_ENCRYPTED(inode)) {
-               if (!fscrypt_dio_supported(inode))
-                       return false;
-               if (!IS_ALIGNED(iocb->ki_pos | iov_iter_alignment(iter),
-                               i_blocksize(inode)))
-                       return false;
-       }
-       if (fsverity_active(inode))
+       if (dio_align == 0)
                return false;
-       if (ext4_should_journal_data(inode))
-               return false;
-       if (ext4_has_inline_data(inode))
-               return false;
-       return true;
+
+       if (dio_align == 1)
+               return true;
+
+       return IS_ALIGNED(iocb->ki_pos | iov_iter_alignment(iter), dio_align);
 }
 
 static ssize_t ext4_dio_read_iter(struct kiocb *iocb, struct iov_iter *to)
@@ -68,7 +78,7 @@ static ssize_t ext4_dio_read_iter(struct kiocb *iocb, struct iov_iter *to)
                inode_lock_shared(inode);
        }
 
-       if (!ext4_dio_supported(iocb, to)) {
+       if (!ext4_should_use_dio(iocb, to)) {
                inode_unlock_shared(inode);
                /*
                 * Fallback to buffered I/O if the operation being performed on
@@ -516,7 +526,7 @@ static ssize_t ext4_dio_write_iter(struct kiocb *iocb, struct iov_iter *from)
        }
 
        /* Fallback to buffered I/O if the inode does not support direct I/O. */
-       if (!ext4_dio_supported(iocb, from)) {
+       if (!ext4_should_use_dio(iocb, from)) {
                if (ilock_shared)
                        inode_unlock_shared(inode);
                else
index 601214453c3aec9163289c70c58426a40cdfb024..364774230d87ac3e3833b6b7d34d6deb7639ce0c 100644 (file)
@@ -5550,6 +5550,22 @@ err_out:
        return error;
 }
 
+u32 ext4_dio_alignment(struct inode *inode)
+{
+       if (fsverity_active(inode))
+               return 0;
+       if (ext4_should_journal_data(inode))
+               return 0;
+       if (ext4_has_inline_data(inode))
+               return 0;
+       if (IS_ENCRYPTED(inode)) {
+               if (!fscrypt_dio_supported(inode))
+                       return 0;
+               return i_blocksize(inode);
+       }
+       return 1; /* use the iomap defaults */
+}
+
 int ext4_getattr(struct user_namespace *mnt_userns, const struct path *path,
                 struct kstat *stat, u32 request_mask, unsigned int query_flags)
 {
@@ -5565,6 +5581,27 @@ int ext4_getattr(struct user_namespace *mnt_userns, const struct path *path,
                stat->btime.tv_nsec = ei->i_crtime.tv_nsec;
        }
 
+       /*
+        * Return the DIO alignment restrictions if requested.  We only return
+        * this information when requested, since on encrypted files it might
+        * take a fair bit of work to get if the file wasn't opened recently.
+        */
+       if ((request_mask & STATX_DIOALIGN) && S_ISREG(inode->i_mode)) {
+               u32 dio_align = ext4_dio_alignment(inode);
+
+               stat->result_mask |= STATX_DIOALIGN;
+               if (dio_align == 1) {
+                       struct block_device *bdev = inode->i_sb->s_bdev;
+
+                       /* iomap defaults */
+                       stat->dio_mem_align = bdev_dma_alignment(bdev) + 1;
+                       stat->dio_offset_align = bdev_logical_block_size(bdev);
+               } else {
+                       stat->dio_mem_align = dio_align;
+                       stat->dio_offset_align = dio_align;
+               }
+       }
+
        flags = ei->i_flags & EXT4_FL_USER_VISIBLE;
        if (flags & EXT4_APPEND_FL)
                stat->attributes |= STATX_ATTR_APPEND;