Skip to contentSkip to conversation

erofs-utils: lib: introduce erofs_xattr_prefix_index()

PATCH In review Hongzhen Luo 2 participants 4 messages Sep 6, 2024 Library code

lore
Share & tools
lore
#0 lore raw

PATCH v6 1/2 erofs-utils: lib: introduce erofs_xattr_prefix_index()

Prepare for the feature of exporting extended attributes for fsck.erofs, which requires obtaining the index based on the name of the extended attribute.

---
 include/erofs/xattr.h | 2 ++
 lib/xattr.c           | 7 +++++++
 2 files changed, 9 insertions(+)
include/erofs/xattr.h +2 -0
index 7643611..7848fe7 100644
@@ -61,6 +61,8 @@ void erofs_clear_opaque_xattr(struct erofs_inode *inode);
 int erofs_set_origin_xattr(struct erofs_inode *inode);
 int erofs_read_xattrs_from_disk(struct erofs_inode *inode);
 
+int erofs_xattr_prefix_index(const char *key);
+
 #ifdef __cplusplus
 }
 #endif
lib/xattr.c +7 -0
index 9f31f2d..1fed7c0 100644
@@ -1681,3 +1681,10 @@ out:
 		erofs_xattr_prefixes_cleanup(sbi);
 	return ret;
 }
+
+int erofs_xattr_prefix_index(const char *key)
+{
+	unsigned int index, len;
+
+	return match_prefix(key, &index, &len) ? index : -EINVAL;
+}
Signature · 1 line
2.43.5
#1 lore raw

PATCH v6 2/2 erofs-utils: fsck: introduce exporting xattrs

The current fsck --extract does not support exporting the extended attributes of files. This patch adds --xattrs option to dump the extended attributes, as well as the --no-xattrs option to not dump the extended attributes.

---
v6:
- update man/fsck.erofs.1;
- do not export non-user. namespace xattrs when operating

    as a non-privileged user;

- use lsetxattr() to set xattrs instead of setxattr().

v5: https://lore.kernel.org/all/[email protected]/
v4: https://lore.kernel.org/all/[email protected]/
v3: https://lore.kernel.org/all/[email protected]/
v2: https://lore.kernel.org/all/[email protected]/
v1: https://lore.kernel.org/all/[email protected]/

---
 fsck/main.c      | 90 +++++++++++++++++++++++++++++++++++++++++++++---
 man/fsck.erofs.1 |  3 ++
 2 files changed, 89 insertions(+), 4 deletions(-)
fsck/main.c +86 -4
index 28f1e7e..e89c43d 100644
@@ -9,10 +9,12 @@
 #include <utime.h>
 #include <unistd.h>
 #include <sys/stat.h>
+#include <sys/xattr.h>
 #include "erofs/print.h"
 #include "erofs/compress.h"
 #include "erofs/decompress.h"
 #include "erofs/dir.h"
+#include "erofs/xattr.h"
 #include "../lib/compressor.h"
 
 static int erofsfsck_check_inode(erofs_nid_t pnid, erofs_nid_t nid);
@@ -31,6 +33,7 @@ struct erofsfsck_cfg {
 	bool overwrite;
 	bool preserve_owner;
 	bool preserve_perms;
+	bool dump_xattrs;
 };
 static struct erofsfsck_cfg fsckcfg;
 
@@ -48,6 +51,8 @@ static struct option long_options[] = {
 	{"no-preserve-owner", no_argument, 0, 10},
 	{"no-preserve-perms", no_argument, 0, 11},
 	{"offset", required_argument, 0, 12},
+	{"xattrs", no_argument, 0, 13},
+	{"no-xattrs", no_argument, 0, 14},
 	{0, 0, 0, 0},
 };
 
@@ -98,6 +103,7 @@ static void usage(int argc, char **argv)
 		" --extract[=X]          check if all files are well encoded, optionally\n"
 		"                        extract to X\n"
 		" --offset=#             skip # bytes at the beginning of IMAGE\n"
+		" --[no-]xattrs          whether to dump extended attributes (default on)\n"
 		"\n"
 		" -a, -A, -y             no-op, for compatibility with fsck of other filesystems\n"
 		"\n"
@@ -225,6 +231,12 @@ static int erofsfsck_parse_options_cfg(int argc, char **argv)
 				return -EINVAL;
 			}
 			break;
+		case 13:
+			fsckcfg.dump_xattrs = true;
+			break;
+		case 14:
+			fsckcfg.dump_xattrs = false;
+			break;
 		default:
 			return -EINVAL;
 		}
@@ -411,6 +423,67 @@ out:
 	return ret;
 }
 
