linux-kernel.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH] erofs: support direct IO for uncompressed file
@ 2020-12-14 14:04 Huang Jianan
  2020-12-14 14:04 ` [PATCH] fsstress: support direct IO Huang Jianan
                   ` (2 more replies)
  0 siblings, 3 replies; 9+ messages in thread
From: Huang Jianan @ 2020-12-14 14:04 UTC (permalink / raw)
  To: linux-erofs; +Cc: huangjianan, guoweichao, zhangshiming, linux-kernel

direct IO is useful in certain scenarios for uncompressed files.
For example, it can avoid double pagecache when use the uncompressed
file to mount upper layer filesystem.

In addition, another patch adds direct IO test for the stress tool
which was mentioned here:
https://lore.kernel.org/linux-erofs/20200206135631.1491-1-hsiangkao@aol.com/

Signed-off-by: Huang Jianan <huangjianan@oppo.com>
Signed-off-by: Guo Weichao <guoweichao@oppo.com>
---
 fs/erofs/data.c | 57 +++++++++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 57 insertions(+)

diff --git a/fs/erofs/data.c b/fs/erofs/data.c
index ea4f693bee22..3067aa3defff 100644
--- a/fs/erofs/data.c
+++ b/fs/erofs/data.c
@@ -6,6 +6,8 @@
  */
 #include "internal.h"
 #include <linux/prefetch.h>
+#include <linux/uio.h>
+#include <linux/blkdev.h>
 
 #include <trace/events/erofs.h>
 
@@ -312,6 +314,60 @@ static void erofs_raw_access_readahead(struct readahead_control *rac)
 		submit_bio(bio);
 }
 
