From patchwork Sun Jun 28 06:47:43 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: =?utf-8?b?5aec6L+O?= X-Patchwork-Id: 1318493 Return-Path: X-Original-To: patchwork-incoming@ozlabs.org Delivered-To: patchwork-incoming@ozlabs.org Authentication-Results: ozlabs.org; spf=pass (sender SPF authorized) smtp.mailfrom=vger.kernel.org (client-ip=23.128.96.18; helo=vger.kernel.org; envelope-from=linux-ext4-owner@vger.kernel.org; receiver=) Authentication-Results: ozlabs.org; dmarc=fail (p=none dis=none) header.from=126.com Authentication-Results: ozlabs.org; dkim=fail reason="signature verification failed" (1024-bit key; unprotected) header.d=126.com header.i=@126.com header.a=rsa-sha256 header.s=s110527 header.b=BrvlO/OZ; dkim-atps=neutral Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by ozlabs.org (Postfix) with ESMTP id 49vhp42fGNz9sQt for ; Sun, 28 Jun 2020 17:19:40 +1000 (AEST) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1726036AbgF1HT3 (ORCPT ); Sun, 28 Jun 2020 03:19:29 -0400 Received: from m1514.mail.126.com ([220.181.15.14]:54939 "EHLO m1514.mail.126.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1725933AbgF1HT2 (ORCPT ); Sun, 28 Jun 2020 03:19:28 -0400 X-Greylist: delayed 1889 seconds by postgrey-1.27 at vger.kernel.org; Sun, 28 Jun 2020 03:19:27 EDT DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=126.com; s=s110527; h=Date:From:Subject:MIME-Version:Message-ID; bh=/EfVl 9jaErOonQ6lQzf9oi45csPLkH5eaVbWiGfOfuQ=; b=BrvlO/OZDrXHWDxY5mnrG 0Qsl/n9Mmaz4+FjNDUVn0U2ZPRXVOrV4h63rTSfg/jcT2bkik9V368kTnskfYhgU hD7myh5Li70B4oOMg8vUkO5S2G/3UphK5iMM+6ToBJSZZ49/KT5RvOs+u7bKjc4Y lFewl1wg4/DGZCPqxCv0u8= Received: from jiangying8582$126.com ( [103.37.140.35] ) by ajax-webmail-wmsvr14 (Coremail) ; Sun, 28 Jun 2020 14:47:43 +0800 (CST) X-Originating-IP: [103.37.140.35] Date: Sun, 28 Jun 2020 14:47:43 +0800 (CST) From: =?gbk?b?varTrQ==?= To: tytso@mit.edu Cc: adilger.kernel@dilger.ca, linux-ext4@vger.kernel.org, linux-kernel@vger.kernel.org Subject: [PATCH] ext4: fix direct I/O read error X-Priority: 3 X-Mailer: Coremail Webmail Server Version XT5.0.10 build 20190724(ac680a23) Copyright (c) 2002-2020 www.mailtech.cn 126com MIME-Version: 1.0 Message-ID: <7925c422.4205.172f9ae864d.Coremail.jiangying8582@126.com> X-Coremail-Locale: zh_CN X-CM-TRANSID: DsqowACXPukRPfheEN3PAA--.28450W X-CM-SenderInfo: xmld0wp1lqwmqvysqiyswou0bp/1tbi6AJRAFpEA+roqAABsN X-Coremail-Antispam: 1U5529EdanIXcx71UUUUU7vcSsGvfC2KfnxnUU== Sender: linux-ext4-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-ext4@vger.kernel.org From: jiangying8582 Date: Wed, 24 Jun 2020 19:02:34 +0800 Subject: [PATCH] ext4: fix direct I/O read error This patch is used to fix ext4 direct I/O read error when the read size is not alignment with block size. Compare the size between read offset with file size, if read offset is greater than file size, then return 0. Then, I will use a test to explain the error. (1) Make the file that is not alignment wiht 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" s 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 rts-segmenter-0.3.7.2.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 is 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 problem. Why this problem can happen? I think commit <9fe55eea7e4b> ("Fix race when checking i_size on direct i/o read") caused. 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. Signed-off-by: jiangying8582 --- 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