From nobody Thu Apr 9 10:53:28 2026 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id E33D1C4332F for ; Thu, 24 Nov 2022 03:42:25 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229773AbiKXDmY (ORCPT ); Wed, 23 Nov 2022 22:42:24 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:60128 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229657AbiKXDmS (ORCPT ); Wed, 23 Nov 2022 22:42:18 -0500 Received: from out30-57.freemail.mail.aliyun.com (out30-57.freemail.mail.aliyun.com [115.124.30.57]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id D87AE5B87E; Wed, 23 Nov 2022 19:42:16 -0800 (PST) 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=ay29a033018046056;MF=jefflexu@linux.alibaba.com;NM=1;PH=DS;RN=8;SR=0;TI=SMTPD_---0VVZ97Yj_1669261333; Received: from localhost(mailfrom:jefflexu@linux.alibaba.com fp:SMTPD_---0VVZ97Yj_1669261333) by smtp.aliyun-inc.com; Thu, 24 Nov 2022 11:42:14 +0800 From: Jingbo Xu To: dhowells@redhat.com, jlayton@kernel.org, xiang@kernel.org, chao@kernel.org, linux-cachefs@redhat.com, linux-erofs@lists.ozlabs.org Cc: linux-fsdevel@vger.kernel.org, linux-kernel@vger.kernel.org Subject: [PATCH v5 1/2] fscache,cachefiles: add prepare_ondemand_read() callback Date: Thu, 24 Nov 2022 11:42:11 +0800 Message-Id: <20221124034212.81892-2-jefflexu@linux.alibaba.com> X-Mailer: git-send-email 2.19.1.6.gb485710b In-Reply-To: <20221124034212.81892-1-jefflexu@linux.alibaba.com> References: <20221124034212.81892-1-jefflexu@linux.alibaba.com> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Type: text/plain; charset="utf-8" Add prepare_ondemand_read() callback dedicated for the on-demand read scenario, so that callers from this scenario can be decoupled from netfs_io_subrequest. The original cachefiles_prepare_read() is now refactored to a generic routine accepting a parameter list instead of netfs_io_subrequest. There's no logic change, except that the debug id of subrequest and request is removed from trace_cachefiles_prep_read(). Reviewed-by: Jeff Layton Signed-off-by: Jingbo Xu Acked-by: David Howells --- fs/cachefiles/io.c | 77 ++++++++++++++++++++----------- include/linux/netfs.h | 8 ++++ include/trace/events/cachefiles.h | 27 +++++------ 3 files changed, 72 insertions(+), 40 deletions(-) diff --git a/fs/cachefiles/io.c b/fs/cachefiles/io.c index 000a28f46e59..175a25fcade8 100644 --- a/fs/cachefiles/io.c +++ b/fs/cachefiles/io.c @@ -385,38 +385,35 @@ static int cachefiles_write(struct netfs_cache_resour= ces *cres, term_func, term_func_priv); } =20 -/* - * Prepare a read operation, shortening it to a cached/uncached - * boundary as appropriate. - */ -static enum netfs_io_source cachefiles_prepare_read(struct netfs_io_subreq= uest *subreq, - loff_t i_size) +static inline enum netfs_io_source +cachefiles_do_prepare_read(struct netfs_cache_resources *cres, + loff_t start, size_t *_len, loff_t i_size, + unsigned long *_flags, ino_t netfs_ino) { enum cachefiles_prepare_read_trace why; - struct netfs_io_request *rreq =3D subreq->rreq; - struct netfs_cache_resources *cres =3D &rreq->cache_resources; - struct cachefiles_object *object; + struct cachefiles_object *object =3D NULL; struct cachefiles_cache *cache; struct fscache_cookie *cookie =3D fscache_cres_cookie(cres); const struct cred *saved_cred; struct file *file =3D cachefiles_cres_file(cres); enum netfs_io_source ret =3D NETFS_DOWNLOAD_FROM_SERVER; + size_t len =3D *_len; loff_t off, to; ino_t ino =3D file ? file_inode(file)->i_ino : 0; int rc; =20 - _enter("%zx @%llx/%llx", subreq->len, subreq->start, i_size); + _enter("%zx @%llx/%llx", len, start, i_size); =20 - if (subreq->start >=3D i_size) { + if (start >=3D i_size) { ret =3D NETFS_FILL_WITH_ZEROES; why =3D cachefiles_trace_read_after_eof; goto out_no_object; } =20 if (test_bit(FSCACHE_COOKIE_NO_DATA_TO_READ, &cookie->flags)) { - __set_bit(NETFS_SREQ_COPY_TO_CACHE, &subreq->flags); + __set_bit(NETFS_SREQ_COPY_TO_CACHE, _flags); why =3D cachefiles_trace_read_no_data; - if (!test_bit(NETFS_SREQ_ONDEMAND, &subreq->flags)) + if (!test_bit(NETFS_SREQ_ONDEMAND, _flags)) goto out_no_object; } =20 @@ -437,7 +434,7 @@ static enum netfs_io_source cachefiles_prepare_read(str= uct netfs_io_subrequest * retry: off =3D cachefiles_inject_read_error(); if (off =3D=3D 0) - off =3D vfs_llseek(file, subreq->start, SEEK_DATA); + off =3D vfs_llseek(file, start, SEEK_DATA); if (off < 0 && off >=3D (loff_t)-MAX_ERRNO) { if (off =3D=3D (loff_t)-ENXIO) { why =3D cachefiles_trace_read_seek_nxio; @@ -449,21 +446,22 @@ static enum netfs_io_source cachefiles_prepare_read(s= truct netfs_io_subrequest * goto out; } =20 - if (off >=3D subreq->start + subreq->len) { + if (off >=3D start + len) { why =3D cachefiles_trace_read_found_hole; goto download_and_store; } =20 - if (off > subreq->start) { + if (off > start) { off =3D round_up(off, cache->bsize); - subreq->len =3D off - subreq->start; + len =3D off - start; + *_len =3D len; why =3D cachefiles_trace_read_found_part; goto download_and_store; } =20 to =3D cachefiles_inject_read_error(); if (to =3D=3D 0) - to =3D vfs_llseek(file, subreq->start, SEEK_HOLE); + to =3D vfs_llseek(file, start, SEEK_HOLE); if (to < 0 && to >=3D (loff_t)-MAX_ERRNO) { trace_cachefiles_io_error(object, file_inode(file), to, cachefiles_trace_seek_error); @@ -471,12 +469,13 @@ static enum netfs_io_source cachefiles_prepare_read(s= truct netfs_io_subrequest * goto out; } =20 - if (to < subreq->start + subreq->len) { - if (subreq->start + subreq->len >=3D i_size) + if (to < start + len) { + if (start + len >=3D i_size) to =3D round_up(to, cache->bsize); else to =3D round_down(to, cache->bsize); - subreq->len =3D to - subreq->start; + len =3D to - start; + *_len =3D len; } =20 why =3D cachefiles_trace_read_have_data; @@ -484,12 +483,11 @@ static enum netfs_io_source cachefiles_prepare_read(s= truct netfs_io_subrequest * goto out; =20 download_and_store: - __set_bit(NETFS_SREQ_COPY_TO_CACHE, &subreq->flags); - if (test_bit(NETFS_SREQ_ONDEMAND, &subreq->flags)) { - rc =3D cachefiles_ondemand_read(object, subreq->start, - subreq->len); + __set_bit(NETFS_SREQ_COPY_TO_CACHE, _flags); + if (test_bit(NETFS_SREQ_ONDEMAND, _flags)) { + rc =3D cachefiles_ondemand_read(object, start, len); if (!rc) { - __clear_bit(NETFS_SREQ_ONDEMAND, &subreq->flags); + __clear_bit(NETFS_SREQ_ONDEMAND, _flags); goto retry; } ret =3D NETFS_INVALID_READ; @@ -497,10 +495,34 @@ static enum netfs_io_source cachefiles_prepare_read(s= truct netfs_io_subrequest * out: cachefiles_end_secure(cache, saved_cred); out_no_object: - trace_cachefiles_prep_read(subreq, ret, why, ino); + trace_cachefiles_prep_read(object, start, len, *_flags, ret, why, ino, ne= tfs_ino); return ret; } =20 +/* + * Prepare a read operation, shortening it to a cached/uncached + * boundary as appropriate. + */ +static enum netfs_io_source cachefiles_prepare_read(struct netfs_io_subreq= uest *subreq, + loff_t i_size) +{ + return cachefiles_do_prepare_read(&subreq->rreq->cache_resources, + subreq->start, &subreq->len, i_size, + &subreq->flags, subreq->rreq->inode->i_ino); +} + +/* + * Prepare an on-demand read operation, shortening it to a cached/uncached + * boundary as appropriate. + */ +static enum netfs_io_source +cachefiles_prepare_ondemand_read(struct netfs_cache_resources *cres, + loff_t start, size_t *_len, loff_t i_size, + unsigned long *_flags, ino_t ino) +{ + return cachefiles_do_prepare_read(cres, start, _len, i_size, _flags, ino); +} + /* * Prepare for a write to occur. */ @@ -621,6 +643,7 @@ static const struct netfs_cache_ops cachefiles_netfs_ca= che_ops =3D { .write =3D cachefiles_write, .prepare_read =3D cachefiles_prepare_read, .prepare_write =3D cachefiles_prepare_write, + .prepare_ondemand_read =3D cachefiles_prepare_ondemand_read, .query_occupancy =3D cachefiles_query_occupancy, }; =20 diff --git a/include/linux/netfs.h b/include/linux/netfs.h index f2402ddeafbf..4c76ddfb6a67 100644 --- a/include/linux/netfs.h +++ b/include/linux/netfs.h @@ -267,6 +267,14 @@ struct netfs_cache_ops { loff_t *_start, size_t *_len, loff_t i_size, bool no_space_allocated_yet); =20 + /* Prepare an on-demand read operation, shortening it to a cached/uncached + * boundary as appropriate. + */ + enum netfs_io_source (*prepare_ondemand_read)(struct netfs_cache_resource= s *cres, + loff_t start, size_t *_len, + loff_t i_size, + unsigned long *_flags, ino_t ino); + /* Query the occupancy of the cache in a region, returning where the * next chunk of data starts and how long it is. */ diff --git a/include/trace/events/cachefiles.h b/include/trace/events/cache= files.h index d8d4d73fe7b6..cf4b98b9a9ed 100644 --- a/include/trace/events/cachefiles.h +++ b/include/trace/events/cachefiles.h @@ -428,16 +428,18 @@ TRACE_EVENT(cachefiles_vol_coherency, ); =20 TRACE_EVENT(cachefiles_prep_read, - TP_PROTO(struct netfs_io_subrequest *sreq, + TP_PROTO(struct cachefiles_object *obj, + loff_t start, + size_t len, + unsigned short flags, enum netfs_io_source source, enum cachefiles_prepare_read_trace why, - ino_t cache_inode), + ino_t cache_inode, ino_t netfs_inode), =20 - TP_ARGS(sreq, source, why, cache_inode), + TP_ARGS(obj, start, len, flags, source, why, cache_inode, netfs_inode= ), =20 TP_STRUCT__entry( - __field(unsigned int, rreq ) - __field(unsigned short, index ) + __field(unsigned int, obj ) __field(unsigned short, flags ) __field(enum netfs_io_source, source ) __field(enum cachefiles_prepare_read_trace, why ) @@ -448,19 +450,18 @@ TRACE_EVENT(cachefiles_prep_read, ), =20 TP_fast_assign( - __entry->rreq =3D sreq->rreq->debug_id; - __entry->index =3D sreq->debug_index; - __entry->flags =3D sreq->flags; + __entry->obj =3D obj ? obj->debug_id : 0; + __entry->flags =3D flags; __entry->source =3D source; __entry->why =3D why; - __entry->len =3D sreq->len; - __entry->start =3D sreq->start; - __entry->netfs_inode =3D sreq->rreq->inode->i_ino; + __entry->len =3D len; + __entry->start =3D start; + __entry->netfs_inode =3D netfs_inode; __entry->cache_inode =3D cache_inode; ), =20 - TP_printk("R=3D%08x[%u] %s %s f=3D%02x s=3D%llx %zx ni=3D%x B=3D%x", - __entry->rreq, __entry->index, + TP_printk("o=3D%08x %s %s f=3D%02x s=3D%llx %zx ni=3D%x B=3D%x", + __entry->obj, __print_symbolic(__entry->source, netfs_sreq_sources), __print_symbolic(__entry->why, cachefiles_prepare_read_traces), __entry->flags, --=20 2.19.1.6.gb485710b