Skip to contentSkip to conversation

erofs-utils: allocate `struct erofs_inode` with zeroed memory

PATCH New Gao Xiang 1 participant 2 messages Apr 5, 2025

Subsystems (2)
lore
Share & tools
lore

Gao Xiang

author
#0 lore raw

PATCH 1/2 erofs-utils: allocate `struct erofs_inode` with zeroed memory

The kernel implementation has the same constraint, so the code can be shared.

Fixes 9fd2a2250fa9 ("erofs-utils: fuse: switch to FUSE 2/3 lowlevel APIs")
Fixes f44043561491 ("erofs-utils: introduce fsck.erofs")
---
 fsck/main.c | 5 +----
 fuse/main.c | 6 +++---
 2 files changed, 4 insertions(+), 7 deletions(-)
fsck/main.c +1 -4
index 0e8b944..f7e33c0 100644
@@ -974,12 +974,9 @@ verify:
 static int erofsfsck_check_inode(erofs_nid_t pnid, erofs_nid_t nid)
 {
 	int ret, i;
-	struct erofs_inode inode;
+	struct erofs_inode inode = {.sbi = &g_sbi, .nid = nid};
 
 	erofs_dbg("check inode: nid(%llu)", nid | 0ULL);
-
-	inode.nid = nid;
-	inode.sbi = &g_sbi;
 	ret = erofs_read_inode_from_disk(&inode);
 	if (ret) {
 		if (ret == -EIO)
fuse/main.c +3 -3
index cb2759e..db4f323 100644
@@ -231,7 +231,7 @@ static void erofsfuse_open(fuse_req_t req, fuse_ino_t ino,
 		return;
 	}
 
-	vi = (struct erofs_inode *)malloc(sizeof(struct erofs_inode));
+	vi = calloc(1, sizeof(struct erofs_inode));
 	if (!vi) {
 		fuse_reply_err(req, ENOMEM);
 		return;
@@ -281,7 +281,7 @@ static void erofsfuse_opendir(fuse_req_t req, fuse_ino_t ino,
 	int ret;
 	struct erofs_inode *vi;
 
-	vi = (struct erofs_inode *)malloc(sizeof(struct erofs_inode));
+	vi = calloc(1, sizeof(struct erofs_inode));
 	if (!vi) {
 		fuse_reply_err(req, ENOMEM);
 		return;
@@ -324,7 +324,7 @@ static void erofsfuse_lookup(fuse_req_t req, fuse_ino_t parent,
 	struct fuse_entry_param fentry = { 0 };
 	struct erofsfuse_lookupdir_context ctx = { 0 };
 
-	vi = (struct erofs_inode *)malloc(sizeof(struct erofs_inode));
+	vi = calloc(1, sizeof(struct erofs_inode));
 	if (!vi) {
 		fuse_reply_err(req, ENOMEM);
 		return;
Signature · 1 line
2.43.5
author
#1 lore raw

PATCH 2/2 erofs-utils: lib: use atomic operations for `vi->flags`

Since vi->flags can be accessed by multiple threads.

---
 include/erofs/atomic.h   | 5 +++++
 include/erofs/internal.h | 9 ++++++---
 lib/xattr.c              | 6 ++----
 lib/zmap.c               | 4 ++--
 4 files changed, 15 insertions(+), 9 deletions(-)
include/erofs/atomic.h +5 -0
index f28687e..142590b 100644
@@ -22,6 +22,11 @@ __n;})
 	__atomic_store(ptr, &__n, __ATOMIC_RELAXED); \
 } while(0)
 
+#define erofs_atomic_set_bit(bit, ptr) do { \
+	typeof(*ptr) __n = (1 << bit);    \
+	__atomic_or_fetch(ptr, __n, __ATOMIC_ACQ_REL); \
+} while(0)
+
 #define erofs_atomic_test_and_set(ptr) \
 	__atomic_test_and_set(ptr, __ATOMIC_RELAXED)
 
include/erofs/internal.h +6 -3
index 2f71557..90bee07 100644
@@ -176,8 +176,11 @@ EROFS_FEATURE_FUNCS(xattr_prefixes, incompat, INCOMPAT_XATTR_PREFIXES)
 EROFS_FEATURE_FUNCS(sb_chksum, compat, COMPAT_SB_CHKSUM)
 EROFS_FEATURE_FUNCS(xattr_filter, compat, COMPAT_XATTR_FILTER)
 
-#define EROFS_I_EA_INITED	(1 << 0)
-#define EROFS_I_Z_INITED	(1 << 1)
+#define EROFS_I_EA_INITED_BIT	0
+#define EROFS_I_Z_INITED_BIT	1
+
+#define EROFS_I_EA_INITED	(1 << EROFS_I_EA_INITED_BIT)
+#define EROFS_I_Z_INITED	(1 << EROFS_I_Z_INITED_BIT)
 
 struct erofs_diskbuf;
 
@@ -191,7 +194,7 @@ struct erofs_inode {
 
 	union {
 		/* (erofsfuse) runtime flags */
-		unsigned int flags;
+		erofs_atomic_t flags;
 
 		/* (mkfs.erofs) next pointer for directory dumping */
 		struct erofs_inode *next_dirwrite;
lib/xattr.c +2 -4
index 976df14..68a96cc 100644
@@ -1056,7 +1056,7 @@ static int init_inode_xattrs(struct erofs_inode *vi)
 	int ret = 0;
 
 	/* the most case is that xattrs of this inode are initialized. */
-	if (vi->flags & EROFS_I_EA_INITED)
+	if (erofs_atomic_read(&vi->flags) & EROFS_I_EA_INITED)
 		return ret;
 
 	/*
@@ -1117,9 +1117,7 @@ static int init_inode_xattrs(struct erofs_inode *vi)
 			le32_to_cpu(*(__le32 *)(it.kaddr + it.ofs));
 		it.ofs += sizeof(__le32);
 	}
-
-	vi->flags |= EROFS_I_EA_INITED;
-
+	erofs_atomic_set_bit(EROFS_I_EA_INITED_BIT, &vi->flags);
 	return ret;
 }
 
lib/zmap.c +2 -2
index 70ff898..2a9baba 100644
@@ -557,7 +557,7 @@ static int z_erofs_fill_inode_lazy(struct erofs_inode *vi)
 	struct erofs_sb_info *sbi = vi->sbi;
 	int err, headnr;
 
-	if (vi->flags & EROFS_I_Z_INITED)
+	if (erofs_atomic_read(&vi->flags) & EROFS_I_Z_INITED)
 		return 0;
 
 	pos = round_up(erofs_iloc(vi) + vi->inode_isize + vi->xattr_isize, 8);
@@ -624,7 +624,7 @@ static int z_erofs_fill_inode_lazy(struct erofs_inode *vi)
 			return err;
 	}
 out:
-	vi->flags |= EROFS_I_Z_INITED;
+	erofs_atomic_set_bit(EROFS_I_Z_INITED_BIT, &vi->flags);
 	return 0;
 }
 
Signature · 1 line
2.43.5