erofs-utils: use erofs_atomic_t for inode->i_count
Subsystems (2)
PATCH v2 1/8 erofs-utils: use erofs_atomic_t for inode->i_count
From: Gao Xiang <[email protected]>
Since inode->i_count can be touched for more than one thread if multi-threading is enabled.
---
patchset v1->v2:
- Fix --all-fragments functionality;
- Fix issues pointed out by Yifan.
include/erofs/atomic.h | 10 ++++++++++ include/erofs/inode.h | 2 +- include/erofs/internal.h | 3 ++- lib/inode.c | 5 +++-- 4 files changed, 16 insertions(+), 4 deletions(-)
include/erofs/atomic.h +10 -0
@@ -25,4 +25,14 @@ __n;})#define erofs_atomic_test_and_set (ptr) \ __atomic_test_and_set(ptr, __ATOMIC_RELAXED) +#define erofs_atomic_add_return (ptr, i) \ + __atomic_add_fetch(ptr, i, __ATOMIC_RELAXED) + +#define erofs_atomic_sub_return (ptr, i) \ + __atomic_sub_fetch(ptr, i, __ATOMIC_RELAXED) + +#define erofs_atomic_inc_return (ptr) erofs_atomic_add_return(ptr, 1) + +#define erofs_atomic_dec_return (ptr) erofs_atomic_sub_return(ptr, 1) +#endif
include/erofs/inode.h +1 -1
@@ -17,7 +17,7 @@ extern "C"static inline struct erofs_inode *erofs_igrab (struct erofs_inode *inode) { - ++inode->i_count; + (void )erofs_atomic_inc_return (&inode->i_count);return inode; }
include/erofs/internal.h +2 -1
@@ -25,6 +25,7 @@ typedef unsigned short umode_t;#ifdef HAVE_PTHREAD_H #include <pthread.h> #endif +#include "atomic.h" #ifndef PATH_MAX #define PATH_MAX 4096/* # chars in a path name including nul */
@@ -169,7 +170,7 @@ struct erofs_inode {/* (mkfs.erofs) next pointer for directory dumping */ struct erofs_inode *next_dirwrite; }; -unsigned int i_count; +erofs_atomic_t i_count;struct erofs_sb_info *sbi;struct erofs_inode *i_parent;
lib/inode.c +3 -2
@@ -129,9 +129,10 @@ struct erofs_inode *erofs_iget_by_nid(erofs_nid_t nid)unsigned int erofs_iput (struct erofs_inode *inode) {struct erofs_dentry *d, *t; +unsigned long got =erofs_atomic_dec_return (&inode->i_count); -if (inode->i_count >1 ) -return --inode->i_count; +if (got >=1 ) +return got;list_for_each_entry_safe (d, t, &inode->i_subdirs, d_child)free (d);
Signature · 1 line
2.30.2
PATCH v2 2/8 erofs-utils: lib: prepare for later deferred work
From: Gao Xiang <[email protected]>
Split out ordered metadata operations and add the following helpers:
- erofs_mkfs_jobfn()
- erofs_mkfs_go()
to handle these mkfs job items for multi-threadding support.
--- lib/inode.c | 69 ++++++++++++++++++++++++++++++++++++++++++++--------- 1 file changed, 58 insertions(+), 11 deletions(-)
lib/inode.c +58 -11
@@ -1133,6 +1133,57 @@ static int erofs_mkfs_handle_nondirectory(struct erofs_inode *inode)return 0 ; } +enum erofs_mkfs_jobtype {/* ordered job types */ +EROFS_MKFS_JOB_NDIR , +EROFS_MKFS_JOB_DIR , +EROFS_MKFS_JOB_DIR_BH , +}; + +struct erofs_mkfs_jobitem { +enum erofs_mkfs_jobtype type; +union { +struct erofs_inode *inode; + } u; +}; + +static int erofs_mkfs_jobfn (struct erofs_mkfs_jobitem *item) +{ +struct erofs_inode *inode = item->u.inode; +int ret; + +if (item->type ==EROFS_MKFS_JOB_NDIR ) +return erofs_mkfs_handle_nondirectory (inode); + +if (item->type ==EROFS_MKFS_JOB_DIR ) { + ret =erofs_prepare_inode_buffer (inode); +if (ret) +return ret; + inode->bh->op = &erofs_skip_write_bhops; +if (IS_ROOT (inode))/* assign root NID */ +erofs_fixup_meta_blkaddr (inode); +return 0 ; + } + +if (item->type ==EROFS_MKFS_JOB_DIR_BH ) { +erofs_write_dir_file (inode); +erofs_write_tail_end (inode); + inode->bh->op = &erofs_write_inode_bhops; +erofs_iput (inode); +return 0 ; + } +return -EINVAL ; +} + +int erofs_mkfs_go (struct erofs_sb_info *sbi, +enum erofs_mkfs_jobtype type,void *elem,int size) +{ +struct erofs_mkfs_jobitem item; + + item.type = type; +memcpy (&item.u, elem, size); +return erofs_mkfs_jobfn (&item); +} +static int erofs_mkfs_handle_directory (struct erofs_inode *dir) {DIR *_dir;
@@ -1213,11 +1264,7 @@ static int erofs_mkfs_handle_directory(struct erofs_inode *dir)else dir->i_nlink = i_nlink; - ret =erofs_prepare_inode_buffer (dir); -if (ret) -return ret; - dir->bh->op = &erofs_skip_write_bhops; -return 0 ; +return erofs_mkfs_go (dir->sbi,EROFS_MKFS_JOB_DIR , &dir,sizeof (dir)); err_closedir:closedir (_dir);
@@ -1243,7 +1290,8 @@ static int erofs_mkfs_handle_inode(struct erofs_inode *inode)return ret;if (!S_ISDIR (inode->i_mode)) - ret =erofs_mkfs_handle_nondirectory (inode); + ret =erofs_mkfs_go (inode->sbi,EROFS_MKFS_JOB_NDIR , + &inode,sizeof (inode));else ret =erofs_mkfs_handle_directory (inode); erofs_info("file %s dumped (mode %05o)", erofs_fspath(inode->i_srcpath),
@@ -1268,7 +1316,6 @@ struct erofs_inode *erofs_mkfs_build_tree_from_path(const char *path) err =erofs_mkfs_handle_inode (root);if (err)return ERR_PTR (err); -erofs_fixup_meta_blkaddr (root); do { int err;
@@ -1302,10 +1349,10 @@ struct erofs_inode *erofs_mkfs_build_tree_from_path(const char *path) } *last = dumpdir;/* fixup the last (or the only) one */ dumpdir = head; -erofs_write_dir_file (dir); -erofs_write_tail_end (dir); - dir->bh->op = &erofs_write_inode_bhops; -erofs_iput (dir); + err =erofs_mkfs_go (dir->sbi,EROFS_MKFS_JOB_DIR_BH , + &dir,sizeof (dir)); +if (err) +return ERR_PTR (err); }while (dumpdir);return root;
Signature · 1 line
2.30.2
PATCH v2 3/8 erofs-utils: lib: split out erofs_commit_compressed_file()
In reply to Gao XiangFrom: Gao Xiang <[email protected]>
Just split out on-disk compressed metadata commit logic.
--- lib/compress.c | 191 +++++++++++++++++++++++++++---------------------- 1 file changed, 105 insertions(+), 86 deletions(-)
lib/compress.c +105 -86
@@ -1031,6 +1031,102 @@ int z_erofs_compress_segment(struct z_erofs_compress_sctx *ctx,return 0 ; } +int erofs_commit_compressed_file (struct z_erofs_compress_ictx *ictx, +struct erofs_buffer_head *bh, +erofs_blk_t blkaddr, +erofs_blk_t compressed_blocks) +{ +struct erofs_inode *inode = ictx->inode; +struct erofs_sb_info *sbi = inode->sbi; +unsigned int legacymetasize; +u8 *compressmeta; +int ret; + +/* fall back to no compression mode */ +DBG_BUGON (compressed_blocks < !!inode->idata_size); + compressed_blocks -= !!inode->idata_size; + + compressmeta =malloc (BLK_ROUND_UP (sbi, inode->i_size) * +sizeof (struct z_erofs_lcluster_index ) + +Z_EROFS_LEGACY_MAP_HEADER_SIZE ); +if (!compressmeta) { + ret = -ENOMEM ; + goto err_free_idata; + } + ictx->metacur = compressmeta +Z_EROFS_LEGACY_MAP_HEADER_SIZE ; +z_erofs_write_indexes (ictx); + + legacymetasize = ictx->metacur - compressmeta; +/* estimate if data compression saves space or not */ +if (!inode->fragment_size && + compressed_blocks *erofs_blksiz (sbi) + inode->idata_size + + legacymetasize >= inode->i_size) { +z_erofs_dedupe_commit (true); + ret = -ENOSPC ; + goto err_free_meta; + } +z_erofs_dedupe_commit (false); +z_erofs_write_mapheader (inode, compressmeta); + +if (!ictx->fragemitted) + sbi->saved_by_deduplication += inode->fragment_size; + +/* if the entire file is a fragment, a simplified form is used. */ +if (inode->i_size <= inode->fragment_size) { +DBG_BUGON (inode->i_size < inode->fragment_size); +DBG_BUGON (inode->fragmentoff >>63 ); + *(__le64 *)compressmeta = +cpu_to_le64 (inode->fragmentoff |1ULL <<63 ); + inode->datalayout =EROFS_INODE_COMPRESSED_FULL ; + legacymetasize =Z_EROFS_LEGACY_MAP_HEADER_SIZE ; + } + +if (compressed_blocks) { + ret =erofs_bh_balloon (bh,erofs_pos (sbi, compressed_blocks)); +DBG_BUGON (ret !=erofs_blksiz (sbi)); + }else { +if (!cfg.c_fragments && !cfg.c_dedupe) +DBG_BUGON (!inode->idata_size); + } + +erofs_info ("compressed %s (%llu bytes) into %u blocks" , + inode->i_srcpath, (unsigned long long )inode->i_size, + compressed_blocks); + +if (inode->idata_size) { + bh->op = &erofs_skip_write_bhops; + inode->bh_data = bh; + }else { +erofs_bdrop (bh, false); + } + + inode->u.i_blocks = compressed_blocks; + +if (inode->datalayout ==EROFS_INODE_COMPRESSED_FULL ) { + inode->extent_isize = legacymetasize; + }else { + ret =z_erofs_convert_to_compacted_format (inode, blkaddr, + legacymetasize, + compressmeta); +DBG_BUGON (ret); + } + inode->compressmeta = compressmeta; +if (!erofs_is_packed_inode (inode)) +erofs_droid_blocklist_write (inode, blkaddr, compressed_blocks); +return 0 ; + +err_free_meta: +free (compressmeta); + inode->compressmeta =NULL ; +err_free_idata: +if (inode->idata) { +free (inode->idata); + inode->idata =NULL ; + } +erofs_bdrop (bh, true);/* revoke buffer */ +return ret; +} +#ifdef EROFS_MT_ENABLED void *z_erofs_mt_wq_tls_alloc (struct erofs_workqueue *wq,void *ptr) {
@@ -1260,23 +1356,9 @@ int erofs_write_compressed_file(struct erofs_inode *inode, int fd, u64 fpos)static struct z_erofs_compress_sctx sctx;struct erofs_compress_cfg *ccfg;erofs_blk_t blkaddr, compressed_blocks =0 ; -unsigned int legacymetasize;int ret;bool ismt = false;struct erofs_sb_info *sbi = inode->sbi; -u8 *compressmeta =malloc (BLK_ROUND_UP (sbi, inode->i_size) * -sizeof (struct z_erofs_lcluster_index ) + -Z_EROFS_LEGACY_MAP_HEADER_SIZE ); - -if (!compressmeta) -return -ENOMEM ; - -/* allocate main data buffer */ - bh =erofs_balloc (DATA ,0 ,0 ,0 ); -if (IS_ERR (bh)) { - ret =PTR_ERR (bh); - goto err_free_meta; - }/* initialize per-file compression setting */ inode->z_advise =0 ;
@@ -1321,20 +1403,24 @@ int erofs_write_compressed_file(struct erofs_inode *inode, int fd, u64 fpos)if (cfg.c_fragments && !erofs_is_packed_inode (inode)) { ret =z_erofs_fragments_dedupe (inode, fd, &ctx.tof_chksum);if (ret <0 ) - goto err_bdrop; +return ret; } - blkaddr =erofs_mapbh (bh->block);/* start_blkaddr */ ctx.inode = inode; ctx.fd = fd; ctx.fpos = fpos; - ctx.metacur = compressmeta +Z_EROFS_LEGACY_MAP_HEADER_SIZE ;init_list_head (&ctx.extents); ctx.fix_dedupedfrag = false; ctx.fragemitted = false; sctx = (struct z_erofs_compress_sctx ) { .ictx = &ctx, };init_list_head (&sctx.extents); +/* allocate main data buffer */ + bh =erofs_balloc (DATA ,0 ,0 ,0 ); +if (IS_ERR (bh)) +return PTR_ERR (bh); + blkaddr =erofs_mapbh (bh->block);/* start_blkaddr */ +if (cfg.c_all_fragments && !erofs_is_packed_inode (inode) && !inode->fragment_size) { ret =z_erofs_pack_file_from_fd (inode, fd, ctx.tof_chksum);
@@ -1363,10 +1449,6 @@ int erofs_write_compressed_file(struct erofs_inode *inode, int fd, u64 fpos) compressed_blocks = sctx.blkaddr - blkaddr; } -/* fall back to no compression mode */ -DBG_BUGON (compressed_blocks < !!inode->idata_size); - compressed_blocks -= !!inode->idata_size; -/* generate an extent for the deduplicated fragment */ if (inode->fragment_size && !ctx.fragemitted) {struct z_erofs_extent_item *ei;
@@ -1388,80 +1470,17 @@ int erofs_write_compressed_file(struct erofs_inode *inode, int fd, u64 fpos)z_erofs_commit_extent (&sctx, ei); }z_erofs_fragments_commit (inode); -if (!ismt)list_splice_tail (&sctx.extents, &ctx.extents); -z_erofs_write_indexes (&ctx); - legacymetasize = ctx.metacur - compressmeta; -/* estimate if data compression saves space or not */ -if (!inode->fragment_size && - compressed_blocks *erofs_blksiz (sbi) + inode->idata_size + - legacymetasize >= inode->i_size) { -z_erofs_dedupe_commit (true); - ret = -ENOSPC ; - goto err_free_idata; - } -z_erofs_dedupe_commit (false); -z_erofs_write_mapheader (inode, compressmeta); - -if (!ctx.fragemitted) - sbi->saved_by_deduplication += inode->fragment_size; - -/* if the entire file is a fragment, a simplified form is used. */ -if (inode->i_size <= inode->fragment_size) { -DBG_BUGON (inode->i_size < inode->fragment_size); -DBG_BUGON (inode->fragmentoff >>63 ); - *(__le64 *)compressmeta = -cpu_to_le64 (inode->fragmentoff |1ULL <<63 ); - inode->datalayout =EROFS_INODE_COMPRESSED_FULL ; - legacymetasize =Z_EROFS_LEGACY_MAP_HEADER_SIZE ; - } - -if (compressed_blocks) { - ret =erofs_bh_balloon (bh,erofs_pos (sbi, compressed_blocks)); -DBG_BUGON (ret !=erofs_blksiz (sbi)); - }else { -if (!cfg.c_fragments && !cfg.c_dedupe) -DBG_BUGON (!inode->idata_size); - } - -erofs_info ("compressed %s (%llu bytes) into %u blocks" , - inode->i_srcpath, (unsigned long long )inode->i_size, - compressed_blocks); - -if (inode->idata_size) { - bh->op = &erofs_skip_write_bhops; - inode->bh_data = bh; - }else { -erofs_bdrop (bh, false); - } - - inode->u.i_blocks = compressed_blocks; - -if (inode->datalayout ==EROFS_INODE_COMPRESSED_FULL ) { - inode->extent_isize = legacymetasize; - }else { - ret =z_erofs_convert_to_compacted_format (inode, blkaddr, - legacymetasize, - compressmeta); -DBG_BUGON (ret); - } - inode->compressmeta = compressmeta; -if (!erofs_is_packed_inode (inode)) -erofs_droid_blocklist_write (inode, blkaddr, compressed_blocks); -return 0 ; - +return erofs_commit_compressed_file (&ctx, bh, blkaddr, + compressed_blocks); err_free_idata:if (inode->idata) {free (inode->idata); inode->idata =NULL ; } -err_bdrop:erofs_bdrop (bh, true);/* revoke buffer */ -err_free_meta: -free (compressmeta); - inode->compressmeta =NULL ;return ret; }
Signature · 1 line
2.30.2
PATCH v2 4/8 erofs-utils: rearrange several fields for multi-threaded mkfs
In reply to Gao XiangFrom: Gao Xiang <[email protected]>
They should be located in struct z_erofs_compress_ictx.
--- lib/compress.c | 57 +++++++++++++++++++++++++++----------------------- 1 file changed, 31 insertions(+), 26 deletions(-)
lib/compress.c +31 -26
@@ -38,6 +38,7 @@ struct z_erofs_extent_item {struct z_erofs_compress_ictx {/* inode context */ struct erofs_inode *inode; +struct erofs_compress_cfg *ccfg;int fd;u64 fpos;
@@ -49,6 +50,14 @@ struct z_erofs_compress_ictx { /* inode context */u8 *metacur;struct list_head extents;u16 clusterofs; + +int seg_num; + +#if EROFS_MT_ENABLED +pthread_mutex_t mutex; +pthread_cond_t cond; +int nfini; +#endif };struct z_erofs_compress_sctx {/* segment context */
@@ -68,7 +77,7 @@ struct z_erofs_compress_sctx { /* segment context */erofs_blk_t blkaddr;/* pointing to the next blkaddr */ u16 clusterofs; -int seg_num, seg_idx; +int seg_idx;void *membuf;erofs_off_t memoff;
@@ -98,9 +107,6 @@ struct erofs_compress_work {static struct {struct erofs_workqueue wq;struct erofs_compress_work *idle; -pthread_mutex_t mutex; -pthread_cond_t cond; -int nfini; } z_erofs_mt_ctrl;#endif
@@ -513,7 +519,7 @@ static int __z_erofs_compress_one(struct z_erofs_compress_sctx *ctx,struct erofs_compress *const h = ctx->chandle;unsigned int len = ctx->tail - ctx->head;bool is_packed_inode =erofs_is_packed_inode (inode); -bool tsg = (ctx->seg_idx +1 >= ctx->seg_num), final = !ctx->remaining; +bool tsg = (ctx->seg_idx +1 >= ictx->seg_num), final = !ctx->remaining;bool may_packing = (cfg.c_fragments && tsg && final && !is_packed_inode && !z_erofs_mt_enabled);bool may_inline = (cfg.c_ztailpacking && tsg && final && !may_packing);
@@ -1201,7 +1207,8 @@ void z_erofs_mt_workfn(struct erofs_work *work, void *tlsp)struct erofs_compress_work *cwork = (struct erofs_compress_work *)work;struct erofs_compress_wq_tls *tls = tlsp;struct z_erofs_compress_sctx *sctx = &cwork->ctx; -struct erofs_inode *inode = sctx->ictx->inode; +struct z_erofs_compress_ictx *ictx = sctx->ictx; +struct erofs_inode *inode = ictx->inode;struct erofs_sb_info *sbi = inode->sbi;int ret =0 ;
@@ -1228,10 +1235,10 @@ void z_erofs_mt_workfn(struct erofs_work *work, void *tlsp) out: cwork->errcode = ret; -pthread_mutex_lock (&z_erofs_mt_ctrl.mutex); - ++z_erofs_mt_ctrl.nfini; -pthread_cond_signal (&z_erofs_mt_ctrl.cond); -pthread_mutex_unlock (&z_erofs_mt_ctrl.mutex); +pthread_mutex_lock (&ictx->mutex); + ++ictx->nfini; +pthread_cond_signal (&ictx->cond); +pthread_mutex_unlock (&ictx->mutex); } int z_erofs_merge_segment(struct z_erofs_compress_ictx *ictx,
@@ -1268,16 +1275,19 @@ int z_erofs_merge_segment(struct z_erofs_compress_ictx *ictx, }int z_erofs_mt_compress (struct z_erofs_compress_ictx *ictx, -struct erofs_compress_cfg *ccfg,erofs_blk_t blkaddr,erofs_blk_t *compressed_blocks) {struct erofs_compress_work *cur, *head =NULL , **last = &head; +struct erofs_compress_cfg *ccfg = ictx->ccfg;struct erofs_inode *inode = ictx->inode;int nsegs =DIV_ROUND_UP (inode->i_size, cfg.c_segment_size);int ret, i; - z_erofs_mt_ctrl.nfini =0 ; + ictx->seg_num = nsegs; + ictx->nfini =0 ; +pthread_mutex_init (&ictx->mutex,NULL ); +pthread_cond_init (&ictx->cond,NULL );for (i =0 ; i < nsegs; i++) {if (z_erofs_mt_ctrl.idle) {
@@ -1294,7 +1304,6 @@ int z_erofs_mt_compress(struct z_erofs_compress_ictx *ictx, cur->ctx = (struct z_erofs_compress_sctx ) { .ictx = ictx, - .seg_num = nsegs, .seg_idx = i, .pivot = &dummy_pivot, };
@@ -1316,11 +1325,10 @@ int z_erofs_mt_compress(struct z_erofs_compress_ictx *ictx,erofs_queue_work (&z_erofs_mt_ctrl.wq, &cur->work); } -pthread_mutex_lock (&z_erofs_mt_ctrl.mutex); -while (z_erofs_mt_ctrl.nfini != nsegs) -pthread_cond_wait (&z_erofs_mt_ctrl.cond, - &z_erofs_mt_ctrl.mutex); -pthread_mutex_unlock (&z_erofs_mt_ctrl.mutex); +pthread_mutex_lock (&ictx->mutex); +while (ictx->nfini < ictx->seg_num) +pthread_cond_wait (&ictx->cond, &ictx->mutex); +pthread_mutex_unlock (&ictx->mutex); ret =0 ;while (head) {
@@ -1354,7 +1362,6 @@ int erofs_write_compressed_file(struct erofs_inode *inode, int fd, u64 fpos)struct erofs_buffer_head *bh;static struct z_erofs_compress_ictx ctx;static struct z_erofs_compress_sctx sctx; -struct erofs_compress_cfg *ccfg;erofs_blk_t blkaddr, compressed_blocks =0 ;int ret;bool ismt = false;
@@ -1389,8 +1396,8 @@ int erofs_write_compressed_file(struct erofs_inode *inode, int fd, u64 fpos) } }#endif - ccfg = &erofs_ccfg[inode->z_algorithmtype[0 ]]; - inode->z_algorithmtype[0 ] = ccfg[0 ].algorithmtype; + ctx.ccfg = &erofs_ccfg[inode->z_algorithmtype[0 ]]; + inode->z_algorithmtype[0 ] = ctx.ccfg->algorithmtype; inode->z_algorithmtype[1 ] =0 ; inode->idata_size =0 ;
@@ -1429,16 +1436,16 @@ int erofs_write_compressed_file(struct erofs_inode *inode, int fd, u64 fpos) #ifdef EROFS_MT_ENABLED }else if (z_erofs_mt_enabled && inode->i_size > cfg.c_segment_size) { ismt = true; - ret =z_erofs_mt_compress (&ctx, ccfg, blkaddr, &compressed_blocks); + ret =z_erofs_mt_compress (&ctx, blkaddr, &compressed_blocks);if (ret) goto err_free_idata;#endif }else { + ctx.seg_num =1 ; sctx.queue = g_queue; sctx.destbuf =NULL ; - sctx.chandle = &ccfg->handle; + sctx.chandle = &ctx.ccfg->handle; sctx.remaining = inode->i_size - inode->fragment_size; - sctx.seg_num =1 ; sctx.seg_idx =0 ; sctx.pivot = &dummy_pivot; sctx.pclustersize =z_erofs_get_max_pclustersize (inode);
@@ -1628,8 +1635,6 @@ int z_erofs_compress_init(struct erofs_sb_info *sbi, struct erofs_buffer_head *s z_erofs_mt_enabled = false;#ifdef EROFS_MT_ENABLED if (cfg.c_mt_workers >1 ) { -pthread_mutex_init (&z_erofs_mt_ctrl.mutex,NULL ); -pthread_cond_init (&z_erofs_mt_ctrl.cond,NULL ); ret = erofs_alloc_workqueue(&z_erofs_mt_ctrl.wq, cfg.c_mt_workers, cfg.c_mt_workers << 2,
Signature · 1 line
2.30.2
PATCH v2 5/8 erofs-utils: lib: split up z_erofs_mt_compress()
In reply to Gao XiangFrom: Gao Xiang <[email protected]>
The on-disk compressed data write will be moved into a new function erofs_mt_write_compressed_file().
--- lib/compress.c | 162 ++++++++++++++++++++++++++++--------------------- 1 file changed, 93 insertions(+), 69 deletions(-)
lib/compress.c +93 -69
@@ -57,6 +57,8 @@ struct z_erofs_compress_ictx { /* inode context */pthread_mutex_t mutex;pthread_cond_t cond;int nfini; + +struct erofs_compress_work *mtworks;#endif };
@@ -530,11 +532,11 @@ static int __z_erofs_compress_one(struct z_erofs_compress_sctx *ctx,if (len <= ctx->pclustersize) {if (!final || !len)return 1 ; +if (inode->fragment_size && !ictx->fix_dedupedfrag) { + ctx->pclustersize =roundup (len, blksz); + goto fix_dedupedfrag; + }if (may_packing) { -if (inode->fragment_size && !ictx->fix_dedupedfrag) { - ctx->pclustersize =roundup (len, blksz); - goto fix_dedupedfrag; - } e->length = len; goto frag_packing; }
@@ -1034,6 +1036,26 @@ int z_erofs_compress_segment(struct z_erofs_compress_sctx *ctx,z_erofs_commit_extent (ctx, ctx->pivot); ctx->pivot =NULL ; } + +/* generate an extra extent for the deduplicated fragment */ +if (ctx->seg_idx >= ictx->seg_num -1 && + ictx->inode->fragment_size && !ictx->fragemitted) { +struct z_erofs_extent_item *ei; + + ei =malloc (sizeof (*ei)); +if (!ei) +return -ENOMEM ; + + ei->e = (struct z_erofs_inmem_extent ) { + .length = ictx->inode->fragment_size, + .compressedblks =0 , + .raw = false, + .partial = false, + .blkaddr = ctx->blkaddr, + }; +init_list_head (&ei->list); +z_erofs_commit_extent (ctx, ei); + }return 0 ; }
@@ -1048,6 +1070,8 @@ int erofs_commit_compressed_file(struct z_erofs_compress_ictx *ictx,u8 *compressmeta;int ret; +z_erofs_fragments_commit (inode); +/* fall back to no compression mode */ DBG_BUGON (compressed_blocks < !!inode->idata_size); compressed_blocks -= !!inode->idata_size;
@@ -1125,11 +1149,11 @@ err_free_meta:free (compressmeta); inode->compressmeta =NULL ; err_free_idata: +erofs_bdrop (bh, true);/* revoke buffer */ if (inode->idata) {free (inode->idata); inode->idata =NULL ; } -erofs_bdrop (bh, true);/* revoke buffer */ return ret; }
@@ -1260,7 +1284,7 @@ int z_erofs_merge_segment(struct z_erofs_compress_ictx *ictx, sctx->blkaddr += ei->e.compressedblks;/* skip write data but leave blkaddr for inline fallback */ -if (ei->e.inlined) +if (ei->e.inlined || !ei->e.compressedblks)continue ; ret2 =blk_write (sbi, sctx->membuf + blkoff *erofs_blksiz (sbi), ei->e.blkaddr, ei->e.compressedblks);
@@ -1274,15 +1298,13 @@ int z_erofs_merge_segment(struct z_erofs_compress_ictx *ictx,return ret; } -int z_erofs_mt_compress (struct z_erofs_compress_ictx *ictx, -erofs_blk_t blkaddr, -erofs_blk_t *compressed_blocks) +int z_erofs_mt_compress (struct z_erofs_compress_ictx *ictx) {struct erofs_compress_work *cur, *head =NULL , **last = &head;struct erofs_compress_cfg *ccfg = ictx->ccfg;struct erofs_inode *inode = ictx->inode;int nsegs =DIV_ROUND_UP (inode->i_size, cfg.c_segment_size); -int ret, i; +int i; ictx->seg_num = nsegs; ictx->nfini =0 ;
@@ -1290,11 +1312,12 @@ int z_erofs_mt_compress(struct z_erofs_compress_ictx *ictx,pthread_cond_init (&ictx->cond,NULL );for (i =0 ; i < nsegs; i++) { -if (z_erofs_mt_ctrl.idle) { - cur = z_erofs_mt_ctrl.idle; + cur = z_erofs_mt_ctrl.idle; +if (cur) { z_erofs_mt_ctrl.idle = cur->next; cur->next =NULL ; - }else { + } +if (!cur) { cur =calloc (1 ,sizeof (*cur));if (!cur)return -ENOMEM ;
@@ -1324,14 +1347,31 @@ int z_erofs_mt_compress(struct z_erofs_compress_ictx *ictx, cur->work.fn = z_erofs_mt_workfn;erofs_queue_work (&z_erofs_mt_ctrl.wq, &cur->work); } + ictx->mtworks = head; +return 0 ; +} + +int erofs_mt_write_compressed_file (struct z_erofs_compress_ictx *ictx) +{ +struct erofs_buffer_head *bh =NULL ; +struct erofs_compress_work *head = ictx->mtworks, *cur; +erofs_blk_t blkaddr, compressed_blocks =0 ; +int ret;pthread_mutex_lock (&ictx->mutex);while (ictx->nfini < ictx->seg_num)pthread_cond_wait (&ictx->cond, &ictx->mutex);pthread_mutex_unlock (&ictx->mutex); + bh =erofs_balloc (DATA ,0 ,0 ,0 ); +if (IS_ERR (bh)) +return PTR_ERR (bh); + +DBG_BUGON (!head); + blkaddr =erofs_mapbh (bh->block); + ret =0 ; -while (head) { + do { cur = head; head = cur->next;
@@ -1345,14 +1385,19 @@ int z_erofs_mt_compress(struct z_erofs_compress_ictx *ictx,if (ret2) ret = ret2; - *compressed_blocks += cur->ctx.blkaddr - blkaddr; + compressed_blocks += cur->ctx.blkaddr - blkaddr; blkaddr = cur->ctx.blkaddr; } cur->next = z_erofs_mt_ctrl.idle; z_erofs_mt_ctrl.idle = cur; - } -return ret; + }while (head); + +if (ret) +return ret; + +return erofs_commit_compressed_file (ictx, bh, + blkaddr - compressed_blocks, compressed_blocks); }#endif
@@ -1362,9 +1407,8 @@ int erofs_write_compressed_file(struct erofs_inode *inode, int fd, u64 fpos)struct erofs_buffer_head *bh;static struct z_erofs_compress_ictx ctx;static struct z_erofs_compress_sctx sctx; -erofs_blk_t blkaddr, compressed_blocks =0 ; +erofs_blk_t blkaddr;int ret; -bool ismt = false;struct erofs_sb_info *sbi = inode->sbi;/* initialize per-file compression setting */
@@ -1419,14 +1463,6 @@ int erofs_write_compressed_file(struct erofs_inode *inode, int fd, u64 fpos)init_list_head (&ctx.extents); ctx.fix_dedupedfrag = false; ctx.fragemitted = false; - sctx = (struct z_erofs_compress_sctx ) { .ictx = &ctx, }; -init_list_head (&sctx.extents); - -/* allocate main data buffer */ - bh =erofs_balloc (DATA ,0 ,0 ,0 ); -if (IS_ERR (bh)) -return PTR_ERR (bh); - blkaddr =erofs_mapbh (bh->block);/* start_blkaddr */ if (cfg.c_all_fragments && !erofs_is_packed_inode (inode) && !inode->fragment_size) {
@@ -1434,60 +1470,48 @@ int erofs_write_compressed_file(struct erofs_inode *inode, int fd, u64 fpos)if (ret) goto err_free_idata; #ifdef EROFS_MT_ENABLED - }else if (z_erofs_mt_enabled && inode->i_size > cfg.c_segment_size) { - ismt = true; - ret =z_erofs_mt_compress (&ctx, blkaddr, &compressed_blocks); + }else if (z_erofs_mt_enabled) { + ret =z_erofs_mt_compress (&ctx);if (ret) goto err_free_idata; +return erofs_mt_write_compressed_file (&ctx);#endif - }else { - ctx.seg_num =1 ; - sctx.queue = g_queue; - sctx.destbuf =NULL ; - sctx.chandle = &ctx.ccfg->handle; - sctx.remaining = inode->i_size - inode->fragment_size; - sctx.seg_idx =0 ; - sctx.pivot = &dummy_pivot; - sctx.pclustersize =z_erofs_get_max_pclustersize (inode); - - ret =z_erofs_compress_segment (&sctx,-1 , blkaddr); -if (ret) - goto err_free_idata; - compressed_blocks = sctx.blkaddr - blkaddr; } - -/* generate an extent for the deduplicated fragment */ -if (inode->fragment_size && !ctx.fragemitted) { -struct z_erofs_extent_item *ei; - - ei =malloc (sizeof (*ei)); -if (!ei) { - ret = -ENOMEM ; - goto err_free_idata; - } - - ei->e = (struct z_erofs_inmem_extent ) { - .length = inode->fragment_size, - .compressedblks =0 , - .raw = false, - .partial = false, - .blkaddr = sctx.blkaddr, - }; -init_list_head (&ei->list); -z_erofs_commit_extent (&sctx, ei); +/* allocate main data buffer */ + bh =erofs_balloc (DATA ,0 ,0 ,0 ); +if (IS_ERR (bh)) { + ret =PTR_ERR (bh); + goto err_free_idata; } -z_erofs_fragments_commit (inode); -if (!ismt) -list_splice_tail (&sctx.extents, &ctx.extents); + blkaddr =erofs_mapbh (bh->block);/* start_blkaddr */ + + ctx.seg_num =1 ; + sctx = (struct z_erofs_compress_sctx ) { + .ictx = &ctx, + .queue = g_queue, + .chandle = &ctx.ccfg->handle, + .remaining = inode->i_size - inode->fragment_size, + .seg_idx =0 , + .pivot = &dummy_pivot, + .pclustersize =z_erofs_get_max_pclustersize (inode), + }; +init_list_head (&sctx.extents); + + ret =z_erofs_compress_segment (&sctx,-1 , blkaddr); +if (ret) + goto err_bdrop; +list_splice_tail (&sctx.extents, &ctx.extents);return erofs_commit_compressed_file (&ctx, bh, blkaddr, - compressed_blocks); + sctx.blkaddr - blkaddr); + +err_bdrop: +erofs_bdrop (bh, true);/* revoke buffer */ err_free_idata:if (inode->idata) {free (inode->idata); inode->idata =NULL ; } -erofs_bdrop (bh, true);/* revoke buffer */ return ret; }
Signature · 1 line
2.30.2
PATCH v2 6/8 erofs-utils: mkfs: prepare inter-file multi-threaded compression
In reply to Gao XiangFrom: Yifan Zhao <[email protected]>
This patch separates the compression process into two parts.
Specifically, erofs_begin_compressed_file() will trigger compression. erofs_write_compressed_file() will wait for the compression finish and write compressed (meta)data.
Note that it's possible that erofs_begin_compressed_file() and erofs_write_compressed_file() run with different threads even the global inode context is used, thus add another synchronization point.
--- include/erofs/compress.h | 5 +- lib/compress.c | 138 ++++++++++++++++++++++++++++----------- lib/inode.c | 17 ++++- 3 files changed, 118 insertions(+), 42 deletions(-)
include/erofs/compress.h +4 -1
@@ -17,8 +17,11 @@ extern "C"#define EROFS_CONFIG_COMPR_MAX_SZ (4000 * 1024)#define Z_EROFS_COMPR_QUEUE_SZ (EROFS_CONFIG_COMPR_MAX_SZ * 2) +struct z_erofs_compress_ictx ; +void z_erofs_drop_inline_pcluster (struct erofs_inode *inode); -int erofs_write_compressed_file (struct erofs_inode *inode,int fd,u64 fpos); +void *erofs_begin_compressed_file (struct erofs_inode *inode,int fd,u64 fpos); +int erofs_write_compressed_file (struct z_erofs_compress_ictx *ictx);int z_erofs_compress_init (struct erofs_sb_info *sbi,struct erofs_buffer_head *bh);
lib/compress.c +100 -38
231 lines not on this page · raw message
lib/inode.c +14 -3
@@ -499,10 +499,15 @@ int erofs_write_file(struct erofs_inode *inode, int fd, u64 fpos)DBG_BUGON (!inode->i_size);if (cfg.c_compr_opts[0 ].alg &&erofs_file_is_compressible (inode)) { +void *ictx;int ret; - ret =erofs_write_compressed_file (inode, fd, fpos); -if (!ret || ret != -ENOSPC ) + ictx =erofs_begin_compressed_file (inode, fd, fpos); +if (IS_ERR (ictx)) +return PTR_ERR (ictx); + + ret =erofs_write_compressed_file (ictx); +if (ret != -ENOSPC )return ret;if (lseek (fd, fpos,SEEK_SET ) <0 )
@@ -1362,6 +1367,7 @@ struct erofs_inode *erofs_mkfs_build_special_from_fd(int fd, const char *name) {struct stat st;struct erofs_inode *inode; +void *ictx;int ret; ret =lseek (fd,0 ,SEEK_SET );
@@ -1392,7 +1398,12 @@ struct erofs_inode *erofs_mkfs_build_special_from_fd(int fd, const char *name) inode->nid = inode->sbi->packed_nid; } - ret =erofs_write_compressed_file (inode, fd,0 ); + ictx =erofs_begin_compressed_file (inode, fd,0 ); +if (IS_ERR (ictx)) +return ERR_CAST (ictx); + +DBG_BUGON (!ictx); + ret =erofs_write_compressed_file (ictx);if (ret == -ENOSPC ) { ret =lseek (fd,0 ,SEEK_SET );if (ret <0 )
Signature · 1 line
2.30.2
PATCH v2 7/8 erofs-utils: lib: introduce non-directory jobitem context
In reply to Gao XiangFrom: Gao Xiang <[email protected]>
It will describe EROFS_MKFS_JOB_NDIR defer work. Also, start compression before queueing EROFS_MKFS_JOB_NDIR.
--- lib/inode.c | 62 +++++++++++++++++++++++++++++++++++++++++++---------- 1 file changed, 51 insertions(+), 11 deletions(-)
lib/inode.c +51 -11
@@ -1107,8 +1107,36 @@ static void erofs_fixup_meta_blkaddr(struct erofs_inode *rootdir) rootdir->nid = (off - meta_offset) >>EROFS_ISLOTBITS ; } -static int erofs_mkfs_handle_nondirectory (struct erofs_inode *inode) +struct erofs_mkfs_job_ndir_ctx { +struct erofs_inode *inode; +void *ictx; +int fd; +}; + +static int erofs_mkfs_job_write_file (struct erofs_mkfs_job_ndir_ctx *ctx) { +struct erofs_inode *inode = ctx->inode; +int ret; + +if (ctx->ictx) { + ret =erofs_write_compressed_file (ctx->ictx); +if (ret != -ENOSPC ) + goto out; +if (lseek (ctx->fd,0 ,SEEK_SET ) <0 ) { + ret = -errno; + goto out; + } + } +/* fallback to all data uncompressed */ + ret =erofs_write_unencoded_file (inode, ctx->fd,0 ); +out: +close (ctx->fd); +return ret; +} + +static int erofs_mkfs_handle_nondirectory (struct erofs_mkfs_job_ndir_ctx *ctx) +{ +struct erofs_inode *inode = ctx->inode;int ret =0 ;if (S_ISLNK (inode->i_mode)) {
@@ -1124,12 +1152,7 @@ static int erofs_mkfs_handle_nondirectory(struct erofs_inode *inode) ret =erofs_write_file_from_buffer (inode, symlink);free (symlink); }else if (inode->i_size) { -int fd =open (inode->i_srcpath,O_RDONLY |O_BINARY ); - -if (fd <0 ) -return -errno; - ret =erofs_write_file (inode, fd,0 ); -close (fd); + ret =erofs_mkfs_job_write_file (ctx); }if (ret)return ret;
@@ -1148,6 +1171,7 @@ struct erofs_mkfs_jobitem {enum erofs_mkfs_jobtype type;union {struct erofs_inode *inode; +struct erofs_mkfs_job_ndir_ctx ndir; } u; };
@@ -1157,7 +1181,7 @@ static int erofs_mkfs_jobfn(struct erofs_mkfs_jobitem *item)int ret;if (item->type ==EROFS_MKFS_JOB_NDIR ) -return erofs_mkfs_handle_nondirectory (inode); +return erofs_mkfs_handle_nondirectory (&item->u.ndir);if (item->type ==EROFS_MKFS_JOB_DIR ) { ret =erofs_prepare_inode_buffer (inode);
@@ -1294,11 +1318,27 @@ static int erofs_mkfs_handle_inode(struct erofs_inode *inode)if (ret <0 )return ret; -if (!S_ISDIR (inode->i_mode)) +if (!S_ISDIR (inode->i_mode)) { +struct erofs_mkfs_job_ndir_ctx ctx = { .inode = inode }; + +if (!S_ISLNK (inode->i_mode) && inode->i_size) { + ctx.fd =open (inode->i_srcpath,O_RDONLY |O_BINARY ); +if (ctx.fd <0 ) +return -errno; + +if (cfg.c_compr_opts[0 ].alg && +erofs_file_is_compressible (inode)) { + ctx.ictx =erofs_begin_compressed_file (inode, + ctx.fd,0 ); +if (IS_ERR (ctx.ictx)) +return PTR_ERR (ctx.ictx); + } + } ret =erofs_mkfs_go (inode->sbi,EROFS_MKFS_JOB_NDIR , - &inode,sizeof (inode)); -else + &ctx,sizeof (ctx)); + }else { ret =erofs_mkfs_handle_directory (inode); + }erofs_info ("file %s dumped (mode %05o)" ,erofs_fspath (inode->i_srcpath), inode->i_mode);return ret;
Signature · 1 line
2.30.2
PATCH v2 8/8 erofs-utils: mkfs: enable inter-file multi-threaded compression
In reply to Gao XiangFrom: Gao Xiang <[email protected]>
Dispatch deferred ops in another per-sb worker thread. Note that deferred ops are strictly FIFOed.
--- include/erofs/internal.h | 6 ++ lib/inode.c | 119 ++++++++++++++++++++++++++++++++++++++- 2 files changed, 123 insertions(+), 2 deletions(-)
include/erofs/internal.h +6 -0
@@ -71,6 +71,7 @@ struct erofs_xattr_prefix_item {#define EROFS_PACKED_NID_UNALLOCATED -1 +struct erofs_mkfs_dfops ;struct erofs_sb_info {struct erofs_device_info *devs;char *devname;
@@ -124,6 +125,11 @@ struct erofs_sb_info {struct list_head list;u64 saved_by_deduplication; + +#ifdef EROFS_MT_ENABLED +pthread_t dfops_worker; +struct erofs_mkfs_dfops *mkfs_dfops; +#endif };/* make sure that any user of the erofs headers has atleast 64bit off_t type */
lib/inode.c +117 -2
152 lines not on this page · raw message
Signature · 1 line
2.30.2