Linux-EROFS Archive on lore.kernel.org
 help / color / Atom feed
* [RFC] erofs-utils:code for detecting and tracking holes in uncompressed sparse files.
@ 2019-12-03 14:02 Pratik Shinde
  2019-12-04  2:27 ` Gao Xiang
  0 siblings, 1 reply; 5+ messages in thread
From: Pratik Shinde @ 2019-12-03 14:02 UTC (permalink / raw)
  To: linux-erofs, bluce.liguifu, miaoxie, fangwei1

NOTE: The patch is not fully complete yet, with this patch I just want to
present rough idea of what I am trying to achieve.

The patch does following :
1) Detect holes (of size EROFS_BLKSIZ) in uncompressed files.
2) Keep track of holes per file.

In-order to track holes, I used an array of size = (file_size / blocksize)
The array basically tracks number of holes before a particular logical file block.
e.g blks[i] = 10 meaning ith block has 10 holes before it.
If a particular block is a hole we set the index to '-1'.

how read logic will change:
1) currently we simply map read offset to a fs block.
2) with holes in place the calculation of block number would be:

   blkno = start_block + (offset >> block_size_shift) - (number of 
   							 holes before block in which offset falls)

3) If a read offset falls inside a hole (which can be found using above array). We
   fill the user buffer with '\0' on the fly.

through this,block no. lookup would still be performed in constant time.

The biggest problem with this approach is - we have to store the hole tracking
array for every file to the disk.Which doesn't seems to be practical.we can use a linkedlist,
but that will make size of inode variable.

Signed-off-by: Pratik Shinde <pratikshinde320@gmail.com>
---
 lib/inode.c | 67 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++-
 1 file changed, 66 insertions(+), 1 deletion(-)

diff --git a/lib/inode.c b/lib/inode.c
index 0e19b11..af31949 100644
--- a/lib/inode.c
+++ b/lib/inode.c
@@ -38,6 +38,61 @@ static unsigned char erofs_type_by_mode[S_IFMT >> S_SHIFT] = {
 
 struct list_head inode_hashtable[NR_INODE_HASHTABLE];
 
+
+#define IS_HOLE(start, end) (roundup(start, EROFS_BLKSIZ) == start &&	\
+		             roundup(end, EROFS_BLKSIZ) == end &&	\
+			    (end - start) % EROFS_BLKSIZ == 0)
+#define HOLE_BLK		-1
+unsigned int erofs_detect_holes(struct erofs_inode *inode, int *blks)
+{
+	int i, fd, st, en;
+	unsigned int nblocks;
+	erofs_off_t data, hole, len;
+
+	nblocks = inode->i_size / EROFS_BLKSIZ;
+	for (i = 0; i < nblocks; i++)
+		blks[i] = 0;
+	fd = open(inode->i_srcpath, O_RDONLY);
+	if (fd < 0) {
+		return -errno;
+	}
+	len = lseek(fd, 0, SEEK_END);
+	if (lseek(fd, 0, SEEK_SET) == -1)
+		return -errno;
+	data = 0;
+	while (data < len) {
+		hole = lseek(fd, data, SEEK_HOLE);
+		if (hole == len)
+			break;
+		data = lseek(fd, hole, SEEK_DATA);
+		if (data < 0 || hole > data) {
+			return -EINVAL;
+		}
+		if (IS_HOLE(hole, data)) {
+			st = hole >> S_SHIFT;
+			en = data >> S_SHIFT;
+			nblocks -= (en - st);
+			for (i = st; i < en; i++)
+				blks[i] = HOLE_BLK;
+		}
+	}
+	return nblocks;
+}
+
+int erofs_fill_holedata(int *blks, unsigned int nblocks) {
+	int i, nholes = 0;
+	for (i = 0; i < nblocks; i++) {
+		if (blks[i] == -1)
+			nholes++;
+		else {
+			blks[i] = nholes;
+			if (nholes >= (i + 1))
+				return -EINVAL;
+		}
+	}
+	return 0;
+}
+
 void erofs_inode_manager_init(void)
 {
 	unsigned int i;
@@ -305,6 +360,7 @@ static bool erofs_file_is_compressible(struct erofs_inode *inode)
 int erofs_write_file(struct erofs_inode *inode)
 {
 	unsigned int nblocks, i;
+	int *blks;
 	int ret, fd;
 
 	if (!inode->i_size) {
@@ -322,7 +378,13 @@ int erofs_write_file(struct erofs_inode *inode)
 	/* fallback to all data uncompressed */
 	inode->datalayout = EROFS_INODE_FLAT_INLINE;
 	nblocks = inode->i_size / EROFS_BLKSIZ;
-
+	blks = malloc(sizeof(int) * nblocks);
+	nblocks = erofs_detect_holes(inode, blks);
+	if (nblocks < 0)
+		return nblocks;
+	if ((ret = erofs_fill_holedata(blks, nblocks)) != 0) {
+		return ret;
+	}
 	ret = __allocate_inode_bh_data(inode, nblocks);
 	if (ret)
 		return ret;
@@ -332,6 +394,8 @@ int erofs_write_file(struct erofs_inode *inode)
 		return -errno;
 
 	for (i = 0; i < nblocks; ++i) {
+		if (blks[i] == HOLE_BLK)
+			continue;
 		char buf[EROFS_BLKSIZ];
 
 		ret = read(fd, buf, EROFS_BLKSIZ);
@@ -962,3 +1026,4 @@ struct erofs_inode *erofs_mkfs_build_tree_from_path(struct erofs_inode *parent,
 	return erofs_mkfs_build_tree(inode);
 }
 
+
-- 
2.9.3


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

* Re: [RFC] erofs-utils:code for detecting and tracking holes in uncompressed sparse files.
  2019-12-03 14:02 [RFC] erofs-utils:code for detecting and tracking holes in uncompressed sparse files Pratik Shinde
@ 2019-12-04  2:27 ` Gao Xiang
  2019-12-09  7:04   ` Pratik Shinde
  0 siblings, 1 reply; 5+ messages in thread
From: Gao Xiang @ 2019-12-04  2:27 UTC (permalink / raw)
  To: Pratik Shinde; +Cc: miaoxie, linux-erofs

Hi Pratik,

I'll give detailed words this weekend if you have more questions
since I'm busying in other stupid intra-company stuffs now...

On Tue, Dec 03, 2019 at 07:32:50PM +0530, Pratik Shinde wrote:
> NOTE: The patch is not fully complete yet, with this patch I just want to
> present rough idea of what I am trying to achieve.
> 
> The patch does following :
> 1) Detect holes (of size EROFS_BLKSIZ) in uncompressed files.
> 2) Keep track of holes per file.
> 
> In-order to track holes, I used an array of size = (file_size / blocksize)
> The array basically tracks number of holes before a particular logical file block.
> e.g blks[i] = 10 meaning ith block has 10 holes before it.
> If a particular block is a hole we set the index to '-1'.
> 
> how read logic will change:
> 1) currently we simply map read offset to a fs block.
> 2) with holes in place the calculation of block number would be:
> 
>    blkno = start_block + (offset >> block_size_shift) - (number of 
>    							 holes before block in which offset falls)
> 
> 3) If a read offset falls inside a hole (which can be found using above array). We
>    fill the user buffer with '\0' on the fly.
> 
> through this,block no. lookup would still be performed in constant time.
> 
> The biggest problem with this approach is - we have to store the hole tracking
> array for every file to the disk.Which doesn't seems to be practical.we can use a linkedlist,
> but that will make size of inode variable.

"variable-sized inode" isn't a problem here, which can be handled
similar to the case of "compress indexes".

Probably no need to write linked list to the disk but generate linked list
in memory when writing data on the fly, and then transfer to a variable-sized
extent array at the time of writing inode metadata (The order is firstly data
and then metadata in erofs-utils so it looks practical.)

Thanks,
Gao Xiang

> 
> Signed-off-by: Pratik Shinde <pratikshinde320@gmail.com>
> ---
>  lib/inode.c | 67 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++-
>  1 file changed, 66 insertions(+), 1 deletion(-)
> 
> diff --git a/lib/inode.c b/lib/inode.c
> index 0e19b11..af31949 100644
> --- a/lib/inode.c
> +++ b/lib/inode.c
> @@ -38,6 +38,61 @@ static unsigned char erofs_type_by_mode[S_IFMT >> S_SHIFT] = {
>  
>  struct list_head inode_hashtable[NR_INODE_HASHTABLE];
>  
> +
> +#define IS_HOLE(start, end) (roundup(start, EROFS_BLKSIZ) == start &&	\
> +		             roundup(end, EROFS_BLKSIZ) == end &&	\
> +			    (end - start) % EROFS_BLKSIZ == 0)
> +#define HOLE_BLK		-1
> +unsigned int erofs_detect_holes(struct erofs_inode *inode, int *blks)
> +{
> +	int i, fd, st, en;
> +	unsigned int nblocks;
> +	erofs_off_t data, hole, len;
> +
> +	nblocks = inode->i_size / EROFS_BLKSIZ;
> +	for (i = 0; i < nblocks; i++)
> +		blks[i] = 0;
> +	fd = open(inode->i_srcpath, O_RDONLY);
> +	if (fd < 0) {
> +		return -errno;
> +	}
> +	len = lseek(fd, 0, SEEK_END);
> +	if (lseek(fd, 0, SEEK_SET) == -1)
> +		return -errno;
> +	data = 0;
> +	while (data < len) {
> +		hole = lseek(fd, data, SEEK_HOLE);
> +		if (hole == len)
> +			break;
> +		data = lseek(fd, hole, SEEK_DATA);
> +		if (data < 0 || hole > data) {
> +			return -EINVAL;
> +		}
> +		if (IS_HOLE(hole, data)) {
> +			st = hole >> S_SHIFT;
> +			en = data >> S_SHIFT;
> +			nblocks -= (en - st);
> +			for (i = st; i < en; i++)
> +				blks[i] = HOLE_BLK;
> +		}
> +	}
> +	return nblocks;
> +}
> +
> +int erofs_fill_holedata(int *blks, unsigned int nblocks) {
> +	int i, nholes = 0;
> +	for (i = 0; i < nblocks; i++) {
> +		if (blks[i] == -1)
> +			nholes++;
> +		else {
> +			blks[i] = nholes;
> +			if (nholes >= (i + 1))
> +				return -EINVAL;
> +		}
> +	}
> +	return 0;
> +}
> +
>  void erofs_inode_manager_init(void)
>  {
>  	unsigned int i;
> @@ -305,6 +360,7 @@ static bool erofs_file_is_compressible(struct erofs_inode *inode)
>  int erofs_write_file(struct erofs_inode *inode)
>  {
>  	unsigned int nblocks, i;
> +	int *blks;
>  	int ret, fd;
>  
>  	if (!inode->i_size) {
> @@ -322,7 +378,13 @@ int erofs_write_file(struct erofs_inode *inode)
>  	/* fallback to all data uncompressed */
>  	inode->datalayout = EROFS_INODE_FLAT_INLINE;
>  	nblocks = inode->i_size / EROFS_BLKSIZ;
> -
> +	blks = malloc(sizeof(int) * nblocks);
> +	nblocks = erofs_detect_holes(inode, blks);
> +	if (nblocks < 0)
> +		return nblocks;
> +	if ((ret = erofs_fill_holedata(blks, nblocks)) != 0) {
> +		return ret;
> +	}
>  	ret = __allocate_inode_bh_data(inode, nblocks);
>  	if (ret)
>  		return ret;
> @@ -332,6 +394,8 @@ int erofs_write_file(struct erofs_inode *inode)
>  		return -errno;
>  
>  	for (i = 0; i < nblocks; ++i) {
> +		if (blks[i] == HOLE_BLK)
> +			continue;
>  		char buf[EROFS_BLKSIZ];
>  
>  		ret = read(fd, buf, EROFS_BLKSIZ);
> @@ -962,3 +1026,4 @@ struct erofs_inode *erofs_mkfs_build_tree_from_path(struct erofs_inode *parent,
>  	return erofs_mkfs_build_tree(inode);
>  }
>  
> +
> -- 
> 2.9.3
> 

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

* Re: [RFC] erofs-utils:code for detecting and tracking holes in uncompressed sparse files.
  2019-12-04  2:27 ` Gao Xiang
