erofs-utils: on-disk extent format for blocks.
RFC v2 1/1 erofs-utils: on-disk extent format for blocks.
Tracking only the data-extents + some minor naming changes.
--- include/erofs/internal.h | 20 ++++++- lib/inode.c | 151 ++++++++++++++++++++++++++++++++++++++++------- 2 files changed, 150 insertions(+), 21 deletions(-)
include/erofs/internal.h +19 -1
@@ -63,7 +63,7 @@ struct erofs_sb_info {extern struct erofs_sb_info sbi;struct erofs_inode { -struct list_head i_hash, i_subdirs, i_xattrs; +struct list_head i_hash, i_subdirs, i_xattrs, i_sparse_extents;unsigned int i_count;struct erofs_inode *i_parent;
@@ -93,6 +93,7 @@ struct erofs_inode {unsigned int xattr_isize;unsigned int extent_isize; +unsigned int sparse_extent_isize;erofs_nid_t nid;struct erofs_buffer_head *bh;
@@ -139,5 +140,22 @@ static inline const char *erofs_strerror(int err) return msg; } +/* on disk extent format */ +struct erofs_extent { +__le32 ee_lblk; +__le32 ee_pblk; +__le32 ee_len; +}; + +struct erofs_extent_node { +struct list_head next; +erofs_blk_t lblk; +erofs_blk_t pblk; +u32 len; +}; + +struct erofs_inline_extent_header { +u32 count; +};#endif
lib/inode.c +131 -20
198 lines not on this page · raw message
Signature · 1 line
2.9.3
Hi Pratik,
Quoted 30 lines · Pratik Shinde
Tracking only the data-extents + some minor naming changes.
Signed-off-by Pratik Shinde <[email protected]>--- include/erofs/internal.h | 20 ++++++- lib/inode.c | 151 ++++++++++++++++++++++++++++++++++++++++------- 2 files changed, 150 insertions(+), 21 deletions(-)include/erofs/internal.h +2
-1@@ -63,7 +63,7 @@ struct erofs_sb_info {extern struct erofs_sb_info sbi;struct erofs_inode { -struct list_head i_hash, i_subdirs, i_xattrs; +struct list_head i_hash, i_subdirs, i_xattrs, i_sparse_extents;unsigned int i_count;struct erofs_inode *i_parent;@@ -93,6 +93,7 @@ struct erofs_inode {unsigned int xattr_isize;unsigned int extent_isize; +unsigned int sparse_extent_isize;erofs_nid_t nid;struct erofs_buffer_head *bh;
include/erofs/internal.h static inline const char *erofs_strerror(int err) · line 143
Quoted 4 lines
include/erofs/internal.h +1
-0@@ -139,5 +140,22 @@ static inline const char *erofs_strerror(int err)return msg; } +/* on disk extent format */
On disk extent format is not useful...
include/erofs/internal.h static inline const char *erofs_strerror(int err) · line 144
Quoted 1 line
include/erofs/internal.h +1
-0+struct erofs_extent {
Maybe add _sparse_ in order to distinguish from compress extents...
struct erofs_sparse_extent
include/erofs/internal.h static inline const char *erofs_strerror(int err) · line 148
Quoted 4 lines
include/erofs/internal.h +4
-0+__le32 ee_lblk; +__le32 ee_pblk; +__le32 ee_len; +};
How about moving all on-disk format to erofs_fs.h?
include/erofs/internal.h static inline const char *erofs_strerror(int err) · line 150
Quoted 2 lines
include/erofs/internal.h +2
-0+ +struct erofs_extent_node {
struct erofs_sparse_extent_node {
include/erofs/internal.h static inline const char *erofs_strerror(int err) · line 157
Quoted 7 lines
include/erofs/internal.h +7
-0+struct list_head next; +erofs_blk_t lblk; +erofs_blk_t pblk; +u32 len; +}; + +struct erofs_inline_extent_header {
struct erofs_sparse_extent_iheader {
Quoted 9 lines
include/erofs/internal.h +2
-0+ u32 count; +};#endif lib/inode.c +0
-0
lib/inode.c static unsigned char erofs_type_by_mode[S_IFMT >> S_SHIFT] = { · line 44
Quoted 7 lines
lib/inode.c +4
-0@@ -38,6 +38,98 @@ 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)
It is used only once, how about getting rid of this macro helper and add some comments above it?
lib/inode.c static unsigned char erofs_type_by_mode[S_IFMT >> S_SHIFT] = { · line 48
Quoted 4 lines
lib/inode.c +4
-0+ +/* returns the number of holes present in the file */ +unsigned int erofs_read_extents (struct erofs_inode *inode, +struct list_head *extents)
erofs_sparse_read_extents()?
lib/inode.c static unsigned char erofs_type_by_mode[S_IFMT >> S_SHIFT] = { · line 50
Quoted 2 lines
lib/inode.c +2
-0+{ +int fd, st, dt, en;
maybe it'd better have some meaningful names...
lib/inode.c static unsigned char erofs_type_by_mode[S_IFMT >> S_SHIFT] = { · line 55
Quoted 5 lines
lib/inode.c +5
-0+unsigned int nholes =0 ; +erofs_off_t data, hole, len =0 , last_data; +struct erofs_extent_node *e_data; + + fd =open (inode->i_srcpath,O_RDONLY );
It is more preferred to reuse the previous fd?
Quoted 36 lines
lib/inode.c +36
-0+if (fd <0 ) +return -errno; + len =lseek (fd,0 ,SEEK_END ); +if (len <0 ) + goto fail; +if (lseek (fd,0 ,SEEK_SET ) ==-1 ) + goto fail; + data =0 ; + last_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) { +close (fd); +return -EINVAL ; + } +if (IS_HOLE (hole, data)) { + st =erofs_blknr (hole); + dt =erofs_blknr (last_data); + en =erofs_blknr (data); + last_data = data; + e_data =malloc (sizeof (struct erofs_extent_node )); +if (e_data ==NULL ) + goto fail; + e_data->lblk = dt; + e_data->len = (st - dt); +list_add_tail (&e_data->next, extents); + nholes += (en - st); + } + } +/* rounddown to exclude tail-end data */ +if (last_data < len && (len - last_data) >=EROFS_BLKSIZ ) { + e_data =malloc (sizeof (struct erofs_extent_node )); +if (e_data ==NULL )
lib/inode.c static unsigned char erofs_type_by_mode[S_IFMT >> S_SHIFT] = { · line 101
Quoted 10 lines
lib/inode.c +10
-0+ goto fail; + st =erofs_blknr (last_data); + e_data->lblk = st; + e_data->len =erofs_blknr (rounddown ((len - last_data), +EROFS_BLKSIZ )); +list_add_tail (&e_data->next, extents); + } +return nholes; + +fail:close (fd);
Leave the label in a single line...
And if we reuse fd, we could omit close(fd) as well..
lib/inode.c static unsigned char erofs_type_by_mode[S_IFMT >> S_SHIFT] = { · line 105
Quoted 4 lines
lib/inode.c +4
-0+return -errno; +} + +char *erofs_create_extent_buffer (struct list_head *extents,unsigned int size)
How about
int erofs_sparse_write_extents(struct erofs_inode *inode, erofs_off_t off)
{
...
ret = blk_write(buf, off, inode->sparse_extent_isize);
...
free(buf);
return ret;
}Quoted 103 lines
lib/inode.c +54
-18+{ +struct erofs_extent_node *e_node; +struct erofs_inline_extent_header *header; +char *buf; +unsigned int p =0 ; + + buf =malloc (size); +if (buf ==NULL ) +return ERR_PTR (-ENOMEM ); + header = (struct erofs_inline_extent_header *) buf; + header->count =0 ; + p +=sizeof (struct erofs_inline_extent_header ); +list_for_each_entry (e_node, extents, next) { +const struct erofs_extent ee = { + .ee_lblk =cpu_to_le32 (e_node->lblk), + .ee_pblk =cpu_to_le32 (e_node->pblk), + .ee_len =cpu_to_le32 (e_node->len) + }; +memcpy (buf + p, &ee,sizeof (struct erofs_extent )); + p +=sizeof (struct erofs_extent ); + header->count++; +list_del (&e_node->next); +free (e_node); + } +return buf; +} +void erofs_inode_manager_init (void ) {unsigned int i;@@ -304,8 +396,9 @@ static bool erofs_file_is_compressible(struct erofs_inode *inode)int erofs_write_file (struct erofs_inode *inode) { -unsigned int nblocks, i; +unsigned int nblocks, i, j, nholes;int ret, fd; +struct erofs_extent_node *e_node;if (!inode->i_size) { inode->datalayout =EROFS_INODE_FLAT_PLAIN ;@@ -322,31 +415,38 @@ 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 ; - - ret =__allocate_inode_bh_data (inode, nblocks); + nholes =erofs_read_extents (inode, &inode->i_sparse_extents); +if (nholes <0 ) +return nholes; + ret =__allocate_inode_bh_data (inode, nblocks - nholes);if (ret)return ret; - fd =open (inode->i_srcpath,O_RDONLY |O_BINARY );if (fd <0 )return -errno; - -for (i =0 ; i < nblocks; ++i) { -char buf[EROFS_BLKSIZ ]; - - ret =read (fd, buf,EROFS_BLKSIZ ); -if (ret !=EROFS_BLKSIZ ) { -if (ret <0 ) + i = inode->u.i_blkaddr; + inode->sparse_extent_isize =sizeof (struct erofs_inline_extent_header ); +list_for_each_entry (e_node, &inode->i_sparse_extents, next) { + inode->sparse_extent_isize +=sizeof (struct erofs_extent ); + e_node->pblk = i; + ret =lseek (fd,blknr_to_addr (e_node->lblk),SEEK_SET ); +if (ret <0 ) + goto fail; +for (j =0 ; j < e_node->len; j++) { +char buf[EROFS_BLKSIZ ]; + ret =read (fd, buf,EROFS_BLKSIZ ); +if (ret !=EROFS_BLKSIZ ) { +if (ret <0 ) + goto fail; +close (fd); +return -EAGAIN ; + } + ret =blk_write (buf, e_node->pblk + j,1 ); +if (ret) goto fail; -close (fd); -return -EAGAIN ; } - - ret =blk_write (buf, inode->u.i_blkaddr + i,1 ); -if (ret) - goto fail; + i += e_node->len; } -/* read the tail-end data */ inode->idata_size = inode->i_size %EROFS_BLKSIZ ;if (inode->idata_size) {@@ -479,8 +579,19 @@ static bool erofs_bh_flush_write_inode(struct erofs_buffer_head *bh)if (ret)return false;free (inode->compressmeta); + off += inode->extent_isize; }
lib/inode.c static bool erofs_bh_flush_write_inode(struct erofs_buffer_head *bh) · line 593
Quoted 10 lines
lib/inode.c +9
-0+if (inode->sparse_extent_isize) { +char *extents =erofs_create_extent_buffer (&inode->i_sparse_extents, + inode->sparse_extent_isize); +if (IS_ERR (extents)) +return false; + ret =dev_write (extents, off, inode->sparse_extent_isize); +if (ret) +return false; +free (extents);
ret = erofs_sparse_write_extents(inode, off); if (ret) return false;
Thanks,
Gao Xiang
Quoted 25 lines
lib/inode.c +3
-2+ } inode->bh =NULL ;erofs_iput (inode);return erofs_bh_flush_generic_end (bh);@@ -737,10 +848,11 @@ struct erofs_inode *erofs_new_inode(void)init_list_head (&inode->i_subdirs);init_list_head (&inode->i_xattrs); +init_list_head (&inode->i_sparse_extents); inode->idata_size =0 ; inode->xattr_isize =0 ; - inode->extent_isize =0 ; + inode->sparse_extent_isize =0 ; inode->bh = inode->bh_inline = inode->bh_data =NULL ; inode->idata =NULL ;@@ -961,4 +1073,3 @@ struct erofs_inode *erofs_mkfs_build_tree_from_path(struct erofs_inode *parent,return erofs_mkfs_build_tree (inode); } -Signature · 1 line
2.9.3