From nobody Sat Feb 7 18:52:06 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 F0AA7C7EE25 for ; Mon, 12 Jun 2023 12:38:10 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S230416AbjFLMiJ (ORCPT ); Mon, 12 Jun 2023 08:38:09 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:58910 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S234498AbjFLMh4 (ORCPT ); Mon, 12 Jun 2023 08:37:56 -0400 Received: from out30-110.freemail.mail.aliyun.com (out30-110.freemail.mail.aliyun.com [115.124.30.110]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 5988AE4A for ; Mon, 12 Jun 2023 05:37:54 -0700 (PDT) X-Alimail-AntiSpam: AC=PASS;BC=-1|-1;BR=01201311R731e4;CH=green;DM=||false|;DS=||;FP=0|-1|-1|-1|0|-1|-1|-1;HT=ay29a033018045176;MF=jefflexu@linux.alibaba.com;NM=1;PH=DS;RN=5;SR=0;TI=SMTPD_---0Vkza4t1_1686573470; Received: from localhost(mailfrom:jefflexu@linux.alibaba.com fp:SMTPD_---0Vkza4t1_1686573470) by smtp.aliyun-inc.com; Mon, 12 Jun 2023 20:37:51 +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 v7 4/5] erofs: unify inline/shared xattr iterators for listxattr/getxattr Date: Mon, 12 Jun 2023 20:37:44 +0800 Message-Id: <20230612123745.36323-5-jefflexu@linux.alibaba.com> X-Mailer: git-send-email 2.19.1.6.gb485710b In-Reply-To: <20230612123745.36323-1-jefflexu@linux.alibaba.com> References: <20230612123745.36323-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" Make inline_{list,get}xattr() as well as inline_xattr_iter_begin() unified as erofs_iter_inline_xattr(), and shared_{list,get}xattr() unified as erofs_iter_shared_xattr(). After these changes, both erofs_iter_{inline,shared}_xattr() return 0 on success, and negative error on failure. One thing worth noting is that, the logic of returning it->buffer_ofs when there's no shared xattrs in shared_listxattr() is moved to erofs_listxattr() to make the unification possible. The only difference is that, semantically the old behavior will return ENOATTR rather than it->buffer_ofs if ENOATTR encountered when listxattr is parsing upon a specific shared xattr, while now the new behavior will return it->buffer_ofs in this case. This is not an issue, as listxattr upon a specific xattr won't return ENOATTR. Signed-off-by: Jingbo Xu --- fs/erofs/xattr.c | 188 ++++++++++++++++++----------------------------- 1 file changed, 73 insertions(+), 115 deletions(-) diff --git a/fs/erofs/xattr.c b/fs/erofs/xattr.c index 8a114c7b6c66..a97a810f24a0 100644 --- a/fs/erofs/xattr.c +++ b/fs/erofs/xattr.c @@ -139,27 +139,6 @@ struct xattr_iter_handlers { unsigned int len); }; =20 -static int inline_xattr_iter_begin(struct erofs_xattr_iter *it, - struct inode *inode) -{ - struct erofs_inode *const vi =3D EROFS_I(inode); - unsigned int xattr_header_sz; - - xattr_header_sz =3D sizeof(struct erofs_xattr_ibody_header) + - sizeof(u32) * vi->xattr_shared_count; - if (xattr_header_sz >=3D vi->xattr_isize) { - DBG_BUGON(xattr_header_sz > vi->xattr_isize); - return -ENOATTR; - } - - it->pos =3D erofs_iloc(inode) + vi->inode_isize + xattr_header_sz; - it->kaddr =3D erofs_bread(&it->buf, erofs_blknr(it->sb, it->pos), - EROFS_KMAP); - if (IS_ERR(it->kaddr)) - return PTR_ERR(it->kaddr); - return vi->xattr_isize - xattr_header_sz; -} - /* * Regardless of success or failure, `xattr_foreach' will end up with * `pos' pointing to the next xattr item rather than an arbitrary position. @@ -333,47 +312,6 @@ static const struct xattr_iter_handlers find_xattr_han= dlers =3D { .value =3D xattr_copyvalue }; =20 -static int inline_getxattr(struct inode *inode, struct erofs_xattr_iter *i= t) -{ - int ret; - unsigned int remaining; - - ret =3D inline_xattr_iter_begin(it, inode); - if (ret < 0) - return ret; - - remaining =3D ret; - while (remaining) { - ret =3D xattr_foreach(it, &find_xattr_handlers, &remaining); - if (ret !=3D -ENOATTR) - break; - } - return ret ? ret : it->buffer_ofs; -} - -static int shared_getxattr(struct inode *inode, struct erofs_xattr_iter *i= t) -{ - struct erofs_inode *const vi =3D EROFS_I(inode); - struct super_block *const sb =3D it->sb; - struct erofs_sb_info *sbi =3D EROFS_SB(sb); - unsigned int i; - int ret =3D -ENOATTR; - - for (i =3D 0; i < vi->xattr_shared_count; ++i) { - it->pos =3D erofs_pos(sb, sbi->xattr_blkaddr) + - vi->xattr_shared_xattrs[i] * sizeof(__le32); - it->kaddr =3D erofs_bread(&it->buf, erofs_blknr(sb, it->pos), - EROFS_KMAP); - if (IS_ERR(it->kaddr)) - return PTR_ERR(it->kaddr); - - ret =3D xattr_foreach(it, &find_xattr_handlers, NULL); - if (ret !=3D -ENOATTR) - break; - } - return ret ? ret : it->buffer_ofs; -} - static bool erofs_xattr_user_list(struct dentry *dentry) { return test_opt(&EROFS_SB(dentry->d_sb)->opt, XATTR_USER); @@ -384,39 +322,6 @@ static bool erofs_xattr_trusted_list(struct dentry *de= ntry) return capable(CAP_SYS_ADMIN); } =20 -int erofs_getxattr(struct inode *inode, int index, - const char *name, - void *buffer, size_t buffer_size) -{ - int ret; - struct erofs_xattr_iter it; - - if (!name) - return -EINVAL; - - ret =3D erofs_init_inode_xattrs(inode); - if (ret) - return ret; - - it.index =3D index; - it.name =3D (struct qstr)QSTR_INIT(name, strlen(name)); - if (it.name.len > EROFS_NAME_LEN) - return -ERANGE; - - it.sb =3D inode->i_sb; - it.buf =3D __EROFS_BUF_INITIALIZER; - erofs_init_metabuf(&it.buf, it.sb); - it.buffer =3D buffer; - it.buffer_size =3D buffer_size; - it.buffer_ofs =3D 0; - - ret =3D inline_getxattr(inode, &it); - if (ret =3D=3D -ENOATTR) - ret =3D shared_getxattr(inode, &it); - erofs_put_metabuf(&it.buf); - return ret; -} - static int erofs_xattr_generic_get(const struct xattr_handler *handler, struct dentry *unused, struct inode *inode, const char *name, void *buffer, size_t size) @@ -521,32 +426,49 @@ static const struct xattr_iter_handlers list_xattr_ha= ndlers =3D { .value =3D NULL }; =20 -static int inline_listxattr(struct erofs_xattr_iter *it) +static int erofs_iter_inline_xattr(struct erofs_xattr_iter *it, + struct inode *inode, bool getxattr) { + struct erofs_inode *const vi =3D EROFS_I(inode); + const struct xattr_iter_handlers *op; + unsigned int xattr_header_sz, remaining; int ret; - unsigned int remaining; =20 - ret =3D inline_xattr_iter_begin(it, d_inode(it->dentry)); - if (ret < 0) - return ret; + xattr_header_sz =3D sizeof(struct erofs_xattr_ibody_header) + + sizeof(u32) * vi->xattr_shared_count; + if (xattr_header_sz >=3D vi->xattr_isize) { + DBG_BUGON(xattr_header_sz > vi->xattr_isize); + return -ENOATTR; + } + + it->pos =3D erofs_iloc(inode) + vi->inode_isize + xattr_header_sz; + it->kaddr =3D erofs_bread(&it->buf, erofs_blknr(it->sb, it->pos), + EROFS_KMAP); + if (IS_ERR(it->kaddr)) + return PTR_ERR(it->kaddr); + + remaining =3D vi->xattr_isize - xattr_header_sz; + op =3D getxattr ? &find_xattr_handlers : &list_xattr_handlers; =20 - remaining =3D ret; while (remaining) { - ret =3D xattr_foreach(it, &list_xattr_handlers, &remaining); - if (ret) + ret =3D xattr_foreach(it, op, &remaining); + if ((getxattr && ret !=3D -ENOATTR) || (!getxattr && ret)) break; } - return ret ? ret : it->buffer_ofs; + return ret; } =20 -static int shared_listxattr(struct erofs_xattr_iter *it) +static int erofs_iter_shared_xattr(struct erofs_xattr_iter *it, + struct inode *inode, bool getxattr) { - struct inode *const inode =3D d_inode(it->dentry); struct erofs_inode *const vi =3D EROFS_I(inode); struct super_block *const sb =3D it->sb; struct erofs_sb_info *sbi =3D EROFS_SB(sb); unsigned int i; - int ret =3D 0; + const struct xattr_iter_handlers *op; + int ret =3D -ENOATTR; + + op =3D getxattr ? &find_xattr_handlers : &list_xattr_handlers; =20 for (i =3D 0; i < vi->xattr_shared_count; ++i) { it->pos =3D erofs_pos(sb, sbi->xattr_blkaddr) + @@ -556,11 +478,44 @@ static int shared_listxattr(struct erofs_xattr_iter *= it) if (IS_ERR(it->kaddr)) return PTR_ERR(it->kaddr); =20 - ret =3D xattr_foreach(it, &list_xattr_handlers, NULL); - if (ret) + ret =3D xattr_foreach(it, op, NULL); + if ((getxattr && ret !=3D -ENOATTR) || (!getxattr && ret)) break; } - return ret ? ret : it->buffer_ofs; + return ret; +} + +int erofs_getxattr(struct inode *inode, int index, + const char *name, + void *buffer, size_t buffer_size) +{ + int ret; + struct erofs_xattr_iter it; + + if (!name) + return -EINVAL; + + ret =3D erofs_init_inode_xattrs(inode); + if (ret) + return ret; + + it.index =3D index; + it.name =3D (struct qstr)QSTR_INIT(name, strlen(name)); + if (it.name.len > EROFS_NAME_LEN) + return -ERANGE; + + it.sb =3D inode->i_sb; + it.buf =3D __EROFS_BUF_INITIALIZER; + erofs_init_metabuf(&it.buf, it.sb); + it.buffer =3D buffer; + it.buffer_size =3D buffer_size; + it.buffer_ofs =3D 0; + + ret =3D erofs_iter_inline_xattr(&it, inode, true); + if (ret =3D=3D -ENOATTR) + ret =3D erofs_iter_shared_xattr(&it, inode, true); + erofs_put_metabuf(&it.buf); + return ret ? ret : it.buffer_ofs; } =20 ssize_t erofs_listxattr(struct dentry *dentry, @@ -568,8 +523,9 @@ ssize_t erofs_listxattr(struct dentry *dentry, { int ret; struct erofs_xattr_iter it; + struct inode *inode =3D d_inode(dentry); =20 - ret =3D erofs_init_inode_xattrs(d_inode(dentry)); + ret =3D erofs_init_inode_xattrs(inode); if (ret =3D=3D -ENOATTR) return 0; if (ret) @@ -583,11 +539,13 @@ ssize_t erofs_listxattr(struct dentry *dentry, it.buffer_size =3D buffer_size; it.buffer_ofs =3D 0; =20 - ret =3D inline_listxattr(&it); - if (ret >=3D 0 || ret =3D=3D -ENOATTR) - ret =3D shared_listxattr(&it); + ret =3D erofs_iter_inline_xattr(&it, inode, false); + if (!ret || ret =3D=3D -ENOATTR) + ret =3D erofs_iter_shared_xattr(&it, inode, false); + if (ret =3D=3D -ENOATTR) + ret =3D 0; erofs_put_metabuf(&it.buf); - return ret; + return ret ? ret : it.buffer_ofs; } =20 void erofs_xattr_prefixes_cleanup(struct super_block *sb) --=20 2.19.1.6.gb485710b