<div dir="ltr"><div>Gao,</div><div><br></div><div>Okay, I will rename the 'size' variables accordingly.</div><div>The 'S_SHIFT' gives the logical block number inside file. I am using it to assign logical block no.</div><div>to extents.</div><div>Also, I will modify the code to record only data extents.</div><div><br></div><div>--Pratik.<br></div></div><br><div class="gmail_quote"><div dir="ltr" class="gmail_attr">On Sun, Dec 29, 2019 at 8:26 AM Gao Xiang <<a href="mailto:hsiangkao@aol.com">hsiangkao@aol.com</a>> wrote:<br></div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex">Hi Pratik,<br>
<br>
On Fri, Dec 27, 2019 at 09:13:48PM +0530, Pratik Shinde wrote:<br>
> since this patch is quite different from previous patches I am treating<br>
> this as new patch.<br>
> <br>
> 1) On disk extent format for erofs data blocks.<br>
> 2) Detect holes inside files & skip allocation for hole blocks.<br>
> <br>
> Signed-off-by: Pratik Shinde <<a href="mailto:pratikshinde320@gmail.com" target="_blank">pratikshinde320@gmail.com</a>><br>
> ---<br>
> include/erofs/internal.h | 21 ++++++-<br>
> lib/inode.c | 155 +++++++++++++++++++++++++++++++++++++++++------<br>
> 2 files changed, 156 insertions(+), 20 deletions(-)<br>
> <br>
> diff --git a/include/erofs/internal.h b/include/erofs/internal.h<br>
> index e13adda..128aa63 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_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 extent_meta_isize;<br>
<br>
maybe sparse_extent_isize is better...<br>
<br>
p.s. maybe we could send another patch rename extent_isize to<br>
compress_extent_isize or some better name...<br>
<br>
> <br>
> erofs_nid_t nid;<br>
> struct erofs_buffer_head *bh;<br>
> @@ -139,5 +140,23 @@ static inline const char *erofs_strerror(int err)<br>
> return msg;<br>
> }<br>
> <br>
> +#define HOLE_BLK -1<br>
> +/* on disk extent format */<br>
> +struct erofs_extent {<br>
> + __le32 ee_lblk;<br>
> + __le32 ee_pblk;<br>
> + __le32 ee_len;<br>
> +};<br>
> +<br>
> +struct erofs_extent_node {<br>
> + struct list_head next;<br>
> + erofs_blk_t lblk;<br>
> + erofs_blk_t pblk;<br>
> + u32 len;<br>
> +};<br>
> +<br>
> +struct erofs_inline_extent_header {<br>
> + u32 count;<br>
> +};<br>
> #endif<br>
> <br>
> diff --git a/lib/inode.c b/lib/inode.c<br>
> index 0e19b11..a6af509 100644<br>
> --- a/lib/inode.c<br>
> +++ b/lib/inode.c<br>
> @@ -38,6 +38,99 @@ 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>
See below..<br>
<br>
> +<br>
> +/* returns the number of holes present in the file */<br>
> +unsigned int erofs_read_extents(struct erofs_inode *inode,<br>
> + struct list_head *extents)<br>
> +{<br>
> + int fd, st, en, dt;<br>
> + unsigned int nholes = 0;<br>
> + erofs_off_t data, hole, len, last_data;<br>
> + struct erofs_extent_node *e_hole, *e_data;<br>
> +<br>
> + fd = open(inode->i_srcpath, O_RDONLY);<br>
> + if (fd < 0) {<br>
> + return -errno;<br>
> + }<br>
> + len = lseek(fd, 0, SEEK_END);<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>
> + }<br>
> + if (IS_HOLE(hole, data)) {<br>
> + st = hole >> S_SHIFT;<br>
> + en = data >> S_SHIFT;<br>
> + dt = last_data >> S_SHIFT;<br>
<br>
Why using S_SHIFT here, some special meaning?<br>
<br>
> + last_data = data;<br>
> + e_data = malloc(sizeof(struct erofs_extent_node));<br>
> + if (e_data == NULL)<br>
> + return -ENOMEM;<br>
> + e_data->lblk = dt;<br>
> + e_data->len = (st - dt);<br>
> + list_add_tail(&e_data->next, extents);<br>
> + e_hole = malloc(sizeof(struct erofs_extent_node));<br>
> + if (e_hole == NULL)<br>
> + return -ENOMEM;<br>
> + e_hole->lblk = st;<br>
> + e_hole->pblk = HOLE_BLK;<br>
> + e_hole->len = (en - st);<br>
> + list_add_tail(&e_hole->next, extents);<br>
> + nholes += e_hole->len;<br>
<br>
Maybe we don't need to emit all HOLK extents since all data extents<br>
are with _length_... It is easy to detect all holes between extents...<br>
<br>
If some block doesn't belong to any extent, it's a hole.<br>
<br>
Thanks,<br>
Gao Xiang<br>
<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_extent_node));<br>
> + if (e_data == NULL)<br>
> + return -ENOMEM;<br>
> + st = last_data >> S_SHIFT;<br>
> + e_data->lblk = st;<br>
> + e_data->len = rounddown((len - last_data), EROFS_BLKSIZ) >> S_SHIFT;<br>
> + list_add_tail(&e_data->next, extents);<br>
> + }<br>
> + return nholes;<br>
> +}<br>
> +<br>
> +char *erofs_create_extent_buffer(struct list_head *extents, unsigned int size)<br>
> +{<br>
> + struct erofs_extent_node *e_node;<br>
> + struct erofs_inline_extent_header *header;<br>
> + char *buf;<br>
> + unsigned int p = 0;<br>
> +<br>
> + buf = malloc(size);<br>
> + if (buf == NULL)<br>
> + return ERR_PTR(-ENOMEM);<br>
> + header = (struct erofs_inline_extent_header *) buf;<br>
> + header->count = 0;<br>
> + p += sizeof(struct erofs_inline_extent_header);<br>
> + list_for_each_entry(e_node, extents, next) {<br>
> + const struct erofs_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_extent));<br>
> + p += sizeof(struct erofs_extent);<br>
> + header->count++;<br>
> + list_del(&e_node->next);<br>
> + free(e_node);<br>
> + }<br>
> + return buf;<br>
> +}<br>
> +<br>
> void erofs_inode_manager_init(void)<br>
> {<br>
> unsigned int i;<br>
> @@ -304,8 +397,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_extent_node *e_node;<br>
> <br>
> if (!inode->i_size) {<br>
> inode->datalayout = EROFS_INODE_FLAT_PLAIN;<br>
> @@ -322,31 +416,43 @@ 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>
> + nholes = erofs_read_extents(inode, &inode->i_extents);<br>
> + if (nholes < 0)<br>
> + return nholes;<br>
> + if (nblocks < 0)<br>
> + return nblocks;<br>
> + ret = __allocate_inode_bh_data(inode, nblocks - nholes);<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>
> - goto fail;<br>
> - close(fd);<br>
> - return -EAGAIN;<br>
> + i = inode->u.i_blkaddr;<br>
> + inode->extent_meta_isize = sizeof(struct erofs_inline_extent_header);<br>
> + list_for_each_entry(e_node, &inode->i_extents, next) {<br>
> + inode->extent_meta_isize += sizeof(struct erofs_extent);<br>
> + if (e_node->pblk == HOLE_BLK) {<br>
> + lseek(fd, e_node->len * EROFS_BLKSIZ, SEEK_CUR);<br>
> + continue;<br>
> }<br>
> + e_node->pblk = i;<br>
> + i += e_node->len;<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>
> <br>
> - ret = blk_write(buf, inode->u.i_blkaddr + i, 1);<br>
> - if (ret)<br>
> - goto fail;<br>
> + }<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 +585,19 @@ 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->extent_meta_isize) {<br>
> + char *extents = erofs_create_extent_buffer(&inode->i_extents,<br>
> + inode->extent_meta_isize);<br>
> + if (IS_ERR(extents))<br>
> + return false;<br>
> + ret = dev_write(extents, off, inode->extent_meta_isize);<br>
> + free(extents);<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 +854,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_extents);<br>
> <br>
> inode->idata_size = 0;<br>
> inode->xattr_isize = 0;<br>
> - inode->extent_isize = 0;<br>
> + inode->extent_meta_isize = 0;<br>
> <br>
> inode->bh = inode->bh_inline = inode->bh_data = NULL;<br>
> inode->idata = NULL;<br>
> @@ -961,4 +1079,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>