Received: by 2002:a05:6a10:413:0:0:0:0 with SMTP id 19csp897655pxp; Wed, 16 Mar 2022 20:33:39 -0700 (PDT) X-Google-Smtp-Source: ABdhPJxTV9VEYUXf8aUKIGYm0LtA8Uf7U4qXf5eVF70vk8T8z+G9iVWl+rEo0e+DpUN/nwMxuKdd X-Received: by 2002:a05:6a00:1a56:b0:4f7:c17b:28da with SMTP id h22-20020a056a001a5600b004f7c17b28damr2702987pfv.50.1647488019013; Wed, 16 Mar 2022 20:33:39 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1647488019; cv=none; d=google.com; s=arc-20160816; b=yrrmJ/Dn9csVTN3F3b7fLp2CpLl5XYnvKnz3OuK3WJHvExJkmFCuYWW1RXXL2a+Nz4 rKbs7bZfh4x5aqdCZdFLGCmU0z/7pA37tkqwFuZWwjcp//HKyBnaOUYd065wkvkW430i FShzVbrl9k4BkhguaR4VbHvSGzsPlRllGHEvrRR2RkklA1YVjkERI/eExPOw3iiiqbir iuOB1fVttnZaXGWVKT8ruggOrP1K8m4Tlf2rFXTUXZjnv6pVyy4F5mpgR3VOrWTSJUdL aWjyACotwm3rMEbu7SGHNGK2nvrubFNQOcL8RZrybkdn5Ok8BGATLoRy60LtPvAWP7IL Otow== 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=v0SEJnBLJxgqSY40G00FHnw3seBTbEP0lnbAgozvRMU=; b=l/H6oqut1Fe/p91aDPs6mE8p5qaY1K325U4KC79SaL0P/H5o4X8rXBaOvus+WiytXA TmJHXNI9FEeRFJzpNnxSDqITSbmwfefNbh326/+ua1vQX1ojKYLPwTbODczwvWPgio4K 9Om0iYb7NOiFycG6VjPQO1U9Haj3kQXpsAUxe43A6USoosTpWfGnZV0lnswi7D6Wddee lls2gZcbzI2ZKdO6FCdxctGDgNEMenr192GbjGEjnHBFiL1Vz/1yEGDgZwOtZ9RTym5k YcznnLgjKLlJFekqfcnqyxvlGdCREWkiXqpaXoIxNsDJxqQf8Sta0alNQv9afiR1neSk 9KXw== ARC-Authentication-Results: i=1; mx.google.com; spf=softfail (google.com: domain of transitioning linux-kernel-owner@vger.kernel.org does not designate 23.128.96.19 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=alibaba.com Return-Path: Received: from lindbergh.monkeyblade.net (lindbergh.monkeyblade.net. [23.128.96.19]) by mx.google.com with ESMTPS id c10-20020a170903234a00b00151f6d0fc1esi3676039plh.108.2022.03.16.20.33.38 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 16 Mar 2022 20:33:39 -0700 (PDT) Received-SPF: softfail (google.com: domain of transitioning linux-kernel-owner@vger.kernel.org does not designate 23.128.96.19 as permitted sender) client-ip=23.128.96.19; Authentication-Results: mx.google.com; spf=softfail (google.com: domain of transitioning linux-kernel-owner@vger.kernel.org does not designate 23.128.96.19 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 vger.kernel.org (vger.kernel.org [23.128.96.18]) by lindbergh.monkeyblade.net (Postfix) with ESMTP id F09C83CA5B; Wed, 16 Mar 2022 20:29:32 -0700 (PDT) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1356278AbiCPNTU (ORCPT + 99 others); Wed, 16 Mar 2022 09:19:20 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:52588 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1356213AbiCPNSw (ORCPT ); Wed, 16 Mar 2022 09:18:52 -0400 Received: from out30-132.freemail.mail.aliyun.com (out30-132.freemail.mail.aliyun.com [115.124.30.132]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id F40EC66AE7; Wed, 16 Mar 2022 06:17:36 -0700 (PDT) X-Alimail-AntiSpam: AC=PASS;BC=-1|-1;BR=01201311R511e4;CH=green;DM=||false|;DS=||;FP=0|-1|-1|-1|0|-1|-1|-1;HT=e01e04357;MF=jefflexu@linux.alibaba.com;NM=1;PH=DS;RN=16;SR=0;TI=SMTPD_---0V7N1EXY_1647436652; Received: from localhost(mailfrom:jefflexu@linux.alibaba.com fp:SMTPD_---0V7N1EXY_1647436652) by smtp.aliyun-inc.com(127.0.0.1); Wed, 16 Mar 2022 21:17:33 +0800 From: Jeffle Xu To: dhowells@redhat.com, linux-cachefs@redhat.com, xiang@kernel.org, chao@kernel.org, linux-erofs@lists.ozlabs.org Cc: torvalds@linux-foundation.org, gregkh@linuxfoundation.org, willy@infradead.org, linux-fsdevel@vger.kernel.org, joseph.qi@linux.alibaba.com, bo.liu@linux.alibaba.com, tao.peng@linux.alibaba.com, gerry@linux.alibaba.com, eguan@linux.alibaba.com, linux-kernel@vger.kernel.org, luodaowen.backend@bytedance.com Subject: [PATCH v5 06/22] cachefiles: implement on-demand read Date: Wed, 16 Mar 2022 21:17:07 +0800 Message-Id: <20220316131723.111553-7-jefflexu@linux.alibaba.com> X-Mailer: git-send-email 2.27.0 In-Reply-To: <20220316131723.111553-1-jefflexu@linux.alibaba.com> References: <20220316131723.111553-1-jefflexu@linux.alibaba.com> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Spam-Status: No, score=-1.9 required=5.0 tests=BAYES_00, HEADER_FROM_DIFFERENT_DOMAINS,MAILING_LIST_MULTI,RDNS_NONE, SPF_HELO_NONE,T_SCC_BODY_TEXT_LINE,UNPARSEABLE_RELAY autolearn=no 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 Implement the data plane of on-demand read mode. A new NETFS_READ_HOLE_ONDEMAND flag is introduced to indicate that on-demand read should be done when a cache miss encountered. In this case, the read routine will send a READ request to user daemon, along with the anonymous fd and the file range that shall be read. Now user daemon is responsible for fetching data in the given file range, and then writing the fetched data into cache file with the given anonymous fd. After sending the READ request, the read routine will hang there, until the READ request is handled by user daemon. Then it will retry to read from the same file range. If a cache miss is encountered again on the same file range, the read routine will fail then. Signed-off-by: Jeffle Xu --- fs/cachefiles/daemon.c | 65 +++++++++++++++++++++++++++++++++ fs/cachefiles/internal.h | 9 +++++ fs/cachefiles/io.c | 11 ++++++ include/linux/netfs.h | 1 + include/uapi/linux/cachefiles.h | 7 ++++ 5 files changed, 93 insertions(+) diff --git a/fs/cachefiles/daemon.c b/fs/cachefiles/daemon.c index 2ecfdf194206..29af8943f270 100644 --- a/fs/cachefiles/daemon.c +++ b/fs/cachefiles/daemon.c @@ -47,6 +47,7 @@ static int cachefiles_daemon_bind(struct cachefiles_cache *, char *); static void cachefiles_daemon_unbind(struct cachefiles_cache *); #ifdef CONFIG_CACHEFILES_ONDEMAND static int cachefiles_ondemand_cinit(struct cachefiles_cache *, char *); +static int cachefiles_ondemand_cread(struct cachefiles_cache *, char *); #endif static unsigned long cachefiles_open; @@ -82,6 +83,7 @@ static const struct cachefiles_daemon_cmd cachefiles_daemon_cmds[] = { { "tag", cachefiles_daemon_tag }, #ifdef CONFIG_CACHEFILES_ONDEMAND { "cinit", cachefiles_ondemand_cinit }, + { "cread", cachefiles_ondemand_cread }, #endif { "", NULL } }; @@ -264,6 +266,36 @@ static int cachefiles_ondemand_cinit(struct cachefiles_cache *cache, char *args) return ret; } +/* + * Read request completion + * - command: "cread " + */ +static int cachefiles_ondemand_cread(struct cachefiles_cache *cache, char *args) +{ + struct cachefiles_req *req; + unsigned long id; + int ret; + + if (!test_bit(CACHEFILES_ONDEMAND_MODE, &cache->flags)) + return -EOPNOTSUPP; + + if (!*args) { + pr_err("Empty id specified\n"); + return -EINVAL; + } + + ret = kstrtoul(args, 0, &id); + if (ret) + return ret; + + req = xa_erase(&cache->reqs, id); + if (!req) + return -EINVAL; + + complete(&req->done); + return 0; +} + static int cachefiles_ondemand_get_fd(struct cachefiles_req *req) { struct cachefiles_object *object; @@ -471,6 +503,28 @@ static int init_close_req(struct cachefiles_req *req, void *private) return 0; } +struct cachefiles_read_ctx { + loff_t off; + size_t len; +}; + +static int init_read_req(struct cachefiles_req *req, void *private) +{ + struct cachefiles_object *object = req->object; + struct cachefiles_read *load = (void *)&req->msg.data; + struct cachefiles_read_ctx *read_ctx = private; + int fd = object->fd; + + /* Stop enqueuig request when daemon closes anon_fd prematurely. */ + if (WARN_ON_ONCE(fd == -1)) + return -EIO; + + load->off = read_ctx->off; + load->len = read_ctx->len; + load->fd = fd; + return 0; +} + int cachefiles_ondemand_init_object(struct cachefiles_object *object) { struct fscache_cookie *cookie = object->cookie; @@ -504,6 +558,17 @@ void cachefiles_ondemand_cleanup_object(struct cachefiles_object *object) init_close_req, NULL); } +int cachefiles_ondemand_read(struct cachefiles_object *object, + loff_t pos, size_t len) +{ + struct cachefiles_read_ctx read_ctx = {pos, len}; + + return cachefiles_ondemand_send_req(object, + CACHEFILES_OP_READ, + sizeof(struct cachefiles_read), + init_read_req, &read_ctx); +} + #else static inline void cachefiles_ondemand_open(struct cachefiles_cache *cache) {} static inline void cachefiles_ondemand_release(struct cachefiles_cache *cache) {} diff --git a/fs/cachefiles/internal.h b/fs/cachefiles/internal.h index eaac9fae74eb..770b37e23bcc 100644 --- a/fs/cachefiles/internal.h +++ b/fs/cachefiles/internal.h @@ -168,6 +168,8 @@ extern const struct file_operations cachefiles_daemon_fops; #ifdef CONFIG_CACHEFILES_ONDEMAND extern int cachefiles_ondemand_init_object(struct cachefiles_object *object); extern void cachefiles_ondemand_cleanup_object(struct cachefiles_object *object); +extern int cachefiles_ondemand_read(struct cachefiles_object *object, + loff_t pos, size_t len); #else static inline @@ -178,6 +180,13 @@ int cachefiles_ondemand_init_object(struct cachefiles_object *object) static inline void cachefiles_ondemand_cleanup_object(struct cachefiles_object *object) {} + +static inline +int cachefiles_ondemand_read(struct cachefiles_object *object, + loff_t pos, size_t len) +{ + return -EOPNOTSUPP; +} #endif /* diff --git a/fs/cachefiles/io.c b/fs/cachefiles/io.c index 8dbc1eb254a3..ee1283ba7a2c 100644 --- a/fs/cachefiles/io.c +++ b/fs/cachefiles/io.c @@ -95,6 +95,7 @@ static int cachefiles_read(struct netfs_cache_resources *cres, file, file_inode(file)->i_ino, start_pos, len, i_size_read(file_inode(file))); +retry: /* If the caller asked us to seek for data before doing the read, then * we should do that now. If we find a gap, we fill it with zeros. */ @@ -119,6 +120,16 @@ static int cachefiles_read(struct netfs_cache_resources *cres, if (read_hole == NETFS_READ_HOLE_FAIL) goto presubmission_error; + if (read_hole == NETFS_READ_HOLE_ONDEMAND) { + if (!cachefiles_ondemand_read(object, off, len)) { + /* fail the read if no progress achieved */ + read_hole = NETFS_READ_HOLE_FAIL; + goto retry; + } + + goto presubmission_error; + } + iov_iter_zero(len, iter); skipped = len; ret = 0; diff --git a/include/linux/netfs.h b/include/linux/netfs.h index 614f22213e21..2a9c50d3a928 100644 --- a/include/linux/netfs.h +++ b/include/linux/netfs.h @@ -203,6 +203,7 @@ enum netfs_read_from_hole { NETFS_READ_HOLE_IGNORE, NETFS_READ_HOLE_CLEAR, NETFS_READ_HOLE_FAIL, + NETFS_READ_HOLE_ONDEMAND, }; /* diff --git a/include/uapi/linux/cachefiles.h b/include/uapi/linux/cachefiles.h index 47e53043cfad..48a0dbac9a92 100644 --- a/include/uapi/linux/cachefiles.h +++ b/include/uapi/linux/cachefiles.h @@ -9,6 +9,7 @@ enum cachefiles_opcode { CACHEFILES_OP_OPEN, CACHEFILES_OP_CLOSE, + CACHEFILES_OP_READ, }; /* @@ -41,4 +42,10 @@ struct cachefiles_close { __u32 fd; }; +struct cachefiles_read { + __u64 off; + __u64 len; + __u32 fd; +}; + #endif -- 2.27.0