+static int erofsfsck_dump_xattrs(struct erofs_inode *inode)
+{
+	char *keylst, *key;
+	ssize_t kllen;
+	int ret;
+
+	kllen = erofs_listxattr(inode, NULL, 0);
+	if (kllen <= 0)
+		return kllen;
+	keylst = malloc(kllen);
+	if (!keylst)
+		return -ENOMEM;
+	ret = erofs_listxattr(inode, keylst, kllen);
+	if (ret != kllen) {
+		ret = -EINVAL;
+		goto out;
+	}
+	ret = 0;
+	for (key = keylst; key < keylst + kllen; key += strlen(key) + 1) {
+		void *value = NULL;
+		size_t size = 0;
+
+		if (!fsckcfg.superuser && erofs_xattr_prefix_index(key)
+		    != EROFS_XATTR_INDEX_USER)
+			continue;
+
+		ret = erofs_getxattr(inode, key, NULL, 0);
+		if (ret < 0)
+			break;
+		if (ret) {
+			size = ret;
+			value = malloc(size);
+			if (!value) {
+				ret = -ENOMEM;
+				break;
+			}
+			ret = erofs_getxattr(inode, key, value, size);
+			if (ret < 0) {
+				erofs_err("fail to get xattr %s @ nid %llu",
+					  key, inode->nid | 0ULL);
+				free(value);
+				break;
+			}
+			if (fsckcfg.extract_path)
+				ret = lsetxattr(fsckcfg.extract_path, key, value,
+						size, 0);
+			else
+				ret = 0;
+			free(value);
+			if (ret) {
+				erofs_err("fail to set xattr %s @ nid %llu",
+					  key, inode->nid | 0ULL);
+				break;
+			}
+		}
+	}
+out:
+	free(keylst);
+	return ret;
+}
+
 static int erofs_verify_inode_data(struct erofs_inode *inode, int outfd)
 {
 	struct erofs_map_blocks map = {
@@ -900,15 +973,23 @@ static int erofsfsck_check_inode(erofs_nid_t pnid, erofs_nid_t nid)
 		goto out;
 	}
 
-	/* verify xattr field */
-	ret = erofs_verify_xattr(&inode);
-	if (ret)
-		goto out;
+	if (!fsckcfg.check_decomp) {
+		/* verify xattr field */
+		ret = erofs_verify_xattr(&inode);
+		if (ret)
+			goto out;
+	}
 
 	ret = erofsfsck_extract_inode(&inode);
 	if (ret && ret != -ECANCELED)
 		goto out;
 
+	if (fsckcfg.check_decomp && fsckcfg.dump_xattrs) {
+		ret = erofsfsck_dump_xattrs(&inode);
+		if (ret)
+			return ret;
+	}
+
 	/* XXXX: the dir depth should be restricted in order to avoid loops */
 	if (S_ISDIR(inode.i_mode)) {
 		struct erofs_dir_context ctx = {
@@ -955,6 +1036,7 @@ int main(int argc, char *argv[])
 	fsckcfg.overwrite = false;
 	fsckcfg.preserve_owner = fsckcfg.superuser;
 	fsckcfg.preserve_perms = fsckcfg.superuser;
+	fsckcfg.dump_xattrs = true;
 
 	err = erofsfsck_parse_options_cfg(argc, argv);
 	if (err) {
man/fsck.erofs.1 +3 -0
index 393ae9e..898f48e 100644
@@ -27,6 +27,9 @@ You may give multiple
 .B --device
 options in the correct order.
 .TP
+.BI "--[no-]xattrs"
+Whether to dump extended attributes (default on).
+.TP
 .BI "\-\-extract" "[=directory]"
 Test to extract the whole file system. It scans all inode data, including
 compressed inode data, which leads to more I/O and CPU load, so it might
Signature · 1 line
2.43.5
#2 lore raw
In reply to Hongzhen Luo
Quoted 28 lines · Hongzhen Luo

Prepare for the feature of exporting extended attributes for fsck.erofs, which requires obtaining the index based on the name of the extended attribute.

---
  include/erofs/xattr.h | 2 ++
  lib/xattr.c           | 7 +++++++
  2 files changed, 9 insertions(+)
include/erofs/xattr.h +2 -0
index 7643611..7848fe7 100644
@@ -61,6 +61,8 @@ void erofs_clear_opaque_xattr(struct erofs_inode *inode);
  int erofs_set_origin_xattr(struct erofs_inode *inode);
  int erofs_read_xattrs_from_disk(struct erofs_inode *inode);
 
+int erofs_xattr_prefix_index(const char *key);
+
  #ifdef __cplusplus
  }
  #endif
lib/xattr.c +0 -0
index 9f31f2d..1fed7c0 100644

lib/xattr.c out: · line 1689

Quoted 9 lines
lib/xattr.c +6 -0
@@ -1681,3 +1681,10 @@ out:
  		erofs_xattr_prefixes_cleanup(sbi);
  	return ret;
  }
+
+int erofs_xattr_prefix_index(const char *key)
+{
+	unsigned int index, len;
+
+	return match_prefix(key, &index, &len) ? index : -EINVAL;

Can we export match_prefix as erofs_xattr_match_prefix() directly?

Thanks,
Gao Xiang

Quoted 1 line
lib/xattr.c +1 -0
+}
#3 lore raw
Quoted 44 lines · Gao Xiang
Quoted 28 lines · Hongzhen Luo

Prepare for the feature of exporting extended attributes for fsck.erofs, which requires obtaining the index based on the name of the extended attribute.

---
  include/erofs/xattr.h | 2 ++
  lib/xattr.c           | 7 +++++++

  2 files changed, 9 insertions(+)
include/erofs/xattr.h +1 -0
index 7643611..7848fe7 100644
@@ -61,6 +61,8 @@ void erofs_clear_opaque_xattr(struct erofs_inode 
 *inode);
   int erofs_set_origin_xattr(struct erofs_inode *inode);
   int erofs_read_xattrs_from_disk(struct erofs_inode *inode);
   +int erofs_xattr_prefix_index(const char *key);
+
   #ifdef __cplusplus
   }

  #endif

lib/xattr.c +0 -0
index 9f31f2d..1fed7c0 100644

lib/xattr.c out: · line 1689

Quoted 9 lines
lib/xattr.c +6 -0
@@ -1681,3 +1681,10 @@ out:
           erofs_xattr_prefixes_cleanup(sbi);
       return ret;
   }
+
+int erofs_xattr_prefix_index(const char *key)
+{
+    unsigned int index, len;
+
+    return match_prefix(key, &index, &len) ? index : -EINVAL;

Can we export match_prefix as erofs_xattr_match_prefix() directly?

Quoted 3 lines

Thanks,
Gao Xiang

Yes, i will do this in the next patch.

Thanks,

Hongzhen

Quoted 1 line
Quoted 1 line
lib/xattr.c +1 -0
+}