erofs-utils: lib: add API to iterate dirs in EROFS
PATCH v6 1/1 erofs-utils: lib: add API to iterate dirs in EROFS
From: Kelvin Zhang <[email protected]>
This introduces erofs_iterate_dir() to read all dirents in a directory inode.
Note that it doesn't recursively walk into sub-directories. If it's really needed, users should handle this in the callback.
---
v5: https://lore.kernel.org/r/[email protected]
Changes since v5:
- heavily changed, most logic was borrowed from fsck.erofs;
- use GPL-2.0+ OR Apache-2.0 dual license.
fuse/Makefile.am | 2 +- fuse/dir.c | 100 ------------------------- fuse/main.c | 49 ++++++++++++- include/erofs/dir.h | 44 +++++++++++ lib/Makefile.am | 2 +- lib/dir.c | 173 ++++++++++++++++++++++++++++++++++++++++++++ 6 files changed, 266 insertions(+), 104 deletions(-) delete mode 100644 fuse/dir.c create mode 100644 include/erofs/dir.h create mode 100644 lib/dir.c
fuse/Makefile.am +1 -1
@@ -3,7 +3,7 @@ AUTOMAKE_OPTIONS = foreign noinst_HEADERS = $(top_srcdir)/fuse/macosx.h bin_PROGRAMS = erofsfuse -erofsfuse_SOURCES = dir.c main.c +erofsfuse_SOURCES = main.c erofsfuse_CFLAGS = -Wall -Werror -I$(top_srcdir)/include erofsfuse_CFLAGS += -DFUSE_USE_VERSION=26 ${libfuse_CFLAGS} ${libselinux_CFLAGS} erofsfuse_LDADD = $(top_builddir)/lib/liberofs.la ${libfuse_LIBS} ${liblz4_LIBS} \
fuse/dir.c deleted +0 -100
100 lines not on this page · raw message
fuse/main.c +47 -2
56 lines not on this page · raw message
include/erofs/dir.h new file +44 -0
44 lines not on this page · raw message
lib/Makefile.am +1 -1
@@ -27,7 +27,7 @@ noinst_HEADERS = $(top_srcdir)/include/erofs_fs.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 + compress_hints.c hashmap.c sha256.c blobchunk.c dir.c liberofs_la_CFLAGS = -Wall -Werror -I$(top_srcdir)/include if ENABLE_LZ4 liberofs_la_CFLAGS += ${LZ4_CFLAGS}
lib/dir.c new file +173 -0
173 lines not on this page · raw message
Signature · 1 line
2.24.4
I don't like the current API design.
1. Input and output to erofs_iterate_dir are mixed in the same struct. It's unclear to the caller which members are required input, and which ones will be filled out by erofs_iterate_dir. I prefer the old way where input parameters are explicitly listed, and the context struct consists of only output members.
2. When erofs_readdir_cb is called, is the callback allowed to modify members inside erofs_dir_context ? If not, the pointer should be marked as const, or pass the struct by value. Or explicitly document that any modification to the context struct will be ignored.
3. We are doing dynamic memory allocations inside a loop. Can we just use two stack buffers?
4. Many of the current use cases(dump, fsck, android) want to traverse directories recursively. Instead of duplicating the work and let each user write its own logic to do recursive traversal, provide a sensical default implementation that traverses directory recursively? This should be a straightforward wrapper around erofs_iterate_dir. For example, adding a erofs_iterate_dir_recursive API.
Quoted 470 lines · Gao Xiang
From: Kelvin Zhang <[email protected]>
This introduces
erofs_iterate_dir()to read all dirents in a directory inode.Note that it doesn't recursively walk into sub-directories. If it's really needed, users should handle this in the callback.
Signed-off-by Kelvin Zhang <[email protected]>[ Gao Xiang: heavily changed and convert erofsfuse to use this. ]Signed-off-by Gao Xiang <[email protected]>---
v5:
https://lore.kernel.org/r/[email protected]
Changes since v5:
- heavily changed, most logic was borrowed from fsck.erofs;
- use GPL-2.0+ OR Apache-2.0 dual license.fuse/Makefile.am | 2 +- fuse/dir.c | 100 ------------------------- fuse/main.c | 49 ++++++++++++- include/erofs/dir.h | 44 +++++++++++ lib/Makefile.am | 2 +- lib/dir.c | 173 ++++++++++++++++++++++++++++++++++++++++++++ 6 files changed, 266 insertions(+), 104 deletions(-) delete mode 100644 fuse/dir.c create mode 100644 include/erofs/dir.h create mode 100644 lib/dir.cfuse/Makefile.am +1
-1@@ -3,7 +3,7 @@ AUTOMAKE_OPTIONS = foreign noinst_HEADERS = $(top_srcdir)/fuse/macosx.h bin_PROGRAMS = erofsfuse -erofsfuse_SOURCES = dir.c main.c +erofsfuse_SOURCES = main.c erofsfuse_CFLAGS = -Wall -Werror -I$(top_srcdir)/include erofsfuse_CFLAGS += -DFUSE_USE_VERSION=26 ${libfuse_CFLAGS} ${libselinux_CFLAGS}
erofsfuse_LDADD= $(top_builddir)/lib/liberofs.la ${libfuse_LIBS}
${liblz4_LIBS} \fuse/dir.c deleted +0
-36@@ -1,100 +0,0 @@ -// SPDX-License-Identifier: GPL-2.0+ -/* -* Created by Li Guifu <[email protected]> -*/ -#include <fuse.h> -#include <fuse_opt.h> -#include "macosx.h" -#include "erofs/internal.h" -#include "erofs/print.h" - -static int erofs_fill_dentries (struct erofs_inode *dir, -fuse_fill_dir_t filler,void *buf, -void *dblk,unsigned int nameoff, -unsigned int maxsize) -{ -struct erofs_dirent *de = dblk; -const struct erofs_dirent *end = dblk + nameoff; -char namebuf[EROFS_NAME_LEN +1 ]; - -while (de < end) { -const char *de_name; -unsigned int de_namelen; - - nameoff =le16_to_cpu (de->nameoff); - de_name = (char *)dblk + nameoff; - -/* the last dirent in the block? */ -if (de +1 >= end) - de_namelen =strnlen (de_name, maxsize - nameoff); -else - de_namelen =le16_to_cpu (de[1 ].nameoff) - nameoff; - -/* a corrupted entry is found */ -if (nameoff + de_namelen > maxsize || - de_namelen >EROFS_NAME_LEN ) { - erofs_err("bogus dirent @ nid %llu", dir->nid |0ULL);
fuse/dir.c deleted +0
-64-DBG_BUGON (1 ); -return -EFSCORRUPTED ; - } - -memcpy (namebuf, de_name, de_namelen); - namebuf[de_namelen] ='\0' ; - -filler (buf, namebuf,NULL ,0 ); - ++de; - } -return 0 ; -} - -int erofsfuse_readdir (const char *path,void *buf,fuse_fill_dir_t filler, -off_t offset,struct fuse_file_info *fi) -{ -int ret; -struct erofs_inode dir; -char dblk[EROFS_BLKSIZ ]; -erofs_off_t pos; - -erofs_dbg ("readdir:%s offset=%llu" , path, (long long )offset); - - ret =erofs_ilookup (path, &dir); -if (ret) -return ret; - -erofs_dbg ("path=%s nid = %llu" , path, dir.nid |0ULL ); - -if (!S_ISDIR (dir.i_mode)) -return -ENOTDIR ; - -if (!dir.i_size) -return 0 ; - - pos =0 ; -while (pos < dir.i_size) { -unsigned int nameoff, maxsize; -struct erofs_dirent *de; - - maxsize = min_t(unsigned int, EROFS_BLKSIZ, - dir.i_size - pos); - ret =erofs_pread (&dir, dblk, maxsize, pos); -if (ret) -return ret; - - de = (struct erofs_dirent *)dblk; - nameoff =le16_to_cpu (de->nameoff); -if (nameoff <sizeof (struct erofs_dirent ) || - nameoff >=PAGE_SIZE ) { -erofs_err ("invalid de[0].nameoff %u @ nid %llu" , - nameoff, dir.nid |0ULL ); - ret = -EFSCORRUPTED ; -break ; - } - - ret =erofs_fill_dentries (&dir, filler, buf, - dblk, nameoff, maxsize); -if (ret) -break ; - pos += maxsize; - } -return 0 ; -}fuse/main.c +47
-2@@ -12,9 +12,54 @@#include "erofs/config.h" #include "erofs/print.h" #include "erofs/io.h" +#include "erofs/dir.h" -int erofsfuse_readdir (const char *path,void *buffer,fuse_fill_dir_t filler, -off_t offset,struct fuse_file_info *fi); +struct erofsfuse_dir_context { +struct erofs_dir_context ctx; +fuse_fill_dir_t filler; +struct fuse_file_info *fi; +void *buf; +}; + +static int erofsfuse_fill_dentries (struct erofs_dir_context *ctx) +{ +struct erofsfuse_dir_context *fusectx = (void *)ctx; + + fusectx->filler (fusectx->buf, ctx->dname,NULL ,0 ); +return 0 ; +} + +int erofsfuse_readdir (const char *path,void *buf,fuse_fill_dir_t filler, +off_t offset,struct fuse_file_info *fi) +{ +int ret; +struct erofs_inode dir; +struct erofsfuse_dir_context ctx = { + .ctx.dir = &dir, + .ctx.cb = erofsfuse_fill_dentries, + .filler = filler, + .fi = fi, + .buf = buf, + }; +erofs_dbg ("readdir:%s offset=%llu" , path, (long long )offset); + + ret =erofs_ilookup (path, &dir); +if (ret) +return ret; + +erofs_dbg ("path=%s nid = %llu" , path, dir.nid |0ULL ); +if (!S_ISDIR (dir.i_mode)) +return -ENOTDIR ; + +if (!dir.i_size) +return 0 ; +#ifdef NDEBUG +return erofs_iterate_dir (&ctx.ctx, false); +#else +return erofs_iterate_dir (&ctx.ctx, true); +#endif + +}static void *erofsfuse_init (struct fuse_conn_info *info){
include/erofs/dir.h new file +44
-0@@ -0,0 +1,44 @@ +/* SPDX-License-Identifier: GPL-2.0+ OR Apache-2.0 */ +#ifndef __EROFS_DIR_H +#define __EROFS_DIR_H + +#ifdef __cplusplus +extern "C" +{ +#endif + +#include "internal.h" + +#define EROFS_READDIR_VALID_PNID 0x0001 +#define EROFS_READDIR_DOTDOT_FOUND 0x0002 +#define EROFS_READDIR_DOT_FOUND 0x0004 + +#define EROFS_READDIR_ALL_SPECIAL_FOUND \ + (EROFS_READDIR_DOTDOT_FOUND | EROFS_READDIR_DOT_FOUND) + +struct erofs_dir_context ; + +/* callback function for iterating over inodes of EROFS */ +typedef int (*erofs_readdir_cb )(struct erofs_dir_context *); + +/* callers could use a wrapper to contain extra information */ +struct erofs_dir_context { +erofs_nid_t pnid;/* optional */ +struct erofs_inode *dir; +erofs_readdir_cb cb; + +/* dirent information which is currently found */ +erofs_nid_t nid; +const char *dname; +u8 ftype, flags; +bool dot_dotdot; +}; + +/* iterate over inodes that are in directory */ +int erofs_iterate_dir (struct erofs_dir_context *ctx,bool fsck); + +#ifdef __cplusplus +} +#endif + +#endif lib/Makefile.am +1
-1@@ -27,7 +27,7 @@ noinst_HEADERS = $(top_srcdir)/include/erofs_fs.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 + compress_hints.c hashmap.c sha256.c blobchunk.c dir.c liberofs_la_CFLAGS = -Wall -Werror -I$(top_srcdir)/includeif
ENABLE_LZ4
liberofs_la_CFLAGS+= ${LZ4_CFLAGS}lib/dir.c new file +8
-0@@ -0,0 +1,173 @@ +// SPDX-License-Identifier: GPL-2.0+ OR Apache-2.0 +#include "erofs/print.h" +#include "erofs/dir.h" +#include <stdlib.h> + +static int traverse_dirents (struct erofs_dir_context *ctx, +void *dentry_blk,unsigned int lblk, + unsigned int next_nameoff, unsigned intmaxsize,
lib/dir.c new file +46
-0+ bool fsck) +{ +struct erofs_dirent *de = dentry_blk; +const struct erofs_dirent *end = dentry_blk + next_nameoff; +char *prev_name =NULL , *cur_name; +const char *errmsg; +int ret =0 ; +bool silent = false; + +while (de < end) { +const char *de_name; +unsigned int de_namelen; +unsigned int nameoff; + + nameoff =le16_to_cpu (de->nameoff); + de_name = (char *)dentry_blk + nameoff; + +/* the last dirent check */ +if (de +1 >= end) + de_namelen =strnlen (de_name, maxsize - nameoff); +else + de_namelen =le16_to_cpu (de[1 ].nameoff) - nameoff; + + cur_name =strndup (de_name, de_namelen); +if (!cur_name) { + errmsg ="failed to allocate dirent name" ; + ret = -ENOMEM ; +break ; + } + +erofs_dbg ("traversed filename(%s)" , cur_name); + + ret = -EFSCORRUPTED ; +/* corrupted entry check */ +if (nameoff != next_nameoff) { + errmsg ="bogus dirent nameoff" ; +break ; + } + +if (nameoff + de_namelen > maxsize || + de_namelen >EROFS_NAME_LEN ) { + errmsg ="bogus dirent namelen" ; +break ; + } + +if (fsck && prev_name &&strcmp (prev_name, cur_name) >=0 ){
lib/dir.c new file +15
-0+ errmsg ="wrong dirent name order" ; +break ; + } + +if (fsck && de->file_type >=EROFS_FT_MAX ) { + errmsg ="invalid file type %u" ; +break ; + } + + ctx->dot_dotdot =is_dot_dotdot (cur_name); +if (ctx->dot_dotdot) { +switch (de_namelen) { +case 2 : + if (fsck && + (ctx->flags &
EROFS_READDIR_DOTDOT_FOUND)) {lib/dir.c new file +7
-0+ errmsg ="duplicated `..' dirent" ; + goto out; + } + ctx->flags |=EROFS_READDIR_DOTDOT_FOUND ; +if (sbi.root_nid == ctx->dir->nid) { + ctx->pnid = sbi.root_nid; + ctx->flags |=
EROFS_READDIR_VALID_PNID;lib/dir.c new file +3
-0+ } +if (fsck && + (ctx->flags & EROFS_READDIR_VALID_PNID)&&
lib/dir.c new file +8
-0+ de->nid != ctx->pnid) { + errmsg ="corrupted `..' dirent" ; + goto out; + } +break ; +case 1 : + if (fsck && + (ctx->flags &
EROFS_READDIR_DOT_FOUND)) {lib/dir.c new file +61
-0+ errmsg ="duplicated `.' dirent" ; + goto out; + } + + ctx->flags |=EROFS_READDIR_DOT_FOUND ; +if (fsck && de->nid != ctx->dir->nid) { + errmsg ="corrupted `.' dirent" ; + goto out; + } +break ; + } + } + ctx->ftype = de->file_type, + ctx->nid = de->nid; + ctx->dname = cur_name; + ret = ctx->cb (ctx); +if (ret) { + silent = true; + goto out; + } + next_nameoff += de_namelen; + ++de; +if (prev_name) +free (prev_name); + prev_name = cur_name; + cur_name =NULL ; + } +out: +if (prev_name) +free (prev_name); +if (cur_name) +free (cur_name); +if (ret && !silent) +erofs_err ("%s @ nid %llu, lblk %u, index %lu" , + errmsg, ctx->dir->nid |0ULL , lblk, + (de - (struct erofs_dirent *)dentry_blk) |0UL ); +return ret; +} + +int erofs_iterate_dir (struct erofs_dir_context *ctx,bool fsck) +{ +struct erofs_inode *dir = ctx->dir; +int err; +erofs_off_t pos; +char buf[EROFS_BLKSIZ ]; + +if ((dir->i_mode &S_IFMT ) !=S_IFDIR ) +return -ENOTDIR ; + + ctx->flags &= ~EROFS_READDIR_ALL_SPECIAL_FOUND ; + pos =0 ; +while (pos < dir->i_size) { +erofs_blk_t lblk =erofs_blknr (pos); +erofs_off_t maxsize =min_t (erofs_off_t, + dir->i_size - pos,EROFS_BLKSIZ ); +const struct erofs_dirent *de = (const void *)buf; +unsigned int nameoff; + + err =erofs_pread (dir, buf, maxsize, pos); +if (err) { + erofs_err("I/O error occurred when reading dirents@ nid %llu, lblk %u: %d",
lib/dir.c new file +8
-0+ dir->nid | 0ULL, lblk, err); +return err; + } + + nameoff =le16_to_cpu (de->nameoff); +if (nameoff <sizeof (struct erofs_dirent ) || + nameoff >=PAGE_SIZE ) { + erofs_err("invalid de[0].nameoff %u @ nid %llu,lblk %u",
lib/dir.c new file +4
-0+ nameoff, dir->nid | 0ULL, lblk); +return -EFSCORRUPTED ; + } + err = traverse_dirents(ctx, buf, lblk, nameoff, maxsize,fsck);
lib/dir.c new file +13
-1+if (err) +break ; + pos += maxsize; + } + +if (fsck && (ctx->flags &EROFS_READDIR_ALL_SPECIAL_FOUND ) != +EROFS_READDIR_ALL_SPECIAL_FOUND ) { +erofs_err ("`.' or `..' dirent is missing @ nid %llu" , + dir->nid |0ULL ); +return -EFSCORRUPTED ; + } +return 0 ; +} --2.24.4
Signature · 3 lines
Sincerely, Kelvin Zhang
Quoted 2 lines · Kelvin Zhang
I don't like the current API design.
Ok, if you don't like the current way, I will convert the author instead.
Here is my answer why it designs like this:
Quoted 5 lines
1. Input and output to
erofs_iterate_dirare mixed in the same struct. It's unclear to the caller which members are required input, and which ones will be filled out byerofs_iterate_dir. I prefer the old way where input parameters are explicitly listed, and the context struct consists of only output members.
The problem is there are too many input paramters, and I don't think they needs to be passed on stack (maybe some by registers, but it depends) over and over again.
Actually if you looked at some kernel code, there are many such usage:
https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/include/linux/pagemap.h#n982
struct readahead_control;
And
https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/include/linux/fs.h#n2023dir_context, etc.
Quoted 4 lines
2. When
erofs_readdir_cbis called, is the callback allowed to modify members insideerofs_dir_context? If not, the pointer should be marked as const, or pass the struct by value. Or explicitly document that any modification to the context struct will be ignored.
Ok, actually I'm either fine to add or not add `const'. But I have to say, erofs-utils is not a C++ project.
But yes, marking const is a good habit. I'm fine to take all of this.
Quoted 2 lines
3. We are doing dynamic memory allocations inside a loop. Can we just use two stack buffers?
I thought before, but I do also care stack overflow (it somewhat happens on my Mac OS computer.) Dynamic-sized object on stack is tricky, I'd like to avoid this like what kernel does.
Quoted 6 lines
4. Many of the current use
cases(dump, fsck, android)want to traverse directories recursively. Instead of duplicating the work and let each user write its own logic to do recursive traversal, provide a sensical default implementation that traverses directory recursively? This should be a straightforward wrapper arounderofs_iterate_dir. For example, adding aerofs_iterate_dir_recursiveAPI.
Ok, if you think that saves code, that's fine.
However, fuse don't need to recursively traverse directories. And many cases don't need to traverse directories in the DFS approach. So, a new erofs_iterate_dir_recursive API is ok if it really saves code.
Thanks,
Gao Xiang
PATCH v7 1/2 Add API to get full pathname of an inode
--- dump/main.c | 70 +---------------------- include/erofs/internal.h | 5 ++ lib/namei.c | 120 +++++++++++++++++++++++++++++++++++++-- 3 files changed, 124 insertions(+), 71 deletions(-)
dump/main.c +3 -67
@@ -11,6 +11,7 @@#include "erofs/print.h" #include "erofs/inode.h" #include "erofs/io.h" +#include "erofs/internal.h" #ifdef HAVE_LIBUUID #include <uuid.h>
@@ -365,71 +366,6 @@ static int erofs_read_dir(erofs_nid_t nid, erofs_nid_t parent_nid)return 0 ; } -static int erofs_get_pathname (erofs_nid_t nid,erofs_nid_t parent_nid, -erofs_nid_t target,char *path,unsigned int pos) -{ -int err; -erofs_off_t offset; -char buf[EROFS_BLKSIZ ]; -struct erofs_inode inode = { .nid = nid }; - - path[pos++] ='/' ; -if (target == sbi.root_nid) -return 0 ; - - err =erofs_read_inode_from_disk (&inode); -if (err) { -erofs_err ("read inode failed @ nid %llu" , nid |0ULL ); -return err; - } - - offset =0 ; -while (offset < inode.i_size) { -erofs_off_t maxsize =min_t (erofs_off_t, - inode.i_size - offset,EROFS_BLKSIZ ); -struct erofs_dirent *de = (void *)buf; -struct erofs_dirent *end; -unsigned int nameoff; - - err =erofs_pread (&inode, buf, maxsize, offset); -if (err) -return err; - - nameoff =le16_to_cpu (de->nameoff); - end = (void *)buf + nameoff; -while (de < end) { -const char *dname; -int len; - - nameoff =le16_to_cpu (de->nameoff); - dname = (char *)buf + nameoff; - len =erofs_checkdirent (de, end, maxsize, dname); -if (len <0 ) -return len; - -if (de->nid == target) { -memcpy (path + pos, dname, len); - path[pos + len] ='\0' ; -return 0 ; - } - -if (de->file_type ==EROFS_FT_DIR && - de->nid != parent_nid && - de->nid != nid) { -memcpy (path + pos, dname, len); - err =erofs_get_pathname (de->nid, nid, - target, path, pos + len); -if (!err) -return 0 ; -memset (path + pos,0 , len); - } - ++de; - } - offset += maxsize; - } -return -1 ; -} -static int erofsdump_map_blocks (struct erofs_inode *inode,struct erofs_map_blocks *map,int flags) {
@@ -478,12 +414,12 @@ static void erofsdump_show_fileinfo(bool show_extent)return ; } - err =erofs_get_pathname (sbi.root_nid, sbi.root_nid, - inode.nid, path,0 ); + err =erofs_get_inode_name (&sbi, inode.nid, path,PATH_MAX +1 );if (err <0 ) {erofs_err ("file path not found @ nid %llu" , inode.nid |0ULL );return ; } +printf ("Path: %s\n" , path);strftime (timebuf,sizeof (timebuf),"%Y-%m-%d %H:%M:%S" ,localtime ((time_t *)&inode.i_ctime));
include/erofs/internal.h +5 -0
@@ -305,6 +305,11 @@ int erofs_read_superblock(void);int erofs_read_inode_from_disk (struct erofs_inode *vi);int erofs_ilookup (const char *path,struct erofs_inode *vi);int erofs_read_inode_from_disk (struct erofs_inode *vi); +// Get full path name of an inode, return 0 if success +int erofs_get_inode_name (const struct erofs_sb_info *sbi, +erofs_nid_t target, +char *output_buffer, +size_t capacity);/* data.c */ int erofs_pread(struct erofs_inode *inode, char *buf,
lib/namei.c +116 -4
147 lines not on this page · raw message
Signature · 1 line
2.34.1.173.g76aa8bc2d0-goog
PATCH v7 2/2 erofs-utils: lib: add API to iterate dirs in EROFS
--- fuse/Makefile.am | 2 +- fuse/dir.c | 100 -------------------- fuse/main.c | 65 +++++++++++-- include/erofs/dir.h | 52 +++++++++++ lib/Makefile.am | 2 +- lib/dir.c | 223 ++++++++++++++++++++++++++++++++++++++++++++ 6 files changed, 334 insertions(+), 110 deletions(-) delete mode 100644 fuse/dir.c create mode 100644 include/erofs/dir.h create mode 100644 lib/dir.c
fuse/Makefile.am +1 -1
@@ -2,7 +2,7 @@ AUTOMAKE_OPTIONS = foreign bin_PROGRAMS = erofsfuse -erofsfuse_SOURCES = dir.c main.c +erofsfuse_SOURCES = main.c erofsfuse_CFLAGS = -Wall -Werror -I$(top_srcdir)/include erofsfuse_CFLAGS += -DFUSE_USE_VERSION=26 ${libfuse_CFLAGS} ${libselinux_CFLAGS} erofsfuse_LDADD = $(top_builddir)/lib/liberofs.la ${libfuse_LIBS} ${liblz4_LIBS} \
fuse/dir.c deleted +0 -100
100 lines not on this page · raw message
fuse/main.c +57 -8
86 lines not on this page · raw message
include/erofs/dir.h new file +52 -0
52 lines not on this page · raw message
lib/Makefile.am +1 -1
@@ -27,7 +27,7 @@ noinst_HEADERS = $(top_srcdir)/include/erofs_fs.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 + compress_hints.c hashmap.c sha256.c blobchunk.c dir.c liberofs_la_CFLAGS = -Wall -Werror -I$(top_srcdir)/include if ENABLE_LZ4 liberofs_la_CFLAGS += ${LZ4_CFLAGS}
lib/dir.c new file +223 -0
223 lines not on this page · raw message
Signature · 1 line
2.34.1.173.g76aa8bc2d0-goog
Hi Gao,
I drafted a new patchset on top of the dev branch. Changes since v6:
1. block buffer moved to the heap, due to stack size concerns when
iterating recursively
2. Added a "recursive" option to input parameters
3. dname buffers are still on the heap, but allocation is done once per
directory, instead of once for each directory entry.
4. Added a void* arg which will be forwarded to the callback function.
I ran scripts/checkpatch.pl . Hopefully this makes your life easier. Thanks for the reply!.
Quoted 555 lines · Kelvin Zhang
Signed-off-by Kelvin Zhang <[email protected]>--- fuse/Makefile.am | 2 +- fuse/dir.c | 100 -------------------- fuse/main.c | 65 +++++++++++-- include/erofs/dir.h | 52 +++++++++++ lib/Makefile.am | 2 +- lib/dir.c | 223 ++++++++++++++++++++++++++++++++++++++++++++ 6 files changed, 334 insertions(+), 110 deletions(-) delete mode 100644 fuse/dir.c create mode 100644 include/erofs/dir.h create mode 100644 lib/dir.cfuse/Makefile.am +1
-1@@ -2,7 +2,7 @@ AUTOMAKE_OPTIONS = foreign bin_PROGRAMS = erofsfuse -erofsfuse_SOURCES = dir.c main.c +erofsfuse_SOURCES = main.c erofsfuse_CFLAGS = -Wall -Werror -I$(top_srcdir)/include erofsfuse_CFLAGS += -DFUSE_USE_VERSION=26 ${libfuse_CFLAGS} ${libselinux_CFLAGS}
erofsfuse_LDADD= $(top_builddir)/lib/liberofs.la ${libfuse_LIBS}
${liblz4_LIBS} \fuse/dir.c deleted +0
-36@@ -1,100 +0,0 @@ -// SPDX-License-Identifier: GPL-2.0+ -/* -* Created by Li Guifu <[email protected]> -*/ -#include <fuse.h> -#include <fuse_opt.h> -#include "macosx.h" -#include "erofs/internal.h" -#include "erofs/print.h" - -static int erofs_fill_dentries (struct erofs_inode *dir, -fuse_fill_dir_t filler,void *buf, -void *dblk,unsigned int nameoff, -unsigned int maxsize) -{ -struct erofs_dirent *de = dblk; -const struct erofs_dirent *end = dblk + nameoff; -char namebuf[EROFS_NAME_LEN +1 ]; - -while (de < end) { -const char *de_name; -unsigned int de_namelen; - - nameoff =le16_to_cpu (de->nameoff); - de_name = (char *)dblk + nameoff; - -/* the last dirent in the block? */ -if (de +1 >= end) - de_namelen =strnlen (de_name, maxsize - nameoff); -else - de_namelen =le16_to_cpu (de[1 ].nameoff) - nameoff; - -/* a corrupted entry is found */ -if (nameoff + de_namelen > maxsize || - de_namelen >EROFS_NAME_LEN ) { - erofs_err("bogus dirent @ nid %llu", dir->nid |0ULL);
fuse/dir.c deleted +0
-64-DBG_BUGON (1 ); -return -EFSCORRUPTED ; - } - -memcpy (namebuf, de_name, de_namelen); - namebuf[de_namelen] ='\0' ; - -filler (buf, namebuf,NULL ,0 ); - ++de; - } -return 0 ; -} - -int erofsfuse_readdir (const char *path,void *buf,fuse_fill_dir_t filler, -off_t offset,struct fuse_file_info *fi) -{ -int ret; -struct erofs_inode dir; -char dblk[EROFS_BLKSIZ ]; -erofs_off_t pos; - -erofs_dbg ("readdir:%s offset=%llu" , path, (long long )offset); - - ret =erofs_ilookup (path, &dir); -if (ret) -return ret; - -erofs_dbg ("path=%s nid = %llu" , path, dir.nid |0ULL ); - -if (!S_ISDIR (dir.i_mode)) -return -ENOTDIR ; - -if (!dir.i_size) -return 0 ; - - pos =0 ; -while (pos < dir.i_size) { -unsigned int nameoff, maxsize; -struct erofs_dirent *de; - - maxsize = min_t(unsigned int, EROFS_BLKSIZ, - dir.i_size - pos); - ret =erofs_pread (&dir, dblk, maxsize, pos); -if (ret) -return ret; - - de = (struct erofs_dirent *)dblk; - nameoff =le16_to_cpu (de->nameoff); -if (nameoff <sizeof (struct erofs_dirent ) || - nameoff >=PAGE_SIZE ) { -erofs_err ("invalid de[0].nameoff %u @ nid %llu" , - nameoff, dir.nid |0ULL ); - ret = -EFSCORRUPTED ; -break ; - } - - ret =erofs_fill_dentries (&dir, filler, buf, - dblk, nameoff, maxsize); -if (ret) -break ; - pos += maxsize; - } -return 0 ; -}fuse/main.c +51
-2@@ -12,9 +12,58 @@#include "erofs/config.h" #include "erofs/print.h" #include "erofs/io.h" +#include "erofs/dir.h" -int erofsfuse_readdir (const char *path,void *buffer,fuse_fill_dir_t filler, -off_t offset,struct fuse_file_info *fi); +struct erofsfuse_dir_context { +fuse_fill_dir_t filler; +struct fuse_file_info *fi; +void *buf; +}; + +static int erofsfuse_fill_dentries (struct erofs_dirent_info *ctx) +{ +struct erofsfuse_dir_context *fusectx = ctx->arg; + + fusectx->filler (fusectx->buf, ctx->dname,NULL ,0 ); +return 0 ; +} + +int erofsfuse_readdir (const char *path,void *buf,fuse_fill_dir_t filler, +off_t offset,struct fuse_file_info *fi) +{ +int ret; +struct erofs_inode dir; + +erofs_dbg ("readdir:%s offset=%llu" , path, (long long )offset); + + ret =erofs_ilookup (path, &dir); +if (ret) +return ret; + +erofs_dbg ("path=%s nid = %llu" , path, dir.nid |0ULL ); +if (!S_ISDIR (dir.i_mode)) +return -ENOTDIR ; + +if (!dir.i_size) +return 0 ; +struct erofsfuse_dir_context ctx = { + .filler = filler, + .fi = fi, + .buf = buf, + }; +struct erofs_iterate_dir_param param = { + .cb = erofsfuse_fill_dentries, + .fsck = false, + .nid = dir.nid, + .recursive = false, + .arg = &ctx, + }; +#ifndef NDEBUG + param.fsck = true; +#endif +return erofs_iterate_dir (¶m); + +}static void *erofsfuse_init (struct fuse_conn_info *info){
fuse/main.c +0
-2@@ -122,13 +171,13 @@ static void usage(void)struct fuse_args args =FUSE_ARGS_INIT (0 ,NULL ); fputs("usage: [options] IMAGE MOUNTPOINT\n\n" -"Options:\n" - " --dbglevel=# set output message level to #(maximum 9)\n"
- " --device=# specify an extra device to be
used together\n"fuse/main.c +2
-0+"Options:\n" + " --dbglevel=# set output message level to# (maximum 9)\n"
+ " --device=# specify an extra device to
be used together\n"
#ifFUSE_MAJOR_VERSION< 3fuse/main.c +3
-3-" --help display this help and exit\n" +" --help display this help and exit\n" #endif - "\n", stderr); + "\n", stderr);#if FUSE_MAJOR_VERSION >=3 fuse_cmdline_help ();@@ -148,7 +197,7 @@ static void erofsfuse_dumpcfg(void) }static int optional_opt_func (void *data,const char *arg,int key, -struct fuse_args *outargs) +struct fuse_args *outargs) {int ret;include/erofs/dir.h new file +35
-0@@ -0,0 +1,52 @@ +/* SPDX-License-Identifier: GPL-2.0+ OR Apache-2.0 */ +#ifndef __EROFS_DIR_H +#define __EROFS_DIR_H + +#ifdef __cplusplus +extern "C" +{ +#endif + +#include "internal.h" + +struct erofs_dir_context; + +struct erofs_dirent_info { + /* inode id of the inode being iterated */ + erofs_nid_t nid; + /* file type, see values before EROFS_FT_MAX */ + u8 ftype; + const char *dname; + /* opaque ptr passed in erofs_iterate_dir_param, + * can be used to persist state across multiple + * callbacks. + */ + void *arg; +}; + +/* callback function for iterating over inodes of EROFS */ +typedef int (*erofs_readdir_cb)(struct erofs_dirent_info *); + +/* callers could use a wrapper to contain extra information */ +struct erofs_iterate_dir_param { + /* inode id of the directory that needs to be iterated. */ + /* Use sbi.root_nid if you want to iterate over rood dir */ + erofs_nid_t nid; + bool fsck; /* Whether to perform validitycheck */
+erofs_nid_tpnid; /* optional, needed if fsck = true
*/include/erofs/dir.h new file +4
-0+erofs_readdir_cb cb; +/* Whether to iterate this directory recursively */ +bool recursive; +/* This will be copied to erofs_dirent_info.arg when invoking callback */
include/erofs/dir.h new file +12
-0+void *arg; +}; + +/* iterate over inodes that are in directory */ +int erofs_iterate_dir (const struct erofs_iterate_dir_param *ctx); + + +#ifdef __cplusplus +} +#endif + +#endif lib/Makefile.am +1
-1@@ -27,7 +27,7 @@ noinst_HEADERS = $(top_srcdir)/include/erofs_fs.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 + compress_hints.c hashmap.c sha256.c blobchunk.c dir.c liberofs_la_CFLAGS = -Wall -Werror -I$(top_srcdir)/includeif
ENABLE_LZ4
liberofs_la_CFLAGS+= ${LZ4_CFLAGS}lib/dir.c new file +66
-0@@ -0,0 +1,223 @@ +// SPDX-License-Identifier: GPL-2.0+ OR Apache-2.0 +#include "erofs/dir.h" + +#include <linux/limits.h> +#include <stdlib.h> + +#include "erofs/internal.h" +#include "erofs/print.h" + +struct erofs_dir_context { +erofs_nid_t pnid;/* optional */ +struct erofs_inode *dir; + +bool dot_found; +bool dot_dot_found; +char cur_name[PATH_MAX ]; +char prev_name[PATH_MAX ]; +}; + + +static int traverse_dirents( + struct erofs_dir_context *ctx, + const struct erofs_iterate_dir_param *params, + const void *dentry_blk, + unsigned int lblk, unsigned int next_nameoff, + unsigned int maxsize, bool fsck) +{ + const struct erofs_dirent *de = dentry_blk; + const struct erofs_dirent *end = dentry_blk + next_nameoff; + char *prev_name = ctx->prev_name; + char *cur_name = ctx->cur_name; + const char *errmsg; + int ret = 0; + bool silent = false; + + while (de < end) { + unsigned int de_namelen; + unsigned int nameoff; + + nameoff = le16_to_cpu(de->nameoff); + const char *de_name = (char *)dentry_blk + nameoff; + + /* the last dirent check */ + if (de + 1 >= end) + de_namelen = strnlen(de_name, maxsize - nameoff); + else + de_namelen = le16_to_cpu(de[1].nameoff) - nameoff; + + strncpy(cur_name, de_name, de_namelen); + cur_name[de_namelen] = '\0'; + if (!cur_name) { + errmsg = "failed to allocate dirent name"; + ret = -ENOMEM; + break; + } + + erofs_dbg("traversed filename(%s)", cur_name); + + ret = -EFSCORRUPTED; + /* corrupted entry check */ + if (nameoff != next_nameoff) { + errmsg = "bogus dirent nameoff"; + break; + } + + if (nameoff + de_namelen > maxsize || de_namelen >
EROFS_NAME_LEN) {lib/dir.c new file +5
-0+ errmsg ="bogus dirent namelen" ; +break ; + } + +if (fsck && prev_name &&strcmp (prev_name, cur_name) >=0 ){
lib/dir.c new file +23
-0+ errmsg ="wrong dirent name order" ; +break ; + } + +if (fsck && de->file_type >=EROFS_FT_MAX ) { + errmsg ="invalid file type %u" ; +break ; + } + +const bool dot_dotdot =is_dot_dotdot (cur_name); + +if (dot_dotdot) { +switch (de_namelen) { +case 2 : +if (fsck && ctx->dot_dot_found) { + errmsg ="duplicated `..' dirent" ; + goto out; + } + ctx->dot_dot_found = true; +if (sbi.root_nid == ctx->dir->nid) { + ctx->pnid = sbi.root_nid; +if (fsck && de->nid != ctx->pnid) { + errmsg = "corrupted `..'dirent";
lib/dir.c new file +30
-0+ goto out; + } + } +break ; +case 1 : +if (fsck && ctx->dot_found) { + errmsg ="duplicated `.' dirent" ; + goto out; + } + + ctx->dot_found = true; +if (fsck && de->nid != ctx->dir->nid) { + errmsg ="corrupted `.' dirent" ; + goto out; + } +break ; + } + } +struct erofs_dirent_info output_info = { + .ftype = de->file_type, + .nid = de->nid, + .arg = params->arg, + .dname = cur_name}; + + ret = params->cb (&output_info); +if (ret) { + silent = true; + goto out; + } + if (params->recursive && de->file_type == EROFS_FT_DIR &&!dot_dotdot) {
+ const structerofs_iterate_dir_paramrecursive_param= {lib/dir.c new file +14
-0+ .nid = de->nid, + .fsck = params->fsck, + .pnid = params->nid, + .cb = params->cb, + .recursive = params->recursive, + .arg = params->arg}; +erofs_iterate_dir (&recursive_param); + } + + next_nameoff += de_namelen; + ++de; + + prev_name = cur_name; + cur_name = prev_name == ctx->prev_name ? ctx->cur_name :
ctx->prev_name;lib/dir.c new file +3
-0+ } +if (fsck && (!ctx->dot_found || !ctx->dot_dot_found)) { + erofs_err("`.' or `..' dirent is missing @ nid %llu",
de->nid| 0ULL);lib/dir.c new file +7
-0+return -EFSCORRUPTED ; + } +out: +if (ret && !silent) + erofs_err("%s @ nid %llu, lblk %u, index %lu", + errmsg, ctx->dir->nid | 0ULL, + lblk, (de - (struct erofs_dirent *)dentry_blk) |0UL);
lib/dir.c new file +27
-0+return ret; +} + +int erofs_iterate_dir (const struct erofs_iterate_dir_param *params) +{ +int err; +struct erofs_inode dir; + + dir.nid = params->nid; + err =erofs_read_inode_from_disk (&dir); +if (err) { +return err; + } +if ((dir.i_mode &S_IFMT ) !=S_IFDIR ) { +return -ENOTDIR ; + } + +struct erofs_dirent_info output_info; +/* Both |buf| and |ctx| can live on the stack, but that might cause +* stack overflow when iterating recursively. So put them on heap. +*/ +char *buf =calloc (EROFS_BLKSIZ ,1 ); + +if (buf ==NULL ) { + goto out; + } + struct erofs_dir_context *ctx = calloc(sizeof(struct
erofs_dir_context), 1);lib/dir.c new file +19
-0+ +if (ctx ==NULL ) { + goto out; + } + ctx->dir = &dir; + ctx->pnid = params->pnid; + +erofs_off_t pos =0 ; + +while (pos < dir.i_size) { +const erofs_blk_t lblk =erofs_blknr (pos); +const erofs_off_t maxsize = +min_t (erofs_off_t, dir.i_size - pos,EROFS_BLKSIZ ); +const struct erofs_dirent *de = (const void *)buf; + + err =erofs_pread (&dir, buf, maxsize, pos); +if (err) { + erofs_err( + "I/O error occurred when reading dirents @ nid%llu, lblk %u: %d",
lib/dir.c new file +7
-0+ dir.nid | 0ULL, lblk, err); +break ; + } + +const unsigned int nameoff =le16_to_cpu (de->nameoff); + + if (nameoff < sizeof(struct erofs_dirent) || nameoff >=
PAGE_SIZE) {
+ erofs_err("invalid de[0].nameoff %u @ nid %llu,
lblk %u",lib/dir.c new file +6
-0+ nameoff, dir.nid | 0ULL, lblk); + err = -EFSCORRUPTED ; +break ; + } + err = traverse_dirents( + ctx, params, buf, lblk, nameoff, maxsize,
params->fsck);lib/dir.c new file +14
-1+if (err) { +break ; + } + pos += maxsize; + } +out: +if (ctx) { +free (ctx); + } +if (buf) { +free (buf); + } +return err; +} --2.34.1.173.g76aa8bc2d0-goog
Signature · 3 lines
Sincerely, Kelvin Zhang
Hi Kelvin,
Quoted 10 lines · Kelvin Zhang
Hi Gao,
I drafted a new patchset on top of the dev branch. Changes since v6:1. block buffer moved to the heap, due to stack size concerns when
iterating recursively
2. Added a "recursive" option to input parameters
3. dname buffers are still on the heap, but allocation is done once per
directory, instead of once for each directory entry.
4. Added a void* arg which will be forwarded to the callback function.
Quoted 4 lines
I ran
scripts/checkpatch.pl. Hopefully this makes your life easier. Thanks for the reply!.
Many thanks for your reply! I plan to take the patches in the experimental branch.
The reason was written in include/erofs/dir.h:
https://git.kernel.org/pub/scm/linux/kernel/git/xiang/erofs-utils.git/tree/include/erofs/dir.h?h=experimenta
erofs_dir_context can be allocated on heap (in summary, by the caller) and chain together in order to avoid recursion totally later. I think it should benefit Android scenario to avoid stack overflow due to deep level as well.
Also Igor Eisberg sent a patch for a new get_pathname in the erofs_dir_context reuse way:
https://lore.kernel.org/linux-erofs/CABjEcnE84FNBgiHFk6Q+V3d-4L-93bUFDkdfN4ftPX19kpC=ww@mail.gmail.com/
I plan to polish and apply them as well.
Thanks,
Gao Xiang
I don't understand why re-using the context struct(<50 bytes) helps prevent stack overflow, when the current implementation of erofs_iterate_dir holds a 4096 bytes buffer on stack. If is erofs_iterate_dir called recursively, it's probably the char buf[EROFS_BLKSIZ]; who's using up stack space.
Quoted 43 lines · Gao Xiang
Hi Kelvin,
Quoted 7 lines · Kelvin Zhang
Hi Gao,
I drafted a new patchset on top of the dev branch. Changes since v6:1. block buffer moved to the heap, due to stack size concerns when
iterating recursively
2. Added a "recursive" option to input parameters
3. dname buffers are still on the heap, but allocation is done onceper
Quoted 2 lines
directory, instead of once for each directory entry.
4. Added a void* arg which will be forwarded to the callbackfunction.
Quoted 2 lines
I ran
scripts/checkpatch.pl. Hopefully this makes your life easier.Thanks
Quoted 3 lines
for the reply!.
Many thanks for your reply! I plan to take the patches in the experimental branch.
The reason was written in
include/erofs/dir.h:
erofs_dir_contextcan be allocated on heap (in summary, by the caller) and chain together in order to avoid recursion totally later. I think it should benefit Android scenario to avoid stack overflow due to deep level as well.Also Igor Eisberg sent a patch for a new
get_pathnamein theerofs_dir_contextreuse way:I plan to polish and apply them as well.
Thanks,
Gao Xiang
Signature · 3 lines
Sincerely, Kelvin Zhang
Quoted 4 lines · Kelvin Zhang
I don't understand why re-using the context struct(<50 bytes) helps prevent stack overflow, when the current implementation of
erofs_iterate_dirholds a 4096 bytes buffer on stack. If iserofs_iterate_dircalled recursively, it's probably the char buf[EROFS_BLKSIZ]; who's using up stack space.
For two reasons,
1) I'd like dir_context totally allocated by callers, rather than introduce another on-stack context, pass together, and long arguments.
2) As I said, there can be a non-recursive version, which has only one level to iterate dir and chain them together in the DFS/BFS approach. Actually I really don't tend to add recursive feature to erofs_iterate_dir().
As for buf[EROFS_BLKSIZ], it can be moved into dir_context with a pointer as well if later needed, so it can be allocated on heap for all recursive versions.
I (and Igor Ostapenko) already converts all use cases (fsck, dump, fsck, including get_pathname and Igor's image extraction) to the unique API these days. I do think it can be easily used by Android upgrade use cases As well.
btw, I have switched to new ztailpacking feature urgently since I'd really like to get it ready for 5.17 cycle, which is more important to the whole ecosystem for us.
Thanks,
Gao Xiang