From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-10.1 required=3.0 tests=DKIM_SIGNED,DKIM_VALID, DKIM_VALID_AU,FREEMAIL_FORGED_FROMDOMAIN,FREEMAIL_FROM, HEADER_FROM_DIFFERENT_DOMAINS,INCLUDES_PATCH,MAILING_LIST_MULTI,SIGNED_OFF_BY, SPF_HELO_NONE,SPF_PASS,USER_AGENT_GIT autolearn=unavailable autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 80B4BC433E1 for ; Mon, 29 Jun 2020 03:41:09 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 61CF1206BE for ; Mon, 29 Jun 2020 03:41:09 +0000 (UTC) Authentication-Results: mail.kernel.org; dkim=pass (1024-bit key) header.d=126.com header.i=@126.com header.b="f66LYiQC" Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1726005AbgF2DlG (ORCPT ); Sun, 28 Jun 2020 23:41:06 -0400 Received: from m15114.mail.126.com ([220.181.15.114]:46649 "EHLO m15114.mail.126.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1725983AbgF2DlG (ORCPT ); Sun, 28 Jun 2020 23:41:06 -0400 X-Greylist: delayed 1827 seconds by postgrey-1.27 at vger.kernel.org; Sun, 28 Jun 2020 23:41:04 EDT DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=126.com; s=s110527; h=From:Subject:Date:Message-Id; bh=w+EIEf24qwNuCWKj35 zeUBF8QLrIvjDA8vW20eFEtc0=; b=f66LYiQCmMi3YvJrnhTruUZ6DBHjqV4AT2 hAY1Z0AHFkQI22ac+4AwLiOTeMRomZO5CLxQtEjXjehIr0Id3WIo/4e/kJwhi56O stn+ekWURI4ZF0zqF3oXt8rBp5l1bqoBGDH5MNiY6mfgLpZ2E/kYciPyxVaEB+vs D8VOnE0k0= Received: from xr-hulk-k8s-node1933.gh.sankuai.com (unknown [101.236.11.3]) by smtp7 (Coremail) with SMTP id DsmowAB3eGhlW_leYwmDGw--.3986S2; Mon, 29 Jun 2020 11:09:30 +0800 (CST) From: Jiang Ying To: Markus.Elfring@web.de, tytso@mit.edu, adilger.kernel@dilger.ca, linux-ext4@vger.kernel.org, linux-kernel@vger.kernel.org Cc: wanglong19@meituan.com Subject: [PATCH v2] ext4: fix direct I/O read error Date: Mon, 29 Jun 2020 11:09:25 +0800 Message-Id: <1593400165-114818-1-git-send-email-jiangying8582@126.com> X-Mailer: git-send-email 1.8.3.1 X-CM-TRANSID: DsmowAB3eGhlW_leYwmDGw--.3986S2 X-Coremail-Antispam: 1Uf129KBjvJXoWxZr47tFWDJrW5JryxJw4UCFg_yoW5tw4fpF sxCa15WrWkZr1fC3ZrKanrZFyFy3yDGFWUXr98uw1UAr4ag3s5KFWIkF17G3y5GrWF9w4F vFZ8tryrAw1UZFJanT9S1TB71UUUUUUqnTZGkaVYY2UrUUUUjbIjqfuFe4nvWSU5nxnvy2 9KBjDUYxBIdaVFxhVjvjDU0xZFpf9x07jYT5dUUUUU= X-Originating-IP: [101.236.11.3] X-CM-SenderInfo: xmld0wp1lqwmqvysqiyswou0bp/1tbimgpSAFpEAcprjQAAsx Sender: linux-ext4-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-ext4@vger.kernel.org Fixes: 9fe55eea7e4b ("Fix race when checking i_size on direct i/o read"). This commit caused ext4 direct I/O read error when the read size is not alignment with block size. Then, I will use a test to explain the error. (1) Make the file that is not alignment with block size: $dd if=/dev/zero of=./test.jar bs=1000 count=3 (2) I wrote a test script named "direct_io_read_file.c" as following: #include #include #include #include #include #include #include #define BUF_SIZE 1024 int main() { int fd; int ret; unsigned char *buf; ret = posix_memalign((void **)&buf, 512, BUF_SIZE); if (ret) { perror("posix_memalign failed"); exit(1); } fd = open("./test.jar", O_RDONLY | O_DIRECT, 0755); if (fd < 0){ perror("open ./test.jar failed"); exit(1); } do { ret = read(fd, buf, BUF_SIZE); printf("ret=%d\n",ret); if (ret < 0) { perror("write test.jar failed"); } } while (ret > 0); free(buf); close(fd); } (3) Compiling the script: $gcc direct_io_read_file.c -D_GNU_SOURCE (4) Exec the script: $./a.out The result is as following: ret=1024 ret=1024 ret=952 ret=-1 write test.jar failed: Invalid argument. I have tested this script on XFS filesystem, XFS does not have this problem, because XFS use iomap_dio_rw() to do direct I/O read. And the comparing between read offset and file size is done in iomap_dio_rw(), the code is as following: if (pos < size) { retval = filemap_write_and_wait_range(mapping, pos, pos + iov_length(iov, nr_segs) - 1); if (!retval) { retval = mapping->a_ops->direct_IO(READ, iocb, iov, pos, nr_segs); } ... } Only when "pos < size", direct I/O can be done, or 0 will be return. I have tested my fix patch, it is up to the mustard of EINVAL in man2(read) as following: #include ssize_t read(int fd, void *buf, size_t count); EINVAL fd is attached to an object which is unsuitable for reading; or the file was opened with the O_DIRECT flag, and either the address specified in buf, the value specified in count, or the current file offset is not suitably aligned. So I think this patch can be applied to fix ext4 direct I/O error. However Ext4 introduces direct I/O read using iomap infrastructure on kernel 5.5, the patch is commit ("ext4: introduce direct I/O read using iomap infrastructure"), then Ext4 will be the same as XFS, they all use iomap_dio_rw() to do direct I/O read. So this problem does not exist on kernel 5.5 for Ext4. >From above description, we can see this problem exists on all the kernel versions between kernel 3.14 and kernel 5.4. Please apply this patch on these kernel versions, or please use the method on kernel 5.5 to fix this problem. Thanks. Co-developed-by: Wang Long Signed-off-by: Wang Long Signed-off-by: Jiang Ying --- fs/ext4/inode.c | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/fs/ext4/inode.c b/fs/ext4/inode.c index 516faa2..d514ff5 100644 --- a/fs/ext4/inode.c +++ b/fs/ext4/inode.c @@ -3821,6 +3821,12 @@ static ssize_t ext4_direct_IO_read(struct kiocb *iocb, struct iov_iter *iter) struct inode *inode = mapping->host; size_t count = iov_iter_count(iter); ssize_t ret; + loff_t offset = iocb->ki_pos; + loff_t size; + + size = i_size_read(inode); + if (offset >= size) + return 0; /* * Shared inode_lock is enough for us - it protects against concurrent -- 1.8.3.1