From patchwork Thu Mar 23 00:09:49 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jingbo Xu X-Patchwork-Id: 73719 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a5d:604a:0:0:0:0:0 with SMTP id j10csp2644725wrt; Wed, 22 Mar 2023 17:26:48 -0700 (PDT) X-Google-Smtp-Source: AK7set8NIe0QBwAA2z7XzG7UUJdQLNHq2ak4v04cHWHjV+EwhwgFGl78Sb45lV3qcGtqqMkjyiJ8 X-Received: by 2002:a17:90a:d908:b0:23f:81ac:80b8 with SMTP id c8-20020a17090ad90800b0023f81ac80b8mr5379341pjv.40.1679531208580; Wed, 22 Mar 2023 17:26:48 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1679531208; cv=none; d=google.com; s=arc-20160816; b=n83mIEOfyqjKtYBc94x+2/7g6yHRb1ENKhtB+l56OguoRuzIflK6DopxQHTDlbyGX6 K0rUiJ7MueMP1PYx67C+Bv0eEzckaTXR3gT4a5D13Izbey/STtiSgfEOgvjik+N1BLqV jsTAGPpqigvcQd6eiaaA0mbGN+x+/Yf9LuIZVNt042ScupgYj1bFgv2gHSWHLrFQI5uN FlPd9fz0pY2ZUo5OwaK75YSXvtsDvPSrpzdL19byzAM1VuDTLsHqAw8bAk1zSpFgxFyr d7FSAw/fIXYQduRL2/V8zEMbsDAmBKHTUxWtj8LmGkYSVzrVNN5xgTIbXf5j21B+OrYI KWVg== 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=QI8AY+9+9K/FnTorIbR8RX2kdy/9ndnqRZai0pImAB8=; b=Z0Fdl807sO7Ttw8vDmO7tBj8EzO/4fUysUDxACLjQMgbcqH07/o6YJCB+TOAL9uxTb FZW8uyoXr7s2XBQQbJh6vFSeV3r/mdV0bGaczfjD2rlj1s6IEPnMY4wEHwtLHe44C43J calKgjDG49dhux8c5V8Zv4Cy3IUzDFzC16jJFdrb3sXJfD9ja9W1eRakdy4h3LNtTNXE kPlAEQLzo8jeUAcfd24MHKxJ9hrIvU9Yqe8efgcNQfl6aq4JDL0rUoq2SUs1fhoe/jAA hlpA4j4pkUv6d+V70cXkAwd7kAE1am/YJXU3/O9TsvHZmPekgHuGboVTsFT3ByKgsDzP p1Gw== 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 y17-20020a170903011100b001a0767b58d4si16354339plc.81.2023.03.22.17.26.36; Wed, 22 Mar 2023 17:26:48 -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 S230500AbjCWALD (ORCPT + 99 others); Wed, 22 Mar 2023 20:11:03 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:45610 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230435AbjCWAKv (ORCPT ); Wed, 22 Mar 2023 20:10:51 -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 E65B233447 for ; Wed, 22 Mar 2023 17:10:11 -0700 (PDT) X-Alimail-AntiSpam: AC=PASS;BC=-1|-1;BR=01201311R201e4;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_---0VeS0V4I_1679530197; Received: from localhost(mailfrom:jefflexu@linux.alibaba.com fp:SMTPD_---0VeS0V4I_1679530197) by smtp.aliyun-inc.com; Thu, 23 Mar 2023 08:09:58 +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 8/8] erofs: use separate xattr parsers for listxattr/getxattr Date: Thu, 23 Mar 2023 08:09:49 +0800 Message-Id: <20230323000949.57608-9-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 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 X-getmail-retrieved-from-mailbox: =?utf-8?q?INBOX?= X-GMAIL-THRID: =?utf-8?q?1761116116547591165?= X-GMAIL-MSGID: =?utf-8?q?1761116116547591165?= 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 | 347 ++++++++++++++++++++++------------------------- 1 file changed, 159 insertions(+), 188 deletions(-) diff --git a/fs/erofs/xattr.c b/fs/erofs/xattr.c index 196b2eb59e29..eac5b7b69691 100644 --- a/fs/erofs/xattr.c +++ b/fs/erofs/xattr.c @@ -24,6 +24,7 @@ struct erofs_xattr_iter { struct qstr name; struct dentry *dentry; struct inode *inode; + unsigned int remaining; /* size of inline xattrs to be iterated */ bool getxattr; }; @@ -144,153 +145,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 - * `ofs' 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; - unsigned int value_sz, processed, slice; - int err; - - /* 0. fixup blkaddr, ofs, ipage */ - err = erofs_xattr_iter_fixup(it); - if (err) - return err; - - /* - * 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); - 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->ofs += 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; - goto out; - } - - /* 2. handle xattr name (ofs will finally be at the end of name) */ - processed = 0; - - while (processed < entry.e_name_len) { - err = erofs_xattr_iter_fixup_aligned(it); - if (err) - goto out; - - slice = min_t(unsigned int, it->sb->s_blocksize - it->ofs, - entry.e_name_len - processed); - - /* handle name */ - err = op->name(it, processed, it->kaddr + it->ofs, slice); - if (err) { - it->ofs += entry.e_name_len - processed + value_sz; - goto out; - } - - it->ofs += slice; - processed += slice; - } - - /* 3. handle xattr value */ - processed = 0; - - if (op->alloc_buffer) { - err = op->alloc_buffer(it, value_sz); - if (err) { - it->ofs += value_sz; - goto out; - } - } - - while (processed < value_sz) { - err = erofs_xattr_iter_fixup_aligned(it); - if (err) - goto out; - - slice = min_t(unsigned int, it->sb->s_blocksize - it->ofs, - value_sz - processed); - op->value(it, processed, it->kaddr + it->ofs, slice); - it->ofs += slice; - processed += slice; - } - -out: - /* xattrs should be 4-byte aligned (on-disk constraint) */ - it->ofs = EROFS_XATTR_ALIGN(it->ofs); - return err < 0 ? err : 0; -} - -static int xattr_entrymatch(struct erofs_xattr_iter *it, - struct erofs_xattr_entry *entry) -{ - return (it->index != entry->e_name_index || - it->name.len != entry->e_name_len) ? -ENOATTR : 0; -} - -static int xattr_namematch(struct erofs_xattr_iter *it, - unsigned int processed, char *buf, unsigned int len) -{ - return memcmp(buf, it->name.name + processed, len) ? -ENOATTR : 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); @@ -367,62 +221,178 @@ static inline const struct xattr_handler *erofs_xattr_handler(unsigned int idx) xattr_handler_map[idx] : NULL; } -static int xattr_entrylist(struct erofs_xattr_iter *it, - struct erofs_xattr_entry *entry) +/* + * 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 + */ +typedef int (*erofs_xattr_body_handler)(struct erofs_xattr_iter *it, + unsigned int processed, + char *buf, unsigned int len); + +static int erofs_xattr_namematch(struct erofs_xattr_iter *it, + unsigned int processed, char *buf, unsigned int len) +{ + return memcmp(buf, it->name.name + processed, len) ? -ENOATTR : 0; +} + +static int erofs_xattr_copy(struct erofs_xattr_iter *it, + unsigned int unused, char *buf, unsigned int len) +{ + memcpy(it->buffer + it->buffer_ofs, buf, len); + it->buffer_ofs += len; + return 0; +} + +static int erofs_xattr_body(struct erofs_xattr_iter *it, unsigned int len, + erofs_xattr_body_handler handler) +{ + unsigned int slice, processed = 0; + + while (processed < len) { + int err = erofs_xattr_iter_fixup_aligned(it); + if (err) + return err; + + slice = min_t(unsigned int, it->sb->s_blocksize - it->ofs, + len - processed); + err = handler(it, processed, it->kaddr + it->ofs, slice); + if (err) { + it->ofs += len - processed; + return err; + } + + it->ofs += slice; + processed += slice; + } + return 0; +} + +static int erofs_xattr_check_entry(struct erofs_xattr_iter *it) { - unsigned int prefix_len; + int err; + + err = erofs_xattr_iter_fixup(it); + if (err) + return err; + + if (it->remaining) { + struct erofs_xattr_entry *entry = it->kaddr + it->ofs; + unsigned int entry_sz = erofs_xattr_entry_size(entry); + /* xattr on-disk corruption: xattr entry beyond xattr_isize */ + if (it->remaining < entry_sz) { + DBG_BUGON(1); + return -EFSCORRUPTED; + } + it->remaining -= entry_sz; + } + return 0; +} + +/* + * Regardless of success or failure, erofs_[get|list]xattr_foreach() will end up + * with `ofs' pointing to the next xattr item rather than an arbitrary position. + */ +static int erofs_listxattr_foreach(struct erofs_xattr_iter *it) +{ + struct erofs_xattr_entry entry; + const struct xattr_handler *h; const char *prefix; + unsigned int value_sz, prefix_len, name_sz; + int err; - const struct xattr_handler *h = - erofs_xattr_handler(entry->e_name_index); + err = erofs_xattr_check_entry(it); + if (err) + return err; - if (!h || (h->list && !h->list(it->dentry))) - return 1; + /* 1. handle xattr entry */ + entry = *(struct erofs_xattr_entry *)(it->kaddr + it->ofs); + it->ofs += sizeof(struct erofs_xattr_entry); + value_sz = le16_to_cpu(entry.e_value_size); + + h = erofs_xattr_handler(entry.e_name_index); + if (!h || (h->list && !h->list(it->dentry))) { + it->ofs += entry.e_name_len + value_sz; + goto out; + } prefix = xattr_prefix(h); prefix_len = strlen(prefix); + name_sz = prefix_len + entry.e_name_len + 1; if (!it->buffer) { - it->buffer_ofs += prefix_len + entry->e_name_len + 1; - return 1; + it->buffer_ofs += name_sz; + it->ofs += entry.e_name_len + value_sz; + goto out; } - - if (it->buffer_ofs + prefix_len - + entry->e_name_len + 1 > it->buffer_size) + if (it->buffer_ofs + name_sz > it->buffer_size) return -ERANGE; memcpy(it->buffer + it->buffer_ofs, prefix, prefix_len); it->buffer_ofs += prefix_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 can't be 1 or ENOATTR) */ + err = erofs_xattr_body(it, entry.e_name_len, erofs_xattr_copy); + if (err) + return err; + + it->buffer[it->buffer_ofs++] = '\0'; + it->ofs += value_sz; +out: + it->ofs = EROFS_XATTR_ALIGN(it->ofs); 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 erofs_xattr_entry entry; + unsigned int value_sz; + int err; -static const struct xattr_iter_handlers list_xattr_handlers = { - .entry = xattr_entrylist, - .name = xattr_namelist, - .alloc_buffer = xattr_skipvalue, - .value = NULL -}; + err = erofs_xattr_check_entry(it); + if (err) + return err; + + /* 1. handle xattr entry */ + entry = *(struct erofs_xattr_entry *)(it->kaddr + it->ofs); + it->ofs += sizeof(struct erofs_xattr_entry); + value_sz = le16_to_cpu(entry.e_value_size); + + if (it->index != entry.e_name_index || it->name.len != entry.e_name_len) { + it->ofs += entry.e_name_len + value_sz; + err = -ENOATTR; + goto out; + } + + /* 2. handle xattr name */ + err = erofs_xattr_body(it, entry.e_name_len, erofs_xattr_namematch); + if (err) { + it->ofs += value_sz; + goto out; + } + + /* 3. handle xattr value */ + if (!it->buffer) { + it->buffer_ofs = value_sz; + it->ofs += value_sz; + goto out; /* err == 0 */ + } + if (it->buffer_size < value_sz) + return -ERANGE; + + /* no need updating ofs on error (err can't be 1 or ENOATTR) */ + err = erofs_xattr_body(it, value_sz, erofs_xattr_copy); +out: + it->ofs = EROFS_XATTR_ALIGN(it->ofs); + return err < 0 ? err : 0; +} 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; + unsigned int xattr_header_sz; erofs_off_t pos; int ret; @@ -440,11 +410,12 @@ static int erofs_iter_inline_xattr(struct erofs_xattr_iter *it) 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; - - while (remaining) { - ret = xattr_foreach(it, op, &remaining); + it->remaining = vi->xattr_isize - xattr_header_sz; + while (it->remaining) { + if (it->getxattr) + ret = erofs_getxattr_foreach(it); + else + ret = erofs_listxattr_foreach(it); if ((it->getxattr && ret != -ENOATTR) || (!it->getxattr && ret)) break; } @@ -455,12 +426,9 @@ static int erofs_iter_shared_xattr(struct erofs_xattr_iter *it) { 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 = -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_SB(sb)->xattr_blkaddr + @@ -470,7 +438,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 (it->getxattr) + ret = erofs_getxattr_foreach(it); + else + ret = erofs_listxattr_foreach(it); if ((it->getxattr && ret != -ENOATTR) || (!it->getxattr && ret)) break; }