From patchwork Fri Oct 14 03:07:41 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jia Zhu X-Patchwork-Id: 2474 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a5d:4ac7:0:0:0:0:0 with SMTP id y7csp590464wrs; Thu, 13 Oct 2022 20:11:26 -0700 (PDT) X-Google-Smtp-Source: AMsMyM7uhWvrXO/tY/V++MYogu8tUtFzVIehkuftBGDybCtOQb+Qu61RRAkOc1+gtrTz0PQ8jeDO X-Received: by 2002:a05:6a00:1410:b0:528:5a5a:d846 with SMTP id l16-20020a056a00141000b005285a5ad846mr2894348pfu.9.1665717086443; Thu, 13 Oct 2022 20:11:26 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1665717086; cv=none; d=google.com; s=arc-20160816; b=aRws8wrfmbtBBn0DxOWfp0QiFO7QjB9SD3zI4uibm882araURkTSmHdUuni+iMeP3t 1svxJScjltLoJzEornrXRbRCK7dGcD/jtMHMJTpLODnYlUynW8MGUt0UN1+8QEQy4Aj7 imFIS2W4K667hr+8JnKb8Cdu85vH4nmvoNjsDAuWUBFn0m7HU8eWLFXy9oFrdr585NhO Sqe9bO5qDZfjCHLd0y2lkDyDHR2bEyzjqN1CMXA/xyenl/3HL5Zr++loFNvBiJk8GJAh BdX7f3tyTMcVeO0UBxr6MGCxWAL3CneUD1BXtErJSTmCo/0rM1otWBZyP9z5fCLCtcG9 Jl8g== 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 :dkim-signature; bh=jexCPXMD1tiAWyMpE7kFB+4P8+aQ3ICPFBRets/3erM=; b=e0SOBwIrGdgv9mM7SjEOgcx4OeSE0EWSImbeaBfKe/yN9ob2GqgYU4V/kt+r4usujL 6URZSbRVx/Kk89Ow/GpTB+4+tXJdUXT+bGjAjTHfqX8cFlW0CUvvx99G332tIaeOGHyz pDLuKKYvvqKngc2gssuzaHW6wPrezWQgL+2F0n1LT7HHAtVnZENF8TJhpUd+dAJABqgw X8h4fw/wQ9PqdUu2h9mI1ny35GrCjBeh0DpLUftVHNa+lJonopCbGwjlu4rc0N9LLgMW 3yW5hPveUE3Nuwm3wYTL19207uA0D/OkIJF4+7KGW8QdqcjjdyJvQg4MV/9DjLxil9on s2Yw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@bytedance-com.20210112.gappssmtp.com header.s=20210112 header.b=fQKg0y8Y; 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=bytedance.com Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id g9-20020a1709026b4900b0017c59fa6b65si1365738plt.597.2022.10.13.20.11.11; Thu, 13 Oct 2022 20:11:26 -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; dkim=pass header.i=@bytedance-com.20210112.gappssmtp.com header.s=20210112 header.b=fQKg0y8Y; 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=bytedance.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229723AbiJNDIj (ORCPT + 99 others); Thu, 13 Oct 2022 23:08:39 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:43642 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229583AbiJNDIc (ORCPT ); Thu, 13 Oct 2022 23:08:32 -0400 Received: from mail-pj1-x102f.google.com (mail-pj1-x102f.google.com [IPv6:2607:f8b0:4864:20::102f]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 7520819B647 for ; Thu, 13 Oct 2022 20:08:22 -0700 (PDT) Received: by mail-pj1-x102f.google.com with SMTP id q10-20020a17090a304a00b0020b1d5f6975so3641040pjl.0 for ; Thu, 13 Oct 2022 20:08:22 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=bytedance-com.20210112.gappssmtp.com; s=20210112; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=jexCPXMD1tiAWyMpE7kFB+4P8+aQ3ICPFBRets/3erM=; b=fQKg0y8YL2d+gPSn3U4m+mr6kpYvDbcL8R79MD7a8yt/UjbteM2AL2JO0itdPoCRGq yOq+0MuO6QP9xkt+pa4kyPCSjPZSVPNGBsC7DSlL2vnJhrvaF0PZACNsmkucaOkdPrfs HbOBqneW95mVZ1xhrNFcLKza2gYsSj7YR8Ak629NYES0ZlBDkIQaq1UkF8cMqgh7tTqW 821pnTxHRtAyTd//3M/lQzRIv+p3T3SOs6pSd+9qXRKaks1qULNxxvJp+saZ16zZXnBA 1ifgn/iQenujEW5MygPT0hXqfUFW+mXdSKA86vtibFC20EqerxRixNYPGrA010QGI2Ei mbKA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=jexCPXMD1tiAWyMpE7kFB+4P8+aQ3ICPFBRets/3erM=; b=FLK+FpgQa91MQ7IWvDoibGvFnU2ooTKnpX603xRmZj5DuSB/UI2RA1Vi2UAWly5j5d 8qXs2Um+ozWw9IDKTFLKIhhgrK9KYbaydcjm7T7Je2qfxL9/FeaG4EQ3AapKHHEdMK9n niFnCP416JyURlW/8+Jn08OeA2lxGvU27YvAtZl50QWsq5Vvr1YinLzsH89nISEXm6W7 9W3oanYcynL/cgXEWKX1CIEFX8lVTq+hlqqYw8xOuP4eGas0iUZrrP5Gjk5H27IWEvKP XW9whi1zbSCUxWO+XxDbyIIGMc2yj6IletSoV04IObVN3IAwjoyyPALXyPCkaim7SCED tBPQ== X-Gm-Message-State: ACrzQf0MjZNDq9DVEapL6rbWcU1l8JsLibrIzmiCSdex3feO8uWiCp09 t83y8nF6zgz+sRyS5TlB2CQ8JA== X-Received: by 2002:a17:90b:17c4:b0:20d:4d79:55b1 with SMTP id me4-20020a17090b17c400b0020d4d7955b1mr3240422pjb.125.1665716884249; Thu, 13 Oct 2022 20:08:04 -0700 (PDT) Received: from C02G705SMD6V.bytedance.net ([63.216.146.183]) by smtp.gmail.com with ESMTPSA id h4-20020a17090a710400b0020ae09e9724sm425524pjk.53.2022.10.13.20.07.59 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 13 Oct 2022 20:08:03 -0700 (PDT) From: Jia Zhu To: dhowells@redhat.com, xiang@kernel.org, jefflexu@linux.alibaba.com Cc: linux-cachefs@redhat.com, linux-erofs@lists.ozlabs.org, linux-fsdevel@vger.kernel.org, linux-kernel@vger.kernel.org, yinxin.x@bytedance.com, Jia Zhu Subject: [PATCH V2 1/5] cachefiles: introduce object ondemand state Date: Fri, 14 Oct 2022 11:07:41 +0800 Message-Id: <20221014030745.25748-2-zhujia.zj@bytedance.com> X-Mailer: git-send-email 2.37.0 (Apple Git-136) In-Reply-To: <20221014030745.25748-1-zhujia.zj@bytedance.com> References: <20221014030745.25748-1-zhujia.zj@bytedance.com> MIME-Version: 1.0 X-Spam-Status: No, score=-1.9 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,RCVD_IN_DNSWL_NONE,SPF_HELO_NONE,SPF_PASS 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?1746630959790370912?= X-GMAIL-MSGID: =?utf-8?q?1746630959790370912?= Previously, @ondemand_id field was used not only to identify ondemand state of the object, but also to represent the index of the xarray. This commit introduces @state field to decouple the role of @ondemand_id and adds helpers to access it. Signed-off-by: Jia Zhu Reviewed-by: Xin Yin Reviewed-by: Jingbo Xu --- fs/cachefiles/internal.h | 22 ++++++++++++++++++++++ fs/cachefiles/ondemand.c | 21 +++++++++------------ 2 files changed, 31 insertions(+), 12 deletions(-) diff --git a/fs/cachefiles/internal.h b/fs/cachefiles/internal.h index 2ad58c465208..39895bbd149a 100644 --- a/fs/cachefiles/internal.h +++ b/fs/cachefiles/internal.h @@ -17,6 +17,7 @@ #include #include #include +#include #define CACHEFILES_DIO_BLOCK_SIZE 4096 @@ -44,6 +45,11 @@ struct cachefiles_volume { struct dentry *fanout[256]; /* Fanout subdirs */ }; +enum cachefiles_object_state { + CACHEFILES_ONDEMAND_OBJSTATE_close, /* Anonymous fd closed by daemon or initial state */ + CACHEFILES_ONDEMAND_OBJSTATE_open, /* Anonymous fd associated with object is available */ +}; + /* * Backing file state. */ @@ -62,6 +68,7 @@ struct cachefiles_object { #define CACHEFILES_OBJECT_USING_TMPFILE 0 /* Have an unlinked tmpfile */ #ifdef CONFIG_CACHEFILES_ONDEMAND int ondemand_id; + enum cachefiles_object_state state; #endif }; @@ -296,6 +303,21 @@ extern void cachefiles_ondemand_clean_object(struct cachefiles_object *object); extern int cachefiles_ondemand_read(struct cachefiles_object *object, loff_t pos, size_t len); +#define CACHEFILES_OBJECT_STATE_FUNCS(_state) \ +static inline bool \ +cachefiles_ondemand_object_is_##_state(const struct cachefiles_object *object) \ +{ \ + return object->state == CACHEFILES_ONDEMAND_OBJSTATE_##_state; \ +} \ + \ +static inline void \ +cachefiles_ondemand_set_object_##_state(struct cachefiles_object *object) \ +{ \ + object->state = CACHEFILES_ONDEMAND_OBJSTATE_##_state; \ +} + +CACHEFILES_OBJECT_STATE_FUNCS(open); +CACHEFILES_OBJECT_STATE_FUNCS(close); #else static inline ssize_t cachefiles_ondemand_daemon_read(struct cachefiles_cache *cache, char __user *_buffer, size_t buflen) diff --git a/fs/cachefiles/ondemand.c b/fs/cachefiles/ondemand.c index 0254ed39f68c..90456b8a4b3e 100644 --- a/fs/cachefiles/ondemand.c +++ b/fs/cachefiles/ondemand.c @@ -15,6 +15,7 @@ static int cachefiles_ondemand_fd_release(struct inode *inode, xa_lock(&cache->reqs); object->ondemand_id = CACHEFILES_ONDEMAND_ID_CLOSED; + cachefiles_ondemand_set_object_close(object); /* * Flush all pending READ requests since their completion depends on @@ -176,6 +177,8 @@ int cachefiles_ondemand_copen(struct cachefiles_cache *cache, char *args) set_bit(FSCACHE_COOKIE_NO_DATA_TO_READ, &cookie->flags); trace_cachefiles_ondemand_copen(req->object, id, size); + cachefiles_ondemand_set_object_open(req->object); + out: complete(&req->done); return ret; @@ -363,7 +366,8 @@ static int cachefiles_ondemand_send_req(struct cachefiles_object *object, /* coupled with the barrier in cachefiles_flush_reqs() */ smp_mb(); - if (opcode != CACHEFILES_OP_OPEN && object->ondemand_id <= 0) { + if (opcode != CACHEFILES_OP_OPEN && + !cachefiles_ondemand_object_is_open(object)) { WARN_ON_ONCE(object->ondemand_id == 0); xas_unlock(&xas); ret = -EIO; @@ -430,18 +434,11 @@ static int cachefiles_ondemand_init_close_req(struct cachefiles_req *req, void *private) { struct cachefiles_object *object = req->object; - int object_id = object->ondemand_id; - /* - * It's possible that object id is still 0 if the cookie looking up - * phase failed before OPEN request has ever been sent. Also avoid - * sending CLOSE request for CACHEFILES_ONDEMAND_ID_CLOSED, which means - * anon_fd has already been closed. - */ - if (object_id <= 0) + if (!cachefiles_ondemand_object_is_open(object)) return -ENOENT; - req->msg.object_id = object_id; + req->msg.object_id = object->ondemand_id; trace_cachefiles_ondemand_close(object, &req->msg); return 0; } @@ -460,7 +457,7 @@ static int cachefiles_ondemand_init_read_req(struct cachefiles_req *req, int object_id = object->ondemand_id; /* Stop enqueuing requests when daemon has closed anon_fd. */ - if (object_id <= 0) { + if (!cachefiles_ondemand_object_is_open(object)) { WARN_ON_ONCE(object_id == 0); pr_info_once("READ: anonymous fd closed prematurely.\n"); return -EIO; @@ -485,7 +482,7 @@ int cachefiles_ondemand_init_object(struct cachefiles_object *object) * creating a new tmpfile as the cache file. Reuse the previously * allocated object ID if any. */ - if (object->ondemand_id > 0) + if (cachefiles_ondemand_object_is_open(object)) return 0; volume_key_size = volume->key[0] + 1;