Skip to contentSkip to conversation

erofs-utils: lib: add list_splice() and list_del_init()

PATCH In review Jingbo Xu 2 participants 14 messages Jul 27, 2023 Library code

lore
Share & tools
lore

Jingbo Xu

author
#0 lore raw

PATCH 1/9 erofs-utils: lib: add list_splice() and list_del_init()

Add three list helpers:

list_del_init
list_splice
list_splice_tail

---
 include/erofs/list.h | 31 +++++++++++++++++++++++++++++++
 1 file changed, 31 insertions(+)
include/erofs/list.h +31 -0
index 3f5da1a..7895ff0 100644
@@ -65,11 +65,42 @@ static inline void list_del(struct list_head *entry)
 	entry->prev = entry->next = NULL;
 }
 
+static inline void list_del_init(struct list_head *entry)
+{
+	__list_del(entry->prev, entry->next);
+	init_list_head(entry);
+}
+
 static inline int list_empty(struct list_head *head)
 {
 	return head->next == head;
 }
 
+static inline void __list_splice(struct list_head *list,
+		struct list_head *prev, struct list_head *next)
+{
+	struct list_head *first = list->next;
+	struct list_head *last = list->prev;
+
+	first->prev = prev;
+	prev->next = first;
+
+	last->next = next;
+	next->prev = last;
+}
+
+static inline void list_splice(struct list_head *list, struct list_head *head)
+{
+	if (!list_empty(list))
+		__list_splice(list, head, head->next);
+}
+
+static inline void list_splice_tail(struct list_head *list, struct list_head *head)
+{
+	if (!list_empty(list))
+		__list_splice(list, head->prev, head);
+}
+
 #define list_entry(ptr, type, member) container_of(ptr, type, member)
 
 #define list_first_entry(ptr, type, member)                                    \
Signature · 1 line
2.19.1.6.gb485710b
author
#1 lore raw

PATCH 2/9 erofs-utils: initialize i_nlink to 2 in erofs_init_empty_dir()

Set dir->i_nlink to 2 as "." and ".." are allocated.

tarerofs_init_empty_dir() is removed then.

---
 lib/inode.c |  2 ++
 lib/tar.c   | 14 ++------------
 mkfs/main.c |  2 +-
 3 files changed, 5 insertions(+), 13 deletions(-)
lib/inode.c +2 -0
index c4d1476..d54f84f 100644
@@ -235,6 +235,8 @@ int erofs_init_empty_dir(struct erofs_inode *dir)
 		return PTR_ERR(d);
 	d->inode = erofs_igrab(dir->i_parent);
 	d->type = EROFS_FT_DIR;
+
+	dir->i_nlink = 2;
 	return 0;
 }
 
lib/tar.c +2 -12
index 76ba69d..3c145e5 100644
@@ -138,16 +138,6 @@ static long long tarerofs_parsenum(const char *ptr, int len)
 	return tarerofs_otoi(ptr, len);
 }
 
