Skip to contentSkip to conversation

erofs-utils: support long xattr name prefixes

PATCH Superseded Jingbo Xu 2 participants 7 messages Aug 16, 2023 Library code

v2 of 3 Superseded Latest is v3 →

lore
Share & tools
lore

Jingbo Xu

author
#0 lore raw

v2 cover erofs-utils: support long xattr name prefixes

v2:
- add patch 3 to introduce erofs_read_metadata() helper, so that the long xattr name prefixes could also be read from meta inode

v1: https://lore.kernel.org/all/[email protected]/

Jingbo Xu (4):
erofs-utils: lib: fix potential out-of-bound in xattr_entrylist()
erofs-utils: lib: add match_base_prefix() helper
erofs-utils: add erofs_read_metadata() helper
erofs-utils: support long xattr name prefixes for erofsfuse

 include/erofs/internal.h |   8 +++
 include/erofs/xattr.h    |   2 +
 lib/data.c               |  84 ++++++++++++++++++++++
 lib/super.c              |  14 +++-
 lib/xattr.c              | 147 +++++++++++++++++++++++++++++++++------
 mkfs/Makefile.am         |   3 +-
 6 files changed, 235 insertions(+), 23 deletions(-)
Signature · 1 line
2.19.1.6.gb485710b
author
#1 lore raw

PATCH v2 1/4 erofs-utils: lib: fix potential out-of-bound in xattr_entrylist()

Check the index before accessing array to avoid the potential out-of-bound access.

Fixes c47df5aa2d16 ("erofs-utils: fuse: introduce xattr support")
---
 lib/xattr.c | 7 +++++--
 1 file changed, 5 insertions(+), 2 deletions(-)
