Received: by 2002:a05:6358:11c7:b0:104:8066:f915 with SMTP id i7csp6852209rwl; Wed, 22 Mar 2023 17:12:08 -0700 (PDT) X-Google-Smtp-Source: AK7set+zcAyCQHYJuPhpzRjmq1s98E6AmCuMGcmWAQny8XvoOelkpE/iRBtZ23ESx0r6GjmRdYwQ X-Received: by 2002:a17:90a:53:b0:237:d2d8:3264 with SMTP id 19-20020a17090a005300b00237d2d83264mr5671376pjb.40.1679530327753; Wed, 22 Mar 2023 17:12:07 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1679530327; cv=none; d=google.com; s=arc-20160816; b=pq1TSd2+nxTHBdkGPEnvP/tuXAi5Z+dsG5Y76byzXyrIZU0TfZj/J2nQw8Gb4XyXVg JicXPxFtbekF8FaHigplCD2SwqeKFyIaUKVy1KZ9FO45Ic/1YNKnjDN8dr/9M6FFHu4Q FJokgMghZCyKYqRs2UohVktDIjb8pASGav0nVW5hAI+Smz6QB1dkLKoCgOyRQak/VgRw bpsjEl0ELakdhjvKm4cktTSp37aS2EACHB44ToHuIRfUaZyvYpVugNl2nHT9mnT0w3yU 1V6MSEPa9Qo5UKta9TbZfOJAIfqhRlFhgWpjAs3AjazALSCTU0ZJyfHZxtJ+8GSV0El1 Ldjw== 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=sojtUn+DbXG+omccaSHvKfQ5aRSaBSbZ3tpJB6XIvs0=; b=by85APLaN7cye2531EGXnYdTHc5ReuGiHyJ4Hz1RTj1p27iIOmoKehiB3jnxERjTor bUvPeIz0Lbt3m8UYEkG65pGaX1YVtE7JRoIQKulLyQNhMYiav52Cssh6Xka9HprOEtxH c2wUxzrBxkLenn8Fh7ZluI7qaxx+kACrxObgJGlw6GKMVRk4JoQI3L97rZkGqR6TpW3p YJ7jfWGGJuzDdD1QqXLLKmcFvCUQHoFC2Z8rDF/cbTT39c8JdzI6K6ismZSR5gosHNWe pik/WUmv29MDZuOQYpa4uSc6QMXjTOWi8XDcjfNXnLxT93CvV6ZKUqK2m7rEksb+8Cz7 D4RA== 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 ks5-20020a170903084500b001a1d78af685si7694268plb.466.2023.03.22.17.11.56; Wed, 22 Mar 2023 17:12:07 -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 S230481AbjCWAK4 (ORCPT + 99 others); Wed, 22 Mar 2023 20:10:56 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:45594 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230429AbjCWAKu (ORCPT ); Wed, 22 Mar 2023 20:10:50 -0400 Received: from out30-133.freemail.mail.aliyun.com (out30-133.freemail.mail.aliyun.com [115.124.30.133]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 4E0DA32E6B for ; Wed, 22 Mar 2023 17:10:11 -0700 (PDT) X-Alimail-AntiSpam: AC=PASS;BC=-1|-1;BR=01201311R801e4;CH=green;DM=||false|;DS=||;FP=0|-1|-1|-1|0|-1|-1|-1;HT=ay29a033018046059;MF=jefflexu@linux.alibaba.com;NM=1;PH=DS;RN=5;SR=0;TI=SMTPD_---0VeS2z8._1679530196; Received: from localhost(mailfrom:jefflexu@linux.alibaba.com fp:SMTPD_---0VeS2z8._1679530196) by smtp.aliyun-inc.com; Thu, 23 Mar 2023 08:09: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 7/8] erofs: unify inline/share xattr iterators for listxattr/getxattr Date: Thu, 23 Mar 2023 08:09:48 +0800 Message-Id: <20230323000949.57608-8-jefflexu@linux.alibaba.com> X-Mailer: git-send-email 2.19.1.6.gb485710b In-Reply-To: <20230323000949.57608-1-jefflexu@linux.alibaba.com> References: <20230323000949.57608-1-jefflexu@linux.alibaba.com> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Spam-Status: No, score=-8.0 required=5.0 tests=ENV_AND_HDR_SPF_MATCH, RCVD_IN_DNSWL_NONE,RCVD_IN_MSPIKE_H2,SPF_HELO_NONE,SPF_PASS, UNPARSEABLE_RELAY,USER_IN_DEF_SPF_WL autolearn=unavailable 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_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 | 210 ++++++++++++++++++----------------------------- 1 file changed, 81 insertions(+), 129 deletions(-) diff --git a/fs/erofs/xattr.c b/fs/erofs/xattr.c index 264561ccaa44..196b2eb59e29 100644 --- a/fs/erofs/xattr.c +++ b/fs/erofs/xattr.c @@ -7,18 +7,8 @@ #include #include "xattr.h" -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)); -} +static int erofs_getxattr(struct inode *inode, int index, const char *name, + void *buffer, size_t buffer_size); struct erofs_xattr_iter { struct super_block *sb; @@ -33,6 +23,8 @@ struct erofs_xattr_iter { int index; struct qstr name; struct dentry *dentry; + struct inode *inode; + bool getxattr; }; static inline int erofs_xattr_iter_fixup(struct erofs_xattr_iter *it) @@ -168,30 +160,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, inline_xattr_ofs; - - 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; - } - - inline_xattr_ofs = vi->inode_isize + xattr_header_sz; - - it->blkaddr = erofs_blknr(it->sb, erofs_iloc(inode) + inline_xattr_ofs); - it->ofs = erofs_blkoff(it->sb, erofs_iloc(inode) + inline_xattr_ofs); - it->kaddr = 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. @@ -323,46 +291,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; - unsigned int i, xsid; - int ret = -ENOATTR; - - for (i = 0; i < vi->xattr_shared_count; ++i) { - xsid = vi->xattr_shared_xattrs[i]; - it->blkaddr = erofs_xattr_blkaddr(sb, xsid); - it->ofs = erofs_xattr_blkoff(sb, xsid); - it->kaddr = erofs_read_metabuf(&it->buf, sb, it->blkaddr, 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); @@ -373,37 +301,6 @@ static bool erofs_xattr_trusted_list(struct dentry *dentry) return capable(CAP_SYS_ADMIN); } -static 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 = erofs_init_inode_xattrs(inode); - if (ret) - return ret; - - it = (struct erofs_xattr_iter) { - .buf = __EROFS_BUF_INITIALIZER, - .sb = inode->i_sb, - .name = QSTR_INIT(name, strlen(name)), - .index = index, - .buffer = buffer, - .buffer_size = buffer_size, - }; - - 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,45 +418,96 @@ 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 erofs_inode *const vi = 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; - 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; + } + + pos = erofs_iloc(it->inode) + vi->inode_isize + xattr_header_sz; + it->blkaddr = erofs_blknr(it->sb, pos); + it->ofs = erofs_blkoff(it->sb, pos); + it->kaddr = erofs_read_metabuf(&it->buf, it->sb, it->blkaddr, EROFS_KMAP); + if (IS_ERR(it->kaddr)) + return PTR_ERR(it->kaddr); + + remaining = vi->xattr_isize - xattr_header_sz; + op = it->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 ((it->getxattr && ret != -ENOATTR) || (!it->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 *const inode = d_inode(it->dentry); - struct erofs_inode *const vi = EROFS_I(inode); + struct erofs_inode *const vi = EROFS_I(it->inode); struct super_block *const sb = it->sb; + const struct xattr_iter_handlers *op; unsigned int i, xsid; - int ret = 0; + int ret = -ENOATTR; + + op = it->getxattr ? &find_xattr_handlers : &list_xattr_handlers; for (i = 0; i < vi->xattr_shared_count; ++i) { xsid = vi->xattr_shared_xattrs[i]; - it->blkaddr = erofs_xattr_blkaddr(sb, xsid); - it->ofs = erofs_xattr_blkoff(sb, xsid); + it->blkaddr = EROFS_SB(sb)->xattr_blkaddr + + erofs_blknr(sb, xsid * sizeof(__u32)); + it->ofs = erofs_blkoff(sb, xsid * sizeof(__u32)); it->kaddr = erofs_read_metabuf(&it->buf, sb, it->blkaddr, EROFS_KMAP); 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 ((it->getxattr && ret != -ENOATTR) || (!it->getxattr && ret)) break; } - return ret ? ret : it->buffer_ofs; + return ret; +} + +static 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 = erofs_init_inode_xattrs(inode); + if (ret) + return ret; + + it = (struct erofs_xattr_iter) { + .buf = __EROFS_BUF_INITIALIZER, + .sb = inode->i_sb, + .inode = inode, + .name = QSTR_INIT(name, strlen(name)), + .index = index, + .buffer = buffer, + .buffer_size = buffer_size, + .getxattr = true, + }; + + ret = erofs_iter_inline_xattr(&it); + if (ret == -ENOATTR) + ret = erofs_iter_shared_xattr(&it); + erofs_put_metabuf(&it.buf); + return ret ? ret : it.buffer_ofs; } ssize_t erofs_listxattr(struct dentry *dentry, @@ -578,15 +526,19 @@ ssize_t erofs_listxattr(struct dentry *dentry, .buf = __EROFS_BUF_INITIALIZER, .sb = dentry->d_sb, .dentry = dentry, + .inode = d_inode(dentry), .buffer = buffer, .buffer_size = buffer_size, + .getxattr = false, }; - ret = inline_listxattr(&it); - if (ret >= 0 || ret == -ENOATTR) - ret = shared_listxattr(&it); + ret = erofs_iter_inline_xattr(&it); + if (!ret || ret == -ENOATTR) + ret = erofs_iter_shared_xattr(&it); + if (ret == -ENOATTR) + ret = 0; erofs_put_metabuf(&it.buf); - return ret; + return ret ? ret : it.buffer_ofs; } #ifdef CONFIG_EROFS_FS_POSIX_ACL -- 2.19.1.6.gb485710b