+static int erofs_get_block(struct inode *inode, sector_t iblock,
+			   struct buffer_head *bh, int create)
+{
+	struct erofs_map_blocks map = {
+		.m_la = blknr_to_addr(iblock),
+	};
+	int err;
+
+	err = erofs_map_blocks(inode, &map, EROFS_GET_BLOCKS_RAW);
+	if (err)
+		return err;
+
+	if (map.m_flags & EROFS_MAP_MAPPED)
+		map_bh(bh, inode->i_sb, erofs_blknr(map.m_pa));
+
+	return err;
+}
+
+static int check_direct_IO(struct inode *inode, struct iov_iter *iter,
+			   loff_t offset)
+{
+	unsigned i_blkbits = READ_ONCE(inode->i_blkbits);
+	unsigned blkbits = i_blkbits;
+	unsigned blocksize_mask = (1 << blkbits) - 1;
+	unsigned long align = offset | iov_iter_alignment(iter);
+	struct block_device *bdev = inode->i_sb->s_bdev;
+
+	if (align & blocksize_mask) {
+		if (bdev)
+			blkbits = blksize_bits(bdev_logical_block_size(bdev));
+		blocksize_mask = (1 << blkbits) - 1;
+		if (align & blocksize_mask)
+			return -EINVAL;
+		return 1;
+	}
+	return 0;
+}
+
+static ssize_t erofs_direct_IO(struct kiocb *iocb, struct iov_iter *iter)
+{
+	struct address_space *mapping = iocb->ki_filp->f_mapping;
+	struct inode *inode = mapping->host;
+	loff_t offset = iocb->ki_pos;
+	int err;
+
+	err = check_direct_IO(inode, iter, offset);
+	if (err)
+		return err < 0 ? err : 0;
+
+	return __blockdev_direct_IO(iocb, inode, inode->i_sb->s_bdev, iter,
+				    erofs_get_block, NULL, NULL,
+				    DIO_LOCKING | DIO_SKIP_HOLES);
+}
+
 static sector_t erofs_bmap(struct address_space *mapping, sector_t block)
 {
 	struct inode *inode = mapping->host;
@@ -336,6 +392,7 @@ static sector_t erofs_bmap(struct address_space *mapping, sector_t block)
 const struct address_space_operations erofs_raw_access_aops = {
 	.readpage = erofs_raw_access_readpage,
 	.readahead = erofs_raw_access_readahead,
+	.direct_IO = erofs_direct_IO,
 	.bmap = erofs_bmap,
 };
 
-- 
2.25.1


^ permalink raw reply related	[flat|nested] 9+ messages in thread

* [PATCH] fsstress: support direct IO
  2020-12-14 14:04 [PATCH] erofs: support direct IO for uncompressed file Huang Jianan
@ 2020-12-14 14:04 ` Huang Jianan
  2020-12-22 13:21 ` [PATCH] erofs: support direct IO for uncompressed file Gao Xiang
  2020-12-22 14:22 ` Christoph Hellwig
  2 siblings, 0 replies; 9+ messages in thread
From: Huang Jianan @ 2020-12-14 14:04 UTC (permalink / raw)
  To: linux-erofs; +Cc: huangjianan, guoweichao, zhangshiming, linux-kernel

From: huangjianan <huangjianan@oppo.com>

add direct IO test for the stress tool which was mentioned here:
https://lore.kernel.org/linux-erofs/20200206135631.1491-1-hsiangkao@aol.com/

Signed-off-by: Huang Jianan <huangjianan@oppo.com>
Signed-off-by: Guo Weichao <guoweichao@oppo.com>
---
 stress.c | 20 +++++++++++++++++---
 1 file changed, 17 insertions(+), 3 deletions(-)

diff --git a/stress.c b/stress.c
index f4bf874..7e7cc93 100644
--- a/stress.c
+++ b/stress.c
@@ -4,12 +4,14 @@
  *
  * Copyright (C) 2019-2020 Gao Xiang <hsiangkao@aol.com>
  */
+#define _GNU_SOURCE
 #define _LARGEFILE64_SOURCE
 #include <errno.h>
 #include <fcntl.h>
 #include <stdio.h>
 #include <stdlib.h>
 #include <stdint.h>
+#include <stdbool.h>
 #include <string.h>
 #include <sys/types.h>
 #include <sys/wait.h>
@@ -21,6 +23,7 @@
 #define MAX_CHUNKSIZE	(4 * 1024 * 1024)
 #define MAX_SCAN_CHUNKSIZE	(256 * 1024)
 
+bool direct_io = false;
 unsigned int nprocs = 512;
 sig_atomic_t should_stop = 0;
 
@@ -98,7 +101,7 @@ int drop_file_cache(int fd, int mode)
 
 int tryopen(char *filename)
 {
-	int fd = open(filename, O_RDONLY);
+	int fd = open(filename, direct_io ? O_RDONLY : O_RDONLY | O_DIRECT);
 
 	if (fd < 0)
 		return -errno;
@@ -166,6 +169,13 @@ int randread(int fd, int chkfd, uint64_t filesize)
 	if (start + length > filesize)
 		length = filesize - start;
 
+	if (direct_io) {
+		length = (((length - 1) >> PAGE_SHIFT) + 1)
+			<< PAGE_SHIFT;
+		if (!length || start + length > filesize)
+			return 0;
+	}
+
 	printf("randread(%u): %llu bytes @ %llu\n",
 	       getpid(), (unsigned long long)length,
 	       (unsigned long long)start);
@@ -212,7 +222,7 @@ int testfd(int fd, int chkfd, int mode)
 		err = doscan(fd, chkfd, filesize, chunksize);
 		if (err)
 			return err;
-	} else if (mode == RANDSCAN_UNALIGNED) {
+	} else if (mode == RANDSCAN_UNALIGNED && !direct_io) {
 		chunksize = (random() * random() % MAX_SCAN_CHUNKSIZE) + 1;
 		err = doscan(fd, chkfd, filesize, chunksize);
 		if (err)
@@ -252,8 +262,11 @@ static int parse_options(int argc, char *argv[])
 {
 	int opt;
 
-	while ((opt = getopt(argc, argv, "p:")) != -1) {
+	while ((opt = getopt(argc, argv, "dp:")) != -1) {
 		switch (opt) {
+		case 'd':
+			direct_io = true;
+			break;
 		case 'p':
 			nprocs = atoi(optarg);
 			if (nprocs < 0) {
@@ -281,6 +294,7 @@ void usage(void)
 {
 	fputs("usage: [options] TESTFILE [COMPRFILE]\n\n"
 	      "stress tester for read-only filesystems\n"
+	      " -d      use direct io\n"
 	      " -p#     set workers to #\n", stderr);
 }
 
-- 
2.7.4


^ permalink raw reply related	[flat|nested] 9+ messages in thread

* Re: [PATCH] erofs: support direct IO for uncompressed file
  2020-12-14 14:04 [PATCH] erofs: support direct IO for uncompressed file Huang Jianan
  2020-12-14 14:04 ` [PATCH] fsstress: support direct IO Huang Jianan
@ 2020-12-22 13:21 ` Gao Xiang
  2020-12-22 14:22 ` Christoph Hellwig
  2 siblings, 0 replies; 9+ messages in thread
From: Gao Xiang @ 2020-12-22 13:21 UTC (permalink / raw)
  To: Huang Jianan; +Cc: linux-erofs, linux-kernel, guoweichao, zhangshiming

Hi Jianan,

On Mon, Dec 14, 2020 at 10:04:27PM +0800, Huang Jianan wrote:
> direct IO is useful in certain scenarios for uncompressed files.
> For example, it can avoid double pagecache when use the uncompressed
> file to mount upper layer filesystem.
> 
> In addition, another patch adds direct IO test for the stress tool
> which was mentioned here:
> https://lore.kernel.org/linux-erofs/20200206135631.1491-1-hsiangkao@aol.com/
> 
> Signed-off-by: Huang Jianan <huangjianan@oppo.com>
> Signed-off-by: Guo Weichao <guoweichao@oppo.com>
> ---
>  fs/erofs/data.c | 57 +++++++++++++++++++++++++++++++++++++++++++++++++
>  1 file changed, 57 insertions(+)
> 
> diff --git a/fs/erofs/data.c b/fs/erofs/data.c
> index ea4f693bee22..3067aa3defff 100644
> --- a/fs/erofs/data.c
> +++ b/fs/erofs/data.c
> @@ -6,6 +6,8 @@
>   */
>  #include "internal.h"
>  #include <linux/prefetch.h>
> +#include <linux/uio.h>
> +#include <linux/blkdev.h>
>  
>  #include <trace/events/erofs.h>
>  
> @@ -312,6 +314,60 @@ static void erofs_raw_access_readahead(struct readahead_control *rac)
>  		submit_bio(bio);
>  }
>  
> +static int erofs_get_block(struct inode *inode, sector_t iblock,
> +			   struct buffer_head *bh, int create)
> +{
> +	struct erofs_map_blocks map = {
> +		.m_la = blknr_to_addr(iblock),
> +	};
> +	int err;
> +
> +	err = erofs_map_blocks(inode, &map, EROFS_GET_BLOCKS_RAW);
> +	if (err)
> +		return err;
> +
> +	if (map.m_flags & EROFS_MAP_MAPPED)
> +		map_bh(bh, inode->i_sb, erofs_blknr(map.m_pa));
> +
> +	return err;
> +}
> +
> +static int check_direct_IO(struct inode *inode, struct iov_iter *iter,
> +			   loff_t offset)
> +{
> +	unsigned i_blkbits = READ_ONCE(inode->i_blkbits);

It would be better to fold in check_direct_IO, also the READ_ONCE above
is somewhat weird...

No rush here, since 5.11-rc1 haven't be out yet, we have >= 2 months to
work it out.

Thanks,
Gao Xiang

> +	unsigned blkbits = i_blkbits;
> +	unsigned blocksize_mask = (1 << blkbits) - 1;
> +	unsigned long align = offset | iov_iter_alignment(iter);
> +	struct block_device *bdev = inode->i_sb->s_bdev;
> +
> +	if (align & blocksize_mask) {
> +		if (bdev)
> +			blkbits = blksize_bits(bdev_logical_block_size(bdev));
> +		blocksize_mask = (1 << blkbits) - 1;
> +		if (align & blocksize_mask)
> +			return -EINVAL;
> +		return 1;
> +	}
> +	return 0;
> +}
> +
> +static ssize_t erofs_direct_IO(struct kiocb *iocb, struct iov_iter *iter)
> +{
> +	struct address_space *mapping = iocb->ki_filp->f_mapping;
> +	struct inode *inode = mapping->host;
> +	loff_t offset = iocb->ki_pos;
> +	int err;
> +
> +	err = check_direct_IO(inode, iter, offset);
> +	if (err)
> +		return err < 0 ? err : 0;
> +
> +	return __blockdev_direct_IO(iocb, inode, inode->i_sb->s_bdev, iter,
> +				    erofs_get_block, NULL, NULL,
> +				    DIO_LOCKING | DIO_SKIP_HOLES);
> +}
> +
>  static sector_t erofs_bmap(struct address_space *mapping, sector_t block)
>  {
>  	struct inode *inode = mapping->host;
> @@ -336,6 +392,7 @@ static sector_t erofs_bmap(struct address_space *mapping, sector_t block)
>  const struct address_space_operations erofs_raw_access_aops = {
>  	.readpage = erofs_raw_access_readpage,
>  	.readahead = erofs_raw_access_readahead,
> +	.direct_IO = erofs_direct_IO,
>  	.bmap = erofs_bmap,
>  };
>  
> -- 
> 2.25.1
> 


^ permalink raw reply	[flat|nested] 9+ messages in thread

* Re: [PATCH] erofs: support direct IO for uncompressed file
  2020-12-14 14:04 [PATCH] erofs: support direct IO for uncompressed file Huang Jianan
  2020-12-14 14:04 ` [PATCH] fsstress: support direct IO Huang Jianan
  2020-12-22 13:21 ` [PATCH] erofs: support direct IO for uncompressed file Gao Xiang
@ 2020-12-22 14:22 ` Christoph Hellwig
  2020-12-22 19:39   ` Gao Xiang
  2 siblings, 1 reply; 9+ messages in thread
From: Christoph Hellwig @ 2020-12-22 14:22 UTC (permalink / raw)
  To: Huang Jianan; +Cc: linux-erofs, guoweichao, zhangshiming, linux-kernel

Please do not add new callers of __blockdev_direct_IO and use the modern
iomap variant instead.

^ permalink raw reply	[flat|nested] 9+ messages in thread

* Re: [PATCH] erofs: support direct IO for uncompressed file
  2020-12-22 14:22 ` Christoph Hellwig
@ 2020-12-22 19:39   ` Gao Xiang
  2020-12-23  7:44     ` Christoph Hellwig
  0 siblings, 1 reply; 9+ messages in thread
From: Gao Xiang @ 2020-12-22 19:39 UTC (permalink / raw)
  To: Christoph Hellwig
  Cc: Huang Jianan, guoweichao, linux-erofs, zhangshiming, linux-kernel

Hi Christoph,

On Tue, Dec 22, 2020 at 02:22:34PM +0000, Christoph Hellwig wrote:
> Please do not add new callers of __blockdev_direct_IO and use the modern
> iomap variant instead.

We've talked about this topic before. The current status is that iomap
doesn't support tail-packing inline data yet (Chao once sent out a version),
and erofs only cares about read intrastructure for now (So we don't think
more about how to deal with tail-packing inline write path). Plus, the
original patch was once lack of inline data regression test from gfs2 folks.

The main use case I know so far is to enable direct I/O and leave loop images
uncompressed for loop devices. And making the content of the loop images
compressed to avoid double caching.

Personally, I'd like to convert it to iomap infrastructure as well. So if it
has some concern for __blockdev_direct_IO as an interim solution, hope that
Jianan could pick this work up. That would be better.

Thanks,
Gao Xiang

> 


^ permalink raw reply	[flat|nested] 9+ messages in thread

* Re: [PATCH] erofs: support direct IO for uncompressed file
  2020-12-22 19:39   ` Gao Xiang
@ 2020-12-23  7:44     ` Christoph Hellwig
  2020-12-23  8:48       ` Huang Jianan
  0 siblings, 1 reply; 9+ messages in thread
From: Christoph Hellwig @ 2020-12-23  7:44 UTC (permalink / raw)
  To: Gao Xiang
  Cc: Christoph Hellwig, Huang Jianan, guoweichao, linux-erofs,
	zhangshiming, linux-kernel

On Wed, Dec 23, 2020 at 03:39:01AM +0800, Gao Xiang wrote:
> Hi Christoph,
> 
> On Tue, Dec 22, 2020 at 02:22:34PM +0000, Christoph Hellwig wrote:
> > Please do not add new callers of __blockdev_direct_IO and use the modern
> > iomap variant instead.
> 
> We've talked about this topic before. The current status is that iomap
> doesn't support tail-packing inline data yet (Chao once sent out a version),
> and erofs only cares about read intrastructure for now (So we don't think
> more about how to deal with tail-packing inline write path). Plus, the
> original patch was once lack of inline data regression test from gfs2 folks.

So resend Chaos prep patch as part of the series switching parts of
erofs to iomap.  We need to move things off the old infrastructure instead
of adding more users and everyone needs to help a little.

^ permalink raw reply	[flat|nested] 9+ messages in thread

* Re: [PATCH] erofs: support direct IO for uncompressed file
  2020-12-23  7:44     ` Christoph Hellwig
@ 2020-12-23  8:48       ` Huang Jianan
  2020-12-23  8:54         ` Christoph Hellwig
  0 siblings, 1 reply; 9+ messages in thread
From: Huang Jianan @ 2020-12-23  8:48 UTC (permalink / raw)
  To: Christoph Hellwig, Gao Xiang
  Cc: guoweichao, linux-erofs, zhangshiming, linux-kernel

Hi Christoph,

The reason we use dio is because we need to deploy the patch on some 
early kernel versions, and we don't pay much attention to the change of 
iomap. Anyway, I will study the problem mentioned by Gao Xiang and try 
to convert the current patch to iomap.

Thanks,

Jianan

> On Wed, Dec 23, 2020 at 03:39:01AM +0800, Gao Xiang wrote:
>> Hi Christoph,
>>
>> On Tue, Dec 22, 2020 at 02:22:34PM +0000, Christoph Hellwig wrote:
>>> Please do not add new callers of __blockdev_direct_IO and use the modern
>>> iomap variant instead.
>> We've talked about this topic before. The current status is that iomap
>> doesn't support tail-packing inline data yet (Chao once sent out a version),
>> and erofs only cares about read intrastructure for now (So we don't think
>> more about how to deal with tail-packing inline write path). Plus, the
>> original patch was once lack of inline data regression test from gfs2 folks.
> So resend Chaos prep patch as part of the series switching parts of
> erofs to iomap.  We need to move things off the old infrastructure instead
> of adding more users and everyone needs to help a little.

^ permalink raw reply	[flat|nested] 9+ messages in thread

* Re: [PATCH] erofs: support direct IO for uncompressed file
  2020-12-23  8:48       ` Huang Jianan
@ 2020-12-23  8:54         ` Christoph Hellwig
  2020-12-23  9:03           ` Gao Xiang
  0 siblings, 1 reply; 9+ messages in thread
From: Christoph Hellwig @ 2020-12-23  8:54 UTC (permalink / raw)
  To: Huang Jianan
  Cc: Christoph Hellwig, Gao Xiang, guoweichao, linux-erofs,
	zhangshiming, linux-kernel

On Wed, Dec 23, 2020 at 04:48:20PM +0800, Huang Jianan wrote:
> Hi Christoph,
> 
> The reason we use dio is because we need to deploy the patch on some early
> kernel versions, and we don't pay much attention to the change of iomap.

No, that is never an excuse for upstream development.

^ permalink raw reply	[flat|nested] 9+ messages in thread

* Re: [PATCH] erofs: support direct IO for uncompressed file
  2020-12-23  8:54         ` Christoph Hellwig
@ 2020-12-23  9:03           ` Gao Xiang
  0 siblings, 0 replies; 9+ messages in thread
From: Gao Xiang @ 2020-12-23  9:03 UTC (permalink / raw)
  To: Christoph Hellwig
  Cc: Huang Jianan, guoweichao, linux-erofs, zhangshiming, linux-kernel

On Wed, Dec 23, 2020 at 08:54:01AM +0000, Christoph Hellwig wrote:
> On Wed, Dec 23, 2020 at 04:48:20PM +0800, Huang Jianan wrote:
> > Hi Christoph,
> > 
> > The reason we use dio is because we need to deploy the patch on some early
> > kernel versions, and we don't pay much attention to the change of iomap.
> 
> No, that is never an excuse for upstream development.

Ok, personally I also agree this, let's go further in this way.

Thanks,
Gao Xiang

> 


^ permalink raw reply	[flat|nested] 9+ messages in thread

end of thread, other threads:[~2020-12-23  9:05 UTC | newest]

Thread overview: 9+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2020-12-14 14:04 [PATCH] erofs: support direct IO for uncompressed file Huang Jianan
2020-12-14 14:04 ` [PATCH] fsstress: support direct IO Huang Jianan
2020-12-22 13:21 ` [PATCH] erofs: support direct IO for uncompressed file Gao Xiang
2020-12-22 14:22 ` Christoph Hellwig
2020-12-22 19:39   ` Gao Xiang
2020-12-23  7:44     ` Christoph Hellwig
2020-12-23  8:48       ` Huang Jianan
2020-12-23  8:54         ` Christoph Hellwig
2020-12-23  9:03           ` Gao Xiang

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).