lib/xattr.c +5 -2
index 12f580e..2548750 100644
@@ -1229,11 +1229,14 @@ static int xattr_entrylist(struct xattr_iter *_it,
 {
 	struct listxattr_iter *it =
 		container_of(_it, struct listxattr_iter, it);
+	unsigned int base_index = entry->e_name_index;
 	unsigned int prefix_len;
 	const char *prefix;
 
-	prefix = xattr_types[entry->e_name_index].prefix;
-	prefix_len = xattr_types[entry->e_name_index].prefix_len;
+	if (base_index >= ARRAY_SIZE(xattr_types))
+		return 1;
+	prefix = xattr_types[base_index].prefix;
+	prefix_len = xattr_types[base_index].prefix_len;
 
 	if (!it->buffer) {
 		it->buffer_ofs += prefix_len + entry->e_name_len + 1;
Signature · 1 line
2.19.1.6.gb485710b
author
#2 lore raw

PATCH v2 2/4 erofs-utils: lib: add match_base_prefix() helper

In reply to Jingbo Xu

Since the introduction of long xattr name prefix, match_prefix() will search among the long xattr name prefixes first and return the matched prefix, while erofs_getxattr() expects a base prefix even when the queried xattr name matches a long prefix.

Thus introduce match_base_prefix() helper to do this.

---
 lib/xattr.c | 25 ++++++++++++++++---------
 1 file changed, 16 insertions(+), 9 deletions(-)
lib/xattr.c +16 -9
index 2548750..4091fe6 100644
@@ -137,27 +137,34 @@ static struct xattr_item *get_xattritem(u8 prefix, char *kvbuf,
 	return item;
 }
 
-static bool match_prefix(const char *key, u8 *index, u16 *len)
+static bool match_base_prefix(const char *key, u8 *index, u16 *len)
 {
 	struct xattr_prefix *p;
-	struct ea_type_node *tnode;
 
-	list_for_each_entry(tnode, &ea_name_prefixes, list) {
-		p = &tnode->type;
+	for (p = xattr_types; p < xattr_types + ARRAY_SIZE(xattr_types); ++p) {
 		if (p->prefix && !strncmp(p->prefix, key, p->prefix_len)) {
 			*len = p->prefix_len;
-			*index = tnode->index;
+			*index = p - xattr_types;
 			return true;
 		}
 	}
-	for (p = xattr_types; p < xattr_types + ARRAY_SIZE(xattr_types); ++p) {
+	return false;
+}
+
+static bool match_prefix(const char *key, u8 *index, u16 *len)
+{
+	struct xattr_prefix *p;
+	struct ea_type_node *tnode;
+
+	list_for_each_entry(tnode, &ea_name_prefixes, list) {
+		p = &tnode->type;
 		if (p->prefix && !strncmp(p->prefix, key, p->prefix_len)) {
 			*len = p->prefix_len;
-			*index = p - xattr_types;
+			*index = tnode->index;
 			return true;
 		}
 	}
-	return false;
+	return match_base_prefix(key, index, len);
 }
 
 static struct xattr_item *parse_one_xattr(const char *path, const char *key,
@@ -1198,7 +1205,7 @@ int erofs_getxattr(struct erofs_inode *vi, const char *name, char *buffer,
 	if (ret)
 		return ret;
 
-	if (!match_prefix(name, &prefix, &prefixlen))
+	if (!match_base_prefix(name, &prefix, &prefixlen))
 		return -ENODATA;
 
 	it.it.sbi = vi->sbi;
Signature · 1 line
2.19.1.6.gb485710b
author
#3 lore raw

PATCH v2 3/4 erofs-utils: add erofs_read_metadata() helper

In reply to Jingbo Xu

Add erofs_read_metadata() helper reading variable-sized metadata from inode specified by @nid. Read from meta inode if @nid is 0.

---
 include/erofs/internal.h |  2 +
 lib/data.c               | 84 ++++++++++++++++++++++++++++++++++++++++
 2 files changed, 86 insertions(+)
include/erofs/internal.h +2 -0
index a04e6a6..3e7319d 100644
@@ -364,6 +364,8 @@ int erofs_read_one_data(struct erofs_inode *inode, struct erofs_map_blocks *map,
 int z_erofs_read_one_data(struct erofs_inode *inode,
 			struct erofs_map_blocks *map, char *raw, char *buffer,
 			erofs_off_t skip, erofs_off_t length, bool trimmed);
+void *erofs_read_metadata(struct erofs_sb_info *sbi, erofs_nid_t nid,
+			  erofs_off_t *offset, int *lengthp);
 
 static inline int erofs_get_occupied_size(const struct erofs_inode *inode,
 					  erofs_off_t *size)
lib/data.c +84 -0

87 lines not on this page · raw message

Signature · 1 line
2.19.1.6.gb485710b
#4 lore raw

Re: [PATCH v2 3/4] erofs-utils: add erofs_read_metadata() helper

Quoted 27 lines · Jingbo Xu

Add erofs_read_metadata() helper reading variable-sized metadata from inode specified by @nid. Read from meta inode if @nid is 0.

---
  include/erofs/internal.h |  2 +
  lib/data.c               | 84 ++++++++++++++++++++++++++++++++++++++++
  2 files changed, 86 insertions(+)
include/erofs/internal.h +2 -0
index a04e6a6..3e7319d 100644
@@ -364,6 +364,8 @@ int erofs_read_one_data(struct erofs_inode *inode, struct erofs_map_blocks *map,
  int z_erofs_read_one_data(struct erofs_inode *inode,
  			struct erofs_map_blocks *map, char *raw, char *buffer,
  			erofs_off_t skip, erofs_off_t length, bool trimmed);
+void *erofs_read_metadata(struct erofs_sb_info *sbi, erofs_nid_t nid,
+			  erofs_off_t *offset, int *lengthp);
 
  static inline int erofs_get_occupied_size(const struct erofs_inode *inode,
  					  erofs_off_t *size)
lib/data.c +0 -0
index a172bb5..3ec5330 100644

lib/data.c int erofs_pread(struct erofs_inode *inode, char *buf, · line 377

Quoted 6 lines
lib/data.c +3 -0
@@ -372,3 +372,87 @@ int erofs_pread(struct erofs_inode *inode, char *buf,
  	}
  	return -EINVAL;
  }
+
+static void *erofs_read_meta(struct erofs_sb_info *sbi, erofs_nid_t nid,
+			     erofs_off_t *offset, int *lengthp)

erofs_read_metadata_nid?

Quoted 26 lines
lib/data.c +26 -0
+{
+	struct erofs_inode vi = { .sbi = sbi, .nid = nid };
+	__le16 __len;
+	int ret, len;
+	char *buffer;
+
+	ret = erofs_read_inode_from_disk(&vi);
+	if (ret)
+		return ERR_PTR(ret);
+
+	*offset = round_up(*offset, 4);
+	ret = erofs_pread(&vi, (void *)&__len, sizeof(__le16), *offset);
+	if (ret)
+		return ERR_PTR(ret);
+
+	len = le16_to_cpu(__len);
+	if (!len)
+		return ERR_PTR(-EFSCORRUPTED);
+
+	buffer = malloc(len);
+	if (!buffer)
+		return ERR_PTR(-ENOMEM);
+	*offset += sizeof(__le16);
+	*lengthp = len;
+
+	ret = erofs_pread(&vi, buffer, len, *offset);

lib/data.c int erofs_pread(struct erofs_inode *inode, char *buf, · line 413

Quoted 10 lines
lib/data.c +10 -0
+	if (ret) {
+		free(buffer);
+		return ERR_PTR(ret);
+	}
+	*offset += len;
+	return buffer;
+}
+
+static void *erofs_read_metainode(struct erofs_sb_info *sbi,
+				  erofs_off_t *offset, int *lengthp)

erofs_read_metadata_bdi?

Thanks,
Gao Xiang

author
#5 lore raw

Re: [PATCH v2 3/4] erofs-utils: add erofs_read_metadata() helper

Quoted 32 lines · Gao Xiang
Quoted 29 lines · Jingbo Xu

Add erofs_read_metadata() helper reading variable-sized metadata from inode specified by @nid.  Read from meta inode if @nid is 0.

---
  include/erofs/internal.h |  2 +
  lib/data.c               | 84 ++++++++++++++++++++++++++++++++++++++++

  2 files changed, 86 insertions(+)
include/erofs/internal.h +2 -0
index a04e6a6..3e7319d 100644
@@ -364,6 +364,8 @@ int erofs_read_one_data(struct erofs_inode *inode,
 struct erofs_map_blocks *map,
   int z_erofs_read_one_data(struct erofs_inode *inode,
               struct erofs_map_blocks *map, char *raw, char *buffer,
               erofs_off_t skip, erofs_off_t length, bool trimmed);
+void *erofs_read_metadata(struct erofs_sb_info *sbi, erofs_nid_t nid,
+              erofs_off_t *offset, int *lengthp);

    static inline int erofs_get_occupied_size(const struct erofs_inode *inode,
                        erofs_off_t *size)

lib/data.c +0 -0
index a172bb5..3ec5330 100644
@@ -372,3 +372,87 @@ int erofs_pread(struct erofs_inode *inode, char
 *buf,
Quoted 8 lines

lib/data.c int erofs_pread(struct erofs_inode *inode, char *buf, · line 377

Quoted 6 lines
lib/data.c +0 -0
       }
       return -EINVAL;

  }

lib/data.c +3 -0
+
+static void *erofs_read_meta(struct erofs_sb_info *sbi, erofs_nid_t nid,
+                 erofs_off_t *offset, int *lengthp)

erofs_read_metadata_nid?

Okay.

Quoted 31 lines
Quoted 31 lines
lib/data.c +30 -0
+{
+    struct erofs_inode vi = { .sbi = sbi, .nid = nid };
+    __le16 __len;
+    int ret, len;
+    char *buffer;
+
+    ret = erofs_read_inode_from_disk(&vi);
+    if (ret)
+        return ERR_PTR(ret);
+
+    *offset = round_up(*offset, 4);
+    ret = erofs_pread(&vi, (void *)&__len, sizeof(__le16), *offset);
+    if (ret)
+        return ERR_PTR(ret);
+
+    len = le16_to_cpu(__len);
+    if (!len)
+        return ERR_PTR(-EFSCORRUPTED);
+
+    buffer = malloc(len);
+    if (!buffer)
+        return ERR_PTR(-ENOMEM);
+    *offset += sizeof(__le16);
+    *lengthp = len;
+
+    ret = erofs_pread(&vi, buffer, len, *offset);
+    if (ret) {
+        free(buffer);
+        return ERR_PTR(ret);
+    }
Quoted 8 lines

lib/data.c int erofs_pread(struct erofs_inode *inode, char *buf, · line 413

Quoted 6 lines

+    *offset += len;
+    return buffer;

lib/data.c +4 -0
+}
+
+static void *erofs_read_metainode(struct erofs_sb_info *sbi,
+                  erofs_off_t *offset, int *lengthp)

erofs_read_metadata_bdi?

Okay.

Signature · 2 lines
Thanks,
Jingbo
author
#6 lore raw

PATCH v2 4/4 erofs-utils: support long xattr name prefixes for erofsfuse

In reply to Jingbo Xu

Make erofs_listxattr() and erofs_getxattr() routine support long xattr name prefixes.

Although the on-disk format allows long xattr name prefixes to be placed in the meta inode or packed inode, currently mkfs.erofs will place them in packed inode by default. Thus let's also read long xattr name prefixes from packed inode by default.

Since we need to read the content of the packed inode from disk when loading long xattr name prefixes, add dependency on zlib_LIBS for mkfs.erofs to resolve the compiling dependency.

---
 include/erofs/internal.h |   6 ++
 include/erofs/xattr.h    |   2 +
 lib/super.c              |  14 ++++-
 lib/xattr.c              | 115 +++++++++++++++++++++++++++++++++++----
 mkfs/Makefile.am         |   3 +-
 5 files changed, 128 insertions(+), 12 deletions(-)
include/erofs/internal.h +6 -0
index 3e7319d..db766ca 100644
@@ -57,6 +57,11 @@ struct erofs_device_info {
 	u32 mapped_blkaddr;
 };
 
+struct erofs_xattr_prefix_item {
+	struct erofs_xattr_long_prefix *prefix;
+	u8 infix_len;
+};
+
 #define EROFS_PACKED_NID_UNALLOCATED	-1
 
 struct erofs_sb_info {
@@ -99,6 +104,7 @@ struct erofs_sb_info {
 
 	u32 xattr_prefix_start;
 	u8 xattr_prefix_count;
+	struct erofs_xattr_prefix_item *xattr_prefixes;
 
 	int devfd;
 	u64 devsz;
include/erofs/xattr.h +2 -0
index dc27cf6..748442a 100644
@@ -82,6 +82,8 @@ int erofs_build_shared_xattrs_from_path(struct erofs_sb_info *sbi, const char *p
 int erofs_xattr_insert_name_prefix(const char *prefix);
 void erofs_xattr_cleanup_name_prefixes(void);
 int erofs_xattr_write_name_prefixes(struct erofs_sb_info *sbi, FILE *f);
+void erofs_xattr_prefixes_cleanup(struct erofs_sb_info *sbi);
+int erofs_xattr_prefixes_init(struct erofs_sb_info *sbi);
 
 int erofs_setxattr(struct erofs_inode *inode, char *key,
 		   const void *value, size_t size);
lib/super.c +13 -1
index e8e84aa..21dc51f 100644
@@ -6,6 +6,7 @@
 #include <stdlib.h>
 #include "erofs/io.h"
 #include "erofs/print.h"
+#include "erofs/xattr.h"
 
 static bool check_layout_compatibility(struct erofs_sb_info *sbi,
 				       struct erofs_super_block *dsb)
@@ -101,6 +102,8 @@ int erofs_read_superblock(struct erofs_sb_info *sbi)
 	sbi->primarydevice_blocks = le32_to_cpu(dsb->blocks);
 	sbi->meta_blkaddr = le32_to_cpu(dsb->meta_blkaddr);
 	sbi->xattr_blkaddr = le32_to_cpu(dsb->xattr_blkaddr);
+	sbi->xattr_prefix_start = le32_to_cpu(dsb->xattr_prefix_start);
+	sbi->xattr_prefix_count = dsb->xattr_prefix_count;
 	sbi->islotbits = EROFS_ISLOTBITS;
 	sbi->root_nid = le16_to_cpu(dsb->root_nid);
 	sbi->packed_nid = le64_to_cpu(dsb->packed_nid);
@@ -117,11 +120,20 @@ int erofs_read_superblock(struct erofs_sb_info *sbi)
 		sbi->available_compr_algs = le16_to_cpu(dsb->u1.available_compr_algs);
 	else
 		sbi->lz4_max_distance = le16_to_cpu(dsb->u1.lz4_max_distance);
-	return erofs_init_devices(sbi, dsb);
+
+	ret = erofs_init_devices(sbi, dsb);
+	if (ret)
+		return ret;
+
+	ret = erofs_xattr_prefixes_init(sbi);
+	if (ret)
+		free(sbi->devs);
+	return ret;
 }
 
 void erofs_put_super(struct erofs_sb_info *sbi)
 {
 	if (sbi->devs)
 		free(sbi->devs);
+	erofs_xattr_prefixes_cleanup(sbi);
 }
lib/xattr.c +105 -10

159 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}
Signature · 1 line
2.19.1.6.gb485710b