erofs-utils: lib: expose erofs_xattr_prefix_matches()
PATCH v8 1/2 erofs-utils: lib: expose erofs_xattr_prefix_matches()
Prepare for the feature of exporting extended attributes for fsck.erofs.
---
v3: Change the name erofs_match_prefix() to erofs_xattr_prefix_matches().
v2: https://lore.kernel.org/all/[email protected]/
v1: https://lore.kernel.org/all/[email protected]/
--- include/erofs/xattr.h | 3 +++ lib/xattr.c | 12 +++++++----- 2 files changed, 10 insertions(+), 5 deletions(-)
include/erofs/xattr.h +3 -0
@@ -61,6 +61,9 @@ 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); +bool erofs_xattr_prefix_matches (const char *key,unsigned int *index, +unsigned int *len); + #ifdef __cplusplus }#endif
lib/xattr.c +7 -5
@@ -138,8 +138,8 @@ struct ea_type_node {static LIST_HEAD (ea_name_prefixes);static unsigned int ea_prefix_count; -static bool match_prefix (const char *key,unsigned int *index, -unsigned int *len) +bool erofs_xattr_prefix_matches (const char *key,unsigned int *index, +unsigned int *len) {struct xattr_prefix *p;
@@ -196,7 +196,8 @@ static struct xattr_item *get_xattritem(char *kvbuf, unsigned int len[2])if (!item)return ERR_PTR (-ENOMEM ); -if (!match_prefix (kvbuf, &item->base_index, &item->prefix_len)) { +if (!erofs_xattr_prefix_matches (kvbuf, &item->base_index, + &item->prefix_len)) {free (item);return ERR_PTR (-ENODATA ); }
@@ -1425,7 +1426,7 @@ int erofs_getxattr(struct erofs_inode *vi, const char *name, char *buffer,if (ret)return ret; -if (!match_prefix (name, &prefix, &prefixlen)) +if (!erofs_xattr_prefix_matches (name, &prefix, &prefixlen))return -ENODATA ; it.it.sbi = vi->sbi;
@@ -1600,7 +1601,8 @@ int erofs_xattr_insert_name_prefix(const char *prefix)if (!tnode)return -ENOMEM ; -if (!match_prefix (prefix, &tnode->base_index, &tnode->base_len)) { +if (!erofs_xattr_prefix_matches (prefix, &tnode->base_index, + &tnode->base_len)) {free (tnode);return -ENODATA ; }
Signature · 1 line
2.43.5
PATCH v8 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.
---
v8: The code has been adjusted to look better.
v7: https://lore.kernel.org/all/[email protected]/
v6: https://lore.kernel.org/all/[email protected]/
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 | 107 +++++++++++++++++++++++++++++++++++++++++++++-- man/fsck.erofs.1 | 3 ++ 2 files changed, 106 insertions(+), 4 deletions(-)
fsck/main.c +103 -4
@@ -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 off)\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,84 @@ out:return ret; } +static int erofsfsck_dump_xattrs (struct erofs_inode *inode) +{ +static bool ignore_xattrs = false; +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) { +erofs_err ("failed to list xattrs @ nid %llu" , + inode->nid |0ULL ); + ret = -EINVAL ; + goto out; + } + ret =0 ; +for (key = keylst; key < keylst + kllen; key +=strlen (key) +1 ) { +unsigned int index, len; +void *value =NULL ; +size_t size =0 ; + + ret =erofs_getxattr (inode, key,NULL ,0 ); +if (ret <=0 ) +break ; + size = ret; + value =malloc (size); +if (!value) { + ret = -ENOMEM ; +break ; + } + ret =erofs_getxattr (inode, key, value, size); +if (ret <0 ) { +erofs_err ("failed to get xattr `%s` @ nid %llu, because of `%s`" , key, + inode->nid |0ULL ,erofs_strerror (ret)); +free (value); +break ; + } +if (fsckcfg.extract_path) + ret =lsetxattr (fsckcfg.extract_path, key, value, size, +0 ); +else + ret =0 ; +free (value); +if (ret == -EPERM && !fsckcfg.superuser) { +if (__erofs_unlikely (!erofs_xattr_prefix_matches (key, + &index, &len))) { +erofs_err ("failed to match the prefix of `%s` @ nid %llu" , + key, inode->nid |0ULL ); + ret = -EINVAL ; +break ; + } +if (index !=EROFS_XATTR_INDEX_USER ) { +if (!ignore_xattrs) { +erofs_warn ("ignored xattr `%s` @ nid %llu, due to non-superuser" , + key, inode->nid |0ULL ); + ignore_xattrs = true; + } + ret =0 ; +continue ; + } + + } +if (ret) { +erofs_err ("failed to set xattr `%s` @ nid %llu because of `%s`" , + key, inode->nid |0ULL ,erofs_strerror (ret)); +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 +990,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 +1053,7 @@ int main(int argc, char *argv[]) fsckcfg.overwrite = false; fsckcfg.preserve_owner = fsckcfg.superuser; fsckcfg.preserve_perms = fsckcfg.superuser; + fsckcfg.dump_xattrs = false; err =erofsfsck_parse_options_cfg (argc, argv);if (err) {
man/fsck.erofs.1 +3 -0
@@ -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 off). +.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
Re: [PATCH v8 2/2] erofs-utils: fsck: introduce exporting xattrs
Quoted 161 lines · Hongzhen Luo
The current
fsck --extractdoes not support exporting the extended attributes of files. This patch adds--xattrsoption to dump the extended attributes, as well as the--no-xattrsoption to not dump the extended attributes.Signed-off-by Hongzhen Luo <[email protected]>---
v8: The code has been adjusted to look better.
v7: https://lore.kernel.org/all/[email protected]/
v6: https://lore.kernel.org/all/[email protected]/
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 | 107 +++++++++++++++++++++++++++++++++++++++++++++-- man/fsck.erofs.1 | 3 ++ 2 files changed, 106 insertions(+), 4 deletions(-)fsck/main.c +90
-0@@ -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 off)\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,84 @@ out:return ret; } +static int erofsfsck_dump_xattrs (struct erofs_inode *inode) +{ +static bool ignore_xattrs = false; +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) { +erofs_err ("failed to list xattrs @ nid %llu" , + inode->nid |0ULL ); + ret = -EINVAL ; + goto out; + } + ret =0 ; +for (key = keylst; key < keylst + kllen; key +=strlen (key) +1 ) { +unsigned int index, len; +void *value =NULL ; +size_t size =0 ; + + ret =erofs_getxattr (inode, key,NULL ,0 ); +if (ret <=0 ) +break ; + size = ret; + value =malloc (size); +if (!value) { + ret = -ENOMEM ; +break ; + } + ret =erofs_getxattr (inode, key, value, size); +if (ret <0 ) { +erofs_err ("failed to get xattr `%s` @ nid %llu, because of `%s`" , key, + inode->nid |0ULL ,erofs_strerror (ret)); +free (value); +break ; + } +if (fsckcfg.extract_path) + ret =lsetxattr (fsckcfg.extract_path, key, value, size, +0 ); +else + ret =0 ; +free (value); +if (ret == -EPERM && !fsckcfg.superuser) { +if (__erofs_unlikely (!erofs_xattr_prefix_matches (key, + &index, &len))) { +erofs_err ("failed to match the prefix of `%s` @ nid %llu" , + key, inode->nid |0ULL ); + ret = -EINVAL ; +break ; + } +if (index !=EROFS_XATTR_INDEX_USER ) { +if (!ignore_xattrs) { +erofs_warn ("ignored xattr `%s` @ nid %llu, due to non-superuser" , + key, inode->nid |0ULL ); + ignore_xattrs = true; + } + ret =0 ; +continue ; + } + + } +if (ret) { +erofs_err ("failed to set xattr `%s` @ nid %llu because of `%s`" , + key, inode->nid |0ULL ,erofs_strerror (ret)); +break ; + } + } +out: +free (keylst); +return ret; +} +static int erofs_verify_inode_data (struct erofs_inode *inode,int outfd) {struct erofs_map_blocks map = {
fsck/main.c static int erofsfsck_check_inode(erofs_nid_t pnid, erofs_nid_t nid) · line 993
Quoted 8 lines
fsck/main.c +1
-4@@ -900,15 +990,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) {
It should be !(fsckcfg.check_decomp && fsckcfg.dump_xattr)?
Quoted 32 lines
fsck/main.c +12
-0+/* 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 +1053,7 @@ int main(int argc, char *argv[]) fsckcfg.overwrite = false; fsckcfg.preserve_owner = fsckcfg.superuser; fsckcfg.preserve_perms = fsckcfg.superuser; + fsckcfg.dump_xattrs = false; err =erofsfsck_parse_options_cfg (argc, argv);if (err) {man/fsck.erofs.1 +0
-0
man/fsck.erofs.1 You may give multiple · line 31
Quoted 5 lines
man/fsck.erofs.1 +2
-0@@ -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 off).
Why is it placed here? (it should be located according to alphabetic order)
Also better to beWhether to dump extended attributes during extraction (default off)
Thanks,
Gao Xiang
Re: [PATCH v8 2/2] erofs-utils: fsck: introduce exporting xattrs
Quoted 168 lines · Gao Xiang
Quoted 165 lines · Hongzhen Luo
The current
fsck --extractdoes not support exporting the extended attributes of files. This patch adds--xattrsoption to dump the extended attributes, as well as the--no-xattrsoption to not dump the extended attributes.Signed-off-by Hongzhen Luo <[email protected]>---
v8: The code has been adjusted to look better.
v7: https://lore.kernel.org/all/[email protected]/
v6: https://lore.kernel.org/all/[email protected]/
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 | 107 +++++++++++++++++++++++++++++++++++++++++++++-- man/fsck.erofs.1 | 3 ++2 files changed, 106 insertions(+), 4 deletions(-)fsck/main.c +3
-0@@ -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 voidusage(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 off)\n"
"\n"
" -a, -A, -y no-op, for compatibility with fsck of other filesystems\n" "\n"fsck/main.c +6
-0@@ -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;
}fsck/main.c +68
-0@@ -411,6 +423,84 @@ out: return ret; } +static int erofsfsck_dump_xattrs(struct erofs_inode *inode) +{ + static bool ignore_xattrs = false; + 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) { + erofs_err("failed to list xattrs @ nid %llu", + inode->nid | 0ULL); + ret = -EINVAL; + goto out; + } + ret = 0; + for (key = keylst; key < keylst + kllen; key += strlen(key) + 1) { + unsigned int index, len; + void *value = NULL; + size_t size = 0; + + ret = erofs_getxattr(inode, key, NULL, 0); + if (ret <= 0) + break; + size = ret; + value = malloc(size); + if (!value) { + ret = -ENOMEM; + break; + } + ret = erofs_getxattr(inode, key, value, size); + if (ret < 0) { + erofs_err("failed to get xattr `%s` @ nid %llu, because of `%s`", key, + inode->nid | 0ULL, erofs_strerror(ret)); + free(value); + break; + } + if (fsckcfg.extract_path) + ret = lsetxattr(fsckcfg.extract_path, key, value, size, + 0); + else + ret = 0; + free(value); + if (ret == -EPERM && !fsckcfg.superuser) { + if (__erofs_unlikely(!erofs_xattr_prefix_matches(key, + &index, &len))) { + erofs_err("failed to match the prefix of `%s` @ nid %llu", + key, inode->nid | 0ULL); + ret = -EINVAL; + break; + } + if (index != EROFS_XATTR_INDEX_USER) { + if (!ignore_xattrs) { + erofs_warn("ignored xattr `%s` @ nid %llu, due to non-superuser", + key, inode->nid | 0ULL); + ignore_xattrs = true; + } + ret = 0; + continue; + } + + } + if (ret) { + erofs_err("failed to set xattr `%s` @ nid %llu becauseof
%s",
+ key,inode->nid| 0ULL,erofs_strerror(ret)); + break;
+ }
+ }fsck/main.c +5
-0+out: + free(keylst); + return ret; +} +static int
erofs_verify_inode_data(struct erofs_inode *inode, int outfd){
structerofs_map_blocksmap = {fsck/main.c +0
-0@@ -900,15 +990,23 @@ static int erofsfsck_check_inode(erofs_nid_t pnid, erofs_nid_t nid) goto out;
Quoted 9 lines
Quoted 6 lines
fsck/main.c +0
-0}- /* verify xattr field */
- ret =erofs_verify_xattr(&inode);
- if (ret)
- goto out;
+ if (!fsckcfg.check_decomp) {It should be
!(fsckcfg.check_decomp && fsckcfg.dump_xattr)?
Yes, I will fix this in the next patch.
Quoted 38 lines
Quoted 29 lines
+ /* 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)) {
structerofs_dir_contextctx = {fsck/main.c +1
-0@@ -955,6 +1053,7 @@ int main(int argc, char *argv[]) fsckcfg.overwrite = false; fsckcfg.preserve_owner = fsckcfg.superuser; fsckcfg.preserve_perms = fsckcfg.superuser; + fsckcfg.dump_xattrs = false; err = erofsfsck_parse_options_cfg(argc, argv); if (err) {man/fsck.erofs.1 +0
-0@@ -27,6 +27,9 @@ You may give multipleQuoted 5 lines
.B --device
options in the correct order.
.TP
+.BI "--[no-]xattrs"
+Whether to dump extended attributes (default off).Why is it placed here? (it should be located according to alphabetic order)
Quoted 3 lines
Also better to be
Whether to dump extended attributes during extraction (default off)
Yes. I will send a new version soon.
Thanks,
Hongzhen Luo
Quoted 2 lines
Thanks,
Gao Xiang