Received: by 2002:a05:6358:3188:b0:123:57c1:9b43 with SMTP id q8csp5510359rwd; Mon, 12 Jun 2023 06:14:24 -0700 (PDT) X-Google-Smtp-Source: ACHHUZ5iH6RUYoTNVyUaTA/eFDdMUOcse1hiwjiEBOUhoMIN82urndpWXCINphOa2GTzq6pj/TM1 X-Received: by 2002:aa7:d5c2:0:b0:50b:c9d7:8b24 with SMTP id d2-20020aa7d5c2000000b0050bc9d78b24mr4070448eds.6.1686575664198; Mon, 12 Jun 2023 06:14:24 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1686575664; cv=none; d=google.com; s=arc-20160816; b=r495qEVmtaGNQfpavIbbG74rcwajDChqIPATct5epC1UwhjgA1+kacFOOSyIBViZfU pO3CCtsH67+2jVdKIPhGPEiD2G8eKA6KM2LGoPsrz3snkuBs3Bbs8WE++IbI1CZt4aXb TTb1JJ1qYeWw4wuH67wXPKhIjkNm1zneXQti8VKa0YUdm/64EEoWx2CdTpC0y1jWpujV QBn0nBj23CpzkdeoL1LEO+G2Pwo1pQF2fjYxk71To94JgKLQKi4sqIZxhcwN5mNaIFdT 4mBgurtGCslyOzQg0rbX9cT4Qw5oG54uf9QQVgfk9YjhSntw3Kx3i3r57OzpF/rRDpwS UM5A== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :references:in-reply-to:message-id:date:subject:cc:to:from; bh=G1SWJGhNnWCEtN1qOqF6mA9CQz8RDRaswYYLtCbfe+c=; b=RDWiTi1ZBd8fOqiP/IiBMBx+kEU8FRXzgwV/gQf3jJrPjIRR3b91wdeQoftPOHQ/iv BwbiwR6HPrOCO0aYeGx2CLoRitHaQuz5L3mRTsAl4He4DK7pfMr0dEphlk1xmDJGM019 ebOvAvWqZiRTGxpD1PRbTkNn7GMQDew0Q+UwCFwdWikvyzjYqoVdHIH7jUtBt/IFqwBN /EaA6ybrMPEN+30/2KGlC9fKlcncwq7hTjrVs5P0p6tKOyAXsAXq1zWuKDPOdpD7AsdD aO42/4T9ir1rUyuqmmRIQlH+bs0uZPhQs+h8XpKmrHjwfxukZFMhf0QG13uXE19B5DDq IZcQ== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=alibaba.com Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id p26-20020aa7cc9a000000b005148f8c24d5si5610809edt.382.2023.06.12.06.13.59; Mon, 12 Jun 2023 06:14:24 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=alibaba.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S234881AbjFLMiI (ORCPT + 99 others); Mon, 12 Jun 2023 08:38:08 -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: 8bit X-Spam-Status: No, score=-9.9 required=5.0 tests=BAYES_00, ENV_AND_HDR_SPF_MATCH,RCVD_IN_DNSWL_NONE,SPF_HELO_NONE,SPF_PASS, T_SCC_BODY_TEXT_LINE,UNPARSEABLE_RELAY,USER_IN_DEF_SPF_WL autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org 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); }; -static int inline_xattr_iter_begin(struct erofs_xattr_iter *it, - struct inode *inode) -{ - struct erofs_inode *const vi = EROFS_I(inode); - unsigned int xattr_header_sz; - - xattr_header_sz = sizeof(struct erofs_xattr_ibody_header) + - sizeof(u32) * vi->xattr_shared_count; - if (xattr_header_sz >= vi->xattr_isize) { - DBG_BUGON(xattr_header_sz > vi->xattr_isize); - return -ENOATTR; - } - - it->pos = erofs_iloc(inode) + vi->inode_isize + xattr_header_sz; - it->kaddr = 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_handlers = { .value = xattr_copyvalue }; -static int inline_getxattr(struct inode *inode, struct erofs_xattr_iter *it) -{ - int ret; - unsigned int remaining; - - ret = inline_xattr_iter_begin(it, inode); - if (ret < 0) - return ret; - - remaining = ret; - while (remaining) { - ret = xattr_foreach(it, &find_xattr_handlers, &remaining); - if (ret != -ENOATTR) - break; - } - return ret ? ret : it->buffer_ofs; -} - -static int shared_getxattr(struct inode *inode, struct erofs_xattr_iter *it) -{ - struct erofs_inode *const vi = EROFS_I(inode); - struct super_block *const sb = it->sb; - struct erofs_sb_info *sbi = EROFS_SB(sb); - unsigned int i; - int ret = -ENOATTR; - - for (i = 0; i < vi->xattr_shared_count; ++i) { - it->pos = erofs_pos(sb, sbi->xattr_blkaddr) + - vi->xattr_shared_xattrs[i] * sizeof(__le32); - it->kaddr = erofs_bread(&it->buf, erofs_blknr(sb, it->pos), - EROFS_KMAP); - if (IS_ERR(it->kaddr)) - return PTR_ERR(it->kaddr); - - ret = xattr_foreach(it, &find_xattr_handlers, NULL); - if (ret != -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 *dentry) return capable(CAP_SYS_ADMIN); } -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 = erofs_init_inode_xattrs(inode); - if (ret) - return ret; - - it.index = index; - it.name = (struct qstr)QSTR_INIT(name, strlen(name)); - if (it.name.len > EROFS_NAME_LEN) - return -ERANGE; - - it.sb = inode->i_sb; - it.buf = __EROFS_BUF_INITIALIZER; - erofs_init_metabuf(&it.buf, it.sb); - it.buffer = buffer; - it.buffer_size = buffer_size; - it.buffer_ofs = 0; - - ret = inline_getxattr(inode, &it); - if (ret == -ENOATTR) - ret = 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_handlers = { .value = NULL }; -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 = EROFS_I(inode); + const struct xattr_iter_handlers *op; + unsigned int xattr_header_sz, remaining; int ret; - unsigned int remaining; - ret = inline_xattr_iter_begin(it, d_inode(it->dentry)); - if (ret < 0) - return ret; + xattr_header_sz = sizeof(struct erofs_xattr_ibody_header) + + sizeof(u32) * vi->xattr_shared_count; + if (xattr_header_sz >= vi->xattr_isize) { + DBG_BUGON(xattr_header_sz > vi->xattr_isize); + return -ENOATTR; + } + + it->pos = erofs_iloc(inode) + vi->inode_isize + xattr_header_sz; + it->kaddr = erofs_bread(&it->buf, erofs_blknr(it->sb, it->pos), + EROFS_KMAP); + if (IS_ERR(it->kaddr)) + return PTR_ERR(it->kaddr); + + remaining = vi->xattr_isize - xattr_header_sz; + op = getxattr ? &find_xattr_handlers : &list_xattr_handlers; - remaining = ret; while (remaining) { - ret = xattr_foreach(it, &list_xattr_handlers, &remaining); - if (ret) + ret = xattr_foreach(it, op, &remaining); + if ((getxattr && ret != -ENOATTR) || (!getxattr && ret)) break; } - return ret ? ret : it->buffer_ofs; + return ret; } -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 = d_inode(it->dentry); struct erofs_inode *const vi = EROFS_I(inode); struct super_block *const sb = it->sb; struct erofs_sb_info *sbi = EROFS_SB(sb); unsigned int i; - int ret = 0; + const struct xattr_iter_handlers *op; + int ret = -ENOATTR; + + op = getxattr ? &find_xattr_handlers : &list_xattr_handlers; for (i = 0; i < vi->xattr_shared_count; ++i) { it->pos = 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); - ret = xattr_foreach(it, &list_xattr_handlers, NULL); - if (ret) + ret = xattr_foreach(it, op, NULL); + if ((getxattr && ret != -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 = erofs_init_inode_xattrs(inode); + if (ret) + return ret; + + it.index = index; + it.name = (struct qstr)QSTR_INIT(name, strlen(name)); + if (it.name.len > EROFS_NAME_LEN) + return -ERANGE; + + it.sb = inode->i_sb; + it.buf = __EROFS_BUF_INITIALIZER; + erofs_init_metabuf(&it.buf, it.sb); + it.buffer = buffer; + it.buffer_size = buffer_size; + it.buffer_ofs = 0; + + ret = erofs_iter_inline_xattr(&it, inode, true); + if (ret == -ENOATTR) + ret = erofs_iter_shared_xattr(&it, inode, true); + erofs_put_metabuf(&it.buf); + return ret ? ret : it.buffer_ofs; } 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 = d_inode(dentry); - ret = erofs_init_inode_xattrs(d_inode(dentry)); + ret = erofs_init_inode_xattrs(inode); if (ret == -ENOATTR) return 0; if (ret) @@ -583,11 +539,13 @@ ssize_t erofs_listxattr(struct dentry *dentry, it.buffer_size = buffer_size; it.buffer_ofs = 0; - ret = inline_listxattr(&it); - if (ret >= 0 || ret == -ENOATTR) - ret = shared_listxattr(&it); + ret = erofs_iter_inline_xattr(&it, inode, false); + if (!ret || ret == -ENOATTR) + ret = erofs_iter_shared_xattr(&it, inode, false); + if (ret == -ENOATTR) + ret = 0; erofs_put_metabuf(&it.buf); - return ret; + return ret ? ret : it.buffer_ofs; } void erofs_xattr_prefixes_cleanup(struct super_block *sb) -- 2.19.1.6.gb485710b