Received: by 2002:a05:6a10:a0d1:0:0:0:0 with SMTP id j17csp690942pxa; Wed, 5 Aug 2020 10:24:25 -0700 (PDT) X-Google-Smtp-Source: ABdhPJy2g31H6c18vFfYleNFm26bQ4Jp4gbl69xMnZ7yS4mXYptg8QsAGo9vMUdGitqjc3J27/rX X-Received: by 2002:a17:906:36c2:: with SMTP id b2mr78263ejc.238.1596648265298; Wed, 05 Aug 2020 10:24:25 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1596648265; cv=none; d=google.com; s=arc-20160816; b=ai1DToj/vCHMCgEo614/E9Mv/TEYasuPgbB5zoMXDRQsBGzERHo+aG6lG2WWjNaQvz laH3XS503IDufhuwrW+O086dd1U8+YICelftJNHXLOsU4oeuuzRmRpfsktKIXaSCVssx +s+RU5jIegFmP2fvbBoDaIUteOQ8CC0LDE51S1UaOgY24bY5C6EOA9u1IXguQ5xnTX6y djPMSdV53NnMSZIVUxIMoIIEo49LZEPGmuE5u3UODYXCL3m9lth9mWzIstEnRRDw7Z/H rndN3/5a19LmbssHCW4hZt+YJ0TbzH1Jo3LQxRyDUVzUK9Og5U+YnytfrZIsvpNOn9w+ tg/A== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:content-transfer-encoding:mime-version :user-agent:references:in-reply-to:message-id:date:subject:cc:to :from:dkim-signature; bh=yk8T1SrD84ynSPMgxRA13h2R3Rh1VXIC4xwhhcYgExs=; b=x9CIDJh/FOaSep6vfUM+rTwGqPxa5RcmFYmbS2xj/lb+zKhuRh6z3cTRGEgXH4BN5Q DsQ80EtXYmiRHtafpn8QSw9MdzXv9rl1Cm07wEtqI2BoLkSXeOz0iHkfLayz7cAcSbuc zBUPYY7R6zZEL0FWInKSluY4tqlDW6TZiChZZ6ftXd6ijOcGiI2PLVAIrbTKl+dd+Cl8 BTqU2j2biJe4pCFKPGa3adKBaNnRQxVm7GtpcxhuC9JHzlfRUTomnKxMB9cDzqOfTGhX ODcREQHf1emPsUcVb1+wFJy/g824DoD56PEfN+lPMay0d1YW9q60SIFVDlljUuwz4t6Z CXYA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=default header.b=aSVj0T1w; 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 Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id b25si1625021edy.296.2020.08.05.10.24.02; Wed, 05 Aug 2020 10:24:25 -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=@kernel.org header.s=default header.b=aSVj0T1w; 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 Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1728384AbgHERXV (ORCPT + 99 others); Wed, 5 Aug 2020 13:23:21 -0400 Received: from mail.kernel.org ([198.145.29.99]:36178 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1728629AbgHERRk (ORCPT ); Wed, 5 Aug 2020 13:17:40 -0400 Received: from localhost (83-86-89-107.cable.dynamic.v4.ziggo.nl [83.86.89.107]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mail.kernel.org (Postfix) with ESMTPSA id 3899D233E2; Wed, 5 Aug 2020 15:53:22 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=default; t=1596642802; bh=ZbssVGpllyh0qYTSG60hN7rg8nKExPvelIXE1QW1lXM=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=aSVj0T1w+Mqr5fBbFHeFZbR4I5zNJG8rjdYwo6scHw8CvxYrvEe1xR2bQM+yTDzSQ 76FfIaz1LE4a4sQQC3/DuaT+Sp+708sLe9eun5TrPU9Tiw25Fbms/mRfQIOvMkumkw sPo1MBo7f7NS3V3b3PoZg9unj+4alP8y/voqAHk0= From: Greg Kroah-Hartman To: linux-kernel@vger.kernel.org Cc: Greg Kroah-Hartman , stable@vger.kernel.org, Jan Kara , Wang Long , Jiang Ying Subject: [PATCH 4.19 6/6] ext4: fix direct I/O read error Date: Wed, 5 Aug 2020 17:53:05 +0200 Message-Id: <20200805153505.794820436@linuxfoundation.org> X-Mailer: git-send-email 2.28.0 In-Reply-To: <20200805153505.472594546@linuxfoundation.org> References: <20200805153505.472594546@linuxfoundation.org> User-Agent: quilt/0.66 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org From: Jiang Ying 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 Co-developed-by: Wang Long Signed-off-by: Wang Long Signed-off-by: Jiang Ying Signed-off-by: Greg Kroah-Hartman --- fs/ext4/inode.c | 5 +++++ 1 file changed, 5 insertions(+) --- a/fs/ext4/inode.c +++ b/fs/ext4/inode.c @@ -3848,6 +3848,11 @@ static ssize_t ext4_direct_IO_read(struc struct inode *inode = mapping->host; size_t count = iov_iter_count(iter); ssize_t ret; + loff_t offset = iocb->ki_pos; + loff_t size = i_size_read(inode); + + if (offset >= size) + return 0; /* * Shared inode_lock is enough for us - it protects against concurrent