From nobody Sun Feb 8 14:12:40 2026 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 155F1C77B7D for ; Thu, 18 May 2023 02:46:13 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229834AbjERCqL (ORCPT ); Wed, 17 May 2023 22:46:11 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:36998 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229751AbjERCqD (ORCPT ); Wed, 17 May 2023 22:46:03 -0400 Received: from out30-131.freemail.mail.aliyun.com (out30-131.freemail.mail.aliyun.com [115.124.30.131]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id D624C3C01 for ; Wed, 17 May 2023 19:46:00 -0700 (PDT) X-Alimail-AntiSpam: AC=PASS;BC=-1|-1;BR=01201311R211e4;CH=green;DM=||false|;DS=||;FP=0|-1|-1|-1|0|-1|-1|-1;HT=ay29a033018046050;MF=jefflexu@linux.alibaba.com;NM=1;PH=DS;RN=5;SR=0;TI=SMTPD_---0ViuseHy_1684377957; Received: from localhost(mailfrom:jefflexu@linux.alibaba.com fp:SMTPD_---0ViuseHy_1684377957) by smtp.aliyun-inc.com; Thu, 18 May 2023 10:45:58 +0800 From: Jingbo Xu To: xiang@kernel.org, chao@kernel.org, huyue2@coolpad.com, linux-erofs@lists.ozlabs.org Cc: linux-kernel@vger.kernel.org Subject: [PATCH v3 5/5] erofs: use separate xattr parsers for listxattr/getxattr Date: Thu, 18 May 2023 10:45:51 +0800 Message-Id: <20230518024551.123990-6-jefflexu@linux.alibaba.com> X-Mailer: git-send-email 2.19.1.6.gb485710b In-Reply-To: <20230518024551.123990-1-jefflexu@linux.alibaba.com> References: <20230518024551.123990-1-jefflexu@linux.alibaba.com> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Type: text/plain; charset="utf-8" There's a callback styled xattr parser, i.e. xattr_foreach(), which is shared among listxattr and getxattr. Convert it to two separate xattr parsers for listxattr and getxattr. Signed-off-by: Jingbo Xu --- fs/erofs/xattr.c | 389 +++++++++++++++++++++-------------------------- 1 file changed, 170 insertions(+), 219 deletions(-) diff --git a/fs/erofs/xattr.c b/fs/erofs/xattr.c index 435146628eed..111735fb6fbc 100644 --- a/fs/erofs/xattr.c +++ b/fs/erofs/xattr.c @@ -12,7 +12,7 @@ struct erofs_xattr_iter { struct erofs_buf buf; void *kaddr; erofs_blk_t blkaddr; - unsigned int ofs; + unsigned int ofs, t_ofs; =20 char *buffer; int buffer_size, buffer_ofs; @@ -21,6 +21,7 @@ struct erofs_xattr_iter { struct qstr name; struct dentry *dentry; struct inode *inode; + unsigned int remaining; /* size of inline xattrs to be iterated */ bool getxattr; }; =20 @@ -46,6 +47,28 @@ static inline int erofs_xattr_iter_fixup_aligned(struct = erofs_xattr_iter *it) return erofs_xattr_iter_fixup(it); } =20 +static int erofs_xattr_iter_prep(struct erofs_xattr_iter *it) +{ + unsigned int entry_sz; + int err; + + err =3D erofs_xattr_iter_fixup(it); + if (err) + return err; + + entry_sz =3D erofs_xattr_entry_size(it->kaddr + it->ofs); + it->t_ofs =3D it->ofs + entry_sz; + if (it->remaining) { + /* xattr on-disk corruption: xattr entry beyond xattr_isize */ + if (it->remaining < entry_sz) { + DBG_BUGON(1); + return -EFSCORRUPTED; + } + it->remaining -=3D entry_sz; + } + return 0; +} + static int erofs_init_inode_xattrs(struct inode *inode) { struct erofs_inode *const vi =3D EROFS_I(inode); @@ -144,183 +167,6 @@ static int erofs_init_inode_xattrs(struct inode *inod= e) return ret; } =20 -/* - * the general idea for these return values is - * if 0 is returned, go on processing the current xattr; - * 1 (> 0) is returned, skip this round to process the next xattr; - * -err (< 0) is returned, an error (maybe ENOXATTR) occurred - * and need to be handled - */ -struct xattr_iter_handlers { - int (*entry)(struct erofs_xattr_iter *it, struct erofs_xattr_entry *entry= ); - int (*name)(struct erofs_xattr_iter *it, unsigned int processed, char *bu= f, - unsigned int len); - int (*alloc_buffer)(struct erofs_xattr_iter *it, unsigned int value_sz); - void (*value)(struct erofs_xattr_iter *it, unsigned int processed, char *= buf, - unsigned int len); -}; - -/* - * Regardless of success or failure, `xattr_foreach' will end up with - * `ofs' pointing to the next xattr item rather than an arbitrary position. - */ -static int xattr_foreach(struct erofs_xattr_iter *it, - const struct xattr_iter_handlers *op, - unsigned int *tlimit) -{ - struct erofs_xattr_entry entry; - unsigned int value_sz, processed, slice; - int err; - - /* 0. fixup blkaddr, ofs, ipage */ - err =3D erofs_xattr_iter_fixup(it); - if (err) - return err; - - /* - * 1. read xattr entry to the memory, - * since we do EROFS_XATTR_ALIGN - * therefore entry should be in the page - */ - entry =3D *(struct erofs_xattr_entry *)(it->kaddr + it->ofs); - if (tlimit) { - unsigned int entry_sz =3D erofs_xattr_entry_size(&entry); - - /* xattr on-disk corruption: xattr entry beyond xattr_isize */ - if (*tlimit < entry_sz) { - DBG_BUGON(1); - return -EFSCORRUPTED; - } - *tlimit -=3D entry_sz; - } - - it->ofs +=3D sizeof(struct erofs_xattr_entry); - value_sz =3D le16_to_cpu(entry.e_value_size); - - /* handle entry */ - err =3D op->entry(it, &entry); - if (err) { - it->ofs +=3D entry.e_name_len + value_sz; - goto out; - } - - /* 2. handle xattr name (ofs will finally be at the end of name) */ - processed =3D 0; - - while (processed < entry.e_name_len) { - err =3D erofs_xattr_iter_fixup_aligned(it); - if (err) - goto out; - - slice =3D min_t(unsigned int, it->sb->s_blocksize - it->ofs, - entry.e_name_len - processed); - - /* handle name */ - err =3D op->name(it, processed, it->kaddr + it->ofs, slice); - if (err) { - it->ofs +=3D entry.e_name_len - processed + value_sz; - goto out; - } - - it->ofs +=3D slice; - processed +=3D slice; - } - - /* 3. handle xattr value */ - processed =3D 0; - - if (op->alloc_buffer) { - err =3D op->alloc_buffer(it, value_sz); - if (err) { - it->ofs +=3D value_sz; - goto out; - } - } - - while (processed < value_sz) { - err =3D erofs_xattr_iter_fixup_aligned(it); - if (err) - goto out; - - slice =3D min_t(unsigned int, it->sb->s_blocksize - it->ofs, - value_sz - processed); - op->value(it, processed, it->kaddr + it->ofs, slice); - it->ofs +=3D slice; - processed +=3D slice; - } - -out: - /* xattrs should be 4-byte aligned (on-disk constraint) */ - it->ofs =3D EROFS_XATTR_ALIGN(it->ofs); - return err < 0 ? err : 0; -} - -static int erofs_xattr_long_entrymatch(struct erofs_xattr_iter *it, - struct erofs_xattr_entry *entry) -{ - struct erofs_sb_info *sbi =3D EROFS_SB(it->sb); - struct erofs_xattr_prefix_item *pf =3D sbi->xattr_prefixes + - (entry->e_name_index & EROFS_XATTR_LONG_PREFIX_MASK); - - if (pf >=3D sbi->xattr_prefixes + sbi->xattr_prefix_count) - return -ENOATTR; - - if (it->index !=3D pf->prefix->base_index || - it->name.len !=3D entry->e_name_len + pf->infix_len) - return -ENOATTR; - - if (memcmp(it->name.name, pf->prefix->infix, pf->infix_len)) - return -ENOATTR; - - it->infix_len =3D pf->infix_len; - return 0; -} - -static int xattr_entrymatch(struct erofs_xattr_iter *it, - struct erofs_xattr_entry *entry) -{ - /* should also match the infix for long name prefixes */ - if (entry->e_name_index & EROFS_XATTR_LONG_PREFIX) - return erofs_xattr_long_entrymatch(it, entry); - - if (it->index !=3D entry->e_name_index || - it->name.len !=3D entry->e_name_len) - return -ENOATTR; - it->infix_len =3D 0; - return 0; -} - -static int xattr_namematch(struct erofs_xattr_iter *it, - unsigned int processed, char *buf, unsigned int len) -{ - if (memcmp(buf, it->name.name + it->infix_len + processed, len)) - return -ENOATTR; - return 0; -} - -static int xattr_checkbuffer(struct erofs_xattr_iter *it, - unsigned int value_sz) -{ - int err =3D it->buffer_size < value_sz ? -ERANGE : 0; - - it->buffer_ofs =3D value_sz; - return !it->buffer ? 1 : err; -} - -static void xattr_copyvalue(struct erofs_xattr_iter *it, - unsigned int processed, - char *buf, unsigned int len) -{ - memcpy(it->buffer + processed, buf, len); -} - -static const struct xattr_iter_handlers find_xattr_handlers =3D { - .entry =3D xattr_entrymatch, - .name =3D xattr_namematch, - .alloc_buffer =3D xattr_checkbuffer, - .value =3D xattr_copyvalue -}; - static bool erofs_xattr_user_list(struct dentry *dentry) { return test_opt(&EROFS_SB(dentry->d_sb)->opt, XATTR_USER); @@ -373,20 +219,74 @@ const struct xattr_handler *erofs_xattr_handlers[] = =3D { NULL, }; =20 -static int xattr_entrylist(struct erofs_xattr_iter *it, - struct erofs_xattr_entry *entry) +typedef int (*erofs_xattr_body_handler)(struct erofs_xattr_iter *it, + unsigned int processed, char *buf, unsigned int len); + +static int erofs_xattr_namematch(struct erofs_xattr_iter *it, + unsigned int processed, char *buf, unsigned int len) +{ + if (memcmp(buf, it->name.name + it->infix_len + processed, len)) + return -ENOATTR; + return 0; +} + +static int erofs_xattr_copy(struct erofs_xattr_iter *it, + unsigned int unused, char *buf, unsigned int len) +{ + memcpy(it->buffer + it->buffer_ofs, buf, len); + it->buffer_ofs +=3D len; + return 0; +} + +static int erofs_xattr_body(struct erofs_xattr_iter *it, unsigned int len, + erofs_xattr_body_handler handler) +{ + unsigned int slice, processed =3D 0; + + while (processed < len) { + int err =3D erofs_xattr_iter_fixup_aligned(it); + if (err) + return err; + + slice =3D min_t(unsigned int, it->sb->s_blocksize - it->ofs, + len - processed); + err =3D handler(it, processed, it->kaddr + it->ofs, slice); + if (err) + return err; + + it->ofs +=3D slice; + processed +=3D slice; + } + return 0; +} + +/* + * Wen returning 0 or ENOATTR, erofs_[list|get]xattr_foreach() will end up + * with `ofs' pointing to the next xattr item rather than an arbitrary pos= ition. + */ +static int erofs_listxattr_foreach(struct erofs_xattr_iter *it) { - unsigned int base_index =3D entry->e_name_index; - unsigned int prefix_len, infix_len =3D 0; + struct erofs_xattr_entry entry; + unsigned int base_index, prefix_len, infix_len =3D 0; const char *prefix, *infix =3D NULL; + int err; + + err =3D erofs_xattr_iter_prep(it); + if (err) + return err; + + /* 1. handle xattr entry */ + entry =3D *(struct erofs_xattr_entry *)(it->kaddr + it->ofs); + it->ofs +=3D sizeof(struct erofs_xattr_entry); + base_index =3D entry.e_name_index; =20 - if (entry->e_name_index & EROFS_XATTR_LONG_PREFIX) { + if (entry.e_name_index & EROFS_XATTR_LONG_PREFIX) { struct erofs_sb_info *sbi =3D EROFS_SB(it->sb); struct erofs_xattr_prefix_item *pf =3D sbi->xattr_prefixes + - (entry->e_name_index & EROFS_XATTR_LONG_PREFIX_MASK); + (entry.e_name_index & EROFS_XATTR_LONG_PREFIX_MASK); =20 if (pf >=3D sbi->xattr_prefixes + sbi->xattr_prefix_count) - return 1; + goto out; infix =3D pf->prefix->infix; infix_len =3D pf->infix_len; base_index =3D pf->prefix->base_index; @@ -394,52 +294,102 @@ static int xattr_entrylist(struct erofs_xattr_iter *= it, =20 prefix =3D erofs_xattr_prefix(base_index, it->dentry); if (!prefix) - return 1; + goto out; prefix_len =3D strlen(prefix); =20 if (!it->buffer) { - it->buffer_ofs +=3D prefix_len + infix_len + - entry->e_name_len + 1; - return 1; + it->buffer_ofs +=3D prefix_len + infix_len + entry.e_name_len + 1; + goto out; } =20 if (it->buffer_ofs + prefix_len + infix_len + - + entry->e_name_len + 1 > it->buffer_size) + entry.e_name_len + 1 > it->buffer_size) return -ERANGE; =20 memcpy(it->buffer + it->buffer_ofs, prefix, prefix_len); memcpy(it->buffer + it->buffer_ofs + prefix_len, infix, infix_len); it->buffer_ofs +=3D prefix_len + infix_len; - return 0; -} =20 -static int xattr_namelist(struct erofs_xattr_iter *it, - unsigned int processed, char *buf, unsigned int len) -{ - memcpy(it->buffer + it->buffer_ofs, buf, len); - it->buffer_ofs +=3D len; + /* 2. handle xattr name (err can't be ENOATTR) */ + err =3D erofs_xattr_body(it, entry.e_name_len, erofs_xattr_copy); + if (err) + return err; + + it->buffer[it->buffer_ofs++] =3D '\0'; + it->ofs +=3D le16_to_cpu(entry.e_value_size); + it->ofs =3D EROFS_XATTR_ALIGN(it->ofs); + return 0; +out: + it->ofs =3D it->t_ofs; return 0; } =20 -static int xattr_skipvalue(struct erofs_xattr_iter *it, - unsigned int value_sz) +static int erofs_getxattr_foreach(struct erofs_xattr_iter *it) { - it->buffer[it->buffer_ofs++] =3D '\0'; - return 1; -} + struct erofs_xattr_entry entry; + unsigned int value_sz; + int err; =20 -static const struct xattr_iter_handlers list_xattr_handlers =3D { - .entry =3D xattr_entrylist, - .name =3D xattr_namelist, - .alloc_buffer =3D xattr_skipvalue, - .value =3D NULL -}; + err =3D erofs_xattr_iter_prep(it); + if (err) + return err; + + /* 1. handle xattr entry */ + entry =3D *(struct erofs_xattr_entry *)(it->kaddr + it->ofs); + it->ofs +=3D sizeof(struct erofs_xattr_entry); + value_sz =3D le16_to_cpu(entry.e_value_size); + + err =3D -ENOATTR; + /* should also match the infix for long name prefixes */ + if (entry.e_name_index & EROFS_XATTR_LONG_PREFIX) { + struct erofs_sb_info *sbi =3D EROFS_SB(it->sb); + struct erofs_xattr_prefix_item *pf =3D sbi->xattr_prefixes + + (entry.e_name_index & EROFS_XATTR_LONG_PREFIX_MASK); + + if (pf >=3D sbi->xattr_prefixes + sbi->xattr_prefix_count) + goto out; + + if (it->index !=3D pf->prefix->base_index || + it->name.len !=3D entry.e_name_len + pf->infix_len) + goto out; + + if (memcmp(it->name.name, pf->prefix->infix, pf->infix_len)) + goto out; + + it->infix_len =3D pf->infix_len; + } else { + if (it->index !=3D entry.e_name_index || + it->name.len !=3D entry.e_name_len) + goto out; + it->infix_len =3D 0; + } + + /* 2. handle xattr name */ + err =3D erofs_xattr_body(it, entry.e_name_len, erofs_xattr_namematch); + if (err) + goto out; + + /* 3. handle xattr value */ + if (!it->buffer) { + it->buffer_ofs =3D value_sz; + goto out; /* err =3D=3D 0 */ + } + if (it->buffer_size < value_sz) + return -ERANGE; + + /* no need normalizing ofs on error (err can't be ENOATTR) */ + err =3D erofs_xattr_body(it, value_sz, erofs_xattr_copy); + it->ofs =3D EROFS_XATTR_ALIGN(it->ofs); + return err; +out: + it->ofs =3D it->t_ofs; + return err; +} =20 static int erofs_iter_inline_xattr(struct erofs_xattr_iter *it) { struct erofs_inode *const vi =3D EROFS_I(it->inode); - const struct xattr_iter_handlers *op; - unsigned int xattr_header_sz, remaining; + unsigned int xattr_header_sz; erofs_off_t pos; int ret; =20 @@ -457,11 +407,12 @@ static int erofs_iter_inline_xattr(struct erofs_xattr= _iter *it) if (IS_ERR(it->kaddr)) return PTR_ERR(it->kaddr); =20 - remaining =3D vi->xattr_isize - xattr_header_sz; - op =3D it->getxattr ? &find_xattr_handlers : &list_xattr_handlers; - - while (remaining) { - ret =3D xattr_foreach(it, op, &remaining); + it->remaining =3D vi->xattr_isize - xattr_header_sz; + while (it->remaining) { + if (it->getxattr) + ret =3D erofs_getxattr_foreach(it); + else + ret =3D erofs_listxattr_foreach(it); if ((it->getxattr && ret !=3D -ENOATTR) || (!it->getxattr && ret)) break; } @@ -472,12 +423,9 @@ static int erofs_iter_shared_xattr(struct erofs_xattr_= iter *it) { struct erofs_inode *const vi =3D EROFS_I(it->inode); struct super_block *const sb =3D it->sb; - const struct xattr_iter_handlers *op; unsigned int i, xsid; int ret =3D -ENOATTR; =20 - op =3D it->getxattr ? &find_xattr_handlers : &list_xattr_handlers; - for (i =3D 0; i < vi->xattr_shared_count; ++i) { xsid =3D vi->xattr_shared_xattrs[i]; it->blkaddr =3D EROFS_SB(sb)->xattr_blkaddr + @@ -487,7 +435,10 @@ static int erofs_iter_shared_xattr(struct erofs_xattr_= iter *it) if (IS_ERR(it->kaddr)) return PTR_ERR(it->kaddr); =20 - ret =3D xattr_foreach(it, op, NULL); + if (it->getxattr) + ret =3D erofs_getxattr_foreach(it); + else + ret =3D erofs_listxattr_foreach(it); if ((it->getxattr && ret !=3D -ENOATTR) || (!it->getxattr && ret)) break; } --=20 2.19.1.6.gb485710b