-int tarerofs_init_empty_dir(struct erofs_inode *inode)
-{
-	int ret = erofs_init_empty_dir(inode);
-
-	if (ret)
-		return ret;
-	inode->i_nlink = 2;
-	return 0;
-}
-
 static struct erofs_dentry *tarerofs_mkdir(struct erofs_inode *dir, const char *s)
 {
 	struct erofs_inode *inode;
@@ -163,7 +153,7 @@ static struct erofs_dentry *tarerofs_mkdir(struct erofs_inode *dir, const char *
 	inode->i_gid = getgid();
 	inode->i_mtime = inode->sbi->build_time;
 	inode->i_mtime_nsec = inode->sbi->build_time_nsec;
-	tarerofs_init_empty_dir(inode);
+	erofs_init_empty_dir(inode);
 
 	d = erofs_d_alloc(dir, s);
 	if (!IS_ERR(d)) {
@@ -759,7 +749,7 @@ new_inode:
 		inode->i_nlink++;
 		ret = 0;
 	} else if (!inode->i_nlink)
-		ret = tarerofs_init_empty_dir(inode);
+		ret = erofs_init_empty_dir(inode);
 	else
 		ret = 0;
 out:
mkfs/main.c +1 -1
index 92a07fd..bc5ed87 100644
@@ -928,7 +928,7 @@ int main(int argc, char **argv)
 		root_inode->i_parent = root_inode;
 		root_inode->i_mtime = sbi.build_time;
 		root_inode->i_mtime_nsec = sbi.build_time_nsec;
-		tarerofs_init_empty_dir(root_inode);
+		erofs_init_empty_dir(root_inode);
 
 		while (!(err = tarerofs_parse_tar(root_inode, &erofstar)));
 
Signature · 1 line
2.19.1.6.gb485710b
author
#3 lore raw

PATCH 3/9 erofs-utils: read i_ino in erofs_read_inode_from_disk()

In reply to Jingbo Xu

Read on-disk ino and store it in i_ino[0].

---
 lib/namei.c | 2 ++
 1 file changed, 2 insertions(+)
lib/namei.c +2 -0
index 1023a9a..2bb1d4c 100644
@@ -60,6 +60,7 @@ int erofs_read_inode_from_disk(struct erofs_inode *vi)
 		die = (struct erofs_inode_extended *)buf;
 		vi->xattr_isize = erofs_xattr_ibody_size(die->i_xattr_icount);
 		vi->i_mode = le16_to_cpu(die->i_mode);
+		vi->i_ino[0] = le32_to_cpu(die->i_ino);
 
 		switch (vi->i_mode & S_IFMT) {
 		case S_IFREG:
@@ -95,6 +96,7 @@ int erofs_read_inode_from_disk(struct erofs_inode *vi)
 		vi->inode_isize = sizeof(struct erofs_inode_compact);
 		vi->xattr_isize = erofs_xattr_ibody_size(dic->i_xattr_icount);
 		vi->i_mode = le16_to_cpu(dic->i_mode);
+		vi->i_ino[0] = le32_to_cpu(dic->i_ino);
 
 		switch (vi->i_mode & S_IFMT) {
 		case S_IFREG:
Signature · 1 line
2.19.1.6.gb485710b
author
#4 lore raw

PATCH 4/9 erofs-utils: add erofs_read_xattrs_from_disk() helper

In reply to Jingbo Xu

Add erofs_read_xattrs_from_disk() helper to read extended attributes from disk.

---
 include/erofs/xattr.h |  1 +
 lib/xattr.c           | 76 +++++++++++++++++++++++++++++++++++++++++++
 2 files changed, 77 insertions(+)
include/erofs/xattr.h +1 -0
index dc27cf6..634daf9 100644
@@ -85,6 +85,7 @@ int erofs_xattr_write_name_prefixes(struct erofs_sb_info *sbi, FILE *f);
 
 int erofs_setxattr(struct erofs_inode *inode, char *key,
 		   const void *value, size_t size);
+int erofs_read_xattrs_from_disk(struct erofs_inode *inode);
 
 #ifdef __cplusplus
 }
lib/xattr.c +76 -0

82 lines not on this page · raw message

Signature · 1 line
2.19.1.6.gb485710b
author
#5 lore raw

PATCH 5/9 erofs-utils: add erofs_inode_tag_opaque() helper

In reply to Jingbo Xu

Add erofs_inode_tag_opaque() helper checking if it's an opaque directory.

---
 include/erofs/internal.h |  2 ++
 include/erofs/xattr.h    | 22 ++++++++++++++++++++
 lib/tar.c                |  5 -----
 lib/xattr.c              | 44 ++++++++++++++++++++++++++++++++++++++++
 4 files changed, 68 insertions(+), 5 deletions(-)
include/erofs/internal.h +2 -0
index 04a9a69..83a2e22 100644
@@ -218,6 +218,8 @@ struct erofs_inode {
 #endif
 	erofs_off_t fragmentoff;
 	unsigned int fragment_size;
+
+	bool opaque;
 };
 
 static inline erofs_off_t erofs_iloc(struct erofs_inode *inode)
include/erofs/xattr.h +22 -0
index 634daf9..21d669b 100644
@@ -73,6 +73,27 @@ static inline unsigned int xattrblock_offset(struct erofs_inode *vi,
 #ifndef XATTR_NAME_POSIX_ACL_DEFAULT
 #define XATTR_NAME_POSIX_ACL_DEFAULT "system.posix_acl_default"
 #endif
+#ifndef OVL_XATTR_NAMESPACE
+#define OVL_XATTR_NAMESPACE "overlay."
+#endif
+#ifndef OVL_XATTR_OPAQUE_POSTFIX
+#define OVL_XATTR_OPAQUE_POSTFIX "opaque"
+#endif
+#ifndef OVL_XATTR_TRUSTED_PREFIX
+#define OVL_XATTR_TRUSTED_PREFIX XATTR_TRUSTED_PREFIX OVL_XATTR_NAMESPACE
+#endif
+#ifndef OVL_XATTR_OPAQUE_SUFFIX
+#define OVL_XATTR_OPAQUE_SUFFIX OVL_XATTR_NAMESPACE OVL_XATTR_OPAQUE_POSTFIX
+#endif
+#ifndef OVL_XATTR_OPAQUE_SUFFIX_LEN
+#define OVL_XATTR_OPAQUE_SUFFIX_LEN (sizeof(OVL_XATTR_OPAQUE_SUFFIX) - 1)
+#endif
+#ifndef OVL_XATTR_OPAQUE
+#define OVL_XATTR_OPAQUE OVL_XATTR_TRUSTED_PREFIX OVL_XATTR_OPAQUE_POSTFIX
+#endif
+#ifndef OVL_XATTR_OPAQUE_LEN
+#define OVL_XATTR_OPAQUE_LEN (sizeof(OVL_XATTR_OPAQUE) - 1)
+#endif
 
 int erofs_scan_file_xattrs(struct erofs_inode *inode);
 int erofs_prepare_xattr_ibody(struct erofs_inode *inode);
@@ -86,6 +107,7 @@ int erofs_xattr_write_name_prefixes(struct erofs_sb_info *sbi, FILE *f);
 int erofs_setxattr(struct erofs_inode *inode, char *key,
 		   const void *value, size_t size);
 int erofs_read_xattrs_from_disk(struct erofs_inode *inode);
+void erofs_inode_tag_opaque(struct erofs_inode *inode);
 
 #ifdef __cplusplus
 }
lib/tar.c +0 -5
index 3c145e5..2932980 100644
@@ -19,11 +19,6 @@
 #include "erofs/xattr.h"
 #include "erofs/blobchunk.h"
 
-#define OVL_XATTR_NAMESPACE "overlay."
-#define OVL_XATTR_TRUSTED_PREFIX XATTR_TRUSTED_PREFIX OVL_XATTR_NAMESPACE
-#define OVL_XATTR_OPAQUE_POSTFIX "opaque"
-#define OVL_XATTR_OPAQUE OVL_XATTR_TRUSTED_PREFIX OVL_XATTR_OPAQUE_POSTFIX
-
 #define EROFS_WHITEOUT_DEV	0
 
 static char erofs_libbuf[16384];
lib/xattr.c +44 -0

50 lines not on this page · raw message

Signature · 1 line
2.19.1.6.gb485710b
author
#6 lore raw

PATCH 6/9 erofs-utils: add inode hash helper

In reply to Jingbo Xu

Add erofs_insert_ihash() helper inserting inode into inode hash table, and erofs_cleanup_ihash() helper cleaning up inode hash table.

Also add prototypes of erofs_iget() and erofs_iget_by_nid() in the header file.

---
 include/erofs/inode.h |  4 ++++
 lib/inode.c           | 22 +++++++++++++++++++---
 2 files changed, 23 insertions(+), 3 deletions(-)
include/erofs/inode.h +4 -0
index e8a5670..aba2a94 100644
@@ -25,6 +25,10 @@ u32 erofs_new_encode_dev(dev_t dev);
 unsigned char erofs_mode_to_ftype(umode_t mode);
 unsigned char erofs_ftype_to_dtype(unsigned int filetype);
 void erofs_inode_manager_init(void);
+void erofs_insert_ihash(struct erofs_inode *inode, dev_t dev, ino_t ino);
+void erofs_cleanup_ihash(void);
+struct erofs_inode *erofs_iget(dev_t dev, ino_t ino);
+struct erofs_inode *erofs_iget_by_nid(erofs_nid_t nid);
 unsigned int erofs_iput(struct erofs_inode *inode);
 erofs_nid_t erofs_lookupnid(struct erofs_inode *inode);
 struct erofs_dentry *erofs_d_alloc(struct erofs_inode *parent,
lib/inode.c +19 -3
index d54f84f..d82ea95 100644
@@ -75,6 +75,24 @@ void erofs_inode_manager_init(void)
 		init_list_head(&inode_hashtable[i]);
 }
 
+void erofs_insert_ihash(struct erofs_inode *inode, dev_t dev, ino_t ino)
+{
+	list_add(&inode->i_hash,
+		 &inode_hashtable[(ino ^ dev) % NR_INODE_HASHTABLE]);
+}
+
+void erofs_cleanup_ihash(void)
+{
+	unsigned int i;
+	struct erofs_inode *inode, *n;
+
+	for (i = 0; i < NR_INODE_HASHTABLE; ++i) {
+		list_for_each_entry_safe(inode, n, &inode_hashtable[i], i_hash)
+			list_del_init(&inode->i_hash);
+		init_list_head(&inode_hashtable[i]);
+	}
+}
+
 /* get the inode from the (source) inode # */
 struct erofs_inode *erofs_iget(dev_t dev, ino_t ino)
 {
@@ -976,9 +994,7 @@ static int erofs_fill_inode(struct erofs_inode *inode, struct stat *st,
 		inode->inode_isize = sizeof(struct erofs_inode_compact);
 	}
 
-	list_add(&inode->i_hash,
-		 &inode_hashtable[(st->st_ino ^ st->st_dev) %
-				  NR_INODE_HASHTABLE]);
+	erofs_insert_ihash(inode, st->st_dev, st->st_ino);
 	return 0;
 }
 
Signature · 1 line
2.19.1.6.gb485710b
#7 lore raw

Re: [PATCH 6/9] erofs-utils: add inode hash helper

Quoted 38 lines · Jingbo Xu

Add erofs_insert_ihash() helper inserting inode into inode hash table, and erofs_cleanup_ihash() helper cleaning up inode hash table.

Also add prototypes of erofs_iget() and erofs_iget_by_nid() in the header file.

---
  include/erofs/inode.h |  4 ++++
  lib/inode.c           | 22 +++++++++++++++++++---
  2 files changed, 23 insertions(+), 3 deletions(-)
include/erofs/inode.h +4 -0
index e8a5670..aba2a94 100644
@@ -25,6 +25,10 @@ u32 erofs_new_encode_dev(dev_t dev);
  unsigned char erofs_mode_to_ftype(umode_t mode);
  unsigned char erofs_ftype_to_dtype(unsigned int filetype);
  void erofs_inode_manager_init(void);
+void erofs_insert_ihash(struct erofs_inode *inode, dev_t dev, ino_t ino);
+void erofs_cleanup_ihash(void);
+struct erofs_inode *erofs_iget(dev_t dev, ino_t ino);
+struct erofs_inode *erofs_iget_by_nid(erofs_nid_t nid);
  unsigned int erofs_iput(struct erofs_inode *inode);
  erofs_nid_t erofs_lookupnid(struct erofs_inode *inode);
  struct erofs_dentry *erofs_d_alloc(struct erofs_inode *parent,
lib/inode.c +3 -0
index d54f84f..d82ea95 100644
@@ -75,6 +75,24 @@ void erofs_inode_manager_init(void)
  		init_list_head(&inode_hashtable[i]);
  }
 
+void erofs_insert_ihash(struct erofs_inode *inode, dev_t dev, ino_t ino)
+{
+	list_add(&inode->i_hash,

lib/inode.c void erofs_inode_manager_init(void) · line 90

Quoted 10 lines
lib/inode.c +10 -0
+		 &inode_hashtable[(ino ^ dev) % NR_INODE_HASHTABLE]);
+}
+
+void erofs_cleanup_ihash(void)
+{
+	unsigned int i;
+	struct erofs_inode *inode, *n;
+
+	for (i = 0; i < NR_INODE_HASHTABLE; ++i) {
+		list_for_each_entry_safe(inode, n, &inode_hashtable[i], i_hash)

Why are there still inodes here? I think we should just check:

DBG_BUGON(!list_empty(&inode_hashtable[i]));

Thanks,
Gao Xiang

author
#8 lore raw

Re: [PATCH 6/9] erofs-utils: add inode hash helper

Quoted 46 lines · Gao Xiang
Quoted 43 lines · Jingbo Xu

Add erofs_insert_ihash() helper inserting inode into inode hash table, and erofs_cleanup_ihash() helper cleaning up inode hash table.

Also add prototypes of erofs_iget() and erofs_iget_by_nid() in the header file.

---
  include/erofs/inode.h |  4 ++++ lib/inode.c           | 22 +++++++++++++++++++---

  2 files changed, 23 insertions(+), 3 deletions(-)
include/erofs/inode.h +4 -0
index e8a5670..aba2a94 100644
@@ -25,6 +25,10 @@ u32 erofs_new_encode_dev(dev_t dev);
   unsigned char erofs_mode_to_ftype(umode_t mode);
   unsigned char erofs_ftype_to_dtype(unsigned int filetype);
   void erofs_inode_manager_init(void);
+void erofs_insert_ihash(struct erofs_inode *inode, dev_t dev, ino_t
 ino);
+void erofs_cleanup_ihash(void);
+struct erofs_inode *erofs_iget(dev_t dev, ino_t ino);
+struct erofs_inode *erofs_iget_by_nid(erofs_nid_t nid);
   unsigned int erofs_iput(struct erofs_inode *inode);
   erofs_nid_t erofs_lookupnid(struct erofs_inode *inode);

  struct erofs_dentry *erofs_d_alloc(struct erofs_inode *parent,

lib/inode.c +0 -0
index d54f84f..d82ea95 100644
@@ -75,6 +75,24 @@ void erofs_inode_manager_init(void)
           init_list_head(&inode_hashtable[i]);
   }

  +void erofs_insert_ihash(struct erofs_inode *inode, dev_t dev, ino_t ino)

lib/inode.c +6 -0
+{
+    list_add(&inode->i_hash,
+         &inode_hashtable[(ino ^ dev) % NR_INODE_HASHTABLE]);
+}
+
+void erofs_cleanup_ihash(void)
Quoted 8 lines

lib/inode.c void erofs_inode_manager_init(void) · line 90

Quoted 6 lines
lib/inode.c +6 -0
+{
+    unsigned int i;
+    struct erofs_inode *inode, *n;
+
+    for (i = 0; i < NR_INODE_HASHTABLE; ++i) {
+        list_for_each_entry_safe(inode, n, &inode_hashtable[i], i_hash)

Why are there still inodes here?

The inode hash table is used to detect hardlink file. In union mode, only ino (dev is always 0) comes to play when calculating the hash. Thus the inode hash table is built on a per-image basis. When we have finished parsing one input image, erofs_cleanup_ihash() is called to clean up the whole inode hash table, otherwise inode (with the same ino) from next input image may conflict when hashing.

Signature · 2 lines
Thanks,
Jingbo
#9 lore raw

Re: [PATCH 6/9] erofs-utils: add inode hash helper

Quoted 58 lines · Jingbo Xu
Quoted 46 lines · Gao Xiang
Quoted 43 lines · Jingbo Xu

Add erofs_insert_ihash() helper inserting inode into inode hash table, and erofs_cleanup_ihash() helper cleaning up inode hash table.

Also add prototypes of erofs_iget() and erofs_iget_by_nid() in the header file.

---
   include/erofs/inode.h |  4 ++++
   lib/inode.c           | 22 +++++++++++++++++++---
   2 files changed, 23 insertions(+), 3 deletions(-)
include/erofs/inode.h +4 -0
index e8a5670..aba2a94 100644
@@ -25,6 +25,10 @@ u32 erofs_new_encode_dev(dev_t dev);
   unsigned char erofs_mode_to_ftype(umode_t mode);
   unsigned char erofs_ftype_to_dtype(unsigned int filetype);
   void erofs_inode_manager_init(void);
+void erofs_insert_ihash(struct erofs_inode *inode, dev_t dev, ino_t
 ino);
+void erofs_cleanup_ihash(void);
+struct erofs_inode *erofs_iget(dev_t dev, ino_t ino);
+struct erofs_inode *erofs_iget_by_nid(erofs_nid_t nid);
   unsigned int erofs_iput(struct erofs_inode *inode);
   erofs_nid_t erofs_lookupnid(struct erofs_inode *inode);

  struct erofs_dentry *erofs_d_alloc(struct erofs_inode *parent,

lib/inode.c +6 -0
index d54f84f..d82ea95 100644
@@ -75,6 +75,24 @@ void erofs_inode_manager_init(void)
           init_list_head(&inode_hashtable[i]);
   }
   +void erofs_insert_ihash(struct erofs_inode *inode, dev_t dev, ino_t
 ino)
+{
+    list_add(&inode->i_hash,
+         &inode_hashtable[(ino ^ dev) % NR_INODE_HASHTABLE]);
+}
+
+void erofs_cleanup_ihash(void)
Quoted 8 lines

lib/inode.c void erofs_inode_manager_init(void) · line 90

Quoted 6 lines
lib/inode.c +6 -0
+{
+    unsigned int i;
+    struct erofs_inode *inode, *n;
+
+    for (i = 0; i < NR_INODE_HASHTABLE; ++i) {
+        list_for_each_entry_safe(inode, n, &inode_hashtable[i], i_hash)

Why are there still inodes here?

Quoted 6 lines

The inode hash table is used to detect hardlink file. In union mode, only ino (dev is always 0) comes to play when calculating the hash. Thus the inode hash table is built on a per-image basis. When we have finished parsing one input image, erofs_cleanup_ihash() is called to clean up the whole inode hash table, otherwise inode (with the same ino) from next input image may conflict when hashing.

I guess we'd better to inline the code directly and add comments for this usage (also you'd better to ensure that hashtable is empty in the beginning) since it's not a safe way for other use cases.

Thanks,
Gao Xiang

author
#10 lore raw

PATCH 7/9 erofs-utils: make erofs_get_dentry() global

In reply to Jingbo Xu

Rename tarerofs_get_dentry() to erofs_get_dentry() and make it global.

Also make whout and 'opq' parameter optional when aufs is false.

---
 include/erofs/tar.h |  2 ++
 lib/tar.c           | 14 ++++++++------
 2 files changed, 10 insertions(+), 6 deletions(-)
include/erofs/tar.h +2 -0
index a14f8ac..8699cb5 100644
@@ -26,4 +26,6 @@ int tarerofs_parse_tar(struct erofs_inode *root, struct erofs_tarfile *tar);
 int tarerofs_reserve_devtable(struct erofs_sb_info *sbi, unsigned int devices);
 int tarerofs_write_devtable(struct erofs_sb_info *sbi, struct erofs_tarfile *tar);
 
+struct erofs_dentry *erofs_get_dentry(struct erofs_inode *pwd, char *path,
+				      bool aufs, bool *whout, bool *opq);
 #endif
lib/tar.c +8 -6
index 2932980..10ae89c 100644
@@ -158,15 +158,17 @@ static struct erofs_dentry *tarerofs_mkdir(struct erofs_inode *dir, const char *
 	return d;
 }
 
-static struct erofs_dentry *tarerofs_get_dentry(struct erofs_inode *pwd, char *path,
-					        bool aufs, bool *whout, bool *opq)
+struct erofs_dentry *erofs_get_dentry(struct erofs_inode *pwd, char *path,
+				      bool aufs, bool *whout, bool *opq)
 {
 	struct erofs_dentry *d = NULL;
 	unsigned int len = strlen(path);
 	char *s = path;
 
-	*whout = false;
-	*opq = false;
+	if (aufs) {
+		*whout = false;
+		*opq = false;
+	}
 
 	while (s < path + len) {
 		char *slash = memchr(s, '/', path + len - s);
@@ -611,7 +613,7 @@ restart:
 
 	erofs_dbg("parsing %s (mode %05o)", eh.path, st.st_mode);
 
-	d = tarerofs_get_dentry(root, eh.path, tar->aufs, &whout, &opq);
+	d = erofs_get_dentry(root, eh.path, tar->aufs, &whout, &opq);
 	if (IS_ERR(d)) {
 		ret = PTR_ERR(d);
 		goto out;
@@ -644,7 +646,7 @@ restart:
 		}
 		d->inode = NULL;
 
-		d2 = tarerofs_get_dentry(root, eh.link, tar->aufs, &dumb, &dumb);
+		d2 = erofs_get_dentry(root, eh.link, tar->aufs, &dumb, &dumb);
 		if (IS_ERR(d2)) {
 			ret = PTR_ERR(d2);
 			goto out;
Signature · 1 line
2.19.1.6.gb485710b
author
#11 lore raw

PATCH 8/9 erofs-utils: fix tar.h

In reply to Jingbo Xu

Include "internal.h" to fix the dependency on prototypes of struct erofs_inode and struct erofs_sb_info.

---
 include/erofs/tar.h | 12 ++++++++++++
 1 file changed, 12 insertions(+)
include/erofs/tar.h +12 -0
index 8699cb5..3a58528 100644
@@ -2,8 +2,15 @@
 #ifndef __EROFS_TAR_H
 #define __EROFS_TAR_H
 
+#ifdef __cplusplus
+extern "C"
+{
+#endif
+
 #include <sys/stat.h>
 
+#include "internal.h"
+
 struct erofs_pax_header {
 	struct stat st;
 	bool use_mtime;
@@ -28,4 +35,9 @@ int tarerofs_write_devtable(struct erofs_sb_info *sbi, struct erofs_tarfile *tar
 
 struct erofs_dentry *erofs_get_dentry(struct erofs_inode *pwd, char *path,
 				      bool aufs, bool *whout, bool *opq);
+
+#ifdef __cplusplus
+}
+#endif
+
 #endif
Signature · 1 line
2.19.1.6.gb485710b
author
#12 lore raw

PATCH 9/9 erofs-utils: introduce union mode

In reply to Jingbo Xu

Introduce a new "union "mode for mkfs.erofs.

The new mode merges multiple erofs images into a new erofs image. It can be enabled with [--union] option.

Currently it is mainly used to merge images generated from tarerofs mode, with following special handling:

- whiteout and opaque directory (marked with "trusted.overlay.opaque" xattr) are specially handled to fit the overlayfs model.
- data of regular file is handled in fallocate like style, i.e. the space is allocated in the image for the file data, while the file content is not written to the image. In this way the erofs image only offers metadata and it depends on other container image orchestra to help derive the corresponding file data somehow.

---
 include/erofs/config.h   |   4 +
 include/erofs/inode.h    |   1 +
 include/erofs/internal.h |  16 +++
 include/erofs/union.h    |  20 +++
 lib/Makefile.am          |   4 +-
 lib/config.c             |  33 +++++
 lib/inode.c              |  95 +++++++++++++-
 lib/tar.c                |   2 -
 lib/union.c              | 266 +++++++++++++++++++++++++++++++++++++++
 mkfs/Makefile.am         |   3 +-
 mkfs/main.c              |  81 ++++++++----
 11 files changed, 496 insertions(+), 29 deletions(-)
 create mode 100644 include/erofs/union.h
 create mode 100644 lib/union.c
include/erofs/config.h +4 -0
index 8f52d2c..bef7085 100644
@@ -53,6 +53,7 @@ struct erofs_configure {
 	bool c_ignore_mtime;
 	bool c_showprogress;
 	bool c_extra_ea_name_prefixes;
+	bool c_union;
 
 #ifdef HAVE_LIBSELINUX
 	struct selabel_handle *sehnd;
@@ -107,6 +108,9 @@ static inline int erofs_selabel_open(const char *file_contexts)
 void erofs_update_progressinfo(const char *fmt, ...);
 char *erofs_trim_for_progressinfo(const char *str, int placeholder);
 
+int erofs_insert_img(const char *path);
+void erofs_cleanup_imgs(void);
+
 #ifdef __cplusplus
 }
 #endif
include/erofs/inode.h +1 -0
index aba2a94..00ecf97 100644
@@ -34,6 +34,7 @@ erofs_nid_t erofs_lookupnid(struct erofs_inode *inode);
 struct erofs_dentry *erofs_d_alloc(struct erofs_inode *parent,
 				   const char *name);
 int tarerofs_dump_tree(struct erofs_inode *dir);
+int erofs_union_dump_tree(struct erofs_inode *dir);
 int erofs_init_empty_dir(struct erofs_inode *dir);
 struct erofs_inode *erofs_new_inode(void);
 struct erofs_inode *erofs_mkfs_build_tree_from_path(const char *path);
include/erofs/internal.h +16 -0
index 83a2e22..b690a1e 100644
@@ -44,6 +44,8 @@ typedef u32 erofs_blk_t;
 /* global sbi */
 extern struct erofs_sb_info sbi;
 
+extern struct list_head imgs_list;
+
 #define erofs_blksiz(sbi)	(1u << (sbi)->blkszbits)
 #define erofs_blknr(sbi, addr)  ((addr) >> (sbi)->blkszbits)
 #define erofs_blkoff(sbi, addr) ((addr) & (erofs_blksiz(sbi) - 1))
@@ -69,6 +71,9 @@ struct erofs_sb_info {
 	erofs_blk_t meta_blkaddr;
 	erofs_blk_t xattr_blkaddr;
 
+	erofs_blk_t data_blkaddr;
+	unsigned long data_blks;
+
 	u32 feature_compat;
 	u32 feature_incompat;
 	u64 build_time;
@@ -136,6 +141,8 @@ EROFS_FEATURE_FUNCS(sb_chksum, compat, COMPAT_SB_CHKSUM)
 #define EROFS_I_EA_INITED	(1 << 0)
 #define EROFS_I_Z_INITED	(1 << 1)
 
+#define S_SHIFT                 12
+
 struct erofs_inode {
 	struct list_head i_hash, i_subdirs, i_xattrs;
 
@@ -183,6 +190,7 @@ struct erofs_inode {
 	bool compressed_idata;
 	bool lazy_tailblock;
 	bool with_tmpfile;
+	bool remap_blkaddr;
 
 	unsigned int xattr_isize;
 	unsigned int extent_isize;
@@ -415,6 +423,14 @@ static inline u32 erofs_crc32c(u32 crc, const u8 *in, size_t len)
 	return crc;
 }
 
+#define EROFS_WHITEOUT_DEV	0
+
+struct erofs_img {
+	struct list_head list;
+	char *path;
+	struct erofs_sb_info sbi;
+};
+
 #ifdef __cplusplus
 }
 #endif
include/erofs/union.h new file +20 -0
new file mode 100644
index 0000000..999a20c
@@ -0,0 +1,20 @@
+/* SPDX-License-Identifier: GPL-2.0+ OR Apache-2.0 */
+#ifndef __EROFS_UNION_H
+#define __EROFS_UNION_H
+
+#ifdef __cplusplus
+extern "C"
+{
+#endif
+
+#include "internal.h"
+
+struct erofs_inode *erofs_union_load_imgs(void);
+int erofs_union_dump_tree(struct erofs_inode *dir);
+int erofs_union_remap(struct erofs_sb_info *sbi);
+
+#ifdef __cplusplus
+}
+#endif
+
+#endif
lib/Makefile.am +3 -1
index ebe466b..b8af149 100644
@@ -24,13 +24,15 @@ noinst_HEADERS = $(top_srcdir)/include/erofs_fs.h \
       $(top_srcdir)/include/erofs/xattr.h \
       $(top_srcdir)/include/erofs/compress_hints.h \
       $(top_srcdir)/include/erofs/fragments.h \
+      $(top_srcdir)/include/erofs/union.h \
       $(top_srcdir)/lib/liberofs_private.h
 
 noinst_HEADERS += compressor.h
 liberofs_la_SOURCES = config.c io.c cache.c super.c inode.c xattr.c exclude.c \
 		      namei.c data.c compress.c compressor.c zmap.c decompress.c \
 		      compress_hints.c hashmap.c sha256.c blobchunk.c dir.c \
-		      fragments.c rb_tree.c dedupe.c uuid_unparse.c uuid.c tar.c
+		      fragments.c rb_tree.c dedupe.c uuid_unparse.c uuid.c tar.c \
+		      union.c
 
 liberofs_la_CFLAGS = -Wall ${libuuid_CFLAGS} -I$(top_srcdir)/include
 if ENABLE_LZ4
lib/config.c +33 -0
index a3235c8..cc214ae 100644
@@ -17,6 +17,8 @@
 struct erofs_configure cfg;
 struct erofs_sb_info sbi;
 
+LIST_HEAD(imgs_list);
+
 void erofs_init_configure(void)
 {
 	memset(&cfg, 0, sizeof(cfg));
@@ -158,3 +160,34 @@ void erofs_update_progressinfo(const char *fmt, ...)
 	__erofs_is_progressmsg = true;
 	fflush(stdout);
 }
+
+int erofs_insert_img(const char *path)
+{
+	struct erofs_img *img;
+
+	img = malloc(sizeof(*img));
+	if (!img)
+		return -ENOMEM;
+
+	img->path = realpath(path, NULL);
+	if (!img->path) {
+		erofs_err("failed to parse image file (%s): %s",
+				path, erofs_strerror(-errno));
+		free(img);
+		return -ENOENT;
+	}
+
+	list_add_tail(&img->list, &imgs_list);
+	return 0;
+}
+
+void erofs_cleanup_imgs(void)
+{
+	struct erofs_img *img, *n;
+
+	list_for_each_entry_safe(img, n, &imgs_list, list) {
+		list_del(&img->list);
+		free(img->path);
+		free(img);
+	}
+}
lib/inode.c +94 -1

112 lines not on this page · raw message

lib/tar.c +0 -2
index 10ae89c..51af638 100644
@@ -19,8 +19,6 @@
 #include "erofs/xattr.h"
 #include "erofs/blobchunk.h"
 
-#define EROFS_WHITEOUT_DEV	0
-
 static char erofs_libbuf[16384];
 
 struct tar_header {
lib/union.c new file +266 -0

266 lines not on this page · raw message

mkfs/Makefile.am +2 -1
index 603c2f3..dd75485 100644
@@ -6,4 +6,5 @@ AM_CPPFLAGS = ${libselinux_CFLAGS}
 mkfs_erofs_SOURCES = main.c
 mkfs_erofs_CFLAGS = -Wall -I$(top_srcdir)/include
 mkfs_erofs_LDADD = $(top_builddir)/lib/liberofs.la ${libselinux_LIBS} \
-	${libuuid_LIBS} ${liblz4_LIBS} ${liblzma_LIBS} ${libdeflate_LIBS}
+	${libuuid_LIBS} ${liblz4_LIBS} ${liblzma_LIBS} ${zlib_LIBS} \
+	${libdeflate_LIBS}
mkfs/main.c +57 -24

145 lines not on this page · raw message

Signature · 1 line
2.19.1.6.gb485710b
#13 lore raw

Re: [PATCH 9/9] erofs-utils: introduce union mode

subject: support rebuilding EROFS image from multiple images

Quoted 1 line · Jingbo Xu

Introduce a new "union "mode for mkfs.erofs.

Add support to rebuild EROFS images.

Quoted 3 lines

The new mode merges multiple erofs images into a new erofs image. It can be enabled with [--union] option.

The new mode merges multiple erofs images into a rebuilt EROFS image..

Quoted 34 lines

Currently it is mainly used to merge images generated from tarerofs mode, with following special handling:

- whiteout and opaque directory (marked with "trusted.overlay.opaque" xattr) are specially handled to fit the overlayfs model.
- data of regular file is handled in fallocate like style, i.e. the space is allocated in the image for the file data, while the file content is not written to the image. In this way the erofs image only offers metadata and it depends on other container image orchestra to help derive the corresponding file data somehow.

---
  include/erofs/config.h   |   4 +
  include/erofs/inode.h    |   1 +
  include/erofs/internal.h |  16 +++
  include/erofs/union.h    |  20 +++
  lib/Makefile.am          |   4 +-
  lib/config.c             |  33 +++++
  lib/inode.c              |  95 +++++++++++++-
  lib/tar.c                |   2 -
  lib/union.c              | 266 +++++++++++++++++++++++++++++++++++++++
  mkfs/Makefile.am         |   3 +-
  mkfs/main.c              |  81 ++++++++----
  11 files changed, 496 insertions(+), 29 deletions(-)

create mode 100644 include/erofs/union.h
create mode 100644 lib/union.c

include/erofs/config.h +0 -0
index 8f52d2c..bef7085 100644

include/erofs/config.h struct erofs_configure { · line 56

Quoted 4 lines
include/erofs/config.h +1 -0
@@ -53,6 +53,7 @@ struct erofs_configure {
  	bool c_ignore_mtime;
  	bool c_showprogress;
  	bool c_extra_ea_name_prefixes;
+	bool c_union;

no need adding this, let's use a static variable in mkfs/main.c instead.

Quoted 66 lines
include/erofs/config.h +3 -0
  #ifdef HAVE_LIBSELINUX
  	struct selabel_handle *sehnd;
@@ -107,6 +108,9 @@ static inline int erofs_selabel_open(const char *file_contexts)
  void erofs_update_progressinfo(const char *fmt, ...);
  char *erofs_trim_for_progressinfo(const char *str, int placeholder);
 
+int erofs_insert_img(const char *path);
+void erofs_cleanup_imgs(void);
+
  #ifdef __cplusplus
  }
  #endif
include/erofs/inode.h +1 -0
index aba2a94..00ecf97 100644
@@ -34,6 +34,7 @@ erofs_nid_t erofs_lookupnid(struct erofs_inode *inode);
  struct erofs_dentry *erofs_d_alloc(struct erofs_inode *parent,
  				   const char *name);
  int tarerofs_dump_tree(struct erofs_inode *dir);
+int erofs_union_dump_tree(struct erofs_inode *dir);
  int erofs_init_empty_dir(struct erofs_inode *dir);
  struct erofs_inode *erofs_new_inode(void);
  struct erofs_inode *erofs_mkfs_build_tree_from_path(const char *path);
include/erofs/internal.h +8 -0
index 83a2e22..b690a1e 100644
@@ -44,6 +44,8 @@ typedef u32 erofs_blk_t;
  /* global sbi */
  extern struct erofs_sb_info sbi;
 
+extern struct list_head imgs_list;
+
  #define erofs_blksiz(sbi)	(1u << (sbi)->blkszbits)
  #define erofs_blknr(sbi, addr)  ((addr) >> (sbi)->blkszbits)
  #define erofs_blkoff(sbi, addr) ((addr) & (erofs_blksiz(sbi) - 1))
@@ -69,6 +71,9 @@ struct erofs_sb_info {
  	erofs_blk_t meta_blkaddr;
  	erofs_blk_t xattr_blkaddr;
 
+	erofs_blk_t data_blkaddr;
+	unsigned long data_blks;
+
  	u32 feature_compat;
  	u32 feature_incompat;
  	u64 build_time;
@@ -136,6 +141,8 @@ EROFS_FEATURE_FUNCS(sb_chksum, compat, COMPAT_SB_CHKSUM)
  #define EROFS_I_EA_INITED	(1 << 0)
  #define EROFS_I_Z_INITED	(1 << 1)
 
+#define S_SHIFT                 12
+
  struct erofs_inode {
  	struct list_head i_hash, i_subdirs, i_xattrs;
 
@@ -183,6 +190,7 @@ struct erofs_inode {
  	bool compressed_idata;
  	bool lazy_tailblock;
  	bool with_tmpfile;
+	bool remap_blkaddr;
 
  	unsigned int xattr_isize;
  	unsigned int extent_isize;

include/erofs/internal.h static inline u32 erofs_crc32c(u32 crc, const u8 *in, size_t len) · line 428

Quoted 6 lines
include/erofs/internal.h +3 -0
@@ -415,6 +423,14 @@ static inline u32 erofs_crc32c(u32 crc, const u8 *in, size_t len)
  	return crc;
  }
 
+#define EROFS_WHITEOUT_DEV	0
+
+struct erofs_img {

move into rebuild.c?

include/erofs/union.h

Quoted 13 lines
include/erofs/internal.h +5 -0
+	struct list_head list;
+	char *path;
+	struct erofs_sb_info sbi;
+};
+
  #ifdef __cplusplus
  }
  #endif
include/erofs/union.h new file +0 -0
new file mode 100644
index 0000000..999a20c

rebuild.h?

Quoted 4 lines
include/erofs/union.h new file +3 -0
@@ -0,0 +1,20 @@
+/* SPDX-License-Identifier: GPL-2.0+ OR Apache-2.0 */
+#ifndef __EROFS_UNION_H
+#define __EROFS_UNION_H

include/erofs/union.h line 13

Quoted 10 lines
include/erofs/union.h new file +10 -0
+
+#ifdef __cplusplus
+extern "C"
+{
+#endif
+
+#include "internal.h"
+
+struct erofs_inode *erofs_union_load_imgs(void);
+int erofs_union_dump_tree(struct erofs_inode *dir);

erofs_rebuild_???

Quoted 33 lines
include/erofs/union.h new file +7 -0
+int erofs_union_remap(struct erofs_sb_info *sbi);
+
+#ifdef __cplusplus
+}
+#endif
+
+#endif
lib/Makefile.am +3 -1
index ebe466b..b8af149 100644
@@ -24,13 +24,15 @@ noinst_HEADERS = $(top_srcdir)/include/erofs_fs.h \
        $(top_srcdir)/include/erofs/xattr.h \
        $(top_srcdir)/include/erofs/compress_hints.h \
        $(top_srcdir)/include/erofs/fragments.h \
+      $(top_srcdir)/include/erofs/union.h \
        $(top_srcdir)/lib/liberofs_private.h
 
  noinst_HEADERS += compressor.h
  liberofs_la_SOURCES = config.c io.c cache.c super.c inode.c xattr.c exclude.c \
  		      namei.c data.c compress.c compressor.c zmap.c decompress.c \
  		      compress_hints.c hashmap.c sha256.c blobchunk.c dir.c \
-		      fragments.c rb_tree.c dedupe.c uuid_unparse.c uuid.c tar.c
+		      fragments.c rb_tree.c dedupe.c uuid_unparse.c uuid.c tar.c \
+		      union.c
 
  liberofs_la_CFLAGS = -Wall ${libuuid_CFLAGS} -I$(top_srcdir)/include
  if ENABLE_LZ4
lib/config.c +0 -0
index a3235c8..cc214ae 100644

lib/config.c line 20

Quoted 4 lines
lib/config.c +1 -0
@@ -17,6 +17,8 @@
  struct erofs_configure cfg;
  struct erofs_sb_info sbi;
 
+LIST_HEAD(imgs_list);

?

Quoted 459 lines
lib/config.c +32 -0
+
  void erofs_init_configure(void)
  {
  	memset(&cfg, 0, sizeof(cfg));
@@ -158,3 +160,34 @@ void erofs_update_progressinfo(const char *fmt, ...)
  	__erofs_is_progressmsg = true;
  	fflush(stdout);
  }
+
+int erofs_insert_img(const char *path)
+{
+	struct erofs_img *img;
+
+	img = malloc(sizeof(*img));
+	if (!img)
+		return -ENOMEM;
+
+	img->path = realpath(path, NULL);
+	if (!img->path) {
+		erofs_err("failed to parse image file (%s): %s",
+				path, erofs_strerror(-errno));
+		free(img);
+		return -ENOENT;
+	}
+
+	list_add_tail(&img->list, &imgs_list);
+	return 0;
+}
+
+void erofs_cleanup_imgs(void)
+{
+	struct erofs_img *img, *n;
+
+	list_for_each_entry_safe(img, n, &imgs_list, list) {
+		list_del(&img->list);
+		free(img->path);
+		free(img);
+	}
+}
lib/inode.c +94 -1
index d82ea95..0a56f3b 100644
@@ -26,9 +26,10 @@
  #include "erofs/compress_hints.h"
  #include "erofs/blobchunk.h"
  #include "erofs/fragments.h"
+#include "erofs/dir.h"
+#include "erofs/tar.h"
  #include "liberofs_private.h"
 
-#define S_SHIFT                 12
  static unsigned char erofs_ftype_by_mode[S_IFMT >> S_SHIFT] = {
  	[S_IFREG >> S_SHIFT]  = EROFS_FT_REG_FILE,
  	[S_IFDIR >> S_SHIFT]  = EROFS_FT_DIR,
@@ -508,6 +509,9 @@ static bool erofs_bh_flush_write_inode(struct erofs_buffer_head *bh)
  	} u = { {0}, };
  	int ret;
 
+	if (inode->remap_blkaddr)
+		inode->u.i_blkaddr += inode->sbi->data_blkaddr;
+
  	switch (inode->inode_isize) {
  	case sizeof(struct erofs_inode_compact):
  		u.dic.i_format = cpu_to_le16(0 | (inode->datalayout << 1));
@@ -1414,3 +1418,92 @@ int tarerofs_dump_tree(struct erofs_inode *dir)
  	dir->bh->op = &erofs_write_inode_bhops;
  	return 0;
  }
+
+static bool erofs_inode_is_whiteout(struct erofs_inode *inode)
+{
+	return S_ISCHR(inode->i_mode) && inode->u.i_rdev == EROFS_WHITEOUT_DEV;
+}
+
+int erofs_union_dump_tree(struct erofs_inode *dir)
+{
+	struct erofs_dentry *d, *n;
+	unsigned int nr_subdirs;
+	int ret;
+
+	if (erofs_should_use_inode_extended(dir)) {
+		if (cfg.c_force_inodeversion == FORCE_INODE_COMPACT) {
+			erofs_err("file %s cannot be in compact form",
+				  dir->i_srcpath);
+			return -EINVAL;
+		}
+		dir->inode_isize = sizeof(struct erofs_inode_extended);
+	} else {
+		dir->inode_isize = sizeof(struct erofs_inode_compact);
+	}
+
+	ret = erofs_prepare_xattr_ibody(dir);
+	if (ret < 0)
+		return ret;
+
+	if (!S_ISDIR(dir->i_mode)) {
+		if (dir->bh)
+			return 0;
+		if (S_ISLNK(dir->i_mode)) {
+			ret = erofs_write_file_from_buffer(dir, dir->i_link);
+			if (ret)
+				return ret;
+			free(dir->i_link);
+			dir->i_link = NULL;
+		}
+		ret = erofs_prepare_inode_buffer(dir);
+		if (ret)
+			return ret;
+		erofs_write_tail_end(dir);
+		return 0;
+	}
+
+	list_for_each_entry_safe(d, n, &dir->i_subdirs, d_child) {
+		if (erofs_inode_is_whiteout(d->inode)) {
+			erofs_dbg("file %s: skip whiteout", d->inode->i_srcpath);
+			erofs_d_invalidate(d);
+			list_del(&d->d_child);
+			free(d);
+			continue;
+		}
+	}
+
+	nr_subdirs = 0;
+	list_for_each_entry(d, &dir->i_subdirs, d_child)
+		++nr_subdirs;
+
+	ret = erofs_prepare_dir_layout(dir, nr_subdirs);
+	if (ret)
+		return ret;
+
+	ret = erofs_prepare_inode_buffer(dir);
+	if (ret)
+		return ret;
+	dir->bh->op = &erofs_skip_write_bhops;
+
+	if (IS_ROOT(dir))
+		erofs_fixup_meta_blkaddr(dir);
+
+	list_for_each_entry(d, &dir->i_subdirs, d_child) {
+		struct erofs_inode *inode;
+
+		if (is_dot_dotdot(d->name))
+			continue;
+
+		inode = erofs_igrab(d->inode);
+		ret = erofs_union_dump_tree(inode);
+		if (erofs_mode_to_ftype(inode->i_mode) == EROFS_FT_DIR)
+			dir->i_nlink++;
+		erofs_iput(inode);
+		if (ret)
+			return ret;
+	}
+	erofs_write_dir_file(dir);
+	erofs_write_tail_end(dir);
+	dir->bh->op = &erofs_write_inode_bhops;
+	return 0;
+}
lib/tar.c +0 -2
index 10ae89c..51af638 100644
@@ -19,8 +19,6 @@
  #include "erofs/xattr.h"
  #include "erofs/blobchunk.h"
 
-#define EROFS_WHITEOUT_DEV	0
-
  static char erofs_libbuf[16384];
 
  struct tar_header {
lib/union.c new file +266 -0
new file mode 100644
index 0000000..15c546c
@@ -0,0 +1,266 @@
+// SPDX-License-Identifier: GPL-2.0+ OR Apache-2.0
+#define _GNU_SOURCE
+#include <unistd.h>
+#include <stdlib.h>
+#include <string.h>
+#include "erofs/print.h"
+#include "erofs/cache.h"
+#include "erofs/tar.h"
+#include "erofs/inode.h"
+#include "erofs/io.h"
+#include "erofs/xattr.h"
+#include "erofs/dir.h"
+#include "erofs/internal.h"
+
+struct erofs_union_dir_context {
+	struct erofs_dir_context ctx;
+	struct erofs_inode *root;
+};
+
+static int erofs_union_fill_inode(struct erofs_inode *inode,
+				  struct erofs_inode *vi)
+{
+	int ret = 0;
+
+	inode->i_srcpath = strdup(vi->i_srcpath);
+	inode->i_mode = vi->i_mode;
+	inode->i_uid = vi->i_uid;
+	inode->i_gid = vi->i_gid;
+	inode->i_mtime = vi->i_mtime;
+	inode->i_ino[1] = vi->i_ino[0];
+	inode->i_nlink = 1;
+
+	switch (inode->i_mode & S_IFMT) {
+	case S_IFCHR:
+	case S_IFBLK:
+	case S_IFIFO:
+	case S_IFSOCK:
+		inode->u.i_rdev = erofs_new_encode_dev(vi->u.i_rdev);
+		inode->i_size = 0;
+		erofs_dbg("\tdev: %d %d", major(vi->u.i_rdev),
+			  minor(vi->u.i_rdev));
+		break;
+	case S_IFDIR:
+		inode->i_size = 0;
+		ret = erofs_init_empty_dir(inode);
+		break;
+	case S_IFLNK:
+		inode->i_size = vi->i_size;
+		inode->i_link = malloc(inode->i_size + 1);
+		ret = erofs_pread(vi, inode->i_link, inode->i_size, 0);
+		erofs_dbg("\tsymlink: %s -> %s", inode->i_srcpath, inode->i_link);
+		break;
+	case S_IFREG:
+		inode->i_size = vi->i_size;
+		if (inode->i_size) {
+			inode->u.i_blkaddr = inode->sbi->data_blks;
+			inode->sbi->data_blks += BLK_ROUND_UP(inode->sbi,
+							      inode->i_size);
+			inode->remap_blkaddr = true;
+		} else {
+			inode->u.i_blkaddr = NULL_ADDR;
+		}
+		erofs_dbg("\tregfile: i_blkaddr %d, i_size %d",
+			  inode->u.i_blkaddr, inode->i_size);
+		break;
+	default:
+		ret = -EINVAL;
+		break;
+	}
+	return ret;
+}
+
+
+static int erofs_union_dirent_iter(struct erofs_dir_context *ctx)
+{
+	struct erofs_union_dir_context *uctx = (void *)ctx;
+	struct erofs_inode *dir = ctx->dir;
+	struct erofs_sb_info *sbi = dir->sbi;
+	struct erofs_inode *inode, vi = {};
+	struct erofs_dentry *d;
+	char *path;
+	int ret;
+
+	if (ctx->dot_dotdot)
+		return 0;
+
+	vi.nid = ctx->de_nid;
+	vi.sbi = sbi;
+	ret = erofs_read_inode_from_disk(&vi);
+	if (ret) {
+		erofs_err("file %s/%.*s: read failed",
+			  dir->i_srcpath, ctx->de_namelen, ctx->dname);
+		return ret;
+	}
+
+	ret = asprintf(&path, "%s/%.*s", dir->i_srcpath, ctx->de_namelen,
+		       ctx->dname);
+	if (ret < 0) {
+		erofs_err("file %s/%.*s: failed to alloc path",
+			  dir->i_srcpath, ctx->de_namelen, ctx->dname);
+		return ret;
+	}
+	vi.i_srcpath = path;
+
+	d = erofs_get_dentry(uctx->root, path, false, NULL, NULL);
+	if (IS_ERR(d)) {
+		ret = PTR_ERR(d);
+		goto exit;
+	}
+
+	if (d->type != EROFS_FT_UNKNOWN) {
+		inode = d->inode;
+		DBG_BUGON((inode->i_mode & S_IFMT) != (vi.i_mode & S_IFMT));
+		if (!S_ISDIR(inode->i_mode) || inode->opaque) {
+			erofs_dbg("file %s: %s (%d) exists",
+				  path, inode->i_srcpath,
+				  erofs_mode_to_ftype(inode->i_mode));
+			free(path);
+			return 0;
+		}
+		erofs_dbg("dir %s: %s merging", path, inode->i_srcpath);
+	} else {
+		erofs_dbg("loading file: %s (%d) (nid %llu)",
+			  path, erofs_mode_to_ftype(vi.i_mode), ctx->de_nid);
+		if (S_ISREG(vi.i_mode) && vi.i_nlink > 1 &&
+		    (inode = erofs_iget(0, vi.i_ino[0]))) {
+			/* hardlink file */
+			if (S_ISDIR(inode->i_mode)) {
+				erofs_dbg("hardlink directory not supported");
+				erofs_iput(inode);
+				return -EISDIR;
+			}
+			inode->i_nlink++;
+			erofs_dbg("\thardlink: %s -> %s",
+				  vi.i_srcpath, inode->i_srcpath);
+		} else {
+			inode = erofs_new_inode();
+			if (IS_ERR(inode)) {
+				ret = PTR_ERR(inode);
+				goto exit;
+			}
+
+			inode->i_parent = d->inode;
+			ret = erofs_union_fill_inode(inode, &vi);
+			if (ret)
+				goto exit_inode;
+
+			ret = erofs_read_xattrs_from_disk(&vi);
+			if (ret)
+				goto exit_inode;
+			list_splice_tail(&vi.i_xattrs, &inode->i_xattrs);
+			erofs_inode_tag_opaque(inode);
+
+			/* build per-image hash table for hardlink querying */
+			erofs_insert_ihash(inode, 0, inode->i_ino[1]);
+		}
+
+		d->inode = inode;
+		d->type = erofs_mode_to_ftype(inode->i_mode);
+	}
+
+	ret = 0;
+	if (S_ISDIR(vi.i_mode)) {
+		struct erofs_union_dir_context nctx = {
+			.ctx.dir = &vi,
+			.ctx.cb = erofs_union_dirent_iter,
+			.root = uctx->root,
+		};
+		ret = erofs_iterate_dir(&nctx.ctx, false);
+	}
+exit:
+	free(path);
+	return ret;
+exit_inode:
+	erofs_iput(inode);
+	goto exit;
+}
+
+static int erofs_union_load_img(struct erofs_inode *root, struct erofs_img *img)
+{
+	struct erofs_inode inode = {};
+	struct erofs_sb_info *sbi = &img->sbi;
+	struct erofs_union_dir_context ctx;
+	int ret;
+
+	erofs_dbg("Loading image %s...", img->path);
+	ret = dev_open_ro(sbi, img->path);
+	if (ret) {
+		erofs_err("failed to open image %s", img->path);
+		return ret;
+	}
+
+	ret = erofs_read_superblock(sbi);
+	if (ret) {
+		erofs_err("failed to read superblock of image %s", img->path);
+		goto exit;
+	}
+
+	inode.nid = sbi->root_nid;
+	inode.sbi = sbi;
+	ret = erofs_read_inode_from_disk(&inode);
+	if (ret) {
+		erofs_err("failed to read root inode of image %s", img->path);
+		goto exit;
+	}
+	inode.i_srcpath = strdup("/");
+
+	ctx = (struct erofs_union_dir_context) {
+		.ctx.dir = &inode,
+		.ctx.cb = erofs_union_dirent_iter,
+		.root = root,
+	};
+	ret = erofs_iterate_dir(&ctx.ctx, false);
+exit:
+	erofs_cleanup_ihash();
+	dev_close(sbi);
+	return ret;
+}
+
+struct erofs_inode *erofs_union_load_imgs(void)
+{
+	struct erofs_img *img;
+	struct erofs_inode *root;
+	int ret;
+
+	sbi.data_blks = 0;
+
+	root = erofs_new_inode();
+	if (IS_ERR(root))
+		return root;
+	root->i_srcpath = strdup("/");
+	root->i_mode = S_IFDIR | 0777;
+	root->i_parent = root;
+	root->i_mtime = root->sbi->build_time;
+	root->i_mtime_nsec = root->sbi->build_time_nsec;
+	erofs_init_empty_dir(root);
+
+	list_for_each_entry(img, &imgs_list, list) {
+		ret = erofs_union_load_img(root, img);
+		if (ret)
+			return ERR_PTR(ret);
+	}
+	return root;
+}
+
+int erofs_union_remap(struct erofs_sb_info *sbi)
+{
+	struct erofs_buffer_head *bh;
+	int ret;
+
+	if (!sbi->data_blks)
+		return 0;
+
+	bh = erofs_balloc(DATA, erofs_pos(sbi, sbi->data_blks), 0, 0);
+	if (IS_ERR(bh))
+		return PTR_ERR(bh);
+
+	ret = erofs_mapbh(bh->block);
+	DBG_BUGON(ret < 0);
+
+	sbi->data_blkaddr = erofs_blknr(sbi, erofs_btell(bh, false));
+	erofs_dbg("data blkaddr %d, blks %d", sbi->data_blkaddr, sbi->data_blks);
+	bh->op = &erofs_drop_directly_bhops;
+	erofs_bdrop(bh, false);
+	return 0;
+}
mkfs/Makefile.am +2 -1
index 603c2f3..dd75485 100644
@@ -6,4 +6,5 @@ AM_CPPFLAGS = ${libselinux_CFLAGS}
  mkfs_erofs_SOURCES = main.c
  mkfs_erofs_CFLAGS = -Wall -I$(top_srcdir)/include
  mkfs_erofs_LDADD = $(top_builddir)/lib/liberofs.la ${libselinux_LIBS} \
-	${libuuid_LIBS} ${liblz4_LIBS} ${liblzma_LIBS} ${libdeflate_LIBS}
+	${libuuid_LIBS} ${liblz4_LIBS} ${liblzma_LIBS} ${zlib_LIBS} \
+	${libdeflate_LIBS}
mkfs/main.c +0 -0
index bc5ed87..4a47b24 100644

mkfs/main.c line 31

Quoted 6 lines
mkfs/main.c +1 -0
@@ -26,6 +26,7 @@
  #include "erofs/compress_hints.h"
  #include "erofs/blobchunk.h"
  #include "erofs/fragments.h"
+#include "erofs/union.h"
  #include "../lib/liberofs_private.h"
  #include "../lib/liberofs_uuid.h"

static bool rebuild_mode. ?

Thanks,
Gao Xiang