Received: by 2002:a05:6a10:2726:0:0:0:0 with SMTP id ib38csp1050366pxb; Wed, 6 Apr 2022 07:33:43 -0700 (PDT) X-Google-Smtp-Source: ABdhPJzDZGT7oGE0fB0Zrt+VusRbGqm5G3YEamlDhVDGbZOlze5cqfXysn63ydw+YHfCCt9yf5Fd X-Received: by 2002:a17:902:f144:b0:156:a030:ee4c with SMTP id d4-20020a170902f14400b00156a030ee4cmr8914251plb.21.1649255622913; Wed, 06 Apr 2022 07:33:42 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1649255622; cv=none; d=google.com; s=arc-20160816; b=xCA4SDLSjkt54xO/oHPPv3TCxAJ3ANMT3JgubPrQsXpsyObBbNV6mjkXhaUSyo/irc mVHKVxhI+luAlYWBufVADuAKKdeneGt3zJoIHgryHTX87u0XjCB9Lk1dRerq7M0OUviQ Spen29YGgI54fD0++9Z8dJNsrL1Bgh4HUzZa3LqxgVNOrTJ5bTQENdrkcGnEHoIbHXzr WMcd1/3Hoab07f3vvdRohoyEJkw053x+MUajT5gaz1Oq/s+wEE2ZYiYVVBkFub41RR4e j3sZ8fNqKaYt8ntQqom7ezo0BUGs9PuCBSgjpk/+s5vEQNb0iRf/g/l4tYBK/lwVaHxI /5+Q== 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=wzQsrFZn2XT+V7sZC+ex9nwIhdBLPPUMsyaT3FBbOhg=; b=hiGb6Zjmpn9EFFnelh3b2JiEYZnSKh4NDXlHyA6vhnCHPBNDssvyWlG8MmkJZ2rowL CwwRm5pDziSbeO+mQl5vJsT46yaBO6L54nzuoMpORCtxh5CulvBY0vmPEaRWQ+l7U1Li kGohkXhAQvDXdAI8A/+XDH5wAYm1lGRbAVAoAV8coLOsWYi7UtG7RSLagD4lZ+FQeULz t8i932JCunXYQH3zsHdDMac7LvMfaR2E3aGgN3KT7SNGCEwU3wL+IdiMYRTnzB5FuXsn csx47XX4qnOIbrLyEoKhnrkrV626TUkMJzFGwOTIyNTk1x7BYu8LHWW6gCK1+nwZP+Rx jQBA== 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 pg6-20020a17090b1e0600b001ca75b6e109si5057562pjb.66.2022.04.06.07.33.42 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 06 Apr 2022 07:33:42 -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 SMTP id 2C039515FBF; Wed, 6 Apr 2022 05:15:10 -0700 (PDT) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231991AbiDFMPL (ORCPT + 99 others); Wed, 6 Apr 2022 08:15:11 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:38252 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231158AbiDFMOD (ORCPT ); Wed, 6 Apr 2022 08:14:03 -0400 Received: from out30-56.freemail.mail.aliyun.com (out30-56.freemail.mail.aliyun.com [115.124.30.56]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id E39B4D3712; Wed, 6 Apr 2022 00:56:25 -0700 (PDT) X-Alimail-AntiSpam: AC=PASS;BC=-1|-1;BR=01201311R171e4;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=18;SR=0;TI=SMTPD_---0V9L0qpr_1649231780; Received: from localhost(mailfrom:jefflexu@linux.alibaba.com fp:SMTPD_---0V9L0qpr_1649231780) by smtp.aliyun-inc.com(127.0.0.1); Wed, 06 Apr 2022 15:56:21 +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, tianzichen@kuaishou.com, fannaihao@baidu.com Subject: [PATCH v8 05/20] cachefiles: implement on-demand read Date: Wed, 6 Apr 2022 15:55:57 +0800 Message-Id: <20220406075612.60298-6-jefflexu@linux.alibaba.com> X-Mailer: git-send-email 2.27.0 In-Reply-To: <20220406075612.60298-1-jefflexu@linux.alibaba.com> References: <20220406075612.60298-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/internal.h | 9 ++++ fs/cachefiles/io.c | 11 +++++ fs/cachefiles/ondemand.c | 83 +++++++++++++++++++++++++++++++++ include/linux/netfs.h | 1 + include/uapi/linux/cachefiles.h | 18 +++++++ 5 files changed, 122 insertions(+) diff --git a/fs/cachefiles/internal.h b/fs/cachefiles/internal.h index 8a397d4da560..b4a834671b6b 100644 --- a/fs/cachefiles/internal.h +++ b/fs/cachefiles/internal.h @@ -281,6 +281,9 @@ extern int cachefiles_ondemand_copen(struct cachefiles_cache *cache, extern int cachefiles_ondemand_init_object(struct cachefiles_object *object); 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); + #else static inline ssize_t cachefiles_ondemand_daemon_read(struct cachefiles_cache *cache, char __user *_buffer, size_t buflen) @@ -296,6 +299,12 @@ static inline int cachefiles_ondemand_init_object(struct cachefiles_object *obje static inline void cachefiles_ondemand_clean_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 50a14e8f0aac..6f2e20cd41f4 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) { + ret = cachefiles_ondemand_read(object, off, len); + if (ret) + goto presubmission_error; + + /* fail the read if no progress achieved */ + read_hole = NETFS_READ_HOLE_FAIL; + goto retry; + } + iov_iter_zero(len, iter); skipped = len; ret = 0; diff --git a/fs/cachefiles/ondemand.c b/fs/cachefiles/ondemand.c index defd65124052..149ae1923955 100644 --- a/fs/cachefiles/ondemand.c +++ b/fs/cachefiles/ondemand.c @@ -11,13 +11,30 @@ static int cachefiles_ondemand_fd_release(struct inode *inode, struct file *file) { struct cachefiles_object *object = file->private_data; + struct cachefiles_cache *cache = object->volume->cache; + struct xarray *xa = &cache->reqs; + struct cachefiles_req *req; + unsigned long index; + xa_lock(xa); /* * Uninstall anon_fd to the cachefiles object, so that no further * associated requests will get enqueued. */ object->fd = -1; + /* + * Flush all pending READ requests since their completion depends on + * anon_fd. + */ + xa_for_each(xa, index, req) { + if (req->msg.opcode == CACHEFILES_OP_READ) { + req->error = -EIO; + complete(&req->done); + } + } + xa_unlock(xa); + cachefiles_put_object(object, cachefiles_obj_put_ondemand_fd); return 0; } @@ -61,11 +78,35 @@ static loff_t cachefiles_ondemand_fd_llseek(struct file *filp, loff_t pos, return vfs_llseek(file, pos, whence); } +static long cachefiles_ondemand_fd_ioctl(struct file *filp, unsigned int ioctl, + unsigned long arg) +{ + struct cachefiles_object *object = filp->private_data; + struct cachefiles_cache *cache = object->volume->cache; + struct cachefiles_req *req; + unsigned long id; + + if (ioctl != CACHEFILES_IOC_CREAD) + return -EINVAL; + + if (!test_bit(CACHEFILES_ONDEMAND_MODE, &cache->flags)) + return -EOPNOTSUPP; + + id = arg; + req = xa_erase(&cache->reqs, id); + if (!req) + return -EINVAL; + + complete(&req->done); + return 0; +} + static const struct file_operations cachefiles_ondemand_fd_fops = { .owner = THIS_MODULE, .release = cachefiles_ondemand_fd_release, .write_iter = cachefiles_ondemand_fd_write_iter, .llseek = cachefiles_ondemand_fd_llseek, + .unlocked_ioctl = cachefiles_ondemand_fd_ioctl, }; /* @@ -283,6 +324,13 @@ static int cachefiles_ondemand_send_req(struct cachefiles_object *object, goto out; } + /* recheck anon_fd for READ request with lock held */ + if (opcode == CACHEFILES_OP_READ && object->fd == -1) { + xas_unlock(&xas); + ret = -EIO; + goto out; + } + xas.xa_index = 0; xas_find_marked(&xas, UINT_MAX, XA_FREE_MARK); if (xas.xa_node == XAS_RESTART) @@ -362,6 +410,30 @@ 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 enqueuing request when daemon closes anon_fd prematurely. */ + if (fd == -1) { + pr_info_once("READ: anonymous fd closed prematurely.\n"); + 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; @@ -394,3 +466,14 @@ void cachefiles_ondemand_clean_object(struct cachefiles_object *object) sizeof(struct cachefiles_close), 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); +} diff --git a/include/linux/netfs.h b/include/linux/netfs.h index c7bf1eaf51d5..c1854e92333e 100644 --- a/include/linux/netfs.h +++ b/include/linux/netfs.h @@ -222,6 +222,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 73397e142ab3..9506b1697e14 100644 --- a/include/uapi/linux/cachefiles.h +++ b/include/uapi/linux/cachefiles.h @@ -3,6 +3,7 @@ #define _LINUX_CACHEFILES_H #include +#include /* * Fscache ensures that the maximum length of cookie key is 255. The volume key @@ -13,6 +14,7 @@ enum cachefiles_opcode { CACHEFILES_OP_OPEN, CACHEFILES_OP_CLOSE, + CACHEFILES_OP_READ, }; /* @@ -51,4 +53,20 @@ struct cachefiles_close { __u32 fd; }; +/* + * @off identifies the starting offset of the requested file range. + * @len identifies the length of the requested file range. + */ +struct cachefiles_read { + __u64 off; + __u64 len; + __u32 fd; +}; + +/* + * Reply for READ request (Completion for READ) + * arg for CACHEFILES_IOC_CREAD ioctl is the @id field of READ request. + */ +#define CACHEFILES_IOC_CREAD _IOW(0x98, 1, int) + #endif -- 2.27.0