From patchwork Mon Jun 12 12:37:41 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jingbo Xu X-Patchwork-Id: 106578 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:994d:0:b0:3d9:f83d:47d9 with SMTP id k13csp2573101vqr; Mon, 12 Jun 2023 06:06:45 -0700 (PDT) X-Google-Smtp-Source: ACHHUZ5zRMm2ps2cMwY2/MhBnLop4+qZoCRModyJtpuZlHjkNIKvPQbnqnqTB5/7SliQIh7Pht7K X-Received: by 2002:a17:907:7e8b:b0:966:471c:2565 with SMTP id qb11-20020a1709077e8b00b00966471c2565mr9541725ejc.48.1686575205311; Mon, 12 Jun 2023 06:06:45 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1686575205; cv=none; d=google.com; s=arc-20160816; b=e1jbbHnJ3iIeyTPEL61UbBzK7lSyNLFfEjjirKJ/i8KclKOlPySM5XeVS1dBY8ieva MOIx1VOGZDZ2AoDCkt7vgSsRxutFYgrbgJktSoMMx0Coz9p2ryRYV8oH8eskMa6/q53Z KnpHP5znU9ZnsjLXBudy88XsG4J8pQ+yCVd91i/1nzpuSQumvB2+0ruiQG8Kr1LJhsQO pvRdsQF3hU7sc08qyGtq0Ng6r98lX9zLbYQkD4am5bLcwKRF3aP4hSIAzKjv8V1NZqQl 0xi5KNkBaoiceqbtJ1kjhL6n37zOKd5SK+Vs1aHE9NBJzeUH9jnb1FHNLlcJRZz6Celd 8EsA== 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=32fN3Xu4+32NSwVKPorqx4snD0YWUM9i+lnJZtF0uEk=; b=i44vaU1vyk2Xp6sCDmsm3+1hcZo1gq0sVgfzUK6JKzWFvgE0zVOc0NJ6nHVX4Yk8iW NA52yfdzYsC4pB+kMlbuLaBoryOQ/Zj2YyAJUL1Ad8SlFUiYAd/poUtPU8qexNbSTdyh m03nDq5x0nlBFrPvVHt8X5VDnFmBXEZX5w/qeQMy4p5P68XGe405gBKULHVr9h4F8vDs fyc2ll2y/zdju/wrf8BlYB7DGRuxRGtPK6hiNlDz7zxkZsOarznuU1Oe7oUSszbNiLze 7t49bPrQcVZUnVSTxuVwl7VcDtxRyWAOvRS8yiDoQugqOEQqp84VBOTtz0PHYB5+5nbn gQaw== 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 Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id ju14-20020a17090798ae00b009655045843fsi1787059ejc.204.2023.06.12.06.06.18; Mon, 12 Jun 2023 06:06:45 -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 S234212AbjFLMhz (ORCPT + 99 others); Mon, 12 Jun 2023 08:37:55 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:58876 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S234145AbjFLMhx (ORCPT ); Mon, 12 Jun 2023 08:37:53 -0400 Received: from out30-130.freemail.mail.aliyun.com (out30-130.freemail.mail.aliyun.com [115.124.30.130]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 92EBCE4C for ; Mon, 12 Jun 2023 05:37:50 -0700 (PDT) X-Alimail-AntiSpam: AC=PASS;BC=-1|-1;BR=01201311R151e4;CH=green;DM=||false|;DS=||;FP=0|-1|-1|-1|0|-1|-1|-1;HT=ay29a033018046051;MF=jefflexu@linux.alibaba.com;NM=1;PH=DS;RN=5;SR=0;TI=SMTPD_---0Vkza4rj_1686573466; Received: from localhost(mailfrom:jefflexu@linux.alibaba.com fp:SMTPD_---0Vkza4rj_1686573466) by smtp.aliyun-inc.com; Mon, 12 Jun 2023 20:37:47 +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 1/5] erofs: use absolute position in xattr iterator Date: Mon, 12 Jun 2023 20:37:41 +0800 Message-Id: <20230612123745.36323-2-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 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 X-getmail-retrieved-from-mailbox: =?utf-8?q?INBOX?= X-GMAIL-THRID: =?utf-8?q?1768502282184632404?= X-GMAIL-MSGID: =?utf-8?q?1768502282184632404?= Replace blkaddr/ofs with pos in 'struct erofs_xattr_iter'. After erofs_bread() is introduced to replace raw page cache APIs for metadata I/Os handling, xattr_iter_fixup() is no longer needed anymore. In addition, it is also unnecessary to check if the iterated position is span over the block boundary as absolute offset is used instead of blkaddr + offset pairs. Signed-off-by: Jingbo Xu Reviewed-by: Gao Xiang --- fs/erofs/xattr.c | 162 +++++++++++++++++++---------------------------- 1 file changed, 65 insertions(+), 97 deletions(-) diff --git a/fs/erofs/xattr.c b/fs/erofs/xattr.c index d9e041d27a35..4c11d4f4cf07 100644 --- a/fs/erofs/xattr.c +++ b/fs/erofs/xattr.c @@ -7,26 +7,11 @@ #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)); -} - struct xattr_iter { struct super_block *sb; struct erofs_buf buf; + erofs_off_t pos; void *kaddr; - - erofs_blk_t blkaddr; - unsigned int ofs; }; static int erofs_init_inode_xattrs(struct inode *inode) @@ -82,17 +67,16 @@ static int erofs_init_inode_xattrs(struct inode *inode) it.buf = __EROFS_BUF_INITIALIZER; erofs_init_metabuf(&it.buf, sb); - it.blkaddr = erofs_blknr(sb, erofs_iloc(inode) + vi->inode_isize); - it.ofs = erofs_blkoff(sb, erofs_iloc(inode) + vi->inode_isize); + it.pos = erofs_iloc(inode) + vi->inode_isize; /* read in shared xattr array (non-atomic, see kmalloc below) */ - it.kaddr = erofs_bread(&it.buf, it.blkaddr, EROFS_KMAP); + it.kaddr = erofs_bread(&it.buf, erofs_blknr(sb, it.pos), EROFS_KMAP); if (IS_ERR(it.kaddr)) { ret = PTR_ERR(it.kaddr); goto out_unlock; } - ih = (struct erofs_xattr_ibody_header *)(it.kaddr + it.ofs); + ih = it.kaddr + erofs_blkoff(sb, it.pos); vi->xattr_shared_count = ih->h_shared_count; vi->xattr_shared_xattrs = kmalloc_array(vi->xattr_shared_count, sizeof(uint), GFP_KERNEL); @@ -103,25 +87,20 @@ static int erofs_init_inode_xattrs(struct inode *inode) } /* let's skip ibody header */ - it.ofs += sizeof(struct erofs_xattr_ibody_header); + it.pos += sizeof(struct erofs_xattr_ibody_header); for (i = 0; i < vi->xattr_shared_count; ++i) { - if (it.ofs >= sb->s_blocksize) { - /* cannot be unaligned */ - DBG_BUGON(it.ofs != sb->s_blocksize); - - it.kaddr = erofs_bread(&it.buf, ++it.blkaddr, EROFS_KMAP); - if (IS_ERR(it.kaddr)) { - kfree(vi->xattr_shared_xattrs); - vi->xattr_shared_xattrs = NULL; - ret = PTR_ERR(it.kaddr); - goto out_unlock; - } - it.ofs = 0; + it.kaddr = erofs_bread(&it.buf, erofs_blknr(sb, it.pos), + EROFS_KMAP); + if (IS_ERR(it.kaddr)) { + kfree(vi->xattr_shared_xattrs); + vi->xattr_shared_xattrs = NULL; + ret = PTR_ERR(it.kaddr); + goto out_unlock; } - vi->xattr_shared_xattrs[i] = - le32_to_cpu(*(__le32 *)(it.kaddr + it.ofs)); - it.ofs += sizeof(__le32); + vi->xattr_shared_xattrs[i] = le32_to_cpu(*(__le32 *) + (it.kaddr + erofs_blkoff(sb, it.pos))); + it.pos += sizeof(__le32); } erofs_put_metabuf(&it.buf); @@ -150,24 +129,11 @@ struct xattr_iter_handlers { unsigned int len); }; -static inline int xattr_iter_fixup(struct xattr_iter *it) -{ - if (it->ofs < it->sb->s_blocksize) - return 0; - - it->blkaddr += erofs_blknr(it->sb, it->ofs); - it->kaddr = erofs_bread(&it->buf, it->blkaddr, EROFS_KMAP); - if (IS_ERR(it->kaddr)) - return PTR_ERR(it->kaddr); - it->ofs = erofs_blkoff(it->sb, it->ofs); - return 0; -} - static int inline_xattr_iter_begin(struct xattr_iter *it, struct inode *inode) { struct erofs_inode *const vi = EROFS_I(inode); - unsigned int xattr_header_sz, inline_xattr_ofs; + unsigned int xattr_header_sz; xattr_header_sz = sizeof(struct erofs_xattr_ibody_header) + sizeof(u32) * vi->xattr_shared_count; @@ -176,11 +142,9 @@ static int inline_xattr_iter_begin(struct xattr_iter *it, 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_bread(&it->buf, it->blkaddr, EROFS_KMAP); + 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; @@ -188,27 +152,29 @@ static int inline_xattr_iter_begin(struct xattr_iter *it, /* * Regardless of success or failure, `xattr_foreach' will end up with - * `ofs' pointing to the next xattr item rather than an arbitrary position. + * `pos' pointing to the next xattr item rather than an arbitrary position. */ static int xattr_foreach(struct xattr_iter *it, const struct xattr_iter_handlers *op, unsigned int *tlimit) { struct erofs_xattr_entry entry; + struct super_block *sb = it->sb; unsigned int value_sz, processed, slice; int err; - /* 0. fixup blkaddr, ofs, ipage */ - err = xattr_iter_fixup(it); - if (err) - return err; + /* 0. fixup blkaddr, pos */ + it->kaddr = erofs_bread(&it->buf, erofs_blknr(sb, it->pos), EROFS_KMAP); + if (IS_ERR(it->kaddr)) + return PTR_ERR(it->kaddr); /* * 1. read xattr entry to the memory, * since we do EROFS_XATTR_ALIGN * therefore entry should be in the page */ - entry = *(struct erofs_xattr_entry *)(it->kaddr + it->ofs); + entry = *(struct erofs_xattr_entry *) + (it->kaddr + erofs_blkoff(sb, it->pos)); if (tlimit) { unsigned int entry_sz = erofs_xattr_entry_size(&entry); @@ -220,40 +186,40 @@ static int xattr_foreach(struct xattr_iter *it, *tlimit -= entry_sz; } - it->ofs += sizeof(struct erofs_xattr_entry); + it->pos += sizeof(struct erofs_xattr_entry); value_sz = le16_to_cpu(entry.e_value_size); /* handle entry */ err = op->entry(it, &entry); if (err) { - it->ofs += entry.e_name_len + value_sz; + it->pos += entry.e_name_len + value_sz; goto out; } - /* 2. handle xattr name (ofs will finally be at the end of name) */ + /* 2. handle xattr name (pos will finally be at the end of name) */ processed = 0; while (processed < entry.e_name_len) { - if (it->ofs >= it->sb->s_blocksize) { - DBG_BUGON(it->ofs > it->sb->s_blocksize); - - err = xattr_iter_fixup(it); - if (err) - goto out; - it->ofs = 0; + it->kaddr = erofs_bread(&it->buf, erofs_blknr(sb, it->pos), + EROFS_KMAP); + if (IS_ERR(it->kaddr)) { + err = PTR_ERR(it->kaddr); + goto out; } - slice = min_t(unsigned int, it->sb->s_blocksize - it->ofs, + slice = min_t(unsigned int, + sb->s_blocksize - erofs_blkoff(sb, it->pos), entry.e_name_len - processed); /* handle name */ - err = op->name(it, processed, it->kaddr + it->ofs, slice); + err = op->name(it, processed, + it->kaddr + erofs_blkoff(sb, it->pos), slice); if (err) { - it->ofs += entry.e_name_len - processed + value_sz; + it->pos += entry.e_name_len - processed + value_sz; goto out; } - it->ofs += slice; + it->pos += slice; processed += slice; } @@ -263,31 +229,31 @@ static int xattr_foreach(struct xattr_iter *it, if (op->alloc_buffer) { err = op->alloc_buffer(it, value_sz); if (err) { - it->ofs += value_sz; + it->pos += value_sz; goto out; } } while (processed < value_sz) { - if (it->ofs >= it->sb->s_blocksize) { - DBG_BUGON(it->ofs > it->sb->s_blocksize); - - err = xattr_iter_fixup(it); - if (err) - goto out; - it->ofs = 0; + it->kaddr = erofs_bread(&it->buf, erofs_blknr(sb, it->pos), + EROFS_KMAP); + if (IS_ERR(it->kaddr)) { + err = PTR_ERR(it->kaddr); + goto out; } - slice = min_t(unsigned int, it->sb->s_blocksize - it->ofs, + slice = min_t(unsigned int, + sb->s_blocksize - erofs_blkoff(sb, it->pos), value_sz - processed); - op->value(it, processed, it->kaddr + it->ofs, slice); - it->ofs += slice; + op->value(it, processed, it->kaddr + erofs_blkoff(sb, it->pos), + slice); + it->pos += slice; processed += slice; } out: /* xattrs should be 4-byte aligned (on-disk constraint) */ - it->ofs = EROFS_XATTR_ALIGN(it->ofs); + it->pos = EROFS_XATTR_ALIGN(it->pos); return err < 0 ? err : 0; } @@ -394,14 +360,15 @@ static int shared_getxattr(struct inode *inode, struct getxattr_iter *it) { struct erofs_inode *const vi = EROFS_I(inode); struct super_block *const sb = it->it.sb; - unsigned int i, xsid; + struct erofs_sb_info *sbi = EROFS_SB(sb); + unsigned int i; int ret = -ENOATTR; for (i = 0; i < vi->xattr_shared_count; ++i) { - xsid = vi->xattr_shared_xattrs[i]; - it->it.blkaddr = erofs_xattr_blkaddr(sb, xsid); - it->it.ofs = erofs_xattr_blkoff(sb, xsid); - it->it.kaddr = erofs_bread(&it->it.buf, it->it.blkaddr, EROFS_KMAP); + it->it.pos = erofs_pos(sb, sbi->xattr_blkaddr) + + vi->xattr_shared_xattrs[i] * sizeof(__le32); + it->it.kaddr = erofs_bread(&it->it.buf, + erofs_blknr(sb, it->it.pos), EROFS_KMAP); if (IS_ERR(it->it.kaddr)) return PTR_ERR(it->it.kaddr); @@ -599,14 +566,15 @@ static int shared_listxattr(struct listxattr_iter *it) struct inode *const inode = d_inode(it->dentry); struct erofs_inode *const vi = EROFS_I(inode); struct super_block *const sb = it->it.sb; - unsigned int i, xsid; + struct erofs_sb_info *sbi = EROFS_SB(sb); + unsigned int i; int ret = 0; for (i = 0; i < vi->xattr_shared_count; ++i) { - xsid = vi->xattr_shared_xattrs[i]; - it->it.blkaddr = erofs_xattr_blkaddr(sb, xsid); - it->it.ofs = erofs_xattr_blkoff(sb, xsid); - it->it.kaddr = erofs_bread(&it->it.buf, it->it.blkaddr, EROFS_KMAP); + it->it.pos = erofs_pos(sb, sbi->xattr_blkaddr) + + vi->xattr_shared_xattrs[i] * sizeof(__le32); + it->it.kaddr = erofs_bread(&it->it.buf, + erofs_blknr(sb, it->it.pos), EROFS_KMAP); if (IS_ERR(it->it.kaddr)) return PTR_ERR(it->it.kaddr); From patchwork Mon Jun 12 12:37:42 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jingbo Xu X-Patchwork-Id: 106583 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:994d:0:b0:3d9:f83d:47d9 with SMTP id k13csp2580728vqr; Mon, 12 Jun 2023 06:16:34 -0700 (PDT) X-Google-Smtp-Source: ACHHUZ6ruMmeC3uGTwdUFjwBU/Li8rZGB/nRkNvuttjW4QQAVPnfDsoFjKRjUJVInpCv45czImOZ X-Received: by 2002:a17:906:dac2:b0:961:a67:28d with SMTP id xi2-20020a170906dac200b009610a67028dmr10022652ejb.22.1686575794053; Mon, 12 Jun 2023 06:16:34 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1686575794; cv=none; d=google.com; s=arc-20160816; b=HHH9XBdYPGeAGWYXsnwKLklgsJmM+tShGIFC7OepVyRi/LK/p9xEmRnLNk+chzmnfg J+TTKp5422no6ytP6gKzsVHWsa7MwfxxQPzqngX6eQQFWREeKRwt8hZxmN+x19akgIx0 qa/III6u5x9purPFPnM1tR5exoQ0DycmyRJb0vMlEsvZATBE3OkudCp9vYVY/QcL5Ae5 98cxkseFJJdeufc7V4bT8QL/mW+1c4a7twMGbP1FWXy9xNr2nfdd6Nmt5nuszVQtQUQb e8RxQerHo84BMFeuO5BA5Xmfaml0jo0bvbQbBSxHLx2F9e9jiponA1yDz79CaIOBwzVv /YaQ== 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=5AGh/WWbRWBSZ56jgCc2GIFymrgW0jHFCwLJJ5+KJ08=; b=H7lW9UaT5vnQmcqGsGWuw1gwxlIHmVTP5xyE4yTGa8s3TWWkwkwR1SKGkFmH3/Wji6 5uSXtC+SMuKixRm+AOY8TOMttaDMsQeN5qXNy90TRxRCrmQ80Z5lmM7vdQbth7Ve/wLb sReS+1JCd3jbpIvn6j1rkd37cmYEClqGop3KnKpwNTYZh/V8zO6PnOkVQ/xf0HQxrUrG osF9CvCX3scRg7/OEcGtzExGuX9oHfjUeiHIUlDpOmy/7A7PDU8NTEuQ/423JdJw1fUd bSM04zFJGaNbc4FrApzZD/55Gj+viw16rXkZ1EYoeMfQncR45XAQmyZxexiRN82qdJwQ bBxg== 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 Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id le9-20020a170907170900b0097738977c35si2084373ejc.850.2023.06.12.06.15.58; Mon, 12 Jun 2023 06:16:34 -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 S234639AbjFLMh7 (ORCPT + 99 others); Mon, 12 Jun 2023 08:37:59 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:58888 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S234238AbjFLMhx (ORCPT ); Mon, 12 Jun 2023 08:37:53 -0400 Received: from out30-113.freemail.mail.aliyun.com (out30-113.freemail.mail.aliyun.com [115.124.30.113]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id CCFE7E51 for ; Mon, 12 Jun 2023 05:37:51 -0700 (PDT) X-Alimail-AntiSpam: AC=PASS;BC=-1|-1;BR=01201311R191e4;CH=green;DM=||false|;DS=||;FP=0|-1|-1|-1|0|-1|-1|-1;HT=ay29a033018045192;MF=jefflexu@linux.alibaba.com;NM=1;PH=DS;RN=5;SR=0;TI=SMTPD_---0VkzU.CW_1686573468; Received: from localhost(mailfrom:jefflexu@linux.alibaba.com fp:SMTPD_---0VkzU.CW_1686573468) by smtp.aliyun-inc.com; Mon, 12 Jun 2023 20:37:48 +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 2/5] erofs: unify xattr_iter structures Date: Mon, 12 Jun 2023 20:37:42 +0800 Message-Id: <20230612123745.36323-3-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 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_FILL_THIS_FORM_SHORT,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 X-getmail-retrieved-from-mailbox: =?utf-8?q?INBOX?= X-GMAIL-THRID: =?utf-8?q?1768502899698503061?= X-GMAIL-MSGID: =?utf-8?q?1768502899698503061?= Unify xattr_iter/listxattr_iter/getxattr_iter structures into erofs_xattr_iter structure. This is in preparation for the following further cleanup. Signed-off-by: Jingbo Xu Reviewed-by: Gao Xiang --- fs/erofs/xattr.c | 146 ++++++++++++++++++++--------------------------- 1 file changed, 62 insertions(+), 84 deletions(-) diff --git a/fs/erofs/xattr.c b/fs/erofs/xattr.c index 4c11d4f4cf07..b2802121e3aa 100644 --- a/fs/erofs/xattr.c +++ b/fs/erofs/xattr.c @@ -7,17 +7,27 @@ #include #include "xattr.h" -struct xattr_iter { +struct erofs_xattr_iter { struct super_block *sb; struct erofs_buf buf; erofs_off_t pos; void *kaddr; + + char *buffer; + int buffer_size, buffer_ofs; + + /* getxattr */ + int index, infix_len; + struct qstr name; + + /* listxattr */ + struct dentry *dentry; }; static int erofs_init_inode_xattrs(struct inode *inode) { struct erofs_inode *const vi = EROFS_I(inode); - struct xattr_iter it; + struct erofs_xattr_iter it; unsigned int i; struct erofs_xattr_ibody_header *ih; struct super_block *sb = inode->i_sb; @@ -121,15 +131,15 @@ static int erofs_init_inode_xattrs(struct inode *inode) * and need to be handled */ struct xattr_iter_handlers { - int (*entry)(struct xattr_iter *_it, struct erofs_xattr_entry *entry); - int (*name)(struct xattr_iter *_it, unsigned int processed, char *buf, + int (*entry)(struct erofs_xattr_iter *it, struct erofs_xattr_entry *entry); + int (*name)(struct erofs_xattr_iter *it, unsigned int processed, char *buf, unsigned int len); - int (*alloc_buffer)(struct xattr_iter *_it, unsigned int value_sz); - void (*value)(struct xattr_iter *_it, unsigned int processed, char *buf, + 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); }; -static int inline_xattr_iter_begin(struct xattr_iter *it, +static int inline_xattr_iter_begin(struct erofs_xattr_iter *it, struct inode *inode) { struct erofs_inode *const vi = EROFS_I(inode); @@ -154,7 +164,7 @@ static int inline_xattr_iter_begin(struct xattr_iter *it, * Regardless of success or failure, `xattr_foreach' will end up with * `pos' pointing to the next xattr item rather than an arbitrary position. */ -static int xattr_foreach(struct xattr_iter *it, +static int xattr_foreach(struct erofs_xattr_iter *it, const struct xattr_iter_handlers *op, unsigned int *tlimit) { @@ -257,18 +267,10 @@ static int xattr_foreach(struct xattr_iter *it, return err < 0 ? err : 0; } -struct getxattr_iter { - struct xattr_iter it; - - char *buffer; - int buffer_size, index, infix_len; - struct qstr name; -}; - -static int erofs_xattr_long_entrymatch(struct getxattr_iter *it, +static int erofs_xattr_long_entrymatch(struct erofs_xattr_iter *it, struct erofs_xattr_entry *entry) { - struct erofs_sb_info *sbi = EROFS_SB(it->it.sb); + struct erofs_sb_info *sbi = EROFS_SB(it->sb); struct erofs_xattr_prefix_item *pf = sbi->xattr_prefixes + (entry->e_name_index & EROFS_XATTR_LONG_PREFIX_MASK); @@ -286,11 +288,9 @@ static int erofs_xattr_long_entrymatch(struct getxattr_iter *it, return 0; } -static int xattr_entrymatch(struct xattr_iter *_it, +static int xattr_entrymatch(struct erofs_xattr_iter *it, struct erofs_xattr_entry *entry) { - struct getxattr_iter *it = container_of(_it, struct getxattr_iter, it); - /* 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); @@ -302,32 +302,27 @@ static int xattr_entrymatch(struct xattr_iter *_it, return 0; } -static int xattr_namematch(struct xattr_iter *_it, +static int xattr_namematch(struct erofs_xattr_iter *it, unsigned int processed, char *buf, unsigned int len) { - struct getxattr_iter *it = container_of(_it, struct getxattr_iter, it); - if (memcmp(buf, it->name.name + it->infix_len + processed, len)) return -ENOATTR; return 0; } -static int xattr_checkbuffer(struct xattr_iter *_it, +static int xattr_checkbuffer(struct erofs_xattr_iter *it, unsigned int value_sz) { - struct getxattr_iter *it = container_of(_it, struct getxattr_iter, it); int err = it->buffer_size < value_sz ? -ERANGE : 0; it->buffer_size = value_sz; return !it->buffer ? 1 : err; } -static void xattr_copyvalue(struct xattr_iter *_it, +static void xattr_copyvalue(struct erofs_xattr_iter *it, unsigned int processed, char *buf, unsigned int len) { - struct getxattr_iter *it = container_of(_it, struct getxattr_iter, it); - memcpy(it->buffer + processed, buf, len); } @@ -338,41 +333,41 @@ static const struct xattr_iter_handlers find_xattr_handlers = { .value = xattr_copyvalue }; -static int inline_getxattr(struct inode *inode, struct getxattr_iter *it) +static int inline_getxattr(struct inode *inode, struct erofs_xattr_iter *it) { int ret; unsigned int remaining; - ret = inline_xattr_iter_begin(&it->it, inode); + ret = inline_xattr_iter_begin(it, inode); if (ret < 0) return ret; remaining = ret; while (remaining) { - ret = xattr_foreach(&it->it, &find_xattr_handlers, &remaining); + ret = xattr_foreach(it, &find_xattr_handlers, &remaining); if (ret != -ENOATTR) break; } return ret ? ret : it->buffer_size; } -static int shared_getxattr(struct inode *inode, struct getxattr_iter *it) +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->it.sb; + 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->it.pos = erofs_pos(sb, sbi->xattr_blkaddr) + + it->pos = erofs_pos(sb, sbi->xattr_blkaddr) + vi->xattr_shared_xattrs[i] * sizeof(__le32); - it->it.kaddr = erofs_bread(&it->it.buf, - erofs_blknr(sb, it->it.pos), EROFS_KMAP); - if (IS_ERR(it->it.kaddr)) - return PTR_ERR(it->it.kaddr); + 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->it, &find_xattr_handlers, NULL); + ret = xattr_foreach(it, &find_xattr_handlers, NULL); if (ret != -ENOATTR) break; } @@ -394,7 +389,7 @@ int erofs_getxattr(struct inode *inode, int index, void *buffer, size_t buffer_size) { int ret; - struct getxattr_iter it; + struct erofs_xattr_iter it; if (!name) return -EINVAL; @@ -404,22 +399,21 @@ int erofs_getxattr(struct inode *inode, int index, return ret; it.index = index; - it.name.len = strlen(name); + it.name = (struct qstr)QSTR_INIT(name, strlen(name)); if (it.name.len > EROFS_NAME_LEN) return -ERANGE; - it.it.sb = inode->i_sb; - it.it.buf = __EROFS_BUF_INITIALIZER; - erofs_init_metabuf(&it.it.buf, it.it.sb); - it.name.name = name; - + 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.it.buf); + erofs_put_metabuf(&it.buf); return ret; } @@ -465,25 +459,15 @@ const struct xattr_handler *erofs_xattr_handlers[] = { NULL, }; -struct listxattr_iter { - struct xattr_iter it; - - struct dentry *dentry; - char *buffer; - int buffer_size, buffer_ofs; -}; - -static int xattr_entrylist(struct xattr_iter *_it, +static int xattr_entrylist(struct erofs_xattr_iter *it, struct erofs_xattr_entry *entry) { - struct listxattr_iter *it = - container_of(_it, struct listxattr_iter, it); unsigned int base_index = entry->e_name_index; unsigned int prefix_len, infix_len = 0; const char *prefix, *infix = NULL; if (entry->e_name_index & EROFS_XATTR_LONG_PREFIX) { - struct erofs_sb_info *sbi = EROFS_SB(_it->sb); + struct erofs_sb_info *sbi = EROFS_SB(it->sb); struct erofs_xattr_prefix_item *pf = sbi->xattr_prefixes + (entry->e_name_index & EROFS_XATTR_LONG_PREFIX_MASK); @@ -515,23 +499,17 @@ static int xattr_entrylist(struct xattr_iter *_it, return 0; } -static int xattr_namelist(struct xattr_iter *_it, +static int xattr_namelist(struct erofs_xattr_iter *it, unsigned int processed, char *buf, unsigned int len) { - struct listxattr_iter *it = - container_of(_it, struct listxattr_iter, it); - memcpy(it->buffer + it->buffer_ofs, buf, len); it->buffer_ofs += len; return 0; } -static int xattr_skipvalue(struct xattr_iter *_it, +static int xattr_skipvalue(struct erofs_xattr_iter *it, unsigned int value_sz) { - struct listxattr_iter *it = - container_of(_it, struct listxattr_iter, it); - it->buffer[it->buffer_ofs++] = '\0'; return 1; } @@ -543,42 +521,42 @@ static const struct xattr_iter_handlers list_xattr_handlers = { .value = NULL }; -static int inline_listxattr(struct listxattr_iter *it) +static int inline_listxattr(struct erofs_xattr_iter *it) { int ret; unsigned int remaining; - ret = inline_xattr_iter_begin(&it->it, d_inode(it->dentry)); + ret = inline_xattr_iter_begin(it, d_inode(it->dentry)); if (ret < 0) return ret; remaining = ret; while (remaining) { - ret = xattr_foreach(&it->it, &list_xattr_handlers, &remaining); + ret = xattr_foreach(it, &list_xattr_handlers, &remaining); if (ret) break; } return ret ? ret : it->buffer_ofs; } -static int shared_listxattr(struct listxattr_iter *it) +static int shared_listxattr(struct erofs_xattr_iter *it) { struct inode *const inode = d_inode(it->dentry); struct erofs_inode *const vi = EROFS_I(inode); - struct super_block *const sb = it->it.sb; + struct super_block *const sb = it->sb; struct erofs_sb_info *sbi = EROFS_SB(sb); unsigned int i; int ret = 0; for (i = 0; i < vi->xattr_shared_count; ++i) { - it->it.pos = erofs_pos(sb, sbi->xattr_blkaddr) + + it->pos = erofs_pos(sb, sbi->xattr_blkaddr) + vi->xattr_shared_xattrs[i] * sizeof(__le32); - it->it.kaddr = erofs_bread(&it->it.buf, - erofs_blknr(sb, it->it.pos), EROFS_KMAP); - if (IS_ERR(it->it.kaddr)) - return PTR_ERR(it->it.kaddr); + 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->it, &list_xattr_handlers, NULL); + ret = xattr_foreach(it, &list_xattr_handlers, NULL); if (ret) break; } @@ -589,7 +567,7 @@ ssize_t erofs_listxattr(struct dentry *dentry, char *buffer, size_t buffer_size) { int ret; - struct listxattr_iter it; + struct erofs_xattr_iter it; ret = erofs_init_inode_xattrs(d_inode(dentry)); if (ret == -ENOATTR) @@ -597,9 +575,9 @@ ssize_t erofs_listxattr(struct dentry *dentry, if (ret) return ret; - it.it.sb = dentry->d_sb; - it.it.buf = __EROFS_BUF_INITIALIZER; - erofs_init_metabuf(&it.it.buf, it.it.sb); + it.sb = dentry->d_sb; + it.buf = __EROFS_BUF_INITIALIZER; + erofs_init_metabuf(&it.buf, it.sb); it.dentry = dentry; it.buffer = buffer; it.buffer_size = buffer_size; @@ -608,7 +586,7 @@ ssize_t erofs_listxattr(struct dentry *dentry, ret = inline_listxattr(&it); if (ret >= 0 || ret == -ENOATTR) ret = shared_listxattr(&it); - erofs_put_metabuf(&it.it.buf); + erofs_put_metabuf(&it.buf); return ret; } From patchwork Mon Jun 12 12:37:43 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jingbo Xu X-Patchwork-Id: 106572 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:994d:0:b0:3d9:f83d:47d9 with SMTP id k13csp2564771vqr; Mon, 12 Jun 2023 05:56:44 -0700 (PDT) X-Google-Smtp-Source: ACHHUZ5zvYjyZs+H/zGu3eBrT0f3B+CBa6FPUrzrU0xQ/Nu3PsjxlM4BjBKPPh6o7B4iBVPWswQU X-Received: by 2002:a05:6a21:3888:b0:10f:9317:1539 with SMTP id yj8-20020a056a21388800b0010f93171539mr7845088pzb.32.1686574603823; Mon, 12 Jun 2023 05:56:43 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1686574603; cv=none; d=google.com; s=arc-20160816; b=OeP3NJpWbnfJmoAuCdrm5Q7Xqyc8q0/exOjAY+uluUVnQg/WtFy9nOp45z0b1wE1jR 2HrMoeREI86srOvK+7dZfen+OweNCRodO6JpM+Xj3TkkmLeV681nozPmJFQG/MtEkvcb JAQ+RMaewwYLhIj/M1DBUJW7/teqJarx9S+7z3VZ39x7+JvzAlfeIWDzFNCFMblPG9gN W26hfc3kn+DWVCCyHdJcW7E73f1BOgkpAjSrX+HP2Q+VQuSinvmNzzRajX3mcrqEHKBU DQG4UF3Yh+U8L4Jj6IaYoWNW7kM2pdPwZzWwNeftJNXOyAnkl35GG8baV6sgd7VrysdL 7QLQ== 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=5CH4qmGTEswLKkIHRBvy3oA0lCQL8ln1Im8LoIxDWfc=; b=IdO2AqZHvKoqF1gsyDMRp4+mK3P6ggXSd5ZjN3tlOGLqrVBEnSlkMefPltv+4/5Up1 ODCVPz2aj0vQtp4IKWXpcrSAnjojltPnNg0UjsC9CBsPqtuIP2bbZwoZ/5/4I0+a2kAX 2dPMNuUtEuShOHvw3RpRHxhYpg/Iix3cl8oBV0VQp4Jrd1LXYqp7gHn8hRwOqdR8edB3 7aypXvVrK0W67jOs/BBoKCC/y7YfKMXskrRmXhTa9zn5NQrvLA3/5x+g/hwA/xoa8kBW WPBdtz5BLSq7r9fr3f5l6JiaBROHt+5tOY5/EoxoUswFSrIyb6x48vDdCTsj+yxVpA1F nqSg== 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 Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id w17-20020a637b11000000b00542ad6380a3si6683887pgc.881.2023.06.12.05.56.29; Mon, 12 Jun 2023 05:56:43 -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 S234803AbjFLMiF (ORCPT + 99 others); Mon, 12 Jun 2023 08:38:05 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:58892 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S234377AbjFLMhy (ORCPT ); Mon, 12 Jun 2023 08:37:54 -0400 Received: from out30-100.freemail.mail.aliyun.com (out30-100.freemail.mail.aliyun.com [115.124.30.100]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 13DDDE47 for ; Mon, 12 Jun 2023 05:37:52 -0700 (PDT) X-Alimail-AntiSpam: AC=PASS;BC=-1|-1;BR=01201311R191e4;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_---0VkzU.DN_1686573469; Received: from localhost(mailfrom:jefflexu@linux.alibaba.com fp:SMTPD_---0VkzU.DN_1686573469) by smtp.aliyun-inc.com; Mon, 12 Jun 2023 20:37:50 +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 3/5] erofs: make the size of read data stored in buffer_ofs Date: Mon, 12 Jun 2023 20:37:43 +0800 Message-Id: <20230612123745.36323-4-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 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 X-getmail-retrieved-from-mailbox: =?utf-8?q?INBOX?= X-GMAIL-THRID: =?utf-8?q?1768501652135082306?= X-GMAIL-MSGID: =?utf-8?q?1768501652135082306?= Since now xattr_iter structures have been unified, make the size of the read data stored in buffer_ofs. Don't bother reusing buffer_size for this use, which may be confusing. This is in preparation for the following further cleanup. Signed-off-by: Jingbo Xu Reviewed-by: Gao Xiang --- fs/erofs/xattr.c | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/fs/erofs/xattr.c b/fs/erofs/xattr.c index b2802121e3aa..8a114c7b6c66 100644 --- a/fs/erofs/xattr.c +++ b/fs/erofs/xattr.c @@ -315,7 +315,7 @@ static int xattr_checkbuffer(struct erofs_xattr_iter *it, { int err = it->buffer_size < value_sz ? -ERANGE : 0; - it->buffer_size = value_sz; + it->buffer_ofs = value_sz; return !it->buffer ? 1 : err; } @@ -348,7 +348,7 @@ static int inline_getxattr(struct inode *inode, struct erofs_xattr_iter *it) if (ret != -ENOATTR) break; } - return ret ? ret : it->buffer_size; + return ret ? ret : it->buffer_ofs; } static int shared_getxattr(struct inode *inode, struct erofs_xattr_iter *it) @@ -371,7 +371,7 @@ static int shared_getxattr(struct inode *inode, struct erofs_xattr_iter *it) if (ret != -ENOATTR) break; } - return ret ? ret : it->buffer_size; + return ret ? ret : it->buffer_ofs; } static bool erofs_xattr_user_list(struct dentry *dentry) From patchwork Mon Jun 12 12:37:44 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jingbo Xu X-Patchwork-Id: 106579 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:994d:0:b0:3d9:f83d:47d9 with SMTP id k13csp2574231vqr; Mon, 12 Jun 2023 06:08:04 -0700 (PDT) X-Google-Smtp-Source: ACHHUZ6vvU7wcH0VPziwytQOQhurJqvvg4fpF5TKhERnM5QNPc2uFfzA94ILdt8+qkNCoKnodp06 X-Received: by 2002:a05:6a20:3d23:b0:10f:195f:6556 with SMTP id y35-20020a056a203d2300b0010f195f6556mr11748165pzi.31.1686575284008; Mon, 12 Jun 2023 06:08:04 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1686575283; cv=none; d=google.com; s=arc-20160816; b=Whg8epDfyweF7lskrvM7Zwut51o91PufSBFU3uoA+9iwVZTdA9Aa1XMSZmlZpj71Fs YsQXrV/gWE+DL8V2sz29DfQsHvVmttB29yE1nmm6SSc/iki+jTkSZkT7Lx2l+bRXTtOJ HJ1/5X9Tlb9ko1UlttWedqhpFZAECsP95NRkwv37dSQEuv0aECAsgr9k76mxNKZZZ4Q8 Rb5C0VT2jJ2j/Zvum+MyC3DhdwV8Ysb651yjOOnfMe3uXhW4r9v7+w2dVR3m14SHnLFf 0GwPBOZ6Iew5R6LKIjl3d3iVmVe/lcEnT1qIb/GrFwApNMiQD1xM6cMILIgme/6fXxZ2 p6Jw== 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 Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id 2-20020a630b02000000b0053f2551834esi7005838pgl.735.2023.06.12.06.07.50; Mon, 12 Jun 2023 06:08:03 -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 S235018AbjFLMiI (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 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 X-getmail-retrieved-from-mailbox: =?utf-8?q?INBOX?= X-GMAIL-THRID: =?utf-8?q?1768502364822234163?= X-GMAIL-MSGID: =?utf-8?q?1768502364822234163?= 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) From patchwork Mon Jun 12 12:37:45 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jingbo Xu X-Patchwork-Id: 106567 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:994d:0:b0:3d9:f83d:47d9 with SMTP id k13csp2561470vqr; Mon, 12 Jun 2023 05:49:53 -0700 (PDT) X-Google-Smtp-Source: ACHHUZ7MhwjpQbGHnYnGj7bhYsdCYPe3lqv8xMGAeFEWcGxciYmA0XmPxvS66cgAv5AIS+Rljc0g X-Received: by 2002:a17:90a:31c:b0:25b:eeb3:adc4 with SMTP id 28-20020a17090a031c00b0025beeb3adc4mr1859933pje.15.1686574193257; Mon, 12 Jun 2023 05:49:53 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1686574193; cv=none; d=google.com; s=arc-20160816; b=Y9kcssWdrlWN2bCrcc+PCP9/Z4uTrgIJQ+QXiK53btatHWkHCpep6w/ubD9QtHw/1j 24lVeqnpBAFhtrnYq0w6dQmi35hGn+taLZZtLjwCT/EB/2e1q4wpTQYpEllY9N4yC4l+ kZrjejjUbYYNHRH8L3ycgDfFCNlt+1SoqWGJhkaC3E2E9/Eonto269PHROKXtGe1kf/d +MNd352jWggQRp9/go6UFtWlOH5AVRXkCU6rPXfUMsTe2qjWULAPtCfg1r8v5SFKaAKk 2NfeHy+o3wTDz+KT9tEnIAqE/X7zUcy3COONQ3GDoXKtrY4cP70txqzh6FjYGGkXLJ9n IOSg== 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=NLRjYGLASQebok6NkrXjySrKyk+eiuzQmHD6UPf8lHw=; b=xlvyxmLe7z6LXMn/pu5LRRO2DD8zXLZWFPxWLuvhN6ld36jBXKaY2/yy6Zn1roxJw9 m6hUtCO9Et0Y/L6yI+wFk5tv8fhXGwfteCoBEdoxuRS0msWmP6RddpfzoEBmT2DtMe+p kxrzILW9wv2z1YldsZPcxKwy8bjjZ+0OTzNY9E4cnHzLIeMZjeSNc077b4QS/mPsKabL ofz6ZH0pA1otlXXY5DN/U3OHHmyWtIuU3dy0JnnTb00xfBPN5hFUob3t92zVikjYhWfc rq7GWABXzD2M31Yva+0kNoy1jIWlBgfxoUUPoK7gU1nRBV0O0hpSkKgUt23fwXmuwdBM RFWg== 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 Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id 26-20020a17090a031a00b0025bfdc637d0si338360pje.147.2023.06.12.05.49.40; Mon, 12 Jun 2023 05:49:53 -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 S235181AbjFLMiL (ORCPT + 99 others); Mon, 12 Jun 2023 08:38:11 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:58912 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S234545AbjFLMh4 (ORCPT ); Mon, 12 Jun 2023 08:37:56 -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 9D179E4C for ; Mon, 12 Jun 2023 05:37:54 -0700 (PDT) X-Alimail-AntiSpam: AC=PASS;BC=-1|-1;BR=01201311R631e4;CH=green;DM=||false|;DS=||;FP=0|-1|-1|-1|0|-1|-1|-1;HT=ay29a033018045192;MF=jefflexu@linux.alibaba.com;NM=1;PH=DS;RN=5;SR=0;TI=SMTPD_---0Vkz1wLF_1686573471; Received: from localhost(mailfrom:jefflexu@linux.alibaba.com fp:SMTPD_---0Vkz1wLF_1686573471) by smtp.aliyun-inc.com; Mon, 12 Jun 2023 20:37:52 +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 5/5] erofs: use separate xattr parsers for listxattr/getxattr Date: Mon, 12 Jun 2023 20:37:45 +0800 Message-Id: <20230612123745.36323-6-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 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 X-getmail-retrieved-from-mailbox: =?utf-8?q?INBOX?= X-GMAIL-THRID: =?utf-8?q?1768501221491246359?= X-GMAIL-MSGID: =?utf-8?q?1768501221491246359?= 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 | 370 ++++++++++++++++++----------------------------- 1 file changed, 137 insertions(+), 233 deletions(-) diff --git a/fs/erofs/xattr.c b/fs/erofs/xattr.c index a97a810f24a0..4d395a95a55e 100644 --- a/fs/erofs/xattr.c +++ b/fs/erofs/xattr.c @@ -123,195 +123,6 @@ static int erofs_init_inode_xattrs(struct inode *inode) return ret; } -/* - * 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 *buf, - 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 - * `pos' 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; - struct super_block *sb = it->sb; - unsigned int value_sz, processed, slice; - int err; - - /* 0. fixup blkaddr, pos */ - it->kaddr = erofs_bread(&it->buf, erofs_blknr(sb, it->pos), EROFS_KMAP); - if (IS_ERR(it->kaddr)) - return PTR_ERR(it->kaddr); - - /* - * 1. read xattr entry to the memory, - * since we do EROFS_XATTR_ALIGN - * therefore entry should be in the page - */ - entry = *(struct erofs_xattr_entry *) - (it->kaddr + erofs_blkoff(sb, it->pos)); - if (tlimit) { - unsigned int entry_sz = erofs_xattr_entry_size(&entry); - - /* xattr on-disk corruption: xattr entry beyond xattr_isize */ - if (*tlimit < entry_sz) { - DBG_BUGON(1); - return -EFSCORRUPTED; - } - *tlimit -= entry_sz; - } - - it->pos += sizeof(struct erofs_xattr_entry); - value_sz = le16_to_cpu(entry.e_value_size); - - /* handle entry */ - err = op->entry(it, &entry); - if (err) { - it->pos += entry.e_name_len + value_sz; - goto out; - } - - /* 2. handle xattr name (pos will finally be at the end of name) */ - processed = 0; - - while (processed < entry.e_name_len) { - it->kaddr = erofs_bread(&it->buf, erofs_blknr(sb, it->pos), - EROFS_KMAP); - if (IS_ERR(it->kaddr)) { - err = PTR_ERR(it->kaddr); - goto out; - } - - slice = min_t(unsigned int, - sb->s_blocksize - erofs_blkoff(sb, it->pos), - entry.e_name_len - processed); - - /* handle name */ - err = op->name(it, processed, - it->kaddr + erofs_blkoff(sb, it->pos), slice); - if (err) { - it->pos += entry.e_name_len - processed + value_sz; - goto out; - } - - it->pos += slice; - processed += slice; - } - - /* 3. handle xattr value */ - processed = 0; - - if (op->alloc_buffer) { - err = op->alloc_buffer(it, value_sz); - if (err) { - it->pos += value_sz; - goto out; - } - } - - while (processed < value_sz) { - it->kaddr = erofs_bread(&it->buf, erofs_blknr(sb, it->pos), - EROFS_KMAP); - if (IS_ERR(it->kaddr)) { - err = PTR_ERR(it->kaddr); - goto out; - } - - slice = min_t(unsigned int, - sb->s_blocksize - erofs_blkoff(sb, it->pos), - value_sz - processed); - op->value(it, processed, it->kaddr + erofs_blkoff(sb, it->pos), - slice); - it->pos += slice; - processed += slice; - } - -out: - /* xattrs should be 4-byte aligned (on-disk constraint) */ - it->pos = EROFS_XATTR_ALIGN(it->pos); - 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 = EROFS_SB(it->sb); - struct erofs_xattr_prefix_item *pf = sbi->xattr_prefixes + - (entry->e_name_index & EROFS_XATTR_LONG_PREFIX_MASK); - - if (pf >= sbi->xattr_prefixes + sbi->xattr_prefix_count) - return -ENOATTR; - - if (it->index != pf->prefix->base_index || - it->name.len != 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 = 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 != entry->e_name_index || - it->name.len != entry->e_name_len) - return -ENOATTR; - it->infix_len = 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 = it->buffer_size < value_sz ? -ERANGE : 0; - - it->buffer_ofs = 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 = { - .entry = xattr_entrymatch, - .name = xattr_namematch, - .alloc_buffer = xattr_checkbuffer, - .value = xattr_copyvalue -}; - static bool erofs_xattr_user_list(struct dentry *dentry) { return test_opt(&EROFS_SB(dentry->d_sb)->opt, XATTR_USER); @@ -364,20 +175,49 @@ const struct xattr_handler *erofs_xattr_handlers[] = { NULL, }; -static int xattr_entrylist(struct erofs_xattr_iter *it, - struct erofs_xattr_entry *entry) +static int erofs_xattr_copy_to_buffer(struct erofs_xattr_iter *it, + unsigned int len) +{ + unsigned int slice, processed; + struct super_block *sb = it->sb; + void *src; + + for (processed = 0; processed < len; processed += slice) { + it->kaddr = erofs_bread(&it->buf, erofs_blknr(sb, it->pos), + EROFS_KMAP); + if (IS_ERR(it->kaddr)) + return PTR_ERR(it->kaddr); + + src = it->kaddr + erofs_blkoff(sb, it->pos); + slice = min_t(unsigned int, sb->s_blocksize - + erofs_blkoff(sb, it->pos), len - processed); + memcpy(it->buffer + it->buffer_ofs, src, slice); + it->buffer_ofs += slice; + it->pos += slice; + } + return 0; +} + +static int erofs_listxattr_foreach(struct erofs_xattr_iter *it) { - unsigned int base_index = entry->e_name_index; - unsigned int prefix_len, infix_len = 0; + struct erofs_xattr_entry entry; + unsigned int base_index, prefix_len, infix_len = 0; const char *prefix, *infix = NULL; + int err; - if (entry->e_name_index & EROFS_XATTR_LONG_PREFIX) { + /* 1. handle xattr entry */ + entry = *(struct erofs_xattr_entry *) + (it->kaddr + erofs_blkoff(it->sb, it->pos)); + it->pos += sizeof(struct erofs_xattr_entry); + + base_index = entry.e_name_index; + if (entry.e_name_index & EROFS_XATTR_LONG_PREFIX) { struct erofs_sb_info *sbi = EROFS_SB(it->sb); struct erofs_xattr_prefix_item *pf = sbi->xattr_prefixes + - (entry->e_name_index & EROFS_XATTR_LONG_PREFIX_MASK); + (entry.e_name_index & EROFS_XATTR_LONG_PREFIX_MASK); if (pf >= sbi->xattr_prefixes + sbi->xattr_prefix_count) - return 1; + return 0; infix = pf->prefix->infix; infix_len = pf->infix_len; base_index = pf->prefix->base_index; @@ -385,53 +225,103 @@ static int xattr_entrylist(struct erofs_xattr_iter *it, prefix = erofs_xattr_prefix(base_index, it->dentry); if (!prefix) - return 1; + return 0; prefix_len = strlen(prefix); if (!it->buffer) { - it->buffer_ofs += prefix_len + infix_len + - entry->e_name_len + 1; - return 1; + it->buffer_ofs += prefix_len + infix_len + entry.e_name_len + 1; + return 0; } 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; memcpy(it->buffer + it->buffer_ofs, prefix, prefix_len); memcpy(it->buffer + it->buffer_ofs + prefix_len, infix, infix_len); it->buffer_ofs += prefix_len + infix_len; - return 0; -} -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 += len; + /* 2. handle xattr name */ + err = erofs_xattr_copy_to_buffer(it, entry.e_name_len); + if (err) + return err; + + it->buffer[it->buffer_ofs++] = '\0'; return 0; } -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++] = '\0'; - return 1; -} + struct super_block *sb = it->sb; + struct erofs_xattr_entry entry; + unsigned int slice, processed, value_sz; + void *src; -static const struct xattr_iter_handlers list_xattr_handlers = { - .entry = xattr_entrylist, - .name = xattr_namelist, - .alloc_buffer = xattr_skipvalue, - .value = NULL -}; + /* 1. handle xattr entry */ + entry = *(struct erofs_xattr_entry *) + (it->kaddr + erofs_blkoff(sb, it->pos)); + it->pos += sizeof(struct erofs_xattr_entry); + value_sz = le16_to_cpu(entry.e_value_size); + + /* should also match the infix for long name prefixes */ + if (entry.e_name_index & EROFS_XATTR_LONG_PREFIX) { + struct erofs_sb_info *sbi = EROFS_SB(sb); + struct erofs_xattr_prefix_item *pf = sbi->xattr_prefixes + + (entry.e_name_index & EROFS_XATTR_LONG_PREFIX_MASK); + + if (pf >= sbi->xattr_prefixes + sbi->xattr_prefix_count) + return -ENOATTR; + + if (it->index != pf->prefix->base_index || + it->name.len != 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 = pf->infix_len; + } else { + if (it->index != entry.e_name_index || + it->name.len != entry.e_name_len) + return -ENOATTR; + + it->infix_len = 0; + } + + /* 2. handle xattr name */ + for (processed = 0; processed < entry.e_name_len; processed += slice) { + it->kaddr = erofs_bread(&it->buf, erofs_blknr(sb, it->pos), + EROFS_KMAP); + if (IS_ERR(it->kaddr)) + return PTR_ERR(it->kaddr); + + src = it->kaddr + erofs_blkoff(sb, it->pos); + slice = min_t(unsigned int, + sb->s_blocksize - erofs_blkoff(sb, it->pos), + entry.e_name_len - processed); + if (memcmp(it->name.name + it->infix_len + processed, src, slice)) + return -ENOATTR; + it->pos += slice; + } + + /* 3. handle xattr value */ + if (!it->buffer) { + it->buffer_ofs = value_sz; + return 0; + } + + if (it->buffer_size < value_sz) + return -ERANGE; + + return erofs_xattr_copy_to_buffer(it, value_sz); +} 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; + unsigned int xattr_header_sz, remaining, entry_sz; + erofs_off_t next_pos; int ret; xattr_header_sz = sizeof(struct erofs_xattr_ibody_header) + @@ -441,19 +331,33 @@ static int erofs_iter_inline_xattr(struct erofs_xattr_iter *it, 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; + it->pos = erofs_iloc(inode) + vi->inode_isize + xattr_header_sz; while (remaining) { - ret = xattr_foreach(it, op, &remaining); + it->kaddr = erofs_bread(&it->buf, erofs_blknr(it->sb, it->pos), + EROFS_KMAP); + if (IS_ERR(it->kaddr)) + return PTR_ERR(it->kaddr); + + entry_sz = erofs_xattr_entry_size(it->kaddr + + erofs_blkoff(it->sb, it->pos)); + /* xattr on-disk corruption: xattr entry beyond xattr_isize */ + if (remaining < entry_sz) { + DBG_BUGON(1); + return -EFSCORRUPTED; + } + remaining -= entry_sz; + next_pos = it->pos + entry_sz; + + if (getxattr) + ret = erofs_getxattr_foreach(it); + else + ret = erofs_listxattr_foreach(it); if ((getxattr && ret != -ENOATTR) || (!getxattr && ret)) break; + + it->pos = next_pos; } return ret; } @@ -465,11 +369,8 @@ static int erofs_iter_shared_xattr(struct erofs_xattr_iter *it, struct super_block *const sb = it->sb; struct erofs_sb_info *sbi = EROFS_SB(sb); unsigned int i; - 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) + vi->xattr_shared_xattrs[i] * sizeof(__le32); @@ -478,7 +379,10 @@ static int erofs_iter_shared_xattr(struct erofs_xattr_iter *it, if (IS_ERR(it->kaddr)) return PTR_ERR(it->kaddr); - ret = xattr_foreach(it, op, NULL); + if (getxattr) + ret = erofs_getxattr_foreach(it); + else + ret = erofs_listxattr_foreach(it); if ((getxattr && ret != -ENOATTR) || (!getxattr && ret)) break; }