On Thu, Aug 14, 2025 at 7:22 PM André Almeida <andrealmeid@xxxxxxxxxx> wrote: > > To add overlayfs support casefold layers, create a new function > ovl_casefold(), to be able to do case-insensitive strncmp(). > > ovl_casefold() allocates a new buffer and stores the casefolded version > of the string on it. If the allocation or the casefold operation fails, > fallback to use the original string. > > The case-insentive name is then used in the rb-tree search/insertion > operation. If the name is found in the rb-tree, the name can be > discarded and the buffer is freed. If the name isn't found, it's then > stored at struct ovl_cache_entry to be used later. > > Signed-off-by: André Almeida <andrealmeid@xxxxxxxxxx> > --- > Changes from v4: > - Move the consumer/free buffer logic out to the caller > - s/aux/c_name > > Changes from v3: > - Improve commit message text > - s/OVL_NAME_LEN/NAME_MAX > - drop #ifdef in favor of if(IS_ENABLED) > - use new helper sb_encoding > - merged patch "Store casefold name..." and "Create ovl_casefold()..." > - Guard all the casefolding inside of IS_ENABLED(UNICODE) > > Changes from v2: > - Refactor the patch to do a single kmalloc() per rb_tree operation > - Instead of casefolding the cache entry name everytime per strncmp(), > casefold it once and reuse it for every strncmp(). > --- > fs/overlayfs/readdir.c | 115 +++++++++++++++++++++++++++++++++++++++++-------- > 1 file changed, 97 insertions(+), 18 deletions(-) > > diff --git a/fs/overlayfs/readdir.c b/fs/overlayfs/readdir.c > index b65cdfce31ce27172d28d879559f1008b9c87320..803ac6a7516d0156ae7793ee1ff884dbbf2e20b0 100644 > --- a/fs/overlayfs/readdir.c > +++ b/fs/overlayfs/readdir.c > @@ -27,6 +27,8 @@ struct ovl_cache_entry { > bool is_upper; > bool is_whiteout; > bool check_xwhiteout; > + const char *cf_name; > + int cf_len; > char name[]; > }; > > @@ -45,6 +47,7 @@ struct ovl_readdir_data { > struct list_head *list; > struct list_head middle; > struct ovl_cache_entry *first_maybe_whiteout; > + struct unicode_map *map; > int count; > int err; > bool is_upper; > @@ -66,6 +69,27 @@ static struct ovl_cache_entry *ovl_cache_entry_from_node(struct rb_node *n) > return rb_entry(n, struct ovl_cache_entry, node); > } > > +static int ovl_casefold(struct unicode_map *map, const char *str, int len, char **dst) > +{ > + const struct qstr qstr = { .name = str, .len = len }; > + int cf_len; > + > + if (!IS_ENABLED(CONFIG_UNICODE) || !map || is_dot_dotdot(str, len)) > + return 0; > + > + *dst = kmalloc(NAME_MAX, GFP_KERNEL); > + > + if (dst) { > + cf_len = utf8_casefold(map, &qstr, *dst, NAME_MAX); > + > + if (cf_len > 0) > + return cf_len; > + } > + > + kfree(*dst); > + return 0; > +} > + > static bool ovl_cache_entry_find_link(const char *name, int len, > struct rb_node ***link, > struct rb_node **parent) > @@ -79,7 +103,7 @@ static bool ovl_cache_entry_find_link(const char *name, int len, > > *parent = *newp; > tmp = ovl_cache_entry_from_node(*newp); > - cmp = strncmp(name, tmp->name, len); > + cmp = strncmp(name, tmp->cf_name, tmp->cf_len); > if (cmp > 0) > newp = &tmp->node.rb_right; > else if (cmp < 0 || len < tmp->len) > @@ -101,7 +125,7 @@ static struct ovl_cache_entry *ovl_cache_entry_find(struct rb_root *root, > while (node) { > struct ovl_cache_entry *p = ovl_cache_entry_from_node(node); > > - cmp = strncmp(name, p->name, len); > + cmp = strncmp(name, p->cf_name, p->cf_len); > if (cmp > 0) > node = p->node.rb_right; > else if (cmp < 0 || len < p->len) > @@ -145,13 +169,16 @@ static bool ovl_calc_d_ino(struct ovl_readdir_data *rdd, > > static struct ovl_cache_entry *ovl_cache_entry_new(struct ovl_readdir_data *rdd, > const char *name, int len, > + const char *cf_name, int cf_len, > u64 ino, unsigned int d_type) > { > struct ovl_cache_entry *p; > > p = kmalloc(struct_size(p, name, len + 1), GFP_KERNEL); > - if (!p) > + if (!p) { > + kfree(cf_name); Not needed. Caller will get -ENOMEM and will free c_name No need to repost just for this I can fix on commit. Reviewed-by: Amir Goldstein <amir73il@xxxxxxxxx> Thanks, Amir.