From: Gao Xiang <hsiang...@linux.alibaba.com> Since `inode->i_count` can be touched for more than one thread if multi-threading is enabled.
Signed-off-by: Gao Xiang <hsiang...@linux.alibaba.com> --- 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(-) diff --git a/include/erofs/atomic.h b/include/erofs/atomic.h index 214cdb1..f28687e 100644 --- a/include/erofs/atomic.h +++ b/include/erofs/atomic.h @@ -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 diff --git a/include/erofs/inode.h b/include/erofs/inode.h index d5a732a..5d6bc98 100644 --- a/include/erofs/inode.h +++ b/include/erofs/inode.h @@ -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; } diff --git a/include/erofs/internal.h b/include/erofs/internal.h index 4cd2059..f31e548 100644 --- a/include/erofs/internal.h +++ b/include/erofs/internal.h @@ -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; diff --git a/lib/inode.c b/lib/inode.c index 7508c74..55969d9 100644 --- a/lib/inode.c +++ b/lib/inode.c @@ -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); -- 2.30.2