<div dir="ltr"><div>Hello Gao,</div><div><br></div><div>Thanks for the review. <br></div><div>If I understand correctly , you wish to keep track of every extent assigned to the file.</div><div>in case of file without any holes in it, there will single extent representing the entire file.</div><div><br></div><div>Also, the current block no. lookup happens in constant time. (since we only record the start blk no.)</div><div>If we use extent record for finding given block no. it can't be done in constant time correct ? (maybe in LogN)</div><div><br></div><div>I think I don't fully understand reason for recording extents assigned to a file.Since the current design</div><div>is already time and space optimized & there are no deletions happening.</div><div>Is it for some future requirement ?</div><div><br></div><div>--Pratik.<br></div></div><br><div class="gmail_quote"><div dir="ltr" class="gmail_attr">On Tue, Dec 24, 2019 at 9:18 AM Gao Xiang <<a href="mailto:gaoxiang25@huawei.com">gaoxiang25@huawei.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>
Thanks for keeping the work. :)<br>
Some inlined comments below as before.<br>
<br>
On Mon, Dec 23, 2019 at 10:59:38PM +0530, Pratik Shinde wrote:<br>
> Made some changes based on comments on previous patch :<br>
> 1) defined an on disk structure for representing hole.<br>
> 2) Maintain a list of this structure (per file) and dump this list to<br>
> disk at the time of writing the inode to disk.<br>
> ---<br>
> include/erofs/internal.h | 8 +++-<br>
> lib/inode.c | 108 ++++++++++++++++++++++++++++++++++++++++++++---<br>
> 2 files changed, 110 insertions(+), 6 deletions(-)<br>
> <br>
> diff --git a/include/erofs/internal.h b/include/erofs/internal.h<br>
> index e13adda..863ef8a 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_holes;<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 holes_isize;<br>
> <br>
> erofs_nid_t nid;<br>
> struct erofs_buffer_head *bh;<br>
> @@ -139,5 +140,10 @@ static inline const char *erofs_strerror(int err)<br>
> return msg;<br>
> }<br>
> <br>
> +struct erofs_hole {<br>
> + erofs_blk_t st;<br>
> + u32 len;<br>
> + struct list_head next;<br>
> +};<br>
<br>
<br>
How about recording all extents rather than holes? since it's more useful<br>
for later random read access.<br>
<br>
struct erofs_extent_node {<br>
struct list_head next;<br>
<br>
erofs_blk_t lblk; /* logical start address */<br>
erofs_blk_t pblk; /* physical start address */<br>
u32 len; /* extent length in blocks */<br>
};<br>
<br>
<br>
> #endif<br>
> <br>
> diff --git a/lib/inode.c b/lib/inode.c<br>
> index 0e19b11..20bbf06 100644<br>
> --- a/lib/inode.c<br>
> +++ b/lib/inode.c<br>
> @@ -38,6 +38,85 @@ 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>
> +#define HOLE_BLK -1<br>
> +unsigned int erofs_detect_holes(struct erofs_inode *inode,<br>
> + struct list_head *holes, unsigned int *htimes)<br>
> +{<br>
> + int fd, st, en;<br>
> + unsigned int nholes = 0;<br>
> + erofs_off_t data, hole, len;<br>
> + struct erofs_hole *eh;<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>
> + 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>
> + eh = malloc(sizeof(struct erofs_hole));<br>
> + if (eh == NULL)<br>
> + return -ENOMEM;<br>
> + eh->st = st;<br>
> + eh->len = (en - st);<br>
> + list_add_tail(&eh->next, holes);<br>
> + nholes += eh->len;<br>
> + *htimes += 1;<br>
> + }<br>
> + }<br>
> + return nholes;<br>
> +}<br>
> +<br>
> +bool erofs_ishole(erofs_blk_t blk, struct list_head holes)<br>
> +{<br>
> + if (list_empty(&holes))<br>
> + return false;<br>
> + struct erofs_hole *eh;<br>
> + list_for_each_entry(eh, &holes, next) {<br>
> + if (eh->st > blk)<br>
> + return false;<br>
> + if (eh->st <= blk && (eh->st + eh->len - 1) >= blk)<br>
> + return true;<br>
> + }<br>
> + return false;<br>
> +}<br>
> +<br>
> +char *erofs_create_holes_buffer(struct list_head *holes, unsigned int size)<br>
> +{<br>
> + struct erofs_hole *eh;<br>
> + char *buf;<br>
> + unsigned int p = 0;<br>
> +<br>
> + buf = malloc(size);<br>
> + if (buf == NULL)<br>
> + return ERR_PTR(-ENOMEM);<br>
> + list_for_each_entry(eh, holes, next) {<br>
> + *(__le32 *)(buf + p) = cpu_to_le32(eh->st);<br>
> + p += sizeof(__le32);<br>
> + *(__le32 *)(buf + p) = cpu_to_le32(eh->len);<br>
> + p += sizeof(__le32);<br>
> + list_del(&eh->next);<br>
> + free(eh);<br>
> + }<br>
<br>
How about introducing some extent header<br>
<br>
/* 12-byte alignment */<br>
struct erofs_inline_extent_header {<br>
/* e.g. we need to record how many total extents here at least. */<br>
...<br>
};<br>
<br>
and some ondisk extent representation.<br>
<br>
struct erofs_extent {<br>
__le32 ee_lblk;<br>
__le32 ee_pblk;<br>
/*<br>
* most significant 4 bits reserved for flags and should be 0<br>
* now, maximum 256M blocks (8TB) for an extent.<br>
*/<br>
__le32 ee_len;<br>
};<br>
<br>
(see fs/ext4/ext4_extents.h for ext4 ondisk definitions)<br>
<br>
And I'd like to call this inline extent representation (for limited<br>
extents) since we could consider some powerful representation<br>
(e.g. using B+ tree) in the future for complicated requirement, so we<br>
have to reserve i_u.raw_blkaddr in erofs_inode to 0 for now (in order<br>
to indicate extra B+-tree blocks).<br>
<br>
<br>
> + return buf;<br>
> +}<br>
> +<br>
> void erofs_inode_manager_init(void)<br>
> {<br>
> unsigned int i;<br>
> @@ -304,7 +383,7 @@ 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, nholes, hitems = 0;<br>
> int ret, fd;<br>
> <br>
> if (!inode->i_size) {<br>
<br>
<br>
The fallback condition from compress file to uncompress file should be updated<br>
and give a new data_mapping for this mode as well, but that is minor for now.<br>
<br>
Thanks,<br>
Gao Xiang<br>
<br>
<br>
> @@ -322,16 +401,24 @@ 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_detect_holes(inode, &inode->i_holes, &hitems);<br>
> + if (nholes < 0)<br>
> + return nholes;<br>
> + inode->holes_isize = (sizeof(struct erofs_hole) -<br>
> + sizeof(struct list_head)) * hitems;<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>
> + if (erofs_ishole(i, inode->i_holes)) {<br>
> + continue;<br>
> + }<br>
> char buf[EROFS_BLKSIZ];<br>
> <br>
> ret = read(fd, buf, EROFS_BLKSIZ);<br>
> @@ -479,8 +566,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->holes_isize) {<br>
> + char *holes = erofs_create_holes_buffer(&inode->i_holes,<br>
> + inode->holes_isize);<br>
> + if (IS_ERR(holes))<br>
> + return false;<br>
> + ret = dev_write(holes, off, inode->holes_isize);<br>
> + free(holes);<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,6 +835,7 @@ 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_holes);<br>
> <br>
> inode->idata_size = 0;<br>
> inode->xattr_isize = 0;<br>
> @@ -961,4 +1060,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>