<div dir="auto">Hi Gao,<div dir="auto"><br></div><div dir="auto">You are correct. The macro logic can be simplified. I will do that.</div><div dir="auto">I will work on the kernel part of this change & do some testing on it.</div><div dir="auto">I will keep you posted about the change and relevant tests I am running on it.</div><div dir="auto"><br></div><div dir="auto">--Pratik.</div><div dir="auto"><br></div><div dir="auto"><br></div><div dir="auto"><br></div><div dir="auto"><br></div><div dir="auto"><br></div><div dir="auto"><br></div><div dir="auto"><br></div><div dir="auto"><br></div><div dir="auto"><br></div><div dir="auto"><br></div></div><br><div class="gmail_quote"><div dir="ltr" class="gmail_attr">On Thu, Jan 2, 2020, 4:17 PM Gao Xiang <<a href="mailto:gaoxiang25@huawei.com">gaoxiang25@huawei.com</a>> 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>
On Thu, Jan 02, 2020 at 03:17:32PM +0530, Pratik Shinde wrote:<br>
> 1)Moved on-disk structures to erofs_fs.h<br>
> 2)Some naming changes.<br>
> <br>
> I think we can keep 'IS_HOLE()' macro, otherwise the code<br>
> does not look clean(if used directly/without macro).Its getting<br>
> used only in inode.c so it can be kept there.<br>
> what do you think ?<br>
<br>
What I'm little concerned is the relationship between<br>
the name of IS_HOLE and its implementation...<br>
<br>
In other words, why<br>
 roundup(start, EROFS_BLKSIZ) == start &&<br>
 roundup(end, EROFS_BLKSIZ) == end &&<br>
 (end - start) % EROFS_BLKSIZ == 0<br>
