Received: by 2002:a05:6a10:a0d1:0:0:0:0 with SMTP id j17csp1245383pxa; Thu, 6 Aug 2020 03:17:06 -0700 (PDT) X-Google-Smtp-Source: ABdhPJyg3Pn243BrV0IWZFjmd8IFR3xMD6SIiAa5YeSFAwwbb0owh7bZlHuN3vDmcDMzhZfm38M0 X-Received: by 2002:a05:6402:b45:: with SMTP id bx5mr3463265edb.22.1596709026031; Thu, 06 Aug 2020 03:17:06 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1596709026; cv=none; d=google.com; s=arc-20160816; b=DAtdJZhio/RAiVWb12QBcHkmIJf2+zmFxrxewKuHJriEoWLr2FF0iRqm7gRwVe7lz8 4urdvB8QuTqx2IIK55gE6MQsAqNKJKPHcg65J+RfB/usS2kAUrREsnCx1L5zDwyuKya8 DRwbunPsXH9iXz7fLLkLfhY2lRFGxCDzoOBbddkvkHlOJ/Q4jafTznH3jarU61Dui90B 8YO5SC52VDRO3m526BOtz7E+9cEhTAFxHfCvZ2IucJRebP+iInisTfvJXDqahXFZ/IzT g2ugtdiytEG2MH1zjgMR14h8edOxkeUPNVzUxiixs/Y8FbzwlNoyq08Ft6LlluK3Tlay 9Rwg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:message-id:date:subject:cc:to:from :dkim-signature; bh=luyhsBjuQ1lgUJ9+5gmeLe6LuVmiVnknnb0rBb/Ua3k=; b=nF1GkVa25NINhsCdXLK2+gHKbF6FLHYYoBQ4YlvAMoVObfSi9D1AEXZDEOC4BwPnya UlZbsVaxItsl5sVPR9h9YKqO57l8ui0CDF3HJCKEbeGbR92EXq7DorF5W1rtZZcGkXyR OkY6fCQqtGzLDBDOTD0vR9IybCeKgHy0BcOMf/Vtskw/fwDly3kRjB+qSyxW9jusTjkj a/cufXAPcXWqJLNWuZJooGUmlTX7D4OEjsgJLHCovG8DsR/u3ooP48DP1gVHtpCfBZHM afBdoWg8R1wt2bsFmQE3bzhkifYRVABTp9RKWLpkFMg15rIs4Qnm4Y7KXNOalEE5AZSc E0cg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@126.com header.s=s110527 header.b=Z4kzREVN; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=126.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id r21si3320330edc.313.2020.08.06.03.16.43; Thu, 06 Aug 2020 03:17:06 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) client-ip=23.128.96.18; Authentication-Results: mx.google.com; dkim=pass header.i=@126.com header.s=s110527 header.b=Z4kzREVN; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=126.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1729293AbgHFKQZ (ORCPT + 99 others); Thu, 6 Aug 2020 06:16:25 -0400 Received: from m15112.mail.126.com ([220.181.15.112]:52946 "EHLO m15112.mail.126.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1729146AbgHFKQS (ORCPT ); Thu, 6 Aug 2020 06:16:18 -0400 X-Greylist: delayed 2058 seconds by postgrey-1.27 at vger.kernel.org; Thu, 06 Aug 2020 06:16:14 EDT DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=126.com; s=s110527; h=From:Subject:Date:Message-Id; bh=luyhsBjuQ1lgUJ9+5g meLe6LuVmiVnknnb0rBb/Ua3k=; b=Z4kzREVNOFyelPCzOHVyCLqXwOlVEyqfuw tV/OdpR7ZZjMhtsJgM14RjtH4dacZjj8/sBCTDntM9m991JbDPIFnAxWwmFB1soB wM7bsKDUB9VcyhWYzdWbTVjzuLRHmIDlEfIrhadb2UG6sS7R7yim9MJOKqEGfoH3 6kYususX8= Received: from xr-hulk-k8s-node1933.gh.sankuai.com (unknown [101.236.11.3]) by smtp2 (Coremail) with SMTP id DMmowAD3_OCDzytfxipQGA--.39882S2; Thu, 06 Aug 2020 17:38:17 +0800 (CST) From: Jiang Ying To: tytso@mit.edu, adilger.kernel@dilger.ca, linux-ext4@vger.kernel.org, linux-kernel@vger.kernel.org, jack@suse.cz, stable@vger.kernel.org Cc: wanglong19@meituan.com, heguanjun@meituan.com Subject: [PATCH v5] ext4: fix direct I/O read error for kernel stable rc4.4 Date: Thu, 6 Aug 2020 17:38:11 +0800 Message-Id: <1596706691-82760-1-git-send-email-jiangying8582@126.com> X-Mailer: git-send-email 1.8.3.1 X-CM-TRANSID: DMmowAD3_OCDzytfxipQGA--.39882S2 X-Coremail-Antispam: 1Uf129KBjvJXoWxAw47WFW8tw4rCr1UZr4xZwb_yoWrCF4xpr sxC3W5WrZYvr4xCanrG3WDuFyFyayDGFWUXF9Y934UZw45Kr95KrWIkF1UGayUJrZY9w4Y qFWqyryfJw1UZaDanT9S1TB71UUUUUUqnTZGkaVYY2UrUUUUjbIjqfuFe4nvWSU5nxnvy2 9KBjDUYxBIdaVFxhVjvjDU0xZFpf9x07jaFAJUUUUU= X-Originating-IP: [101.236.11.3] X-CM-SenderInfo: xmld0wp1lqwmqvysqiyswou0bp/1tbimgl4AFpEAtuHOQAAsS Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org This patch is used to fix ext4 direct I/O read error when the read size is not aligned with block size. Then, I will use a test to explain the error. (1) Make a file that is not aligned with block size: $dd if=/dev/zero of=./test.jar bs=1000 count=3 (2) I wrote a source file 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) Compile the source file: $gcc direct_io_read_file.c -D_GNU_SOURCE (4) Run the test program: $./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 program 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 the fix patch on Ext4, 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. It will cause the Applications to fail to read. For example, when the search service downloads a new full index file, the search engine is loading the previous index file and is processing the search request, it can not use buffer io that may squeeze the previous index file in use from pagecache, so the serch service must use direct I/O read. Please apply this patch on these kernel versions, or please use the method on kernel 5.5 to fix this problem. Fixes: 9fe55eea7e4b ("Fix race when checking i_size on direct i/o read") Reviewed-by: Jan Kara Reviewed-by: Wang Long Signed-off-by: Jiang Ying Changes since V4: Fix build error on kernel stable rc 4.4. This patch only for kernel 4.4. Changes since V3: Add the info: this bug could break some application that use the stable kernel releases. Changes since V2: Optimize the description of the commit message and make a variation for the patch, e.g. with: Before: loff_t size; size = i_size_read(inode); After: loff_t size = i_size_read(inode); Changes since V1: Signed-off use real name and add "Fixes:" flag --- fs/ext4/inode.c | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/fs/ext4/inode.c b/fs/ext4/inode.c index 8e79970..8816016 100644 --- a/fs/ext4/inode.c +++ b/fs/ext4/inode.c @@ -3353,6 +3353,13 @@ static ssize_t ext4_direct_IO(struct kiocb *iocb, struct iov_iter *iter, size_t count = iov_iter_count(iter); ssize_t ret; + if (iov_iter_rw(iter) == READ) { + loff_t size = i_size_read(inode); + + if (offset >= size) + return 0; + } + #ifdef CONFIG_EXT4_FS_ENCRYPTION if (ext4_encrypted_inode(inode) && S_ISREG(inode->i_mode)) return 0; -- 1.8.3.1