@ 2019-12-09  7:04   ` Pratik Shinde
  2019-12-09  7:18     ` Gao Xiang
  0 siblings, 1 reply; 5+ messages in thread
From: Pratik Shinde @ 2019-12-09  7:04 UTC (permalink / raw)
  To: Gao Xiang; +Cc: miaoxie, linux-erofs

[-- Attachment #1: Type: text/plain, Size: 6094 bytes --]

Hello Gao,

Did you get any chance to look at this in detail.

--Pratik.

On Wed, Dec 4, 2019, 7:52 AM Gao Xiang <gaoxiang25@huawei.com> wrote:

> Hi Pratik,
>
> I'll give detailed words this weekend if you have more questions
> since I'm busying in other stupid intra-company stuffs now...
>
> On Tue, Dec 03, 2019 at 07:32:50PM +0530, Pratik Shinde wrote:
> > NOTE: The patch is not fully complete yet, with this patch I just want to
> > present rough idea of what I am trying to achieve.
> >
> > The patch does following :
> > 1) Detect holes (of size EROFS_BLKSIZ) in uncompressed files.
> > 2) Keep track of holes per file.
> >
> > In-order to track holes, I used an array of size = (file_size /
> blocksize)
> > The array basically tracks number of holes before a particular logical
> file block.
> > e.g blks[i] = 10 meaning ith block has 10 holes before it.
> > If a particular block is a hole we set the index to '-1'.
> >
> > how read logic will change:
> > 1) currently we simply map read offset to a fs block.
> > 2) with holes in place the calculation of block number would be:
> >
> >    blkno = start_block + (offset >> block_size_shift) - (number of
> >                                                        holes before
> block in which offset falls)
> >
> > 3) If a read offset falls inside a hole (which can be found using above
> array). We
> >    fill the user buffer with '\0' on the fly.
> >
> > through this,block no. lookup would still be performed in constant time.
> >
> > The biggest problem with this approach is - we have to store the hole
> tracking
> > array for every file to the disk.Which doesn't seems to be practical.we
> can use a linkedlist,
> > but that will make size of inode variable.
>
> "variable-sized inode" isn't a problem here, which can be handled
> similar to the case of "compress indexes".
>
> Probably no need to write linked list to the disk but generate linked list
> in memory when writing data on the fly, and then transfer to a
> variable-sized
> extent array at the time of writing inode metadata (The order is firstly
> data
> and then metadata in erofs-utils so it looks practical.)
>
> Thanks,
> Gao Xiang
>
> >
> > Signed-off-by: Pratik Shinde <pratikshinde320@gmail.com>
> > ---
> >  lib/inode.c | 67
> ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++-
> >  1 file changed, 66 insertions(+), 1 deletion(-)
> >
> > diff --git a/lib/inode.c b/lib/inode.c
> > index 0e19b11..af31949 100644
> > --- a/lib/inode.c
> > +++ b/lib/inode.c
> > @@ -38,6 +38,61 @@ static unsigned char erofs_type_by_mode[S_IFMT >>
> S_SHIFT] = {
> >
> >  struct list_head inode_hashtable[NR_INODE_HASHTABLE];
> >
> > +
> > +#define IS_HOLE(start, end) (roundup(start, EROFS_BLKSIZ) == start &&
>       \
> > +                          roundup(end, EROFS_BLKSIZ) == end &&       \
> > +                         (end - start) % EROFS_BLKSIZ == 0)
> > +#define HOLE_BLK             -1
> > +unsigned int erofs_detect_holes(struct erofs_inode *inode, int *blks)
> > +{
> > +     int i, fd, st, en;
> > +     unsigned int nblocks;
> > +     erofs_off_t data, hole, len;
> > +
> > +     nblocks = inode->i_size / EROFS_BLKSIZ;
> > +     for (i = 0; i < nblocks; i++)
> > +             blks[i] = 0;
> > +     fd = open(inode->i_srcpath, O_RDONLY);
> > +     if (fd < 0) {
> > +             return -errno;
> > +     }
> > +     len = lseek(fd, 0, SEEK_END);
> > +     if (lseek(fd, 0, SEEK_SET) == -1)
> > +             return -errno;
> > +     data = 0;
> > +     while (data < len) {
> > +             hole = lseek(fd, data, SEEK_HOLE);
> > +             if (hole == len)
> > +                     break;
> > +             data = lseek(fd, hole, SEEK_DATA);
> > +             if (data < 0 || hole > data) {
> > +                     return -EINVAL;
> > +             }
> > +             if (IS_HOLE(hole, data)) {
> > +                     st = hole >> S_SHIFT;
> > +                     en = data >> S_SHIFT;
> > +                     nblocks -= (en - st);
> > +                     for (i = st; i < en; i++)
> > +                             blks[i] = HOLE_BLK;
> > +             }
> > +     }
> > +     return nblocks;
> > +}
> > +
> > +int erofs_fill_holedata(int *blks, unsigned int nblocks) {
> > +     int i, nholes = 0;
> > +     for (i = 0; i < nblocks; i++) {
> > +             if (blks[i] == -1)
> > +                     nholes++;
> > +             else {
> > +                     blks[i] = nholes;
> > +                     if (nholes >= (i + 1))
> > +                             return -EINVAL;
> > +             }
> > +     }
> > +     return 0;
> > +}
> > +
> >  void erofs_inode_manager_init(void)
> >  {
> >       unsigned int i;
> > @@ -305,6 +360,7 @@ static bool erofs_file_is_compressible(struct
> erofs_inode *inode)
> >  int erofs_write_file(struct erofs_inode *inode)
> >  {
> >       unsigned int nblocks, i;
> > +     int *blks;
> >       int ret, fd;
> >
> >       if (!inode->i_size) {
> > @@ -322,7 +378,13 @@ int erofs_write_file(struct erofs_inode *inode)
> >       /* fallback to all data uncompressed */
> >       inode->datalayout = EROFS_INODE_FLAT_INLINE;
> >       nblocks = inode->i_size / EROFS_BLKSIZ;
> > -
> > +     blks = malloc(sizeof(int) * nblocks);
> > +     nblocks = erofs_detect_holes(inode, blks);
> > +     if (nblocks < 0)
> > +             return nblocks;
> > +     if ((ret = erofs_fill_holedata(blks, nblocks)) != 0) {
> > +             return ret;
> > +     }
> >       ret = __allocate_inode_bh_data(inode, nblocks);
> >       if (ret)
> >               return ret;
> > @@ -332,6 +394,8 @@ int erofs_write_file(struct erofs_inode *inode)
> >               return -errno;
> >
> >       for (i = 0; i < nblocks; ++i) {
> > +             if (blks[i] == HOLE_BLK)
> > +                     continue;
> >               char buf[EROFS_BLKSIZ];
> >
> >               ret = read(fd, buf, EROFS_BLKSIZ);
> > @@ -962,3 +1026,4 @@ struct erofs_inode
> *erofs_mkfs_build_tree_from_path(struct erofs_inode *parent,
> >       return erofs_mkfs_build_tree(inode);
> >  }
> >
> > +
> > --
> > 2.9.3
> >
>

[-- Attachment #2: Type: text/html, Size: 8050 bytes --]

<div dir="auto">Hello Gao,<div dir="auto"><br></div><div dir="auto">Did you get any chance to look at this in detail.</div><div dir="auto"><br></div><div dir="auto">--Pratik.</div></div><br><div class="gmail_quote"><div dir="ltr" class="gmail_attr">On Wed, Dec 4, 2019, 7:52 AM Gao Xiang &lt;<a href="mailto:gaoxiang25@huawei.com">gaoxiang25@huawei.com</a>&gt; wrote:<br></div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">Hi Pratik,<br>
<br>
I&#39;ll give detailed words this weekend if you have more questions<br>
since I&#39;m busying in other stupid intra-company stuffs now...<br>
<br>
On Tue, Dec 03, 2019 at 07:32:50PM +0530, Pratik Shinde wrote:<br>
&gt; NOTE: The patch is not fully complete yet, with this patch I just want to<br>
&gt; present rough idea of what I am trying to achieve.<br>
&gt; <br>
&gt; The patch does following :<br>
&gt; 1) Detect holes (of size EROFS_BLKSIZ) in uncompressed files.<br>
&gt; 2) Keep track of holes per file.<br>
&gt; <br>
&gt; In-order to track holes, I used an array of size = (file_size / blocksize)<br>
&gt; The array basically tracks number of holes before a particular logical file block.<br>
&gt; e.g blks[i] = 10 meaning ith block has 10 holes before it.<br>
&gt; If a particular block is a hole we set the index to &#39;-1&#39;.<br>
&gt; <br>
&gt; how read logic will change:<br>
&gt; 1) currently we simply map read offset to a fs block.<br>
&gt; 2) with holes in place the calculation of block number would be:<br>
&gt; <br>
&gt;    blkno = start_block + (offset &gt;&gt; block_size_shift) - (number of <br>
&gt;                                                        holes before block in which offset falls)<br>
&gt; <br>
&gt; 3) If a read offset falls inside a hole (which can be found using above array). We<br>
&gt;    fill the user buffer with &#39;\0&#39; on the fly.<br>
&gt; <br>
&gt; through this,block no. lookup would still be performed in constant time.<br>
&gt; <br>
&gt; The biggest problem with this approach is - we have to store the hole tracking<br>
&gt; array for every file to the disk.Which doesn&#39;t seems to be practical.we can use a linkedlist,<br>
&gt; but that will make size of inode variable.<br>
<br>
&quot;variable-sized inode&quot; isn&#39;t a problem here, which can be handled<br>
similar to the case of &quot;compress indexes&quot;.<br>
<br>
Probably no need to write linked list to the disk but generate linked list<br>
in memory when writing data on the fly, and then transfer to a variable-sized<br>
extent array at the time of writing inode metadata (The order is firstly data<br>
and then metadata in erofs-utils so it looks practical.)<br>
<br>
Thanks,<br>
Gao Xiang<br>
<br>
&gt; <br>
&gt; Signed-off-by: Pratik Shinde &lt;<a href="mailto:pratikshinde320@gmail.com" target="_blank" rel="noreferrer">pratikshinde320@gmail.com</a>&gt;<br>
&gt; ---<br>
&gt;  lib/inode.c | 67 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++-<br>
&gt;  1 file changed, 66 insertions(+), 1 deletion(-)<br>
&gt; <br>
&gt; diff --git a/lib/inode.c b/lib/inode.c<br>
&gt; index 0e19b11..af31949 100644<br>
&gt; --- a/lib/inode.c<br>
&gt; +++ b/lib/inode.c<br>
&gt; @@ -38,6 +38,61 @@ static unsigned char erofs_type_by_mode[S_IFMT &gt;&gt; S_SHIFT] = {<br>
&gt;  <br>
&gt;  struct list_head inode_hashtable[NR_INODE_HASHTABLE];<br>
&gt;  <br>
&gt; +<br>
&gt; +#define IS_HOLE(start, end) (roundup(start, EROFS_BLKSIZ) == start &amp;&amp;        \<br>
&gt; +                          roundup(end, EROFS_BLKSIZ) == end &amp;&amp;       \<br>
&gt; +                         (end - start) % EROFS_BLKSIZ == 0)<br>
&gt; +#define HOLE_BLK             -1<br>
&gt; +unsigned int erofs_detect_holes(struct erofs_inode *inode, int *blks)<br>
&gt; +{<br>
&gt; +     int i, fd, st, en;<br>
&gt; +     unsigned int nblocks;<br>
&gt; +     erofs_off_t data, hole, len;<br>
&gt; +<br>
&gt; +     nblocks = inode-&gt;i_size / EROFS_BLKSIZ;<br>
&gt; +     for (i = 0; i &lt; nblocks; i++)<br>
&gt; +             blks[i] = 0;<br>
&gt; +     fd = open(inode-&gt;i_srcpath, O_RDONLY);<br>
&gt; +     if (fd &lt; 0) {<br>
&gt; +             return -errno;<br>
&gt; +     }<br>
&gt; +     len = lseek(fd, 0, SEEK_END);<br>
&gt; +     if (lseek(fd, 0, SEEK_SET) == -1)<br>
&gt; +             return -errno;<br>
&gt; +     data = 0;<br>
&gt; +     while (data &lt; len) {<br>
&gt; +             hole = lseek(fd, data, SEEK_HOLE);<br>
&gt; +             if (hole == len)<br>
&gt; +                     break;<br>
&gt; +             data = lseek(fd, hole, SEEK_DATA);<br>
&gt; +             if (data &lt; 0 || hole &gt; data) {<br>
&gt; +                     return -EINVAL;<br>
&gt; +             }<br>
&gt; +             if (IS_HOLE(hole, data)) {<br>
&gt; +                     st = hole &gt;&gt; S_SHIFT;<br>
&gt; +                     en = data &gt;&gt; S_SHIFT;<br>
&gt; +                     nblocks -= (en - st);<br>
&gt; +                     for (i = st; i &lt; en; i++)<br>
&gt; +                             blks[i] = HOLE_BLK;<br>
&gt; +             }<br>
&gt; +     }<br>
&gt; +     return nblocks;<br>
&gt; +}<br>
&gt; +<br>
&gt; +int erofs_fill_holedata(int *blks, unsigned int nblocks) {<br>
&gt; +     int i, nholes = 0;<br>
&gt; +     for (i = 0; i &lt; nblocks; i++) {<br>
&gt; +             if (blks[i] == -1)<br>
&gt; +                     nholes++;<br>
&gt; +             else {<br>
&gt; +                     blks[i] = nholes;<br>
&gt; +                     if (nholes &gt;= (i + 1))<br>
&gt; +                             return -EINVAL;<br>
&gt; +             }<br>
&gt; +     }<br>
&gt; +     return 0;<br>
&gt; +}<br>
&gt; +<br>
&gt;  void erofs_inode_manager_init(void)<br>
&gt;  {<br>
&gt;       unsigned int i;<br>
&gt; @@ -305,6 +360,7 @@ static bool erofs_file_is_compressible(struct erofs_inode *inode)<br>
&gt;  int erofs_write_file(struct erofs_inode *inode)<br>
&gt;  {<br>
&gt;       unsigned int nblocks, i;<br>
&gt; +     int *blks;<br>
&gt;       int ret, fd;<br>
&gt;  <br>
&gt;       if (!inode-&gt;i_size) {<br>
&gt; @@ -322,7 +378,13 @@ int erofs_write_file(struct erofs_inode *inode)<br>
&gt;       /* fallback to all data uncompressed */<br>
&gt;       inode-&gt;datalayout = EROFS_INODE_FLAT_INLINE;<br>
&gt;       nblocks = inode-&gt;i_size / EROFS_BLKSIZ;<br>
&gt; -<br>
&gt; +     blks = malloc(sizeof(int) * nblocks);<br>
&gt; +     nblocks = erofs_detect_holes(inode, blks);<br>
&gt; +     if (nblocks &lt; 0)<br>
&gt; +             return nblocks;<br>
&gt; +     if ((ret = erofs_fill_holedata(blks, nblocks)) != 0) {<br>
&gt; +             return ret;<br>
&gt; +     }<br>
&gt;       ret = __allocate_inode_bh_data(inode, nblocks);<br>
&gt;       if (ret)<br>
&gt;               return ret;<br>
&gt; @@ -332,6 +394,8 @@ int erofs_write_file(struct erofs_inode *inode)<br>
&gt;               return -errno;<br>
&gt;  <br>
&gt;       for (i = 0; i &lt; nblocks; ++i) {<br>
&gt; +             if (blks[i] == HOLE_BLK)<br>
&gt; +                     continue;<br>
&gt;               char buf[EROFS_BLKSIZ];<br>
&gt;  <br>
&gt;               ret = read(fd, buf, EROFS_BLKSIZ);<br>
&gt; @@ -962,3 +1026,4 @@ struct erofs_inode *erofs_mkfs_build_tree_from_path(struct erofs_inode *parent,<br>
&gt;       return erofs_mkfs_build_tree(inode);<br>
&gt;  }<br>
&gt;  <br>
&gt; +<br>
&gt; -- <br>
&gt; 2.9.3<br>
&gt; <br>
</blockquote></div>

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

* Re: [RFC] erofs-utils:code for detecting and tracking holes in uncompressed sparse files.
  2019-12-09  7:04   ` Pratik Shinde
@ 2019-12-09  7:18     ` Gao Xiang
  2019-12-11  5:49       ` Gao Xiang
  0 siblings, 1 reply; 5+ messages in thread
From: Gao Xiang @ 2019-12-09  7:18 UTC (permalink / raw)
  To: Pratik Shinde; +Cc: miaoxie, linux-erofs

Hi Pratik,

On Mon, Dec 09, 2019 at 12:34:50PM +0530, Pratik Shinde wrote:
> Hello Gao,
> 
> Did you get any chance to look at this in detail.

I looked into your implementation weeks before.

As my reply in the previous email, did you see it and could you check it out?

Kernel emails are not top-posting so you could scroll down to the end.

> > "variable-sized inode" isn't a problem here, which can be handled
> > similar to the case of "compress indexes".
> >
> > Probably no need to write linked list to the disk but generate linked list
> > in memory when writing data on the fly, and then transfer to a
> > variable-sized
> > extent array at the time of writing inode metadata (The order is firstly
> > data
> > and then metadata in erofs-utils so it looks practical.)

Thanks,
Gao Xiang

> 
> --Pratik.
> 
> On Wed, Dec 4, 2019, 7:52 AM Gao Xiang <gaoxiang25@huawei.com> wrote:
> 
> > Hi Pratik,
> >
> > I'll give detailed words this weekend if you have more questions
> > since I'm busying in other stupid intra-company stuffs now...
> >
> > On Tue, Dec 03, 2019 at 07:32:50PM +0530, Pratik Shinde wrote:
> > > NOTE: The patch is not fully complete yet, with this patch I just want to
> > > present rough idea of what I am trying to achieve.
> > >
> > > The patch does following :
> > > 1) Detect holes (of size EROFS_BLKSIZ) in uncompressed files.
> > > 2) Keep track of holes per file.
> > >
> > > In-order to track holes, I used an array of size = (file_size /
> > blocksize)
> > > The array basically tracks number of holes before a particular logical
> > file block.
> > > e.g blks[i] = 10 meaning ith block has 10 holes before it.
> > > If a particular block is a hole we set the index to '-1'.
> > >
> > > how read logic will change:
> > > 1) currently we simply map read offset to a fs block.
> > > 2) with holes in place the calculation of block number would be:
> > >
> > >    blkno = start_block + (offset >> block_size_shift) - (number of
> > >                                                        holes before
> > block in which offset falls)
> > >
> > > 3) If a read offset falls inside a hole (which can be found using above
> > array). We
> > >    fill the user buffer with '\0' on the fly.
> > >
> > > through this,block no. lookup would still be performed in constant time.
> > >
> > > The biggest problem with this approach is - we have to store the hole
> > tracking
> > > array for every file to the disk.Which doesn't seems to be practical.we
> > can use a linkedlist,
> > > but that will make size of inode variable.
> >
> > "variable-sized inode" isn't a problem here, which can be handled
> > similar to the case of "compress indexes".
> >
> > Probably no need to write linked list to the disk but generate linked list
> > in memory when writing data on the fly, and then transfer to a
> > variable-sized
> > extent array at the time of writing inode metadata (The order is firstly
> > data
> > and then metadata in erofs-utils so it looks practical.)
> >
> > Thanks,
> > Gao Xiang
> >
> > >
> > > Signed-off-by: Pratik Shinde <pratikshinde320@gmail.com>
> > > ---
> > >  lib/inode.c | 67
> > ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++-
> > >  1 file changed, 66 insertions(+), 1 deletion(-)
> > >
> > > diff --git a/lib/inode.c b/lib/inode.c
> > > index 0e19b11..af31949 100644
> > > --- a/lib/inode.c
> > > +++ b/lib/inode.c
> > > @@ -38,6 +38,61 @@ static unsigned char erofs_type_by_mode[S_IFMT >>
> > S_SHIFT] = {
> > >
> > >  struct list_head inode_hashtable[NR_INODE_HASHTABLE];
> > >
> > > +
> > > +#define IS_HOLE(start, end) (roundup(start, EROFS_BLKSIZ) == start &&
> >       \
> > > +                          roundup(end, EROFS_BLKSIZ) == end &&       \
> > > +                         (end - start) % EROFS_BLKSIZ == 0)
> > > +#define HOLE_BLK             -1
> > > +unsigned int erofs_detect_holes(struct erofs_inode *inode, int *blks)
> > > +{
> > > +     int i, fd, st, en;
> > > +     unsigned int nblocks;
> > > +     erofs_off_t data, hole, len;
> > > +
> > > +     nblocks = inode->i_size / EROFS_BLKSIZ;
> > > +     for (i = 0; i < nblocks; i++)
> > > +             blks[i] = 0;
> > > +     fd = open(inode->i_srcpath, O_RDONLY);
> > > +     if (fd < 0) {
> > > +             return -errno;
> > > +     }
> > > +     len = lseek(fd, 0, SEEK_END);
> > > +     if (lseek(fd, 0, SEEK_SET) == -1)
> > > +             return -errno;
> > > +     data = 0;
> > > +     while (data < len) {
> > > +             hole = lseek(fd, data, SEEK_HOLE);
> > > +             if (hole == len)
> > > +                     break;
> > > +             data = lseek(fd, hole, SEEK_DATA);
> > > +             if (data < 0 || hole > data) {
> > > +                     return -EINVAL;
> > > +             }
> > > +             if (IS_HOLE(hole, data)) {
> > > +                     st = hole >> S_SHIFT;
> > > +                     en = data >> S_SHIFT;
> > > +                     nblocks -= (en - st);
> > > +                     for (i = st; i < en; i++)
> > > +                             blks[i] = HOLE_BLK;
> > > +             }
> > > +     }
> > > +     return nblocks;
> > > +}
> > > +
> > > +int erofs_fill_holedata(int *blks, unsigned int nblocks) {
> > > +     int i, nholes = 0;
> > > +     for (i = 0; i < nblocks; i++) {
> > > +             if (blks[i] == -1)
> > > +                     nholes++;
> > > +             else {
> > > +                     blks[i] = nholes;
> > > +                     if (nholes >= (i + 1))
> > > +                             return -EINVAL;
> > > +             }
> > > +     }
> > > +     return 0;
> > > +}
> > > +
> > >  void erofs_inode_manager_init(void)
> > >  {
> > >       unsigned int i;
> > > @@ -305,6 +360,7 @@ static bool erofs_file_is_compressible(struct
> > erofs_inode *inode)
> > >  int erofs_write_file(struct erofs_inode *inode)
> > >  {
> > >       unsigned int nblocks, i;
> > > +     int *blks;
> > >       int ret, fd;
> > >
> > >       if (!inode->i_size) {
> > > @@ -322,7 +378,13 @@ int erofs_write_file(struct erofs_inode *inode)
> > >       /* fallback to all data uncompressed */
> > >       inode->datalayout = EROFS_INODE_FLAT_INLINE;
> > >       nblocks = inode->i_size / EROFS_BLKSIZ;
> > > -
> > > +     blks = malloc(sizeof(int) * nblocks);
> > > +     nblocks = erofs_detect_holes(inode, blks);
> > > +     if (nblocks < 0)
> > > +             return nblocks;
> > > +     if ((ret = erofs_fill_holedata(blks, nblocks)) != 0) {
> > > +             return ret;
> > > +     }
> > >       ret = __allocate_inode_bh_data(inode, nblocks);
> > >       if (ret)
> > >               return ret;
> > > @@ -332,6 +394,8 @@ int erofs_write_file(struct erofs_inode *inode)
> > >               return -errno;
> > >
> > >       for (i = 0; i < nblocks; ++i) {
> > > +             if (blks[i] == HOLE_BLK)
> > > +                     continue;
> > >               char buf[EROFS_BLKSIZ];
> > >
> > >               ret = read(fd, buf, EROFS_BLKSIZ);
> > > @@ -962,3 +1026,4 @@ struct erofs_inode
> > *erofs_mkfs_build_tree_from_path(struct erofs_inode *parent,
> > >       return erofs_mkfs_build_tree(inode);
> > >  }
> > >
> > > +
> > > --
> > > 2.9.3
> > >
> >

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

* Re: [RFC] erofs-utils:code for detecting and tracking holes in uncompressed sparse files.
  2019-12-09  7:18     ` Gao Xiang
@ 2019-12-11  5:49       ` Gao Xiang
  0 siblings, 0 replies; 5+ messages in thread
From: Gao Xiang @ 2019-12-11  5:49 UTC (permalink / raw)
  To: Pratik Shinde; +Cc: linux-erofs, miaoxie

On Mon, Dec 09, 2019 at 03:18:17PM +0800, Gao Xiang wrote:
> Hi Pratik,
> 
> On Mon, Dec 09, 2019 at 12:34:50PM +0530, Pratik Shinde wrote:
> > Hello Gao,
> > 
> > Did you get any chance to look at this in detail.
> 
> I looked into your implementation weeks before.
> 
> As my reply in the previous email, did you see it and could you check it out?
> 
> Kernel emails are not top-posting so you could scroll down to the end.
> 
> > > "variable-sized inode" isn't a problem here, which can be handled
> > > similar to the case of "compress indexes".
> > >
> > > Probably no need to write linked list to the disk but generate linked list
> > > in memory when writing data on the fly, and then transfer to a
> > > variable-sized
> > > extent array at the time of writing inode metadata (The order is firstly
> > > data
> > > and then metadata in erofs-utils so it looks practical.)


Some feedback words here? Do you think it is unnecessary to use such
array for limited fragmented holes (either in-memory or ondisk) as well?

Thanks,
Gao Xiang


> 
> Thanks,
> Gao Xiang
> 
> > 
> > --Pratik.
> > 
> > On Wed, Dec 4, 2019, 7:52 AM Gao Xiang <gaoxiang25@huawei.com> wrote:
> > 
> > > Hi Pratik,
> > >
> > > I'll give detailed words this weekend if you have more questions
> > > since I'm busying in other stupid intra-company stuffs now...
> > >
> > > On Tue, Dec 03, 2019 at 07:32:50PM +0530, Pratik Shinde wrote:
> > > > NOTE: The patch is not fully complete yet, with this patch I just want to
> > > > present rough idea of what I am trying to achieve.
> > > >
> > > > The patch does following :
> > > > 1) Detect holes (of size EROFS_BLKSIZ) in uncompressed files.
> > > > 2) Keep track of holes per file.
> > > >
> > > > In-order to track holes, I used an array of size = (file_size /
> > > blocksize)
> > > > The array basically tracks number of holes before a particular logical
> > > file block.
> > > > e.g blks[i] = 10 meaning ith block has 10 holes before it.
> > > > If a particular block is a hole we set the index to '-1'.
> > > >
> > > > how read logic will change:
> > > > 1) currently we simply map read offset to a fs block.
> > > > 2) with holes in place the calculation of block number would be:
> > > >
> > > >    blkno = start_block + (offset >> block_size_shift) - (number of
> > > >                                                        holes before
> > > block in which offset falls)
> > > >
> > > > 3) If a read offset falls inside a hole (which can be found using above
> > > array). We
> > > >    fill the user buffer with '\0' on the fly.
> > > >
> > > > through this,block no. lookup would still be performed in constant time.
> > > >
> > > > The biggest problem with this approach is - we have to store the hole
> > > tracking
> > > > array for every file to the disk.Which doesn't seems to be practical.we
> > > can use a linkedlist,
> > > > but that will make size of inode variable.
> > >
> > > "variable-sized inode" isn't a problem here, which can be handled
> > > similar to the case of "compress indexes".
> > >
> > > Probably no need to write linked list to the disk but generate linked list
> > > in memory when writing data on the fly, and then transfer to a
> > > variable-sized
> > > extent array at the time of writing inode metadata (The order is firstly
> > > data
> > > and then metadata in erofs-utils so it looks practical.)
> > >
> > > Thanks,
> > > Gao Xiang
> > >
> > > >
> > > > Signed-off-by: Pratik Shinde <pratikshinde320@gmail.com>
> > > > ---
> > > >  lib/inode.c | 67
> > > ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++-
> > > >  1 file changed, 66 insertions(+), 1 deletion(-)
> > > >
> > > > diff --git a/lib/inode.c b/lib/inode.c
> > > > index 0e19b11..af31949 100644
> > > > --- a/lib/inode.c
> > > > +++ b/lib/inode.c
> > > > @@ -38,6 +38,61 @@ static unsigned char erofs_type_by_mode[S_IFMT >>
> > > S_SHIFT] = {
> > > >
> > > >  struct list_head inode_hashtable[NR_INODE_HASHTABLE];
> > > >
> > > > +
> > > > +#define IS_HOLE(start, end) (roundup(start, EROFS_BLKSIZ) == start &&
> > >       \
> > > > +                          roundup(end, EROFS_BLKSIZ) == end &&       \
> > > > +                         (end - start) % EROFS_BLKSIZ == 0)
> > > > +#define HOLE_BLK             -1
> > > > +unsigned int erofs_detect_holes(struct erofs_inode *inode, int *blks)
> > > > +{
> > > > +     int i, fd, st, en;
> > > > +     unsigned int nblocks;
> > > > +     erofs_off_t data, hole, len;
> > > > +
> > > > +     nblocks = inode->i_size / EROFS_BLKSIZ;
> > > > +     for (i = 0; i < nblocks; i++)
> > > > +             blks[i] = 0;
> > > > +     fd = open(inode->i_srcpath, O_RDONLY);
> > > > +     if (fd < 0) {
> > > > +             return -errno;
> > > > +     }
> > > > +     len = lseek(fd, 0, SEEK_END);
> > > > +     if (lseek(fd, 0, SEEK_SET) == -1)
> > > > +             return -errno;
> > > > +     data = 0;
> > > > +     while (data < len) {
> > > > +             hole = lseek(fd, data, SEEK_HOLE);
> > > > +             if (hole == len)
> > > > +                     break;
> > > > +             data = lseek(fd, hole, SEEK_DATA);
> > > > +             if (data < 0 || hole > data) {
> > > > +                     return -EINVAL;
> > > > +             }
> > > > +             if (IS_HOLE(hole, data)) {
> > > > +                     st = hole >> S_SHIFT;
> > > > +                     en = data >> S_SHIFT;
> > > > +                     nblocks -= (en - st);
> > > > +                     for (i = st; i < en; i++)
> > > > +                             blks[i] = HOLE_BLK;
> > > > +             }
> > > > +     }
> > > > +     return nblocks;
> > > > +}
> > > > +
> > > > +int erofs_fill_holedata(int *blks, unsigned int nblocks) {
> > > > +     int i, nholes = 0;
> > > > +     for (i = 0; i < nblocks; i++) {
> > > > +             if (blks[i] == -1)
> > > > +                     nholes++;
> > > > +             else {
> > > > +                     blks[i] = nholes;
> > > > +                     if (nholes >= (i + 1))
> > > > +                             return -EINVAL;
> > > > +             }
> > > > +     }
> > > > +     return 0;
> > > > +}
> > > > +
> > > >  void erofs_inode_manager_init(void)
> > > >  {
> > > >       unsigned int i;
> > > > @@ -305,6 +360,7 @@ static bool erofs_file_is_compressible(struct
> > > erofs_inode *inode)
> > > >  int erofs_write_file(struct erofs_inode *inode)
> > > >  {
> > > >       unsigned int nblocks, i;
> > > > +     int *blks;
> > > >       int ret, fd;
> > > >
> > > >       if (!inode->i_size) {
> > > > @@ -322,7 +378,13 @@ int erofs_write_file(struct erofs_inode *inode)
> > > >       /* fallback to all data uncompressed */
> > > >       inode->datalayout = EROFS_INODE_FLAT_INLINE;
> > > >       nblocks = inode->i_size / EROFS_BLKSIZ;
> > > > -
> > > > +     blks = malloc(sizeof(int) * nblocks);
> > > > +     nblocks = erofs_detect_holes(inode, blks);
> > > > +     if (nblocks < 0)
> > > > +             return nblocks;
> > > > +     if ((ret = erofs_fill_holedata(blks, nblocks)) != 0) {
> > > > +             return ret;
> > > > +     }
> > > >       ret = __allocate_inode_bh_data(inode, nblocks);
> > > >       if (ret)
> > > >               return ret;
> > > > @@ -332,6 +394,8 @@ int erofs_write_file(struct erofs_inode *inode)
> > > >               return -errno;
> > > >
> > > >       for (i = 0; i < nblocks; ++i) {
> > > > +             if (blks[i] == HOLE_BLK)
> > > > +                     continue;
> > > >               char buf[EROFS_BLKSIZ];
> > > >
> > > >               ret = read(fd, buf, EROFS_BLKSIZ);
> > > > @@ -962,3 +1026,4 @@ struct erofs_inode
> > > *erofs_mkfs_build_tree_from_path(struct erofs_inode *parent,
> > > >       return erofs_mkfs_build_tree(inode);
> > > >  }
> > > >
> > > > +
> > > > --
> > > > 2.9.3
> > > >
> > >

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

end of thread, back to index

Thread overview: 5+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2019-12-03 14:02 [RFC] erofs-utils:code for detecting and tracking holes in uncompressed sparse files Pratik Shinde
2019-12-04  2:27 ` Gao Xiang
2019-12-09  7:04   ` Pratik Shinde
2019-12-09  7:18     ` Gao Xiang
2019-12-11  5:49       ` Gao Xiang

Linux-EROFS Archive on lore.kernel.org

Archives are clonable:
	git clone --mirror https://lore.kernel.org/linux-erofs/0 linux-erofs/git/0.git

	# If you have public-inbox 1.1+ installed, you may
	# initialize and index your mirror using the following commands:
	public-inbox-init -V2 linux-erofs linux-erofs/ https://lore.kernel.org/linux-erofs \
		linux-erofs@lists.ozlabs.org linux-erofs@ozlabs.org
	public-inbox-index linux-erofs

Example config snippet for mirrors

Newsgroup available over NNTP:
	nntp://nntp.lore.kernel.org/org.ozlabs.lists.linux-erofs


AGPL code for this site: git clone https://public-inbox.org/public-inbox.git