From nobody Sun Feb 8 20:28:59 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 888A4C7EE2A for ; Thu, 18 May 2023 02:46:09 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229810AbjERCqI (ORCPT ); Wed, 17 May 2023 22:46:08 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:36974 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229668AbjERCqB (ORCPT ); Wed, 17 May 2023 22:46:01 -0400 Received: from out30-99.freemail.mail.aliyun.com (out30-99.freemail.mail.aliyun.com [115.124.30.99]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 138FC135 for ; Wed, 17 May 2023 19:45:59 -0700 (PDT) X-Alimail-AntiSpam: AC=PASS;BC=-1|-1;BR=01201311R171e4;CH=green;DM=||false|;DS=||;FP=0|-1|-1|-1|0|-1|-1|-1;HT=ay29a033018045170;MF=jefflexu@linux.alibaba.com;NM=1;PH=DS;RN=5;SR=0;TI=SMTPD_---0Viut.Db_1684377956; Received: from localhost(mailfrom:jefflexu@linux.alibaba.com fp:SMTPD_---0Viut.Db_1684377956) by smtp.aliyun-inc.com; Thu, 18 May 2023 10:45:57 +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 4/5] erofs: unify inline/share xattr iterators for listxattr/getxattr Date: Thu, 18 May 2023 10:45:50 +0800 Message-Id: <20230518024551.123990-5-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" Make inline_getxattr() and inline_listxattr() unified as iter_inline_xattr(), shared_getxattr() and shared_listxattr() unified as iter_shared_xattr(). After the unification, both iter_inline_xattr() and iter_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 | 211 ++++++++++++++++++----------------------------- 1 file changed, 80 insertions(+), 131 deletions(-) diff --git a/fs/erofs/xattr.c b/fs/erofs/xattr.c index 87d76ccea692..435146628eed 100644 --- a/fs/erofs/xattr.c +++ b/fs/erofs/xattr.c @@ -7,19 +7,6 @@ #include #include "xattr.h" =20 -static inline erofs_blk_t erofs_xattr_blkaddr(struct super_block *sb, - unsigned int xattr_id) -{ - return EROFS_SB(sb)->xattr_blkaddr + - erofs_blknr(sb, xattr_id * sizeof(__u32)); -} - -static inline unsigned int erofs_xattr_blkoff(struct super_block *sb, - unsigned int xattr_id) -{ - return erofs_blkoff(sb, xattr_id * sizeof(__u32)); -} - struct erofs_xattr_iter { struct super_block *sb; struct erofs_buf buf; @@ -33,6 +20,8 @@ struct erofs_xattr_iter { int index, infix_len; struct qstr name; struct dentry *dentry; + struct inode *inode; + bool getxattr; }; =20 static inline int erofs_xattr_iter_fixup(struct erofs_xattr_iter *it) @@ -171,30 +160,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, inline_xattr_ofs; - - 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; - } - - inline_xattr_ofs =3D vi->inode_isize + xattr_header_sz; - - it->blkaddr =3D erofs_blknr(it->sb, erofs_iloc(inode) + inline_xattr_ofs); - it->ofs =3D erofs_blkoff(it->sb, erofs_iloc(inode) + inline_xattr_ofs); - it->kaddr =3D erofs_read_metabuf(&it->buf, inode->i_sb, it->blkaddr, - 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 * `ofs' pointing to the next xattr item rather than an arbitrary position. @@ -356,46 +321,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; - unsigned int i, xsid; - int ret =3D -ENOATTR; - - for (i =3D 0; i < vi->xattr_shared_count; ++i) { - xsid =3D vi->xattr_shared_xattrs[i]; - it->blkaddr =3D erofs_xattr_blkaddr(sb, xsid); - it->ofs =3D erofs_xattr_blkoff(sb, xsid); - it->kaddr =3D erofs_read_metabuf(&it->buf, sb, it->blkaddr, 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); @@ -406,38 +331,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; - if (strlen(name) > EROFS_NAME_LEN) - return -ERANGE; - - ret =3D erofs_init_inode_xattrs(inode); - if (ret) - return ret; - - it =3D (struct erofs_xattr_iter) { - .buf =3D __EROFS_BUF_INITIALIZER, - .sb =3D inode->i_sb, - .name =3D QSTR_INIT(name, strlen(name)), - .index =3D index, - .buffer =3D buffer, - .buffer_size =3D buffer_size, - }; - - 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) @@ -542,45 +435,97 @@ 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 erofs_inode *const vi =3D EROFS_I(it->inode); + const struct xattr_iter_handlers *op; + unsigned int xattr_header_sz, remaining; + erofs_off_t pos; 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; + } + + pos =3D erofs_iloc(it->inode) + vi->inode_isize + xattr_header_sz; + it->blkaddr =3D erofs_blknr(it->sb, pos); + it->ofs =3D erofs_blkoff(it->sb, pos); + it->kaddr =3D erofs_read_metabuf(&it->buf, it->sb, it->blkaddr, EROFS_KMA= P); + if (IS_ERR(it->kaddr)) + return PTR_ERR(it->kaddr); + + remaining =3D vi->xattr_isize - xattr_header_sz; + op =3D it->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 ((it->getxattr && ret !=3D -ENOATTR) || (!it->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 *const inode =3D d_inode(it->dentry); - struct erofs_inode *const vi =3D EROFS_I(inode); + 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 0; + int ret =3D -ENOATTR; + + op =3D it->getxattr ? &find_xattr_handlers : &list_xattr_handlers; =20 for (i =3D 0; i < vi->xattr_shared_count; ++i) { xsid =3D vi->xattr_shared_xattrs[i]; - it->blkaddr =3D erofs_xattr_blkaddr(sb, xsid); - it->ofs =3D erofs_xattr_blkoff(sb, xsid); + it->blkaddr =3D EROFS_SB(sb)->xattr_blkaddr + + erofs_blknr(sb, xsid * sizeof(__u32)); + it->ofs =3D erofs_blkoff(sb, xsid * sizeof(__u32)); it->kaddr =3D erofs_read_metabuf(&it->buf, sb, it->blkaddr, EROFS_KMAP); 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 ((it->getxattr && ret !=3D -ENOATTR) || (!it->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; + if (strlen(name) > EROFS_NAME_LEN) + return -ERANGE; + + ret =3D erofs_init_inode_xattrs(inode); + if (ret) + return ret; + + it =3D (struct erofs_xattr_iter) { + .buf =3D __EROFS_BUF_INITIALIZER, + .sb =3D inode->i_sb, + .inode =3D inode, + .name =3D QSTR_INIT(name, strlen(name)), + .index =3D index, + .buffer =3D buffer, + .buffer_size =3D buffer_size, + .getxattr =3D true, + }; + + ret =3D erofs_iter_inline_xattr(&it); + if (ret =3D=3D -ENOATTR) + ret =3D erofs_iter_shared_xattr(&it); + erofs_put_metabuf(&it.buf); + return ret ? ret : it.buffer_ofs; } =20 ssize_t erofs_listxattr(struct dentry *dentry, @@ -599,15 +544,19 @@ ssize_t erofs_listxattr(struct dentry *dentry, .buf =3D __EROFS_BUF_INITIALIZER, .sb =3D dentry->d_sb, .dentry =3D dentry, + .inode =3D d_inode(dentry), .buffer =3D buffer, .buffer_size =3D buffer_size, + .getxattr =3D false, }; =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); + if (!ret || ret =3D=3D -ENOATTR) + ret =3D erofs_iter_shared_xattr(&it); + 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