should be an erofs hole...<br>
<br>
But that is minor, I reserve my opinion on this for now...<br>
<br>
This patch generally looks good to me, yet I haven't<br>
played with it till now.<br>
<br>
Could you implement a workable RFC patch for kernel side<br>
as well? Since I'm still busying in XZ library and other<br>
convert patches for 5.6...<br>
<br>
I'd like to hear if some other opinions from Chao and Guifu.<br>
Since it enhances the on-disk format, we need to think it<br>
over (especially its future expandability).<br>
<br>
<br>
To Chao and Guifu,<br>
Could you have some extra time looking at this stuff as well?<br>
<br>
<br>
Thanks,<br>
Gao Xiang<br>
<br>
> <br>
> Signed-off-by: Pratik Shinde <<a href="mailto:pratikshinde320@gmail.com" target="_blank" rel="noreferrer">pratikshinde320@gmail.com</a>><br>
> ---<br>
>  include/erofs/internal.h |   9 ++-<br>
>  include/erofs_fs.h       |  11 ++++<br>
>  lib/inode.c              | 153 ++++++++++++++++++++++++++++++++++++++++-------<br>
>  3 files changed, 150 insertions(+), 23 deletions(-)<br>
> <br>
> diff --git a/include/erofs/internal.h b/include/erofs/internal.h<br>
> index e13adda..2d7466b 100644<br>
> --- a/include/erofs/internal.h<br>
> +++ b/include/erofs/internal.h<br>
> @@ -63,7 +63,7 @@ struct erofs_sb_info {<br>
>  extern struct erofs_sb_info sbi;<br>
>  <br>
>  struct erofs_inode {<br>
> -     struct list_head i_hash, i_subdirs, i_xattrs;<br>
> +     struct list_head i_hash, i_subdirs, i_xattrs, i_sparse_extents;<br>
>  <br>
>       unsigned int i_count;<br>
>       struct erofs_inode *i_parent;<br>
> @@ -93,6 +93,7 @@ struct erofs_inode {<br>
>  <br>
>       unsigned int xattr_isize;<br>
>       unsigned int extent_isize;<br>
> +     unsigned int sparse_extent_isize;<br>
>  <br>
>       erofs_nid_t nid;<br>
>       struct erofs_buffer_head *bh;<br>
> @@ -139,5 +140,11 @@ static inline const char *erofs_strerror(int err)<br>
>       return msg;<br>
>  }<br>
>  <br>
> +struct erofs_sparse_extent_node {<br>
> +     struct list_head next;<br>
> +     erofs_blk_t lblk;<br>
> +     erofs_blk_t pblk;<br>
> +     u32 len;<br>
> +};<br>
>  #endif<br>
>  <br>
> diff --git a/include/erofs_fs.h b/include/erofs_fs.h<br>
> index bcc4f0c..a63e1c6 100644<br>
> --- a/include/erofs_fs.h<br>
> +++ b/include/erofs_fs.h<br>
> @@ -321,5 +321,16 @@ static inline void erofs_check_ondisk_layout_definitions(void)<br>
>                    Z_EROFS_VLE_CLUSTER_TYPE_MAX - 1);<br>
>  }<br>
>  <br>
> +/* on-disk sparse extent format */<br>
> +struct erofs_sparse_extent {<br>
> +     __le32 ee_lblk;<br>
> +     __le32 ee_pblk;<br>
> +     __le32 ee_len;<br>
> +};<br>
> +<br>
> +struct erofs_sparse_extent_iheader {<br>
> +     u32 count;<br>
> +};<br>
> +<br>
>  #endif<br>
>  <br>
> diff --git a/lib/inode.c b/lib/inode.c<br>
> index 0e19b11..da20599 100644<br>
> --- a/lib/inode.c<br>
> +++ b/lib/inode.c<br>
> @@ -38,6 +38,97 @@ static unsigned char erofs_type_by_mode[S_IFMT >> S_SHIFT] = {<br>
>  <br>
>  struct list_head inode_hashtable[NR_INODE_HASHTABLE];<br>
>  <br>
> +<br>
> +#define IS_HOLE(start, end) (roundup(start, EROFS_BLKSIZ) == start &&        \<br>
> +                          roundup(end, EROFS_BLKSIZ) == end &&       \<br>
> +                         (end - start) % EROFS_BLKSIZ == 0)<br>
> +<br>
> +/**<br>
> +   read extents of the given file.<br>
> +   record the data extents and link them into a chain.<br>
> +   exclude holes present in file.<br>
> + */<br>
> +unsigned int erofs_read_sparse_extents(int fd, struct list_head *extents)<br>
> +{<br>
> +     erofs_blk_t startblk, endblk, datablk;<br>
> +     unsigned int nholes = 0;<br>
> +     erofs_off_t data, hole, len = 0, last_data;<br>
> +     struct erofs_sparse_extent_node *e_data;<br>
> +<br>
> +     len = lseek(fd, 0, SEEK_END);<br>
> +     if (len < 0)<br>
> +             return -errno;<br>
> +     if (lseek(fd, 0, SEEK_SET) == -1)<br>
> +             return -errno;<br>
> +     data = 0;<br>
> +     last_data = 0;<br>
> +     while (data < len) {<br>
> +             hole = lseek(fd, data, SEEK_HOLE);<br>
> +             if (hole == len)<br>
> +                     break;<br>
> +             data = lseek(fd, hole, SEEK_DATA);<br>
> +             if (data < 0 || hole > data)<br>
> +                     return -EINVAL;<br>
> +             if (IS_HOLE(hole, data)) {<br>
> +                     startblk = erofs_blknr(hole);<br>
> +                     datablk = erofs_blknr(last_data);<br>
> +                     endblk = erofs_blknr(data);<br>
> +                     last_data = data;<br>
> +                     e_data = malloc(sizeof(<br>
> +                                      struct erofs_sparse_extent_node));<br>
> +                     if (e_data == NULL)<br>
> +                             return -ENOMEM;<br>
> +                     e_data->lblk = datablk;<br>
> +                     e_data->len = (startblk - datablk);<br>
> +                     list_add_tail(&e_data->next, extents);<br>
> +                     nholes += (endblk - startblk);<br>
> +             }<br>
> +     }<br>
> +     /* rounddown to exclude tail-end data */<br>
> +     if (last_data < len && (len - last_data) >= EROFS_BLKSIZ) {<br>
> +             e_data = malloc(sizeof(struct erofs_sparse_extent_node));<br>
> +             if (e_data == NULL)<br>
> +                     return -ENOMEM;<br>
> +             startblk = erofs_blknr(last_data);<br>
> +             e_data->lblk = startblk;<br>
> +             e_data->len = erofs_blknr(rounddown((len - last_data),<br>
> +                                       EROFS_BLKSIZ));<br>
> +             list_add_tail(&e_data->next, extents);<br>
> +     }<br>
> +     return nholes;<br>
> +}<br>
> +<br>
> +int erofs_write_sparse_extents(struct erofs_inode *inode, erofs_off_t off)<br>
> +{<br>
> +     struct erofs_sparse_extent_node *e_node;<br>
> +     struct erofs_sparse_extent_iheader *header;<br>
> +     char *buf;<br>
> +     unsigned int p = 0;<br>
> +     int ret;<br>
> +<br>
> +     buf = malloc(inode->sparse_extent_isize);<br>
> +     if (buf == NULL)<br>
> +             return -ENOMEM;<br>
> +     header = (struct erofs_sparse_extent_iheader *) buf;<br>
> +     header->count = 0;<br>
> +     p += sizeof(struct erofs_sparse_extent_iheader);<br>
> +     list_for_each_entry(e_node, &inode->i_sparse_extents, next) {<br>
> +             const struct erofs_sparse_extent ee = {<br>
> +                     .ee_lblk = cpu_to_le32(e_node->lblk),<br>
> +                     .ee_pblk = cpu_to_le32(e_node->pblk),<br>
> +                     .ee_len  = cpu_to_le32(e_node->len)<br>
> +             };<br>
> +             memcpy(buf + p, &ee, sizeof(struct erofs_sparse_extent));<br>
> +             p += sizeof(struct erofs_sparse_extent);<br>
> +             header->count++;<br>
> +             list_del(&e_node->next);<br>
> +             free(e_node);<br>
> +     }<br>
> +     ret = dev_write(buf, off, inode->sparse_extent_isize);<br>
> +     free(buf);<br>
> +     return ret;<br>
> +}<br>
> +<br>
>  void erofs_inode_manager_init(void)<br>
>  {<br>
>       unsigned int i;<br>
> @@ -304,8 +395,9 @@ static bool erofs_file_is_compressible(struct erofs_inode *inode)<br>
>  <br>
>  int erofs_write_file(struct erofs_inode *inode)<br>
>  {<br>
> -     unsigned int nblocks, i;<br>
> +     unsigned int nblocks, i, j, nholes;<br>
>       int ret, fd;<br>
> +     struct erofs_sparse_extent_node *e_node;<br>
>  <br>
>       if (!inode->i_size) {<br>
>               inode->datalayout = EROFS_INODE_FLAT_PLAIN;<br>
> @@ -322,31 +414,42 @@ int erofs_write_file(struct erofs_inode *inode)<br>
>       /* fallback to all data uncompressed */<br>
>       inode->datalayout = EROFS_INODE_FLAT_INLINE;<br>
>       nblocks = inode->i_size / EROFS_BLKSIZ;<br>
> -<br>
> -     ret = __allocate_inode_bh_data(inode, nblocks);<br>
> -     if (ret)<br>
> -             return ret;<br>
> -<br>
>       fd = open(inode->i_srcpath, O_RDONLY | O_BINARY);<br>
>       if (fd < 0)<br>
>               return -errno;<br>
> -<br>
> -     for (i = 0; i < nblocks; ++i) {<br>
> -             char buf[EROFS_BLKSIZ];<br>
> -<br>
> -             ret = read(fd, buf, EROFS_BLKSIZ);<br>
> -             if (ret != EROFS_BLKSIZ) {<br>
> -                     if (ret < 0)<br>
> +     nholes = erofs_read_sparse_extents(fd, &inode->i_sparse_extents);<br>
> +     if (nholes < 0) {<br>
> +             close(fd);<br>
> +             return nholes;<br>
> +     }<br>
> +     ret = __allocate_inode_bh_data(inode, nblocks - nholes);<br>
> +     if (ret) {<br>
> +             close(fd);<br>
> +             return ret;<br>
> +     }<br>
> +     i = inode->u.i_blkaddr;<br>
> +     inode->sparse_extent_isize = sizeof(struct erofs_sparse_extent_iheader);<br>
> +     list_for_each_entry(e_node, &inode->i_sparse_extents, next) {<br>
> +             inode->sparse_extent_isize += sizeof(struct erofs_sparse_extent);<br>
> +             e_node->pblk = i;<br>
> +             ret = lseek(fd, blknr_to_addr(e_node->lblk), SEEK_SET);<br>
> +             if (ret < 0)<br>
> +                     goto fail;<br>
> +             for (j = 0; j < e_node->len; j++) {<br>
> +                     char buf[EROFS_BLKSIZ];<br>
> +                     ret = read(fd, buf, EROFS_BLKSIZ);<br>
> +                     if (ret != EROFS_BLKSIZ) {<br>
> +                             if (ret < 0)<br>
> +                                     goto fail;<br>
> +                             close(fd);<br>
> +                             return -EAGAIN;<br>
> +                     }<br>
> +                     ret = blk_write(buf, e_node->pblk + j, 1);<br>
> +                     if (ret)<br>
>                               goto fail;<br>
> -                     close(fd);<br>
> -                     return -EAGAIN;<br>
>               }<br>
> -<br>
> -             ret = blk_write(buf, inode->u.i_blkaddr + i, 1);<br>
> -             if (ret)<br>
> -                     goto fail;<br>
> +             i += e_node->len;<br>
>       }<br>
> -<br>
>       /* read the tail-end data */<br>
>       inode->idata_size = inode->i_size % EROFS_BLKSIZ;<br>
>       if (inode->idata_size) {<br>
> @@ -479,8 +582,14 @@ static bool erofs_bh_flush_write_inode(struct erofs_buffer_head *bh)<br>
>               if (ret)<br>
>                       return false;<br>
>               free(inode->compressmeta);<br>
> +             off += inode->extent_isize;<br>
>       }<br>
>  <br>
> +     if (inode->sparse_extent_isize) {<br>
> +             ret = erofs_write_sparse_extents(inode, off);<br>
> +             if (ret)<br>
> +                     return false;<br>
> +     }<br>
>       inode->bh = NULL;<br>
>       erofs_iput(inode);<br>
>       return erofs_bh_flush_generic_end(bh);<br>
> @@ -737,10 +846,11 @@ struct erofs_inode *erofs_new_inode(void)<br>
>  <br>
>       init_list_head(&inode->i_subdirs);<br>
>       init_list_head(&inode->i_xattrs);<br>
> +     init_list_head(&inode->i_sparse_extents);<br>
>  <br>
>       inode->idata_size = 0;<br>
>       inode->xattr_isize = 0;<br>
> -     inode->extent_isize = 0;<br>
> +     inode->sparse_extent_isize = 0;<br>
>  <br>
>       inode->bh = inode->bh_inline = inode->bh_data = NULL;<br>
>       inode->idata = NULL;<br>
> @@ -961,4 +1071,3 @@ struct erofs_inode *erofs_mkfs_build_tree_from_path(struct erofs_inode *parent,<br>
>  <br>
>       return erofs_mkfs_build_tree(inode);<br>
>  }<br>
> -<br>
> -- <br>
> 2.9.3<br>
> <br>
</blockquote></div>