fscache,erofs: fscache-based on-demand read semantics
Subsystems (6)
v11 cover fscache,erofs: fscache-based on-demand read semantics
changes since v10:
- rebase to 5.18-rc5
- append the patchset with a patch from Xin Yin, implementing the asynchronous readahead (patch 22)
Kernel Patchset
---------------
Git tree:
https://github.com/lostjeffle/linux.git jingbo/dev-erofs-fscache-v11
Gitweb:
https://github.com/lostjeffle/linux/commits/jingbo/dev-erofs-fscache-v11
User Guide for E2E Container Use Case
-------------------------------------
User guide:
https://github.com/dragonflyoss/image-service/blob/fscache/docs/nydus-fscache.md
Video:
https://youtu.be/F4IF2_DENXo
User Daemon for Quick Test
--------------------------
Git tree:
https://github.com/lostjeffle/demand-read-cachefilesd.git main
Gitweb:
https://github.com/lostjeffle/demand-read-cachefilesd
RFC: https://lore.kernel.org/all/[email protected]/t/
v1: https://lore.kernel.org/lkml/[email protected]/T/
v2: https://lore.kernel.org/all/[email protected]/t/
v3: https://lore.kernel.org/lkml/[email protected]/T/
v4: https://lore.kernel.org/lkml/[email protected]/T/#t
v5: https://lore.kernel.org/lkml/[email protected]/T/
v6: https://lore.kernel.org/lkml/[email protected]/T/
v7: https://lore.kernel.org/lkml/[email protected]/T/
v8: https://lore.kernel.org/all/[email protected]/T/
v9: https://lore.kernel.org/lkml/[email protected]/T/
v10:https://lore.kernel.org/all/[email protected]/t/
[Background]
============
Nydus [1] is an image distribution service especially optimized for distribution over network. Nydus is an excellent container image acceleration solution, since it only pulls data from remote when needed, a.k.a. on-demand reading and it also supports chunk-based deduplication, compression, etc.
erofs (Enhanced Read-Only File System) is a filesystem designed for read-only scenarios. (Documentation/filesystem/erofs.rst)
Over the past months we've been focusing on supporting Nydus image service with in-kernel erofs format[2]. In that case, each container image will be organized in one bootstrap (metadata) and (optional) multiple data blobs in erofs format. Massive container images will be stored on one machine.
To accelerate the container startup (fetching container images from remote and then start the container), we do hope that the bootstrap & blob files could support on-demand read. That is, erofs can be mounted and accessed even when the bootstrap/data blob files have not been fully downloaded. Then it'll have native performance after data is available locally.
That means we have to manage the cache state of the bootstrap/data blob files (if cache hit, read directly from the local cache; if cache miss, fetch the data somehow). It would be painful and may be dumb for erofs to implement the cache management itself. Thus we prefer fscache/cachefiles to do the cache management instead.
The fscache on-demand read feature aims to be implemented in a generic way so that it can benefit other use cases and/or filesystems if it's implemented in the fscache subsystem.
[1] https://nydus.dev
[2] https://sched.co/pcdL
[Overall Design]
================
Please refer to patch 7 ("cachefiles: document on-demand read mode") for more details.
When working in the original mode, cachefiles mainly serves as a local cache for remote networking fs, while in on-demand read mode, cachefiles can work in the scenario where on-demand read semantics is needed, e.g. container image distribution.
The essential difference between these two modes is that, in original mode, when cache miss, netfs itself will fetch data from remote, and then write the fetched data into cache file. While in on-demand read mode, a user daemon is responsible for fetching data and then feeds to the kernel fscache side.
The on-demand read mode relies on a simple protocol used for communication between kernel and user daemon.
The proposed implementation relies on the anonymous fd mechanism to avoid the dependence on the format of cache file. When a fscache cachefile is opened for the first time, an anon_fd associated with the cache file is sent to the user daemon. With the given anon_fd, user daemon could fetch and write data into the cache file in the background, even when kernel has not triggered the cache miss. Besides, the write() syscall to the anon_fd will finally call cachefiles kernel module, which will write data to cache file in the latest format of cache file.
1. cache miss
When cache miss, cachefiles kernel module will notify user daemon with the anon_fd, along with the requested file range. When notified, user daemon needs to fetch data of the requested file range, and then write the fetched data into cache file with the given anonymous fd. When finished processing the request, user daemon needs to notify the kernel.
After notifying the user daemon, the kernel read routine will wait there, until the request is handled by user daemon. When it's awaken by the notification from user daemon, i.e. the corresponding hole has been filled by the user daemon, it will retry to read from the same file range.
2. cache hit
Once data is already ready in cache file, netfs will read from cache file directly.
[Advantage of fscache-based on-demand read]
========================================
1. Asynchronous prefetch
In current mechanism, fscache is responsible for cache state management, while the data plane (fetching data from local/remote on cache miss) is done on the user daemon side even without any file system request driven. In addition, if cached data has already been available locally, fscache will use it instead of trapping to user space anymore.
Therefore, different from event-driven approaches, the fscache on-demand user daemon could also fetch data (from remote) asynchronously in the background just like most multi-threaded HTTP downloaders.
2. Flexible request amplification
Since the data plane can be independently controlled by the user daemon, the user daemon can also fetch more data from remote than that the file system actually requests for small I/O sizes. Then, fetched data in bulk will be available at once and fscache won't be trapped into the user daemon again.
3. Support massive blobs
This mechanism can naturally support a large amount of backing files, and thus can benefit the densely employed scenarios. In our use cases, one container image can be formed of one bootstrap (required) and multiple chunk-deduplicated data blobs (optional).
For example, one container image for node.js will correspond to ~20 files in total. In densely employed environment, there could be hundreds of containers and thus thousands of backing files on one machine.
[Following Steps]
=================
The following improvements are on our TODO list, and will be formed in shape with the development process:
- Data blobs can be shared between multiple filesystems. Whilst in the current implementation, each filesystem registers a unique fscache_volume, causing the backing file for the data blob can not be shared between different erofs filesystems. Later we need to introduce shared domain in order to share fscache_volume, so that data blobs can be shared between container images to some degree.
- in-memory extent-based data sharing, e.g., different files can share the same chunk of the data blob. In the current implementation, each erofs file maintains its own page cache, thus the page caches for the same chunk may be duplicated among multiple files sharing the same chunk.
- other useful features, including multiple cachefiles daemon support, etc.
Jeffle Xu (21):
cachefiles: extract write routine
cachefiles: notify the user daemon when looking up cookie
cachefiles: unbind cachefiles gracefully in on-demand mode
cachefiles: notify the user daemon when withdrawing cookie
cachefiles: implement on-demand read
cachefiles: enable on-demand read mode
cachefiles: add tracepoints for on-demand read mode
cachefiles: document on-demand read mode
erofs: make erofs_map_blocks() generally available
erofs: add fscache mode check helper
erofs: register fscache volume
erofs: add fscache context helper functions
erofs: add anonymous inode caching metadata for data blobs
erofs: add erofs_fscache_read_folios() helper
erofs: register fscache context for primary data blob
erofs: register fscache context for extra data blobs
erofs: implement fscache-based metadata read
erofs: implement fscache-based data read for non-inline layout
erofs: implement fscache-based data read for inline layout
erofs: implement fscache-based data readahead
erofs: add 'fsid' mount option
Xin Yin (1):
erofs: change to use asynchronous io for fscache readpage/readahead
.../filesystems/caching/cachefiles.rst | 178 ++++++ fs/cachefiles/Kconfig | 12 + fs/cachefiles/Makefile | 1 + fs/cachefiles/daemon.c | 117 +++- fs/cachefiles/interface.c | 2 + fs/cachefiles/internal.h | 78 +++ fs/cachefiles/io.c | 76 ++- fs/cachefiles/namei.c | 16 +- fs/cachefiles/ondemand.c | 503 +++++++++++++++++ fs/erofs/Kconfig | 10 + fs/erofs/Makefile | 1 + fs/erofs/data.c | 26 +- fs/erofs/fscache.c | 522 ++++++++++++++++++ fs/erofs/inode.c | 4 + fs/erofs/internal.h | 49 ++ fs/erofs/super.c | 105 +++- fs/erofs/sysfs.c | 4 +- include/linux/fscache.h | 1 + include/linux/netfs.h | 1 + include/trace/events/cachefiles.h | 176 ++++++ include/uapi/linux/cachefiles.h | 68 +++ 21 files changed, 1871 insertions(+), 79 deletions(-) create mode 100644 fs/cachefiles/ondemand.c create mode 100644 fs/erofs/fscache.c create mode 100644 include/uapi/linux/cachefiles.h
Signature · 1 line
2.27.0
PATCH v11 1/22 cachefiles: extract write routine
Extract the generic routine of writing data to cache files, and make it generally available.
This will be used by the following patch implementing on-demand read mode. Since it's called inside CacheFiles module, make the interface generic and unrelated to netfs_cache_resources.
It is worth noting that, ki->inval_counter is not initialized after this cleanup. It shall not make any visible difference, since inval_counter is no longer used in the write completion routine, i.e. cachefiles_write_complete().
--- fs/cachefiles/internal.h | 10 +++++++ fs/cachefiles/io.c | 61 +++++++++++++++++++++++----------------- 2 files changed, 45 insertions(+), 26 deletions(-)
fs/cachefiles/internal.h +10 -0
@@ -201,6 +201,16 @@ extern void cachefiles_put_object(struct cachefiles_object *object,*/ extern bool cachefiles_begin_operation (struct netfs_cache_resources *cres,enum fscache_want_state want_state); +extern int __cachefiles_prepare_write (struct cachefiles_object *object, +struct file *file, +loff_t *_start,size_t *_len, +bool no_space_allocated_yet); +extern int __cachefiles_write (struct cachefiles_object *object, +struct file *file, +loff_t start_pos, +struct iov_iter *iter, +netfs_io_terminated_t term_func, +void *term_func_priv);/* * key.c
fs/cachefiles/io.c +35 -26
109 lines not on this page · raw message
Signature · 1 line
2.27.0
PATCH v11 2/22 cachefiles: notify the user daemon when looking up cookie
In reply to Jeffle XuFscache/CacheFiles used to serve as a local cache for a remote networking fs. A new on-demand read mode will be introduced for CacheFiles, which can boost the scenario where on-demand read semantics are needed, e.g. container image distribution.
The essential difference between these two modes is seen when a cache miss occurs: In the original mode, the netfs will fetch the data from the remote server and then write it to the cache file; in on-demand read mode, fetching the data and writing it into the cache is delegated to a user daemon.
As the first step, notify the user daemon when looking up cookie. In this case, an anonymous fd is sent to the user daemon, through which the user daemon can write the fetched data to the cache file. Since the user daemon may move the anonymous fd around, e.g. through dup(), an object ID uniquely identifying the cache file is also attached.
Also add one advisory flag (FSCACHE_ADV_WANT_CACHE_SIZE) suggesting that the cache file size shall be retrieved at runtime. This helps the scenario where one cache file contains multiple netfs files, e.g. for the purpose of deduplication. In this case, netfs itself has no idea the size of the cache file, whilst the user daemon should give the hint on it.
--- fs/cachefiles/Kconfig | 12 + fs/cachefiles/Makefile | 1 + fs/cachefiles/daemon.c | 81 ++++++- fs/cachefiles/internal.h | 51 ++++ fs/cachefiles/namei.c | 16 +- fs/cachefiles/ondemand.c | 378 ++++++++++++++++++++++++++++++ include/linux/fscache.h | 1 + include/trace/events/cachefiles.h | 2 + include/uapi/linux/cachefiles.h | 50 ++++ 9 files changed, 577 insertions(+), 15 deletions(-) create mode 100644 fs/cachefiles/ondemand.c create mode 100644 include/uapi/linux/cachefiles.h
fs/cachefiles/Kconfig +12 -0
@@ -26,3 +26,15 @@ config CACHEFILES_ERROR_INJECTION help This permits error injection to be enabled in cachefiles whilst a cache is in service. + +config CACHEFILES_ONDEMAND + bool "Support for on-demand read" + depends on CACHEFILES + default n + help + This permits userspace to enable the cachefiles on-demand read mode. + In this mode, when a cache miss occurs, responsibility for fetching + the data lies with the cachefiles backend instead of with the netfs + and is delegated to userspace. + + If unsure, say N.
fs/cachefiles/Makefile +1 -0
@@ -16,5 +16,6 @@ cachefiles-y := \ xattr.o cachefiles-$(CONFIG_CACHEFILES_ERROR_INJECTION) += error_inject.o +cachefiles-$(CONFIG_CACHEFILES_ONDEMAND) += ondemand.o obj-$(CONFIG_CACHEFILES) := cachefiles.o
fs/cachefiles/daemon.c +68 -13
129 lines not on this page · raw message
fs/cachefiles/internal.h +51 -0
82 lines not on this page · raw message
fs/cachefiles/namei.c +14 -2
@@ -452,10 +452,9 @@ struct file *cachefiles_create_tmpfile(struct cachefiles_object *object)struct dentry *fan = volume->fanout[(u8 )object->cookie->key_hash];struct file *file;struct path path; -uint64_t ni_size = object->cookie->object_size; +uint64_t ni_size;long ret; - ni_size =round_up (ni_size,CACHEFILES_DIO_BLOCK_SIZE );cachefiles_begin_secure (cache, &saved_cred);
@@ -481,6 +480,15 @@ struct file *cachefiles_create_tmpfile(struct cachefiles_object *object) goto out_dput; } + ret =cachefiles_ondemand_init_object (object); +if (ret <0 ) { + file =ERR_PTR (ret); + goto out_unuse; + } + + ni_size = object->cookie->object_size; + ni_size =round_up (ni_size,CACHEFILES_DIO_BLOCK_SIZE ); +if (ni_size >0 ) {trace_cachefiles_trunc (object,d_backing_inode (path.dentry),0 , ni_size, cachefiles_trunc_expand_tmpfile);
@@ -586,6 +594,10 @@ static bool cachefiles_open_file(struct cachefiles_object *object, }_debug ("file -> %pd positive" , dentry); + ret =cachefiles_ondemand_init_object (object); +if (ret <0 ) + goto error_fput; + ret =cachefiles_check_auxdata (object, file);if (ret <0 ) goto check_failed;
fs/cachefiles/ondemand.c new file +378 -0
378 lines not on this page · raw message
include/linux/fscache.h +1 -0
@@ -39,6 +39,7 @@ struct fscache_cookie;#define FSCACHE_ADV_SINGLE_CHUNK 0x01/* The object is a single chunk of data */ #define FSCACHE_ADV_WRITE_CACHE 0x00/* Do cache if written to locally */ #define FSCACHE_ADV_WRITE_NOCACHE 0x02/* Don't cache if written to locally */ +#define FSCACHE_ADV_WANT_CACHE_SIZE 0x04/* Retrieve cache size at runtime */ #define FSCACHE_INVAL_DIO_WRITE 0x01/* Invalidate due to DIO write */
include/trace/events/cachefiles.h +2 -0
@@ -31,6 +31,8 @@ enum cachefiles_obj_ref_trace { cachefiles_obj_see_lookup_failed, cachefiles_obj_see_withdraw_cookie, cachefiles_obj_see_withdrawal, + cachefiles_obj_get_ondemand_fd, + cachefiles_obj_put_ondemand_fd, }; enum fscache_why_object_killed {
include/uapi/linux/cachefiles.h new file +50 -0
50 lines not on this page · raw message
Signature · 1 line
2.27.0
Re: [PATCH v11 02/22] cachefiles: notify the user daemon when looking up cookie
Quoted 17 lines · Jeffle Xu
Fscache/CacheFiles used to serve as a local cache for a remote networking fs. A new on-demand read mode will be introduced for CacheFiles, which can boost the scenario where on-demand read semantics are needed, e.g. container image distribution.
The essential difference between these two modes is seen when a cache miss occurs: In the original mode, the netfs will fetch the data from the remote server and then write it to the cache file; in on-demand read mode, fetching the data and writing it into the cache is delegated to a user daemon.
As the first step, notify the user daemon when looking up cookie. In this case, an anonymous fd is sent to the user daemon, through which the user daemon can write the fetched data to the cache file. Since the user daemon may move the anonymous fd around, e.g. through
dup(), an object ID uniquely identifying the cache file is also attached.
Quoted 8 lines
Also add one advisory flag (
FSCACHE_ADV_WANT_CACHE_SIZE) suggesting that the cache file size shall be retrieved at runtime. This helps the scenario where one cache file contains multiple netfs files, e.g. for the purpose of deduplication. In this case, netfs itself has no idea the size of the cache file, whilst the user daemon should give the hint on it.Signed-off-by Jeffle Xu <[email protected]>
PATCH v11 3/22 cachefiles: unbind cachefiles gracefully in on-demand mode
In reply to Jeffle XuAdd a refcount to avoid the deadlock in on-demand read mode. The on-demand read mode will pin the corresponding cachefiles object for each anonymous fd. The cachefiles object is unpinned when the anonymous fd gets closed. When the user daemon exits and the fd of "/dev/cachefiles" device node gets closed, it will wait for all cahcefiles objects getting withdrawn. Then if there's any anonymous fd getting closed after the fd of the device node, the user daemon will hang forever, waiting for all objects getting withdrawn.
To fix this, add a refcount indicating if there's any object pinned by anonymous fds. The cachefiles cache gets unbound and withdrawn when the refcount is decreased to 0. It won't change the behaviour of the original mode, in which case the cachefiles cache gets unbound and withdrawn as long as the fd of the device node gets closed.
--- fs/cachefiles/daemon.c | 19 ++++++++++++++++--- fs/cachefiles/internal.h | 3 +++ fs/cachefiles/ondemand.c | 3 +++ 3 files changed, 22 insertions(+), 3 deletions(-)
fs/cachefiles/daemon.c +16 -3
@@ -111,6 +111,7 @@ static int cachefiles_daemon_open(struct inode *inode, struct file *file)INIT_LIST_HEAD (&cache->volumes);INIT_LIST_HEAD (&cache->object_list);spin_lock_init (&cache->object_list_lock); +refcount_set (&cache->unbind_pincount,1 );xa_init_flags (&cache->reqs,XA_FLAGS_ALLOC );xa_init_flags (&cache->ondemand_ids,XA_FLAGS_ALLOC1 );
@@ -164,6 +165,20 @@ static void cachefiles_flush_reqs(struct cachefiles_cache *cache)xa_destroy (&cache->ondemand_ids); } +void cachefiles_put_unbind_pincount (struct cachefiles_cache *cache) +{ +if (refcount_dec_and_test (&cache->unbind_pincount)) { +cachefiles_daemon_unbind (cache); + cachefiles_open =0 ; +kfree (cache); + } +} + +void cachefiles_get_unbind_pincount (struct cachefiles_cache *cache) +{ +refcount_inc (&cache->unbind_pincount); +} +/* * Release a cache. */
@@ -179,14 +194,12 @@ static int cachefiles_daemon_release(struct inode *inode, struct file *file)if (cachefiles_in_ondemand_mode (cache))cachefiles_flush_reqs (cache); -cachefiles_daemon_unbind (cache);/* clean up the control file interface */ cache->cachefilesd =NULL ; file->private_data =NULL ; - cachefiles_open =0 ; -kfree (cache); +cachefiles_put_unbind_pincount (cache);_leave ("" );return 0 ;
fs/cachefiles/internal.h +3 -0
@@ -109,6 +109,7 @@ struct cachefiles_cache {char *rootdirname;/* name of cache root directory */ char *secctx;/* LSM security context */ char *tag;/* cache binding tag */ +refcount_t unbind_pincount;/* refcount to do daemon unbind */ struct xarray reqs;/* xarray of pending on-demand requests */ struct xarray ondemand_ids;/* xarray for ondemand_id allocation */ u32 ondemand_id_next;
@@ -171,6 +172,8 @@ extern int cachefiles_has_space(struct cachefiles_cache *cache,* daemon.c */ extern const struct file_operations cachefiles_daemon_fops; +extern void cachefiles_get_unbind_pincount (struct cachefiles_cache *cache); +extern void cachefiles_put_unbind_pincount (struct cachefiles_cache *cache);/* * error_inject.c
fs/cachefiles/ondemand.c +3 -0
@@ -14,6 +14,7 @@ static int cachefiles_ondemand_fd_release(struct inode *inode, object->ondemand_id =CACHEFILES_ONDEMAND_ID_CLOSED ;xa_erase (&cache->ondemand_ids, object_id);cachefiles_put_object (object, cachefiles_obj_put_ondemand_fd); +cachefiles_put_unbind_pincount (cache);return 0 ; }
@@ -169,6 +170,8 @@ static int cachefiles_ondemand_get_fd(struct cachefiles_req *req) load->fd = fd; req->msg.object_id = object_id; object->ondemand_id = object_id; + +cachefiles_get_unbind_pincount (cache);return 0 ; err_put_fd:
Signature · 1 line
2.27.0
Re: [PATCH v11 03/22] cachefiles: unbind cachefiles gracefully in on-demand mode
Quoted 9 lines · Jeffle Xu
Add a refcount to avoid the deadlock in on-demand read mode. The on-demand read mode will pin the corresponding cachefiles object for each anonymous fd. The cachefiles object is unpinned when the anonymous fd gets closed. When the user daemon exits and the fd of "
/dev/cachefiles" device node gets closed, it will wait for all cahcefiles objects getting withdrawn. Then if there's any anonymous fd getting closed after the fd of the device node, the user daemon will hang forever, waiting for all objects getting withdrawn.
Quoted 7 lines
To fix this, add a refcount indicating if there's any object pinned by anonymous fds. The cachefiles cache gets unbound and withdrawn when the refcount is decreased to 0. It won't change the behaviour of the original mode, in which case the cachefiles cache gets unbound and withdrawn as long as the fd of the device node gets closed.
Signed-off-by Jeffle Xu <[email protected]>
PATCH v11 4/22 cachefiles: notify the user daemon when withdrawing cookie
In reply to Jeffle XuNotify the user daemon that cookie is going to be withdrawn, providing a hint that the associated anonymous fd can be closed.
Be noted that this is only a hint. The user daemon may close the associated anonymous fd when receiving the CLOSE request, then it will receive another anonymous fd when the cookie gets looked up. Or it may ignore the CLOSE request, and keep writing data through the anonymous fd. However the next time the cookie gets looked up, the user daemon will still receive another new anonymous fd.
--- fs/cachefiles/interface.c | 2 ++ fs/cachefiles/internal.h | 5 +++++ fs/cachefiles/ondemand.c | 38 +++++++++++++++++++++++++++++++++ include/uapi/linux/cachefiles.h | 1 + 4 files changed, 46 insertions(+)
fs/cachefiles/interface.c +2 -0
@@ -362,6 +362,8 @@ static void cachefiles_withdraw_cookie(struct fscache_cookie *cookie)spin_unlock (&cache->object_list_lock); } +cachefiles_ondemand_clean_object (object); +if (object->file) {cachefiles_begin_secure (cache, &saved_cred);cachefiles_clean_up_object (object, cache);
fs/cachefiles/internal.h +5 -0
@@ -290,6 +290,7 @@ extern int cachefiles_ondemand_copen(struct cachefiles_cache *cache, char *args);extern int cachefiles_ondemand_init_object (struct cachefiles_object *object); +extern void cachefiles_ondemand_clean_object (struct cachefiles_object *object);#else static inline ssize_t cachefiles_ondemand_daemon_read(struct cachefiles_cache *cache,
@@ -302,6 +303,10 @@ static inline int cachefiles_ondemand_init_object(struct cachefiles_object *obje {return 0 ; } + +static inline void cachefiles_ondemand_clean_object (struct cachefiles_object *object) +{ +}#endif /*
fs/cachefiles/ondemand.c +38 -0
@@ -229,6 +229,12 @@ ssize_t cachefiles_ondemand_daemon_read(struct cachefiles_cache *cache, goto err_put_fd; } +/* CLOSE request has no reply */ +if (msg->opcode ==CACHEFILES_OP_CLOSE ) { +xa_erase (&cache->reqs, id); +complete (&req->done); + } +return n; err_put_fd:
@@ -300,6 +306,13 @@ 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 ) { +WARN_ON_ONCE (object->ondemand_id ==0 ); +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 )
@@ -356,6 +369,25 @@ static int cachefiles_ondemand_init_open_req(struct cachefiles_req *req,return 0 ; } +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 ) +return -ENOENT ; + + req->msg.object_id = object_id; +return 0 ; +} +int cachefiles_ondemand_init_object (struct cachefiles_object *object) {struct fscache_cookie *cookie = object->cookie;
@@ -379,3 +411,9 @@ int cachefiles_ondemand_init_object(struct cachefiles_object *object)return cachefiles_ondemand_send_req (object,CACHEFILES_OP_OPEN , data_len, cachefiles_ondemand_init_open_req,NULL ); } + +void cachefiles_ondemand_clean_object (struct cachefiles_object *object) +{ +cachefiles_ondemand_send_req (object,CACHEFILES_OP_CLOSE ,0 , + cachefiles_ondemand_init_close_req,NULL ); +}
include/uapi/linux/cachefiles.h +1 -0
@@ -12,6 +12,7 @@enum cachefiles_opcode {CACHEFILES_OP_OPEN , +CACHEFILES_OP_CLOSE , };/*
Signature · 1 line
2.27.0
PATCH v11 5/22 cachefiles: implement on-demand read
In reply to Jeffle XuImplement the data plane of on-demand read mode.
The early implementation [1] place the entry to cachefiles_ondemand_read() in fscache_read(). However, fscache_read() can only detect if the requested file range is fully cache miss, whilst we need to notify the user daemon as long as there's a hole inside the requested file range.
Thus the entry is now placed in cachefiles_prepare_read(). When working in on-demand read mode, once a hole detected, the read routine will send a READ request to the user daemon. The user daemon needs to fetch the data and write it to the cache file. After sending the READ request, the read routine will hang there, until the READ request is handled by the user daemon. Then it will retry to read from the same file range. If no progress encountered, the read routine will fail then.
A new NETFS_SREQ_ONDEMAND flag is introduced to indicate that on-demand read should be done when a cache miss encountered.
[1] https://lore.kernel.org/all/[email protected]/ #v8
--- fs/cachefiles/internal.h | 9 ++++ fs/cachefiles/io.c | 15 ++++++- fs/cachefiles/ondemand.c | 77 +++++++++++++++++++++++++++++++++ include/linux/netfs.h | 1 + include/uapi/linux/cachefiles.h | 17 ++++++++ 5 files changed, 117 insertions(+), 2 deletions(-)
fs/cachefiles/internal.h +9 -0
@@ -292,6 +292,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)
@@ -307,6 +310,12 @@ static inline int cachefiles_ondemand_init_object(struct cachefiles_object *objestatic 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 /*
fs/cachefiles/io.c +13 -2
@@ -403,6 +403,7 @@ static enum netfs_io_source cachefiles_prepare_read(struct netfs_io_subrequest *enum netfs_io_source ret =NETFS_DOWNLOAD_FROM_SERVER ;loff_t off, to;ino_t ino = file ?file_inode (file)->i_ino :0 ; +int rc;_enter ("%zx @%llx/%llx" , subreq->len, subreq->start, i_size);
@@ -415,7 +416,8 @@ static enum netfs_io_source cachefiles_prepare_read(struct netfs_io_subrequest *if (test_bit (FSCACHE_COOKIE_NO_DATA_TO_READ , &cookie->flags)) {__set_bit (NETFS_SREQ_COPY_TO_CACHE , &subreq->flags); why = cachefiles_trace_read_no_data; - goto out_no_object; +if (!test_bit (NETFS_SREQ_ONDEMAND , &subreq->flags)) + goto out_no_object; }/* The object and the file may be being created in the background. */
@@ -432,7 +434,7 @@ static enum netfs_io_source cachefiles_prepare_read(struct netfs_io_subrequest * object =cachefiles_cres_object (cres); cache = object->volume->cache;cachefiles_begin_secure (cache, &saved_cred); - +retry: off =cachefiles_inject_read_error ();if (off ==0 ) off =vfs_llseek (file, subreq->start,SEEK_DATA );
@@ -483,6 +485,15 @@ static enum netfs_io_source cachefiles_prepare_read(struct netfs_io_subrequest * download_and_store:__set_bit (NETFS_SREQ_COPY_TO_CACHE , &subreq->flags); +if (test_bit (NETFS_SREQ_ONDEMAND , &subreq->flags)) { + rc =cachefiles_ondemand_read (object, subreq->start, + subreq->len); +if (!rc) { +__clear_bit (NETFS_SREQ_ONDEMAND , &subreq->flags); + goto retry; + } + ret =NETFS_INVALID_READ ; + } out:cachefiles_end_secure (cache, saved_cred); out_no_object:
fs/cachefiles/ondemand.c +77 -0
105 lines not on this page · raw message
include/linux/netfs.h +1 -0
@@ -159,6 +159,7 @@ struct netfs_io_subrequest {#define NETFS_SREQ_SHORT_IO 2/* Set if the I/O was short */ #define NETFS_SREQ_SEEK_DATA_READ 3/* Set if ->read() should SEEK_DATA first */ #define NETFS_SREQ_NO_PROGRESS 4/* Set if we didn't manage to read any data */ +#define NETFS_SREQ_ONDEMAND 5/* Set if it's from on-demand read mode */ };enum netfs_io_origin {
include/uapi/linux/cachefiles.h +17 -0
@@ -3,6 +3,7 @@#define _LINUX_CACHEFILES_H#include <linux/types.h> +#include <linux/ioctl.h> /* * 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 , };/*
@@ -48,4 +50,19 @@ struct cachefiles_open { __u8 data[]; }; +/* +* @off indicates the starting offset of the requested file range +* @len indicates the length of the requested file range +*/ +struct cachefiles_read { +__u64 off; +__u64 len; +}; + +/* +* Reply for READ request +* @arg for this ioctl is the @id field of READ request. +*/ +#define CACHEFILES_IOC_READ_COMPLETE _IOW(0x98, 1, int) +#endif
Signature · 1 line
2.27.0
PATCH v11 6/22 cachefiles: enable on-demand read mode
In reply to Jeffle XuEnable on-demand read mode by adding an optional parameter to the "bind" command.
On-demand mode will be turned on when this parameter is "ondemand", i.e. "bind ondemand". Otherwise cachefiles will work in the original mode.
--- fs/cachefiles/daemon.c | 17 ++++++++++++----- 1 file changed, 12 insertions(+), 5 deletions(-)
fs/cachefiles/daemon.c +12 -5
@@ -755,11 +755,6 @@ static int cachefiles_daemon_bind(struct cachefiles_cache *cache, char *args) cache->brun_percent >= 100)return -ERANGE ; -if (*args) { -pr_err ("'bind' command doesn't take an argument\n" ); -return -EINVAL ; - } -if (!cache->rootdirname) {pr_err ("No cache directory specified\n" );return -EINVAL ;
@@ -771,6 +766,18 @@ static int cachefiles_daemon_bind(struct cachefiles_cache *cache, char *args)return -EBUSY ; } +if (IS_ENABLED (CONFIG_CACHEFILES_ONDEMAND )) { +if (!strcmp (args,"ondemand" )) { +set_bit (CACHEFILES_ONDEMAND_MODE , &cache->flags); + }else if (*args) { +pr_err ("Invalid argument to the 'bind' command\n" ); +return -EINVAL ; + } + }else if (*args) { +pr_err ("'bind' command doesn't take an argument\n" ); +return -EINVAL ; + } +/* Make sure we have copies of the tag string */ if (!cache->tag) {/*
Signature · 1 line
2.27.0
Re: [PATCH v11 06/22] cachefiles: enable on-demand read mode
Quoted 7 lines · Jeffle Xu
Enable on-demand read mode by adding an optional parameter to the "bind" command.
On-demand mode will be turned on when this parameter is "ondemand", i.e. "bind ondemand". Otherwise cachefiles will work in the original mode.
Signed-off-by Jeffle Xu <[email protected]>
Re: [PATCH v11 06/22] cachefiles: enable on-demand read mode
Quoted 11 lines · David Howells
Quoted 7 lines · Jeffle Xu
Enable on-demand read mode by adding an optional parameter to the "bind" command.
On-demand mode will be turned on when this parameter is "ondemand", i.e. "bind ondemand". Otherwise cachefiles will work in the original mode.
Signed-off-by Jeffle Xu <[email protected]>Acked-by David Howells <[email protected]>
Thank you very much, David! Thanks for your so much time and patience!
Many thanks!
Gao Xiang
PATCH v11 7/22 cachefiles: add tracepoints for on-demand read mode
In reply to Jeffle XuAdd tracepoints for on-demand read mode. Currently following tracepoints are added:
OPEN request / COPEN reply
CLOSE request
READ request / CREAD reply
write through anonymous fd
release of anonymous fd
--- fs/cachefiles/ondemand.c | 7 ++ include/trace/events/cachefiles.h | 174 ++++++++++++++++++++++++++++++ 2 files changed, 181 insertions(+)
fs/cachefiles/ondemand.c +7 -0
@@ -30,6 +30,7 @@ static int cachefiles_ondemand_fd_release(struct inode *inode,xa_unlock (&cache->reqs);xa_erase (&cache->ondemand_ids, object_id); +trace_cachefiles_ondemand_fd_release (object, object_id);cachefiles_put_object (object, cachefiles_obj_put_ondemand_fd);cachefiles_put_unbind_pincount (cache);return 0 ;
@@ -55,6 +56,7 @@ static ssize_t cachefiles_ondemand_fd_write_iter(struct kiocb *kiocb,if (ret <0 )return ret; +trace_cachefiles_ondemand_fd_write (object,file_inode (file), pos, len); ret =__cachefiles_write (object, file, pos, iter,NULL ,NULL );if (!ret) ret = len;
@@ -93,6 +95,7 @@ static long cachefiles_ondemand_fd_ioctl(struct file *filp, unsigned int ioctl,if (!req)return -EINVAL ; +trace_cachefiles_ondemand_cread (object, id);complete (&req->done);return 0 ; }
@@ -166,6 +169,7 @@ int cachefiles_ondemand_copen(struct cachefiles_cache *cache, char *args)clear_bit (FSCACHE_COOKIE_NO_DATA_TO_READ , &cookie->flags); else set_bit(FSCACHE_COOKIE_NO_DATA_TO_READ, &cookie->flags); +trace_cachefiles_ondemand_copen (req->object, id, size); out:complete (&req->done);
@@ -213,6 +217,7 @@ static int cachefiles_ondemand_get_fd(struct cachefiles_req *req) object->ondemand_id = object_id;cachefiles_get_unbind_pincount (cache); +trace_cachefiles_ondemand_open (object, &req->msg, load);return 0 ; err_put_fd:
@@ -426,6 +431,7 @@ static int cachefiles_ondemand_init_close_req(struct cachefiles_req *req,return -ENOENT ; req->msg.object_id = object_id; +trace_cachefiles_ondemand_close (object, &req->msg);return 0 ; }
@@ -452,6 +458,7 @@ static int cachefiles_ondemand_init_read_req(struct cachefiles_req *req, req->msg.object_id = object_id; load->off = read_ctx->off; load->len = read_ctx->len; +trace_cachefiles_ondemand_read (object, &req->msg, load);return 0 ; }
include/trace/events/cachefiles.h +174 -0
180 lines not on this page · raw message
Signature · 1 line
2.27.0
PATCH v11 8/22 cachefiles: document on-demand read mode
In reply to Jeffle XuDocument new user interface introduced by on-demand read mode.
--- .../filesystems/caching/cachefiles.rst | 178 ++++++++++++++++++ 1 file changed, 178 insertions(+)
Documentation/filesystems/caching/cachefiles.rst +178 -0
@@ -28,6 +28,7 @@ Cache on Already Mounted Filesystem (*) Debugging. + (*) On-demand Read. Overview
@@ -482,3 +483,180 @@ the control file. For example:: echo $((1|4|8)) >/sys/module/cachefiles/parameters/debug will turn on all function entry debugging. + + +On-demand Read +============== + +When working in its original mode, CacheFiles serves as a local cache for a +remote networking fs - while in on-demand read mode, CacheFiles can boost the +scenario where on-demand read semantics are needed, e.g. container image +distribution. + +The essential difference between these two modes is seen when a cache miss +occurs: In the original mode, the netfs will fetch the data from the remote +server and then write it to the cache file; in on-demand read mode, fetching +the data and writing it into the cache is delegated to a user daemon. + +``CONFIG_CACHEFILES_ONDEMAND`` should be enabled to support on-demand read mode. + + +Protocol Communication +---------------------- + +The on-demand read mode uses a simple protocol for communication between kernel +and user daemon. The protocol can be modeled as:: + + kernel --[request]--> user daemon --[reply]--> kernel + +CacheFiles will send requests to the user daemon when needed. The user daemon +should poll the devnode ('/dev/cachefiles') to check if there's a pending +request to be processed. A POLLIN event will be returned when there's a pending +request. + +The user daemon then reads the devnode to fetch a request to process. It should +be noted that each read only gets one request. When it has finished processing +the request, the user daemon should write the reply to the devnode. + +Each request starts with a message header of the form:: + + struct cachefiles_msg { + __u32 msg_id; + __u32 opcode; + __u32 len; + __u32 object_id; + __u8 data[]; + }; + +where: + + * ``msg_id`` is a unique ID identifying this request among all pending + requests. + + * ``opcode`` indicates the type of this request. + + * ``object_id`` is a unique ID identifying the cache file operated on. + + * ``data`` indicates the payload of this request. + + * ``len`` indicates the whole length of this request, including the + header and following type-specific payload. + + +Turning on On-demand Mode +------------------------- + +An optional parameter becomes available to the "bind" command:: + + bind [ondemand] + +When the "bind" command is given no argument, it defaults to the original mode. +When it is given the "ondemand" argument, i.e. "bind ondemand", on-demand read +mode will be enabled. + + +The OPEN Request +---------------- + +When the netfs opens a cache file for the first time, a request with the +CACHEFILES_OP_OPEN opcode, a.k.a an OPEN request will be sent to the user +daemon. The payload format is of the form:: + + struct cachefiles_open { + __u32 volume_key_size; + __u32 cookie_key_size; + __u32 fd; + __u32 flags; + __u8 data[]; + }; + +where: + + * ``data`` contains the volume_key followed directly by the cookie_key. + The volume key is a NUL-terminated string; the cookie key is binary + data. + + * ``volume_key_size`` indicates the size of the volume key in bytes. + + * ``cookie_key_size`` indicates the size of the cookie key in bytes. + + * ``fd`` indicates an anonymous fd referring to the cache file, through + which the user daemon can perform write/llseek file operations on the + cache file. + + +The user daemon can use the given (volume_key, cookie_key) pair to distinguish +the requested cache file. With the given anonymous fd, the user daemon can +fetch the data and write it to the cache file in the background, even when +kernel has not triggered a cache miss yet. + +Be noted that each cache file has a unique object_id, while it may have multiple +anonymous fds. The user daemon may duplicate anonymous fds from the initial +anonymous fd indicated by the @fd field through dup(). Thus each object_id can +be mapped to multiple anonymous fds, while the usr daemon itself needs to +maintain the mapping. + +When implementing a user daemon, please be careful of RLIMIT_NOFILE, +``/proc/sys/fs/nr_open`` and ``/proc/sys/fs/file-max``. Typically these needn't +be huge since they're related to the number of open device blobs rather than +open files of each individual filesystem. + +The user daemon should reply the OPEN request by issuing a "copen" (complete +open) command on the devnode:: + + copen <msg_id>,<cache_size> + +where: + + * ``msg_id`` must match the msg_id field of the OPEN request. + + * When >= 0, ``cache_size`` indicates the size of the cache file; + when < 0, ``cache_size`` indicates any error code encountered by the + user daemon. + + +The CLOSE Request +----------------- + +When a cookie withdrawn, a CLOSE request (opcode CACHEFILES_OP_CLOSE) will be +sent to the user daemon. This tells the user daemon to close all anonymous fds +associated with the given object_id. The CLOSE request has no extra payload, +and shouldn't be replied. + + +The READ Request +---------------- + +When a cache miss is encountered in on-demand read mode, CacheFiles will send a +READ request (opcode CACHEFILES_OP_READ) to the user daemon. This tells the user +daemon to fetch the contents of the requested file range. The payload is of the +form:: + + struct cachefiles_read { + __u64 off; + __u64 len; + }; + +where: + + * ``off`` indicates the starting offset of the requested file range. + + * ``len`` indicates the length of the requested file range. + + +When it receives a READ request, the user daemon should fetch the requested data +and write it to the cache file identified by object_id. + +When it has finished processing the READ request, the user daemon should reply +by using the CACHEFILES_IOC_READ_COMPLETE ioctl on one of the anonymous fds +associated with the object_id given in the READ request. The ioctl is of the +form:: + + ioctl(fd, CACHEFILES_IOC_READ_COMPLETE, msg_id); + +where: + + * ``fd`` is one of the anonymous fds associated with the object_id + given. + + * ``msg_id`` must match the msg_id field of the READ request.
Signature · 1 line
2.27.0
Re: [PATCH v11 08/22] cachefiles: document on-demand read mode
Quoted 3 lines · Jeffle Xu
Document new user interface introduced by on-demand read mode.
Signed-off-by Jeffle Xu <[email protected]>
PATCH v11 9/22 erofs: make erofs_map_blocks() generally available
In reply to Jeffle Xu... so that it can be used in the following introduced fscache mode.
--- fs/erofs/data.c | 4 ++-- fs/erofs/internal.h | 2 ++ 2 files changed, 4 insertions(+), 2 deletions(-)
fs/erofs/data.c +2 -2
@@ -110,8 +110,8 @@ static int erofs_map_blocks_flatmode(struct inode *inode,return 0 ; } -static int erofs_map_blocks (struct inode *inode, -struct erofs_map_blocks *map,int flags) +int erofs_map_blocks (struct inode *inode, +struct erofs_map_blocks *map,int flags) {struct super_block *sb = inode->i_sb;struct erofs_inode *vi =EROFS_I (inode);
fs/erofs/internal.h +2 -0
@@ -486,6 +486,8 @@ void *erofs_read_metabuf(struct erofs_buf *buf, struct super_block *sb,int erofs_map_dev (struct super_block *sb,struct erofs_map_dev *dev);int erofs_fiemap (struct inode *inode,struct fiemap_extent_info *fieinfo,u64 start,u64 len); +int erofs_map_blocks (struct inode *inode, +struct erofs_map_blocks *map,int flags);/* inode.c */ static inline unsigned long erofs_inode_hash (erofs_nid_t nid)
Signature · 1 line
2.27.0
PATCH v11 10/22 erofs: add fscache mode check helper
In reply to Jeffle XuUntil then erofs is exactly blockdev based filesystem.
A new fscache-based mode is going to be introduced for erofs to support scenarios where on-demand read semantics is needed, e.g. container image distribution. In this case, erofs could be mounted from data blobs through fscache.
Add a helper checking which mode erofs works in, and twist the code in preparation for the upcoming fscache mode.
--- fs/erofs/internal.h | 5 +++++ fs/erofs/super.c | 44 +++++++++++++++++++++++++++++--------------- 2 files changed, 34 insertions(+), 15 deletions(-)
fs/erofs/internal.h +5 -0
@@ -161,6 +161,11 @@ struct erofs_sb_info {#define set_opt (opt, option) ((opt)->mount_opt |= EROFS_MOUNT_##option)#define test_opt (opt, option) ((opt)->mount_opt & EROFS_MOUNT_##option) +static inline bool erofs_is_fscache_mode (struct super_block *sb) +{ +return IS_ENABLED (CONFIG_EROFS_FS_ONDEMAND ) && !sb->s_bdev; +} +enum {EROFS_ZIP_CACHE_DISABLED ,EROFS_ZIP_CACHE_READAHEAD ,
fs/erofs/super.c +29 -15
72 lines not on this page · raw message
Signature · 1 line
2.27.0
PATCH v11 11/22 erofs: register fscache volume
In reply to Jeffle XuA new fscache based mode is going to be introduced for erofs, in which case on-demand read semantics is implemented through fscache.
As the first step, register fscache volume for each erofs filesystem. That means, data blobs can not be shared among erofs filesystems. In the following iteration, we are going to introduce the domain semantics, in which case several erofs filesystems can belong to one domain, and data blobs can be shared among these erofs filesystems of one domain.
--- fs/erofs/Kconfig | 10 ++++++++++ fs/erofs/Makefile | 1 + fs/erofs/fscache.c | 37 +++++++++++++++++++++++++++++++++++++ fs/erofs/internal.h | 16 ++++++++++++++++ fs/erofs/super.c | 5 +++++ 5 files changed, 69 insertions(+) create mode 100644 fs/erofs/fscache.c
fs/erofs/Kconfig +10 -0
@@ -98,3 +98,13 @@ config EROFS_FS_ZIP_LZMA systems will be readable without selecting this option. If unsure, say N. + +config EROFS_FS_ONDEMAND + bool "EROFS fscache-based on-demand read support" + depends on CACHEFILES_ONDEMAND && (EROFS_FS=m && FSCACHE || EROFS_FS=y && FSCACHE=y) + default n + help + This permits EROFS to use fscache-backed data blobs with on-demand + read support. + + If unsure, say N.
fs/erofs/Makefile +1 -0
@@ -5,3 +5,4 @@ erofs-objs := super.o inode.o data.o namei.o dir.o utils.o pcpubuf.o sysfs.o erofs-$(CONFIG_EROFS_FS_XATTR) += xattr.o erofs-$(CONFIG_EROFS_FS_ZIP) += decompressor.o zmap.o zdata.o erofs-$(CONFIG_EROFS_FS_ZIP_LZMA) += decompressor_lzma.o +erofs-$(CONFIG_EROFS_FS_ONDEMAND) += fscache.o
fs/erofs/fscache.c new file +37 -0
@@ -0,0 +1,37 @@ +// SPDX-License-Identifier: GPL-2.0-or-later +/* +* Copyright (C) 2022, Alibaba Cloud +*/ +#include <linux/fscache.h> +#include "internal.h" + +int erofs_fscache_register_fs (struct super_block *sb) +{ +struct erofs_sb_info *sbi =EROFS_SB (sb); +struct fscache_volume *volume; +char *name; +int ret =0 ; + + name =kasprintf (GFP_KERNEL ,"erofs,%s" , sbi->opt.fsid); +if (!name) +return -ENOMEM ; + + volume =fscache_acquire_volume (name,NULL ,NULL ,0 ); +if (IS_ERR_OR_NULL (volume)) { +erofs_err (sb,"failed to register volume for %s" , name); + ret = volume ?PTR_ERR (volume) : -EOPNOTSUPP ; + volume =NULL ; + } + + sbi->volume = volume; +kfree (name); +return ret; +} + +void erofs_fscache_unregister_fs (struct super_block *sb) +{ +struct erofs_sb_info *sbi =EROFS_SB (sb); + +fscache_relinquish_volume (sbi->volume,NULL , false); + sbi->volume =NULL ; +}
fs/erofs/internal.h +16 -0
@@ -74,6 +74,7 @@ struct erofs_mount_opts {unsigned int max_sync_decompress_pages;#endif unsigned int mount_opt; +char *fsid; };struct erofs_dev_context {
@@ -146,6 +147,9 @@ struct erofs_sb_info {/* sysfs support */ struct kobject s_kobj;/* /sys/fs/erofs/<devname> */ struct completion s_kobj_unregister; + +/* fscache support */ +struct fscache_volume *volume; };#define EROFS_SB (sb) ((struct erofs_sb_info *)(sb)->s_fs_info)
@@ -618,6 +622,18 @@ static inline int z_erofs_load_lzma_config(struct super_block *sb, }#endif /* !CONFIG_EROFS_FS_ZIP */ +/* fscache.c */ +#ifdef CONFIG_EROFS_FS_ONDEMAND +int erofs_fscache_register_fs (struct super_block *sb); +void erofs_fscache_unregister_fs (struct super_block *sb); +#else +static inline int erofs_fscache_register_fs (struct super_block *sb) +{ +return 0 ; +} +static inline void erofs_fscache_unregister_fs (struct super_block *sb) {} +#endif +#define EFSCORRUPTED EUCLEAN/* Filesystem is corrupted */ #endif /* __EROFS_INTERNAL_H */
fs/erofs/super.c +5 -0
@@ -602,6 +602,10 @@ static int erofs_fc_fill_super(struct super_block *sb, struct fs_context *fc)if (erofs_is_fscache_mode (sb)) { sb->s_blocksize =EROFS_BLKSIZ ; sb->s_blocksize_bits =LOG_BLOCK_SIZE ; + + err =erofs_fscache_register_fs (sb); +if (err) +return err; }else {if (!sb_set_blocksize (sb,EROFS_BLKSIZ )) {erofs_err (sb,"failed to set erofs blksize" );
@@ -768,6 +772,7 @@ static void erofs_kill_sb(struct super_block *sb)erofs_free_dev_context (sbi->devs);fs_put_dax (sbi->dax_dev); +erofs_fscache_unregister_fs (sb);kfree (sbi); sb->s_fs_info =NULL ; }
Signature · 1 line
2.27.0
PATCH v11 12/22 erofs: add fscache context helper functions
In reply to Jeffle XuIntroduce a context structure for managing data blobs, and helper functions for initializing and cleaning up this context structure.
--- fs/erofs/fscache.c | 41 +++++++++++++++++++++++++++++++++++++++++ fs/erofs/internal.h | 19 +++++++++++++++++++ 2 files changed, 60 insertions(+)
fs/erofs/fscache.c +41 -0
@@ -5,6 +5,47 @@#include <linux/fscache.h> #include "internal.h" +int erofs_fscache_register_cookie (struct super_block *sb, +struct erofs_fscache **fscache,char *name) +{ +struct fscache_volume *volume =EROFS_SB (sb)->volume; +struct erofs_fscache *ctx; +struct fscache_cookie *cookie; + + ctx =kzalloc (sizeof (*ctx),GFP_KERNEL ); +if (!ctx) +return -ENOMEM ; + + cookie =fscache_acquire_cookie (volume,FSCACHE_ADV_WANT_CACHE_SIZE , + name,strlen (name),NULL ,0 ,0 ); +if (!cookie) { +erofs_err (sb,"failed to get cookie for %s" , name); +kfree (name); +return -EINVAL ; + } + +fscache_use_cookie (cookie, false); + ctx->cookie = cookie; + + *fscache = ctx; +return 0 ; +} + +void erofs_fscache_unregister_cookie (struct erofs_fscache **fscache) +{ +struct erofs_fscache *ctx = *fscache; + +if (!ctx) +return ; + +fscache_unuse_cookie (ctx->cookie,NULL ,NULL ); +fscache_relinquish_cookie (ctx->cookie, false); + ctx->cookie =NULL ; + +kfree (ctx); + *fscache =NULL ; +} +int erofs_fscache_register_fs (struct super_block *sb) {struct erofs_sb_info *sbi =EROFS_SB (sb);
fs/erofs/internal.h +19 -0
@@ -97,6 +97,10 @@ struct erofs_sb_lz4_info {u16 max_pclusterblks; }; +struct erofs_fscache { +struct fscache_cookie *cookie; +}; +struct erofs_sb_info {struct erofs_mount_opts opt;/* options */ #ifdef CONFIG_EROFS_FS_ZIP
@@ -626,12 +630,27 @@ static inline int z_erofs_load_lzma_config(struct super_block *sb,#ifdef CONFIG_EROFS_FS_ONDEMAND int erofs_fscache_register_fs (struct super_block *sb);void erofs_fscache_unregister_fs (struct super_block *sb); + +int erofs_fscache_register_cookie (struct super_block *sb, +struct erofs_fscache **fscache,char *name); +void erofs_fscache_unregister_cookie (struct erofs_fscache **fscache);#else static inline int erofs_fscache_register_fs (struct super_block *sb) {return 0 ; }static inline void erofs_fscache_unregister_fs (struct super_block *sb) {} + +static inline int erofs_fscache_register_cookie (struct super_block *sb, +struct erofs_fscache **fscache, +char *name) +{ +return -EOPNOTSUPP ; +} + +static inline void erofs_fscache_unregister_cookie (struct erofs_fscache **fscache) +{ +}#endif #define EFSCORRUPTED EUCLEAN/* Filesystem is corrupted */
Signature · 1 line
2.27.0
PATCH v11 13/22 erofs: add anonymous inode caching metadata for data blobs
In reply to Jeffle XuIntroduce one anonymous inode for data blobs so that erofs can cache metadata directly within such anonymous inode.
--- fs/erofs/fscache.c | 39 ++++++++++++++++++++++++++++++++++++--- fs/erofs/internal.h | 6 ++++-- 2 files changed, 40 insertions(+), 5 deletions(-)
fs/erofs/fscache.c +36 -3
@@ -5,12 +5,17 @@#include <linux/fscache.h> #include "internal.h" +static const struct address_space_operations erofs_fscache_meta_aops = { +}; +int erofs_fscache_register_cookie (struct super_block *sb, -struct erofs_fscache **fscache,char *name) +struct erofs_fscache **fscache, +char *name,bool need_inode) {struct fscache_volume *volume =EROFS_SB (sb)->volume;struct erofs_fscache *ctx;struct fscache_cookie *cookie; +int ret; ctx =kzalloc (sizeof (*ctx),GFP_KERNEL );if (!ctx)
@@ -20,15 +25,40 @@ int erofs_fscache_register_cookie(struct super_block *sb, name, strlen(name), NULL, 0, 0);if (!cookie) {erofs_err (sb,"failed to get cookie for %s" , name); -kfree (name); -return -EINVAL ; + ret = -EINVAL ; + goto err; }fscache_use_cookie (cookie, false); ctx->cookie = cookie; +if (need_inode) { +struct inode *const inode =new_inode (sb); + +if (!inode) { +erofs_err (sb,"failed to get anon inode for %s" , name); + ret = -ENOMEM ; + goto err_cookie; + } + +set_nlink (inode,1 ); + inode->i_size =OFFSET_MAX ; + inode->i_mapping->a_ops = &erofs_fscache_meta_aops; +mapping_set_gfp_mask (inode->i_mapping,GFP_NOFS ); + + ctx->inode = inode; + } + *fscache = ctx;return 0 ; + +err_cookie: +fscache_unuse_cookie (ctx->cookie,NULL ,NULL ); +fscache_relinquish_cookie (ctx->cookie, false); + ctx->cookie =NULL ; +err: +kfree (ctx); +return ret; }void erofs_fscache_unregister_cookie (struct erofs_fscache **fscache)
@@ -42,6 +72,9 @@ void erofs_fscache_unregister_cookie(struct erofs_fscache **fscache)fscache_relinquish_cookie (ctx->cookie, false); ctx->cookie =NULL ; +iput (ctx->inode); + ctx->inode =NULL ; +kfree (ctx); *fscache =NULL ; }
fs/erofs/internal.h +4 -2
@@ -99,6 +99,7 @@ struct erofs_sb_lz4_info {struct erofs_fscache {struct fscache_cookie *cookie; +struct inode *inode; };struct erofs_sb_info {
@@ -632,7 +633,8 @@ int erofs_fscache_register_fs(struct super_block *sb);void erofs_fscache_unregister_fs (struct super_block *sb);int erofs_fscache_register_cookie (struct super_block *sb, -struct erofs_fscache **fscache,char *name); +struct erofs_fscache **fscache, +char *name,bool need_inode);void erofs_fscache_unregister_cookie (struct erofs_fscache **fscache);#else static inline int erofs_fscache_register_fs (struct super_block *sb)
@@ -643,7 +645,7 @@ static inline void erofs_fscache_unregister_fs(struct super_block *sb) {}static inline int erofs_fscache_register_cookie (struct super_block *sb,struct erofs_fscache **fscache, -char *name) +char *name,bool need_inode) {return -EOPNOTSUPP ; }
Signature · 1 line
2.27.0
PATCH v11 14/22 erofs: add erofs_fscache_read_folios() helper
In reply to Jeffle XuAdd erofs_fscache_read_folios() helper reading from fscache. It supports on-demand read semantics. That is, it will make the backend prepare for the data when cache miss. Once data ready, it will read from the cache.
This helper can then be used to implement .readpage()/.readahead() of on-demand read semantics.
--- fs/erofs/fscache.c | 54 ++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 54 insertions(+)
fs/erofs/fscache.c +54 -0
@@ -5,6 +5,60 @@#include <linux/fscache.h> #include "internal.h" +/* +* Read data from fscache and fill the read data into page cache described by +* @start/len, which shall be both aligned with PAGE_SIZE. @pstart describes +* the start physical address in the cache file. +*/ +static int erofs_fscache_read_folios (struct fscache_cookie *cookie, +struct address_space *mapping, +loff_t start,size_t len, +loff_t pstart) +{ +enum netfs_io_source source; +struct netfs_io_request rreq = {}; +struct netfs_io_subrequest subreq = { .rreq = &rreq, }; +struct netfs_cache_resources *cres = &rreq.cache_resources; +struct super_block *sb = mapping->host->i_sb; +struct iov_iter iter; +size_t done =0 ; +int ret; + + ret =fscache_begin_read_operation (cres, cookie); +if (ret) +return ret; + +while (done < len) { + subreq.start = pstart + done; + subreq.len = len - done; + subreq.flags =1 <<NETFS_SREQ_ONDEMAND ; + + source = cres->ops->prepare_read (&subreq,LLONG_MAX ); +if (WARN_ON (subreq.len ==0 )) + source =NETFS_INVALID_READ ; +if (source !=NETFS_READ_FROM_CACHE ) { +erofs_err (sb,"failed to fscache prepare_read (source %d)" , + source); + ret = -EIO ; + goto out; + } + +iov_iter_xarray (&iter,READ , &mapping->i_pages, + start + done, subreq.len); + ret =fscache_read (cres, subreq.start, &iter, +NETFS_READ_HOLE_FAIL ,NULL ,NULL ); +if (ret) { +erofs_err (sb,"failed to fscache_read (ret %d)" , ret); + goto out; + } + + done += subreq.len; + } +out: +fscache_end_operation (cres); +return ret; +} +static const struct address_space_operations erofs_fscache_meta_aops = { };
Signature · 1 line
2.27.0
PATCH v11 15/22 erofs: register fscache context for primary data blob
In reply to Jeffle XuRegisters fscache context for primary data blob. Also move the initialization of s_op and related fields forward, since anonymous inode will be allocated under the super block when registering the fscache context.
Something worth mentioning about the cleanup routine.
1. The fscache context will instantiate anonymous inodes under the super block. Release these anonymous inodes when .put_super() is called, or we'll get "VFS: Busy inodes after unmount." warning.
2. The fscache context is initialized prior to the root inode. If .kill_sb() is called when mount failed, .put_super() won't be called when root inode has not been initialized yet. Thus .kill_sb() shall also contain the cleanup routine.
--- fs/erofs/internal.h | 1 + fs/erofs/super.c | 15 +++++++++++---- 2 files changed, 12 insertions(+), 4 deletions(-)
fs/erofs/internal.h +1 -0
@@ -155,6 +155,7 @@ struct erofs_sb_info {/* fscache support */ struct fscache_volume *volume; +struct erofs_fscache *s_fscache; };#define EROFS_SB (sb) ((struct erofs_sb_info *)(sb)->s_fs_info)
fs/erofs/super.c +11 -4
@@ -589,6 +589,9 @@ static int erofs_fc_fill_super(struct super_block *sb, struct fs_context *fc)int err; sb->s_magic =EROFS_SUPER_MAGIC ; + sb->s_flags |=SB_RDONLY |SB_NOATIME ; + sb->s_maxbytes =MAX_LFS_FILESIZE ; + sb->s_op = &erofs_sops; sbi =kzalloc (sizeof (*sbi),GFP_KERNEL );if (!sbi)
@@ -606,6 +609,11 @@ static int erofs_fc_fill_super(struct super_block *sb, struct fs_context *fc) err =erofs_fscache_register_fs (sb);if (err)return err; + + err =erofs_fscache_register_cookie (sb, &sbi->s_fscache, + sbi->opt.fsid, true); +if (err) +return err; }else {if (!sb_set_blocksize (sb,EROFS_BLKSIZ )) {erofs_err (sb,"failed to set erofs blksize" );
@@ -628,11 +636,8 @@ static int erofs_fc_fill_super(struct super_block *sb, struct fs_context *fc)clear_opt (&sbi->opt,DAX_ALWAYS ); } } - sb->s_flags |=SB_RDONLY |SB_NOATIME ; - sb->s_maxbytes =MAX_LFS_FILESIZE ; - sb->s_time_gran =1 ; - sb->s_op = &erofs_sops; + sb->s_time_gran =1 ; sb->s_xattr = erofs_xattr_handlers;if (test_opt (&sbi->opt,POSIX_ACL ))
@@ -772,6 +777,7 @@ static void erofs_kill_sb(struct super_block *sb)erofs_free_dev_context (sbi->devs);fs_put_dax (sbi->dax_dev); +erofs_fscache_unregister_cookie (&sbi->s_fscache);erofs_fscache_unregister_fs (sb);kfree (sbi); sb->s_fs_info =NULL ;
@@ -790,6 +796,7 @@ static void erofs_put_super(struct super_block *sb)iput (sbi->managed_cache); sbi->managed_cache =NULL ;#endif +erofs_fscache_unregister_cookie (&sbi->s_fscache); }static struct file_system_type erofs_fs_type = {
Signature · 1 line
2.27.0
PATCH v11 16/22 erofs: register fscache context for extra data blobs
In reply to Jeffle XuSimilar to the multi device mode, erofs could be mounted from one primary data blob (mandatory) and multiple extra data blobs (optional).
Register fscache context for each extra data blob.
--- fs/erofs/data.c | 3 +++ fs/erofs/internal.h | 2 ++ fs/erofs/super.c | 8 +++++++- 3 files changed, 12 insertions(+), 1 deletion(-)
fs/erofs/data.c +3 -0
@@ -199,6 +199,7 @@ int erofs_map_dev(struct super_block *sb, struct erofs_map_dev *map) map->m_bdev = sb->s_bdev; map->m_daxdev =EROFS_SB (sb)->dax_dev; map->m_dax_part_off =EROFS_SB (sb)->dax_part_off; + map->m_fscache =EROFS_SB (sb)->s_fscache;if (map->m_deviceid) {down_read (&devs->rwsem);
@@ -210,6 +211,7 @@ int erofs_map_dev(struct super_block *sb, struct erofs_map_dev *map) map->m_bdev = dif->bdev; map->m_daxdev = dif->dax_dev; map->m_dax_part_off = dif->dax_part_off; + map->m_fscache = dif->fscache;up_read (&devs->rwsem); }else if (devs->extra_devices) {down_read (&devs->rwsem);
@@ -227,6 +229,7 @@ int erofs_map_dev(struct super_block *sb, struct erofs_map_dev *map) map->m_bdev = dif->bdev; map->m_daxdev = dif->dax_dev; map->m_dax_part_off = dif->dax_part_off; + map->m_fscache = dif->fscache;break ; } }
fs/erofs/internal.h +2 -0
@@ -49,6 +49,7 @@ typedef u32 erofs_blk_t;struct erofs_device_info {char *path; +struct erofs_fscache *fscache;struct block_device *bdev;struct dax_device *dax_dev;u64 dax_part_off;
@@ -482,6 +483,7 @@ static inline int z_erofs_map_blocks_iter(struct inode *inode,#endif /* !CONFIG_EROFS_FS_ZIP */ struct erofs_map_dev { +struct erofs_fscache *m_fscache;struct block_device *m_bdev;struct dax_device *m_daxdev;u64 m_dax_part_off;
fs/erofs/super.c +7 -1
@@ -259,7 +259,12 @@ static int erofs_init_devices(struct super_block *sb, } dis = ptr +erofs_blkoff (pos); -if (!erofs_is_fscache_mode (sb)) { +if (erofs_is_fscache_mode (sb)) { + err =erofs_fscache_register_cookie (sb, &dif->fscache, + dif->path, false); +if (err) +break ; + }else { bdev =blkdev_get_by_path (dif->path,FMODE_READ |FMODE_EXCL , sb->s_type);
@@ -710,6 +715,7 @@ static int erofs_release_device_info(int id, void *ptr, void *data)fs_put_dax (dif->dax_dev);if (dif->bdev)blkdev_put (dif->bdev,FMODE_READ |FMODE_EXCL ); +erofs_fscache_unregister_cookie (&dif->fscache);kfree (dif->path);kfree (dif);return 0 ;
Signature · 1 line
2.27.0
PATCH v11 17/22 erofs: implement fscache-based metadata read
In reply to Jeffle XuImplement the data plane of reading metadata from primary data blob over fscache.
--- fs/erofs/data.c | 19 +++++++++++++++---- fs/erofs/fscache.c | 25 +++++++++++++++++++++++++ 2 files changed, 40 insertions(+), 4 deletions(-)
fs/erofs/data.c +15 -4
@@ -6,6 +6,7 @@*/ #include "internal.h" #include <linux/prefetch.h> +#include <linux/sched/mm.h> #include <linux/dax.h> #include <trace/events/erofs.h>
@@ -35,14 +36,20 @@ void *erofs_bread(struct erofs_buf *buf, struct inode *inode,erofs_off_t offset =blknr_to_addr (blkaddr);pgoff_t index = offset >>PAGE_SHIFT ;struct page *page = buf->page; +struct folio *folio; +unsigned int nofs_flag;if (!page || page->index != index) {erofs_put_metabuf (buf); - page =read_cache_page_gfp (mapping, index, -mapping_gfp_constraint (mapping, ~__GFP_FS)); -if (IS_ERR (page)) -return page; + + nofs_flag =memalloc_nofs_save (); + folio =read_cache_folio (mapping, index,NULL ,NULL ); +memalloc_nofs_restore (nofs_flag); +if (IS_ERR (folio)) +return folio; +/* should already be PageUptodate, no need to lock page */ + page =folio_file_page (folio, index); buf->page = page; }if (buf->kmap_type ==EROFS_NO_KMAP ) {
@@ -63,6 +70,10 @@ void *erofs_bread(struct erofs_buf *buf, struct inode *inode,void *erofs_read_metabuf (struct erofs_buf *buf,struct super_block *sb,erofs_blk_t blkaddr,enum erofs_kmap_type type) { +if (erofs_is_fscache_mode (sb)) +return erofs_bread (buf,EROFS_SB (sb)->s_fscache->inode, + blkaddr, type); +return erofs_bread (buf, sb->s_bdev->bd_inode, blkaddr, type); }
fs/erofs/fscache.c +25 -0
@@ -59,7 +59,32 @@ static int erofs_fscache_read_folios(struct fscache_cookie *cookie,return ret; } +static int erofs_fscache_meta_readpage (struct file *data,struct page *page) +{ +int ret; +struct folio *folio =page_folio (page); +struct super_block *sb =folio_mapping (folio)->host->i_sb; +struct erofs_map_dev mdev = { + .m_deviceid =0 , + .m_pa =folio_pos (folio), + }; + + ret =erofs_map_dev (sb, &mdev); +if (ret) + goto out; + + ret =erofs_fscache_read_folios (mdev.m_fscache->cookie, +folio_mapping (folio),folio_pos (folio), +folio_size (folio), mdev.m_pa); +if (!ret) +folio_mark_uptodate (folio); +out: +folio_unlock (folio); +return ret; +} +static const struct address_space_operations erofs_fscache_meta_aops = { + .readpage = erofs_fscache_meta_readpage, }; int erofs_fscache_register_cookie(struct super_block *sb,
Signature · 1 line
2.27.0
PATCH v11 18/22 erofs: implement fscache-based data read for non-inline layout
In reply to Jeffle XuImplement the data plane of reading data from data blobs over fscache for non-inline layout.
--- fs/erofs/fscache.c | 51 +++++++++++++++++++++++++++++++++++++++++++++ fs/erofs/inode.c | 4 ++++ fs/erofs/internal.h | 2 ++ 3 files changed, 57 insertions(+)
fs/erofs/fscache.c +51 -0
@@ -83,10 +83,61 @@ static int erofs_fscache_meta_readpage(struct file *data, struct page *page)return ret; } +static int erofs_fscache_readpage (struct file *file,struct page *page) +{ +struct folio *folio =page_folio (page); +struct inode *inode =folio_mapping (folio)->host; +struct super_block *sb = inode->i_sb; +struct erofs_map_blocks map; +struct erofs_map_dev mdev; +erofs_off_t pos; +loff_t pstart; +int ret; + +DBG_BUGON (folio_size (folio) !=EROFS_BLKSIZ ); + + pos =folio_pos (folio); + map.m_la = pos; + + ret =erofs_map_blocks (inode, &map,EROFS_GET_BLOCKS_RAW ); +if (ret) + goto out_unlock; + +if (!(map.m_flags &EROFS_MAP_MAPPED )) { +folio_zero_range (folio,0 ,folio_size (folio)); + goto out_uptodate; + } + + mdev = (struct erofs_map_dev ) { + .m_deviceid = map.m_deviceid, + .m_pa = map.m_pa, + }; + + ret =erofs_map_dev (sb, &mdev); +if (ret) + goto out_unlock; + + pstart = mdev.m_pa + (pos - map.m_la); + ret =erofs_fscache_read_folios (mdev.m_fscache->cookie, +folio_mapping (folio),folio_pos (folio), +folio_size (folio), pstart); + +out_uptodate: +if (!ret) +folio_mark_uptodate (folio); +out_unlock: +folio_unlock (folio); +return ret; +} +static const struct address_space_operations erofs_fscache_meta_aops = { .readpage = erofs_fscache_meta_readpage, }; +const struct address_space_operations erofs_fscache_access_aops = { + .readpage = erofs_fscache_readpage, +}; +int erofs_fscache_register_cookie (struct super_block *sb,struct erofs_fscache **fscache,char *name,bool need_inode)
fs/erofs/inode.c +4 -0
@@ -297,6 +297,10 @@ static int erofs_fill_inode(struct inode *inode, int isdir) goto out_unlock; } inode->i_mapping->a_ops = &erofs_raw_access_aops; +#ifdef CONFIG_EROFS_FS_ONDEMAND +if (erofs_is_fscache_mode (inode->i_sb)) + inode->i_mapping->a_ops = &erofs_fscache_access_aops; +#endif out_unlock:erofs_put_metabuf (&buf);
fs/erofs/internal.h +2 -0
@@ -639,6 +639,8 @@ int erofs_fscache_register_cookie(struct super_block *sb,struct erofs_fscache **fscache, char *name, bool need_inode);void erofs_fscache_unregister_cookie (struct erofs_fscache **fscache); + +extern const struct address_space_operations erofs_fscache_access_aops;#else static inline int erofs_fscache_register_fs (struct super_block *sb) {
Signature · 1 line
2.27.0
PATCH v11 19/22 erofs: implement fscache-based data read for inline layout
In reply to Jeffle XuImplement the data plane of reading data from data blobs over fscache for inline layout.
For the heading non-inline part, the data plane for non-inline layout is reused, while only the tail packing part needs special handling.
--- fs/erofs/fscache.c | 32 ++++++++++++++++++++++++++++++++ 1 file changed, 32 insertions(+)
fs/erofs/fscache.c +32 -0
@@ -83,6 +83,33 @@ static int erofs_fscache_meta_readpage(struct file *data, struct page *page)return ret; } +static int erofs_fscache_readpage_inline (struct folio *folio, +struct erofs_map_blocks *map) +{ +struct super_block *sb =folio_mapping (folio)->host->i_sb; +struct erofs_buf buf = __EROFS_BUF_INITIALIZER; +erofs_blk_t blknr; +size_t offset, len; +void *src, *dst; + +/* For tail packing layout, the offset may be non-zero. */ + offset =erofs_blkoff (map->m_pa); + blknr =erofs_blknr (map->m_pa); + len = map->m_llen; + + src =erofs_read_metabuf (&buf, sb, blknr,EROFS_KMAP ); +if (IS_ERR (src)) +return PTR_ERR (src); + + dst =kmap_local_folio (folio,0 ); +memcpy (dst, src + offset, len); +memset (dst + len,0 ,PAGE_SIZE - len); +kunmap_local (dst); + +erofs_put_metabuf (&buf); +return 0 ; +} +static int erofs_fscache_readpage (struct file *file,struct page *page) {struct folio *folio =page_folio (page);
@@ -108,6 +135,11 @@ static int erofs_fscache_readpage(struct file *file, struct page *page) goto out_uptodate; } +if (map.m_flags &EROFS_MAP_META ) { + ret =erofs_fscache_readpage_inline (folio, &map); + goto out_uptodate; + } + mdev = (struct erofs_map_dev ) { .m_deviceid = map.m_deviceid, .m_pa = map.m_pa,
Signature · 1 line
2.27.0
PATCH v11 20/22 erofs: implement fscache-based data readahead
In reply to Jeffle XuImplement fscache-based data readahead. Also registers an individual bdi for each erofs instance to enable readahead.
--- fs/erofs/fscache.c | 90 ++++++++++++++++++++++++++++++++++++++++++++++ fs/erofs/super.c | 4 +++ 2 files changed, 94 insertions(+)
fs/erofs/fscache.c +90 -0
@@ -162,12 +162,102 @@ static int erofs_fscache_readpage(struct file *file, struct page *page)return ret; } +static void erofs_fscache_unlock_folios (struct readahead_control *rac, +size_t len) +{ +while (len) { +struct folio *folio =readahead_folio (rac); + + len -=folio_size (folio); +folio_mark_uptodate (folio); +folio_unlock (folio); + } +} + +static void erofs_fscache_readahead (struct readahead_control *rac) +{ +struct inode *inode = rac->mapping->host; +struct super_block *sb = inode->i_sb; +size_t len, count, done =0 ; +erofs_off_t pos; +loff_t start, offset; +int ret; + +if (!readahead_count (rac)) +return ; + + start =readahead_pos (rac); + len =readahead_length (rac); + +do { +struct erofs_map_blocks map; +struct erofs_map_dev mdev; + + pos = start + done; + map.m_la = pos; + + ret =erofs_map_blocks (inode, &map,EROFS_GET_BLOCKS_RAW ); +if (ret) +return ; + + offset = start + done; + count =min_t (size_t, map.m_llen - (pos - map.m_la), + len - done); + +if (!(map.m_flags &EROFS_MAP_MAPPED )) { +struct iov_iter iter; + +iov_iter_xarray (&iter,READ , &rac->mapping->i_pages, + offset, count); +iov_iter_zero (count, &iter); + +erofs_fscache_unlock_folios (rac, count); + ret = count; +continue ; + } + +if (map.m_flags &EROFS_MAP_META ) { +struct folio *folio =readahead_folio (rac); + + ret =erofs_fscache_readpage_inline (folio, &map); +if (!ret) { +folio_mark_uptodate (folio); + ret =folio_size (folio); + } + +folio_unlock (folio); +continue ; + } + + mdev = (struct erofs_map_dev ) { + .m_deviceid = map.m_deviceid, + .m_pa = map.m_pa, + }; + ret =erofs_map_dev (sb, &mdev); +if (ret) +return ; + + ret =erofs_fscache_read_folios (mdev.m_fscache->cookie, + rac->mapping, offset, count, + mdev.m_pa + (pos - map.m_la)); +/* +* For the error cases, the folios will be unlocked when +* .readahead() returns. +*/ +if (!ret) { +erofs_fscache_unlock_folios (rac, count); + ret = count; + } + }while (ret >0 && ((done += ret) < len)); +} +static const struct address_space_operations erofs_fscache_meta_aops = { .readpage = erofs_fscache_meta_readpage, };const struct address_space_operations erofs_fscache_access_aops = { .readpage = erofs_fscache_readpage, + .readahead = erofs_fscache_readahead, }; int erofs_fscache_register_cookie(struct super_block *sb,
fs/erofs/super.c +4 -0
@@ -619,6 +619,10 @@ static int erofs_fc_fill_super(struct super_block *sb, struct fs_context *fc) sbi->opt.fsid, true);if (err)return err; + + err =super_setup_bdi (sb); +if (err) +return err; }else {if (!sb_set_blocksize (sb,EROFS_BLKSIZ )) {erofs_err (sb,"failed to set erofs blksize" );
Signature · 1 line
2.27.0
PATCH v11 21/22 erofs: add 'fsid' mount option
In reply to Jeffle XuIntroduce 'fsid' mount option to enable on-demand read sementics, in which case, erofs will be mounted from data blobs. Users could specify the name of primary data blob by this mount option.
--- fs/erofs/super.c | 31 ++++++++++++++++++++++++++++++- fs/erofs/sysfs.c | 4 ++-- 2 files changed, 32 insertions(+), 3 deletions(-)
fs/erofs/super.c +30 -1
@@ -371,6 +371,8 @@ static int erofs_read_superblock(struct super_block *sb)if (erofs_sb_has_ztailpacking (sbi))erofs_info (sb,"EXPERIMENTAL compressed inline data feature in use. Use at your own risk!" ); +if (erofs_is_fscache_mode (sb)) +erofs_info (sb,"EXPERIMENTAL fscache-based on-demand read feature in use. Use at your own risk!" ); out:erofs_put_metabuf (&buf);return ret;
@@ -399,6 +401,7 @@ enum { Opt_dax, Opt_dax_enum, Opt_device, + Opt_fsid, Opt_err };
@@ -423,6 +426,7 @@ static const struct fs_parameter_spec erofs_fs_parameters[] = {fsparam_flag ("dax" , Opt_dax),fsparam_enum ("dax" , Opt_dax_enum, erofs_dax_param_enums),fsparam_string ("device" , Opt_device), + fsparam_string("fsid", Opt_fsid), {} };
@@ -518,6 +522,16 @@ static int erofs_fc_parse_param(struct fs_context *fc, } ++ctx->devs->extra_devices;break ; +case Opt_fsid: +#ifdef CONFIG_EROFS_FS_ONDEMAND +kfree (ctx->opt.fsid); + ctx->opt.fsid =kstrdup (param->string,GFP_KERNEL ); +if (!ctx->opt.fsid) +return -ENOMEM ; +#else +errorfc (fc,"fsid option not supported" ); +#endif +break ;default :return -ENOPARAM ; }
@@ -604,6 +618,7 @@ static int erofs_fc_fill_super(struct super_block *sb, struct fs_context *fc) sb->s_fs_info = sbi; sbi->opt = ctx->opt; + ctx->opt.fsid =NULL ; sbi->devs = ctx->devs; ctx->devs =NULL ;
@@ -690,6 +705,11 @@ static int erofs_fc_fill_super(struct super_block *sb, struct fs_context *fc)static int erofs_fc_get_tree (struct fs_context *fc) { +struct erofs_fs_context *ctx = fc->fs_private; + +if (IS_ENABLED (CONFIG_EROFS_FS_ONDEMAND ) && ctx->opt.fsid) +return get_tree_nodev (fc, erofs_fc_fill_super); +return get_tree_bdev (fc, erofs_fc_fill_super); }
@@ -739,6 +759,7 @@ static void erofs_fc_free(struct fs_context *fc)struct erofs_fs_context *ctx = fc->fs_private;erofs_free_dev_context (ctx->devs); +kfree (ctx->opt.fsid);kfree (ctx); }
@@ -779,7 +800,10 @@ static void erofs_kill_sb(struct super_block *sb)WARN_ON (sb->s_magic !=EROFS_SUPER_MAGIC ); -kill_block_super (sb); +if (erofs_is_fscache_mode (sb)) +generic_shutdown_super (sb); +else +kill_block_super (sb); sbi =EROFS_SB (sb);if (!sbi)
@@ -789,6 +813,7 @@ static void erofs_kill_sb(struct super_block *sb)fs_put_dax (sbi->dax_dev);erofs_fscache_unregister_cookie (&sbi->s_fscache);erofs_fscache_unregister_fs (sb); +kfree (sbi->opt.fsid);kfree (sbi); sb->s_fs_info =NULL ; }
@@ -938,6 +963,10 @@ static int erofs_show_options(struct seq_file *seq, struct dentry *root)seq_puts (seq,",dax=always" );if (test_opt (opt,DAX_NEVER ))seq_puts (seq,",dax=never" ); +#ifdef CONFIG_EROFS_FS_ONDEMAND +if (opt->fsid) +seq_printf (seq,",fsid=%s" , opt->fsid); +#endif return 0 ; }
fs/erofs/sysfs.c +2 -2
@@ -205,8 +205,8 @@ int erofs_register_sysfs(struct super_block *sb) sbi->s_kobj.kset = &erofs_root;init_completion (&sbi->s_kobj_unregister); - err =kobject_init_and_add (&sbi->s_kobj, &erofs_sb_ktype,NULL , -"%s" , sb->s_id); + err =kobject_init_and_add (&sbi->s_kobj, &erofs_sb_ktype,NULL ,"%s" , +erofs_is_fscache_mode (sb) ? sbi->opt.fsid : sb->s_id);if (err) goto put_sb_kobj;return 0 ;
Signature · 1 line
2.27.0
PATCH v11 22/22 erofs: change to use asynchronous io for fscache readpage/readahead
In reply to Jeffle XuFrom: Xin Yin <[email protected]>
Use asynchronous io to read data from fscache may greatly improve IO bandwidth for sequential buffer read scenario.
Change erofs_fscache_read_folios to erofs_fscache_read_folios_async, and read data from fscache asynchronously.
Make .readpage()/.readahead() to use this new helper.
--- fs/erofs/fscache.c | 239 +++++++++++++++++++++++++++++++++++++-------- 1 file changed, 199 insertions(+), 40 deletions(-)
fs/erofs/fscache.c +199 -40
@@ -5,57 +5,203 @@#include <linux/fscache.h> #include "internal.h" +static struct netfs_io_request *erofs_fscache_alloc_request (struct address_space *mapping, +loff_t start,size_t len) +{ +struct netfs_io_request *rreq; + + rreq =kzalloc (sizeof (struct netfs_io_request ),GFP_KERNEL ); +if (!rreq) +return ERR_PTR (-ENOMEM ); + + rreq->start = start; + rreq->len = len; + rreq->mapping = mapping; +INIT_LIST_HEAD (&rreq->subrequests); +refcount_set (&rreq->ref,1 ); + +return rreq; +} + +static void erofs_fscache_put_request (struct netfs_io_request *rreq) +{ +if (refcount_dec_and_test (&rreq->ref)) { +if (rreq->cache_resources.ops) + rreq->cache_resources.ops->end_operation (&rreq->cache_resources); +kfree (rreq); + } +} + +static void erofs_fscache_put_subrequest (struct netfs_io_subrequest *subreq) +{ +if (refcount_dec_and_test (&subreq->ref)) { +erofs_fscache_put_request (subreq->rreq); +kfree (subreq); + } +} + +static void erofs_fscache_clear_subrequests (struct netfs_io_request *rreq) +{ +struct netfs_io_subrequest *subreq; + +while (!list_empty (&rreq->subrequests)) { + subreq =list_first_entry (&rreq->subrequests, + struct netfs_io_subrequest, rreq_link); +list_del (&subreq->rreq_link); +erofs_fscache_put_subrequest (subreq); + } +} + +static void erofs_fscache_rreq_unlock_folios (struct netfs_io_request *rreq) +{ +struct netfs_io_subrequest *subreq; +struct folio *folio; +unsigned int iopos; +pgoff_t start_page = rreq->start /PAGE_SIZE ; +pgoff_t last_page = ((rreq->start + rreq->len) /PAGE_SIZE ) -1 ; +bool subreq_failed = false; + +XA_STATE (xas, &rreq->mapping->i_pages, start_page); + + subreq =list_first_entry (&rreq->subrequests, + struct netfs_io_subrequest, rreq_link); + iopos =0 ; + subreq_failed = (subreq->error <0 ); + +rcu_read_lock (); +xas_for_each (&xas, folio, last_page) { +unsigned int pgpos = (folio_index (folio) - start_page) *PAGE_SIZE ; +unsigned int pgend = pgpos +folio_size (folio); +bool pg_failed = false; + +for (;;) { +if (!subreq) { + pg_failed = true; +break ; + } + + pg_failed |= subreq_failed; +if (pgend < iopos + subreq->len) +break ; + + iopos += subreq->len; +if (!list_is_last (&subreq->rreq_link, &rreq->subrequests)) { + subreq =list_next_entry (subreq, rreq_link); + subreq_failed = (subreq->error <0 ); + }else { + subreq =NULL ; + subreq_failed = false; + } +if (pgend == iopos) +break ; + } + +if (!pg_failed) +folio_mark_uptodate (folio); + +folio_unlock (folio); + } +rcu_read_unlock (); +} + +static void erofs_fscache_rreq_complete (struct netfs_io_request *rreq) +{ +erofs_fscache_rreq_unlock_folios (rreq); +erofs_fscache_clear_subrequests (rreq); +erofs_fscache_put_request (rreq); +} + +static void erofc_fscache_subreq_complete (void *priv, +ssize_t transferred_or_error,bool was_async) +{ +struct netfs_io_subrequest *subreq = priv; +struct netfs_io_request *rreq = subreq->rreq; + +if (IS_ERR_VALUE (transferred_or_error)) + subreq->error = transferred_or_error; + +if (atomic_dec_and_test (&rreq->nr_outstanding)) +erofs_fscache_rreq_complete (rreq); + +erofs_fscache_put_subrequest (subreq); +} +/* * Read data from fscache and fill the read data into page cache described by -* @start/len, which shall be both aligned with PAGE_SIZE. @pstart describes +* @rreq, which shall be both aligned with PAGE_SIZE. @pstart describes * the start physical address in the cache file. */ -static int erofs_fscache_read_folios (struct fscache_cookie *cookie, -struct address_space *mapping, -loff_t start,size_t len, +static int erofs_fscache_read_folios_async (struct fscache_cookie *cookie, +struct netfs_io_request *rreq,loff_t pstart) {enum netfs_io_source source; -struct netfs_io_request rreq = {}; -struct netfs_io_subrequest subreq = { .rreq = &rreq, }; -struct netfs_cache_resources *cres = &rreq.cache_resources; -struct super_block *sb = mapping->host->i_sb; +struct super_block *sb = rreq->mapping->host->i_sb; +struct netfs_io_subrequest *subreq; +struct netfs_cache_resources *cres = &rreq->cache_resources;struct iov_iter iter; +loff_t start = rreq->start; +size_t len = rreq->len;size_t done =0 ;int ret; +atomic_set (&rreq->nr_outstanding,1 ); + ret =fscache_begin_read_operation (cres, cookie);if (ret) -return ret; + goto out;while (done < len) { - subreq.start = pstart + done; - subreq.len = len - done; - subreq.flags =1 <<NETFS_SREQ_ONDEMAND ; + subreq =kzalloc (sizeof (struct netfs_io_subrequest ),GFP_KERNEL ); +if (subreq) { +INIT_LIST_HEAD (&subreq->rreq_link); +refcount_set (&subreq->ref,2 ); + subreq->rreq = rreq; +refcount_inc (&rreq->ref); + }else { + ret = -ENOMEM ; + goto out; + } + + subreq->start = pstart + done; + subreq->len = len - done; + subreq->flags =1 <<NETFS_SREQ_ONDEMAND ; - source = cres->ops->prepare_read (&subreq,LLONG_MAX ); -if (WARN_ON (subreq.len ==0 )) +list_add_tail (&subreq->rreq_link, &rreq->subrequests); + + source = cres->ops->prepare_read (subreq,LLONG_MAX ); +if (WARN_ON (subreq->len ==0 )) source =NETFS_INVALID_READ ;if (source !=NETFS_READ_FROM_CACHE ) {erofs_err (sb,"failed to fscache prepare_read (source %d)" , source); ret = -EIO ; + subreq->error = ret; +erofs_fscache_put_subrequest (subreq); goto out; } -iov_iter_xarray (&iter,READ , &mapping->i_pages, - start + done, subreq.len); - ret =fscache_read (cres, subreq.start, &iter, -NETFS_READ_HOLE_FAIL ,NULL ,NULL ); +atomic_inc (&rreq->nr_outstanding); + +iov_iter_xarray (&iter,READ , &rreq->mapping->i_pages, + start + done, subreq->len); + + ret =fscache_read (cres, subreq->start, &iter, +NETFS_READ_HOLE_FAIL , + erofc_fscache_subreq_complete, subreq); +if (ret == -EIOCBQUEUED ) + ret =0 ;if (ret) {erofs_err (sb,"failed to fscache_read (ret %d)" , ret); goto out; } - done += subreq.len; + done += subreq->len; } out: -fscache_end_operation (cres); +if (atomic_dec_and_test (&rreq->nr_outstanding)) +erofs_fscache_rreq_complete (rreq); +return ret; }
@@ -64,6 +210,7 @@ static int erofs_fscache_meta_readpage(struct file *data, struct page *page)int ret;struct folio *folio =page_folio (page);struct super_block *sb =folio_mapping (folio)->host->i_sb; +struct netfs_io_request *rreq;struct erofs_map_dev mdev = { .m_deviceid =0 , .m_pa =folio_pos (folio),
@@ -73,11 +220,13 @@ static int erofs_fscache_meta_readpage(struct file *data, struct page *page)if (ret) goto out; - ret =erofs_fscache_read_folios (mdev.m_fscache->cookie, -folio_mapping (folio),folio_pos (folio), -folio_size (folio), mdev.m_pa); -if (!ret) -folio_mark_uptodate (folio); + rreq =erofs_fscache_alloc_request (folio_mapping (folio), +folio_pos (folio),folio_size (folio)); +if (IS_ERR (rreq)) + goto out; + +return erofs_fscache_read_folios_async (mdev.m_fscache->cookie, + rreq, mdev.m_pa); out:folio_unlock (folio);return ret;
@@ -117,6 +266,7 @@ static int erofs_fscache_readpage(struct file *file, struct page *page)struct super_block *sb = inode->i_sb;struct erofs_map_blocks map;struct erofs_map_dev mdev; +struct netfs_io_request *rreq;erofs_off_t pos;loff_t pstart;int ret;
@@ -149,10 +299,15 @@ static int erofs_fscache_readpage(struct file *file, struct page *page)if (ret) goto out_unlock; + + rreq =erofs_fscache_alloc_request (folio_mapping (folio), +folio_pos (folio),folio_size (folio)); +if (IS_ERR (rreq)) + goto out_unlock; + pstart = mdev.m_pa + (pos - map.m_la); - ret =erofs_fscache_read_folios (mdev.m_fscache->cookie, -folio_mapping (folio),folio_pos (folio), -folio_size (folio), pstart); +return erofs_fscache_read_folios_async (mdev.m_fscache->cookie, + rreq, pstart); out_uptodate:if (!ret)
@@ -162,15 +317,16 @@ static int erofs_fscache_readpage(struct file *file, struct page *page)return ret; } -static void erofs_fscache_unlock_folios (struct readahead_control *rac, -size_t len) +static void erofs_fscache_advance_folios (struct readahead_control *rac, +size_t len,bool unlock) {while (len) {struct folio *folio =readahead_folio (rac); - len -=folio_size (folio); -folio_mark_uptodate (folio); -folio_unlock (folio); +if (unlock) { +folio_mark_uptodate (folio); +folio_unlock (folio); + } } }
@@ -192,6 +348,7 @@ static void erofs_fscache_readahead(struct readahead_control *rac) do { struct erofs_map_blocks map; struct erofs_map_dev mdev; + struct netfs_io_request *rreq; pos = start + done; map.m_la = pos;
@@ -211,7 +368,7 @@ static void erofs_fscache_readahead(struct readahead_control *rac) offset, count);iov_iter_zero (count, &iter); -erofs_fscache_unlock_folios (rac, count); +erofs_fscache_advance_folios (rac, count, true); ret = count;continue ; }
@@ -237,15 +394,17 @@ static void erofs_fscache_readahead(struct readahead_control *rac)if (ret)return ; - ret =erofs_fscache_read_folios (mdev.m_fscache->cookie, - rac->mapping, offset, count, - mdev.m_pa + (pos - map.m_la)); + rreq =erofs_fscache_alloc_request (rac->mapping, offset, count); +if (IS_ERR (rreq)) +return ;/* -* For the error cases, the folios will be unlocked when -* .readahead() returns. +* Drop the ref of folios here. Unlock them in +* rreq_unlock_folios() when rreq complete. */ +erofs_fscache_advance_folios (rac, count, false); + ret =erofs_fscache_read_folios_async (mdev.m_fscache->cookie, + rreq, mdev.m_pa + (pos - map.m_la));if (!ret) { -erofs_fscache_unlock_folios (rac, count); ret = count; } }while (ret >0 && ((done += ret) < len));
Signature · 1 line
2.27.0
Re: [PATCH v11 00/22] fscache, erofs: fscache-based on-demand read semantics
In reply to Jeffle XuQuoted 236 lines · Jeffle Xu
changes since v10:
- rebase to 5.18-rc5
- append the patchset with a patch from Xin Yin, implementing the asynchronous readahead (patch 22)Kernel Patchset
---------------
Git tree:https://github.com/lostjeffle/linux.git jingbo/dev-erofs-fscache-v11
Gitweb:
https://github.com/lostjeffle/linux/commits/jingbo/dev-erofs-fscache-v11
User Guide for E2E Container Use Case
-------------------------------------
User guide:https://github.com/dragonflyoss/image-service/blob/fscache/docs/nydus-fscache.md
Video:
https://youtu.be/F4IF2_DENXoUser Daemon for Quick Test
--------------------------
Git tree:https://github.com/lostjeffle/demand-read-cachefilesd.git main
Gitweb:
https://github.com/lostjeffle/demand-read-cachefilesd
Tested-by Zichen Tian <[email protected]>Tested-by Jia Zhu <[email protected]>RFC: https://lore.kernel.org/all/[email protected]/t/
v1: https://lore.kernel.org/lkml/[email protected]/T/
v2: https://lore.kernel.org/all/[email protected]/t/
v3: https://lore.kernel.org/lkml/[email protected]/T/
v4: https://lore.kernel.org/lkml/[email protected]/T/#t
v5: https://lore.kernel.org/lkml/[email protected]/T/
v6: https://lore.kernel.org/lkml/[email protected]/T/
v7: https://lore.kernel.org/lkml/[email protected]/T/
v8: https://lore.kernel.org/all/[email protected]/T/
v9: https://lore.kernel.org/lkml/[email protected]/T/
v10:https://lore.kernel.org/all/[email protected]/t/[Background]
============
Nydus [1] is an image distribution service especially optimized for distribution over network. Nydus is an excellent container image acceleration solution, since it only pulls data from remote when needed, a.k.a. on-demand reading and it also supports chunk-based deduplication, compression, etc.erofs (Enhanced Read-Only File System) is a filesystem designed for read-only scenarios. (
Documentation/filesystem/erofs.rst)Over the past months we've been focusing on supporting Nydus image service with in-kernel erofs format[2]. In that case, each container image will be organized in one bootstrap (metadata) and (optional) multiple data blobs in erofs format. Massive container images will be stored on one machine.
To accelerate the container startup (fetching container images from remote and then start the container), we do hope that the bootstrap & blob files could support on-demand read. That is, erofs can be mounted and accessed even when the bootstrap/data blob files have not been fully downloaded. Then it'll have native performance after data is available locally.
That means we have to manage the cache state of the bootstrap/data blob files (if cache hit, read directly from the local cache; if cache miss, fetch the data somehow). It would be painful and may be dumb for erofs to implement the cache management itself. Thus we prefer fscache/cachefiles to do the cache management instead.
The fscache on-demand read feature aims to be implemented in a generic way so that it can benefit other use cases and/or filesystems if it's implemented in the fscache subsystem.
[1] https://nydus.dev
[2] https://sched.co/pcdL[Overall Design]
================
Please refer to patch 7 ("cachefiles: document on-demand read mode") for more details.When working in the original mode, cachefiles mainly serves as a local cache for remote networking fs, while in on-demand read mode, cachefiles can work in the scenario where on-demand read semantics is needed, e.g. container image distribution.
The essential difference between these two modes is that, in original mode, when cache miss, netfs itself will fetch data from remote, and then write the fetched data into cache file. While in on-demand read mode, a user daemon is responsible for fetching data and then feeds to the kernel fscache side.
The on-demand read mode relies on a simple protocol used for communication between kernel and user daemon.
The proposed implementation relies on the anonymous fd mechanism to avoid the dependence on the format of cache file. When a fscache cachefile is opened for the first time, an
anon_fdassociated with the cache file is sent to the user daemon. With the givenanon_fd, user daemon could fetch and write data into the cache file in the background, even when kernel has not triggered the cache miss. Besides, thewrite()syscall to theanon_fdwill finally call cachefiles kernel module, which will write data to cache file in the latest format of cache file.1. cache miss
When cache miss, cachefiles kernel module will notify user daemon with theanon_fd, along with the requested file range. When notified, user daemon needs to fetch data of the requested file range, and then write the fetched data into cache file with the given anonymous fd. When finished processing the request, user daemon needs to notify the kernel.After notifying the user daemon, the kernel read routine will wait there, until the request is handled by user daemon. When it's awaken by the notification from user daemon, i.e. the corresponding hole has been filled by the user daemon, it will retry to read from the same file range.
2. cache hit
Once data is already ready in cache file, netfs will read from cache file directly.[Advantage of fscache-based on-demand read]
========================================
1. Asynchronous prefetch
In current mechanism, fscache is responsible for cache state management, while the data plane (fetching data from local/remote on cache miss) is done on the user daemon side even without any file system request driven. In addition, if cached data has already been available locally, fscache will use it instead of trapping to user space anymore.Therefore, different from event-driven approaches, the fscache on-demand user daemon could also fetch data (from remote) asynchronously in the background just like most multi-threaded HTTP downloaders.
2. Flexible request amplification
Since the data plane can be independently controlled by the user daemon, the user daemon can also fetch more data from remote than that the file system actually requests for small I/O sizes. Then, fetched data in bulk will be available at once and fscache won't be trapped into the user daemon again.3. Support massive blobs
This mechanism can naturally support a large amount of backing files, and thus can benefit the densely employed scenarios. In our use cases, one container image can be formed of one bootstrap (required) and multiple chunk-deduplicated data blobs (optional).For example, one container image for node.js will correspond to ~20 files in total. In densely employed environment, there could be hundreds of containers and thus thousands of backing files on one machine.
[Following Steps]
=================
The following improvements are on our TODO list, and will be formed in shape with the development process:- Data blobs can be shared between multiple filesystems. Whilst in the current implementation, each filesystem registers a unique
fscache_volume, causing the backing file for the data blob can not be shared between different erofs filesystems. Later we need to introduce shared domain in order to sharefscache_volume, so that data blobs can be shared between container images to some degree.- in-memory extent-based data sharing, e.g., different files can share the same chunk of the data blob. In the current implementation, each erofs file maintains its own page cache, thus the page caches for the same chunk may be duplicated among multiple files sharing the same chunk.
- other useful features, including multiple cachefiles daemon support, etc.
Jeffle Xu (21):
cachefiles: extract write routine
cachefiles: notify the user daemon when looking up cookie
cachefiles: unbind cachefiles gracefully in on-demand mode
cachefiles: notify the user daemon when withdrawing cookie
cachefiles: implement on-demand read
cachefiles: enable on-demand read mode
cachefiles: add tracepoints for on-demand read mode
cachefiles: document on-demand read mode
erofs: makeerofs_map_blocks()generally available
erofs: add fscache mode check helper
erofs: register fscache volume
erofs: add fscache context helper functions
erofs: add anonymous inode caching metadata for data blobs
erofs: adderofs_fscache_read_folios()helper
erofs: register fscache context for primary data blob
erofs: register fscache context for extra data blobs
erofs: implement fscache-based metadata read
erofs: implement fscache-based data read for non-inline layout
erofs: implement fscache-based data read for inline layout
erofs: implement fscache-based data readahead
erofs: add 'fsid' mount optionXin Yin (1):
erofs: change to use asynchronous io for fscache readpage/readahead.../filesystems/caching/cachefiles.rst | 178 ++++++ fs/cachefiles/Kconfig | 12 + fs/cachefiles/Makefile | 1 + fs/cachefiles/daemon.c | 117 +++- fs/cachefiles/interface.c | 2 + fs/cachefiles/internal.h | 78 +++ fs/cachefiles/io.c | 76 ++- fs/cachefiles/namei.c | 16 +- fs/cachefiles/ondemand.c | 503 +++++++++++++++++ fs/erofs/Kconfig | 10 + fs/erofs/Makefile | 1 + fs/erofs/data.c | 26 +- fs/erofs/fscache.c | 522 ++++++++++++++++++ fs/erofs/inode.c | 4 + fs/erofs/internal.h | 49 ++ fs/erofs/super.c | 105 +++- fs/erofs/sysfs.c | 4 +- include/linux/fscache.h | 1 + include/linux/netfs.h | 1 + include/trace/events/cachefiles.h | 176 ++++++ include/uapi/linux/cachefiles.h | 68 +++ 21 files changed, 1871 insertions(+), 79 deletions(-)
Quoted 4 lines
create mode 100644
fs/cachefiles/ondemand.ccreate mode 100644fs/erofs/fscache.ccreate mode 100644include/uapi/linux/cachefiles.h
Hi Jeffle & Xiang,
We started the container using containerd with the daemon and snapshotter in userspace, which give us a performance boost based on fscache compared to the fuse based solution, hope this patch will be an upstream feature, we are considering using it in our production soon.
Thanks,
Song
Quoted 202 lines · Jeffle Xu
changes since v10:
- rebase to 5.18-rc5
- append the patchset with a patch from Xin Yin, implementing the asynchronous readahead (patch 22)Kernel Patchset
---------------
Git tree:https://github.com/lostjeffle/linux.git jingbo/dev-erofs-fscache-v11
Gitweb:
https://github.com/lostjeffle/linux/commits/jingbo/dev-erofs-fscache-v11
User Guide for E2E Container Use Case
-------------------------------------
User guide:https://github.com/dragonflyoss/image-service/blob/fscache/docs/nydus-fscache.md
Video:
https://youtu.be/F4IF2_DENXoUser Daemon for Quick Test
--------------------------
Git tree:https://github.com/lostjeffle/demand-read-cachefilesd.git main
Gitweb:
https://github.com/lostjeffle/demand-read-cachefilesd
Tested-by Zichen Tian <[email protected]>Tested-by Jia Zhu <[email protected]>RFC: https://lore.kernel.org/all/[email protected]/t/
v1: https://lore.kernel.org/lkml/[email protected]/T/
v2: https://lore.kernel.org/all/[email protected]/t/
v3: https://lore.kernel.org/lkml/[email protected]/T/
v4: https://lore.kernel.org/lkml/[email protected]/T/#t
v5: https://lore.kernel.org/lkml/[email protected]/T/
v6: https://lore.kernel.org/lkml/[email protected]/T/
v7: https://lore.kernel.org/lkml/[email protected]/T/
v8: https://lore.kernel.org/all/[email protected]/T/
v9: https://lore.kernel.org/lkml/[email protected]/T/
v10:https://lore.kernel.org/all/[email protected]/t/[Background]
============
Nydus [1] is an image distribution service especially optimized for distribution over network. Nydus is an excellent container image acceleration solution, since it only pulls data from remote when needed, a.k.a. on-demand reading and it also supports chunk-based deduplication, compression, etc.erofs (Enhanced Read-Only File System) is a filesystem designed for read-only scenarios. (
Documentation/filesystem/erofs.rst)Over the past months we've been focusing on supporting Nydus image service with in-kernel erofs format[2]. In that case, each container image will be organized in one bootstrap (metadata) and (optional) multiple data blobs in erofs format. Massive container images will be stored on one machine.
To accelerate the container startup (fetching container images from remote and then start the container), we do hope that the bootstrap & blob files could support on-demand read. That is, erofs can be mounted and accessed even when the bootstrap/data blob files have not been fully downloaded. Then it'll have native performance after data is available locally.
That means we have to manage the cache state of the bootstrap/data blob files (if cache hit, read directly from the local cache; if cache miss, fetch the data somehow). It would be painful and may be dumb for erofs to implement the cache management itself. Thus we prefer fscache/cachefiles to do the cache management instead.
The fscache on-demand read feature aims to be implemented in a generic way so that it can benefit other use cases and/or filesystems if it's implemented in the fscache subsystem.
[1] https://nydus.dev
[2] https://sched.co/pcdL[Overall Design]
================
Please refer to patch 7 ("cachefiles: document on-demand read mode") for more details.When working in the original mode, cachefiles mainly serves as a local cache for remote networking fs, while in on-demand read mode, cachefiles can work in the scenario where on-demand read semantics is needed, e.g. container image distribution.
The essential difference between these two modes is that, in original mode, when cache miss, netfs itself will fetch data from remote, and then write the fetched data into cache file. While in on-demand read mode, a user daemon is responsible for fetching data and then feeds to the kernel fscache side.
The on-demand read mode relies on a simple protocol used for communication between kernel and user daemon.
The proposed implementation relies on the anonymous fd mechanism to avoid the dependence on the format of cache file. When a fscache cachefile is opened for the first time, an
anon_fdassociated with the cache file is sent to the user daemon. With the givenanon_fd, user daemon could fetch and write data into the cache file in the background, even when kernel has not triggered the cache miss. Besides, thewrite()syscall to theanon_fdwill finally call cachefiles kernel module, which will write data to cache file in the latest format of cache file.1. cache miss
When cache miss, cachefiles kernel module will notify user daemon with theanon_fd, along with the requested file range. When notified, user daemon needs to fetch data of the requested file range, and then write the fetched data into cache file with the given anonymous fd. When finished processing the request, user daemon needs to notify the kernel.After notifying the user daemon, the kernel read routine will wait there, until the request is handled by user daemon. When it's awaken by the notification from user daemon, i.e. the corresponding hole has been filled by the user daemon, it will retry to read from the same file range.
2. cache hit
Once data is already ready in cache file, netfs will read from cache file directly.[Advantage of fscache-based on-demand read]
========================================
1. Asynchronous prefetch
In current mechanism, fscache is responsible for cache state management, while the data plane (fetching data from local/remote on cache miss) is done on the user daemon side even without any file system request driven. In addition, if cached data has already been available locally, fscache will use it instead of trapping to user space anymore.Therefore, different from event-driven approaches, the fscache on-demand user daemon could also fetch data (from remote) asynchronously in the background just like most multi-threaded HTTP downloaders.
2. Flexible request amplification
Since the data plane can be independently controlled by the user daemon, the user daemon can also fetch more data from remote than that the file system actually requests for small I/O sizes. Then, fetched data in bulk will be available at once and fscache won't be trapped into the user daemon again.3. Support massive blobs
This mechanism can naturally support a large amount of backing files, and thus can benefit the densely employed scenarios. In our use cases, one container image can be formed of one bootstrap (required) and multiple chunk-deduplicated data blobs (optional).For example, one container image for node.js will correspond to ~20 files in total. In densely employed environment, there could be hundreds of containers and thus thousands of backing files on one machine.
[Following Steps]
=================
The following improvements are on our TODO list, and will be formed in shape with the development process:- Data blobs can be shared between multiple filesystems. Whilst in the current implementation, each filesystem registers a unique
fscache_volume, causing the backing file for the data blob can not be shared between different erofs filesystems. Later we need to introduce shared domain in order to sharefscache_volume, so that data blobs can be shared between container images to some degree.- in-memory extent-based data sharing, e.g., different files can share the same chunk of the data blob. In the current implementation, each erofs file maintains its own page cache, thus the page caches for the same chunk may be duplicated among multiple files sharing the same chunk.
- other useful features, including multiple cachefiles daemon support, etc.
Jeffle Xu (21):
cachefiles: extract write routine
cachefiles: notify the user daemon when looking up cookie
cachefiles: unbind cachefiles gracefully in on-demand mode
cachefiles: notify the user daemon when withdrawing cookie
cachefiles: implement on-demand read
cachefiles: enable on-demand read mode
cachefiles: add tracepoints for on-demand read mode
cachefiles: document on-demand read mode
erofs: makeerofs_map_blocks()generally available
erofs: add fscache mode check helper
erofs: register fscache volume
erofs: add fscache context helper functions
erofs: add anonymous inode caching metadata for data blobs
Quoted 8 lines
erofs: add
erofs_fscache_read_folios()helper
erofs: register fscache context for primary data blob
erofs: register fscache context for extra data blobs
erofs: implement fscache-based metadata read
erofs: implement fscache-based data read for non-inline layout
erofs: implement fscache-based data read for inline layout
erofs: implement fscache-based data readahead
erofs: add 'fsid' mount option
For erofs parts:
Thanks,
Quoted 30 lines
Xin Yin (1):
erofs: change to use asynchronous io for fscache readpage/readahead.../filesystems/caching/cachefiles.rst | 178 ++++++ fs/cachefiles/Kconfig | 12 + fs/cachefiles/Makefile | 1 + fs/cachefiles/daemon.c | 117 +++- fs/cachefiles/interface.c | 2 + fs/cachefiles/internal.h | 78 +++ fs/cachefiles/io.c | 76 ++- fs/cachefiles/namei.c | 16 +- fs/cachefiles/ondemand.c | 503 +++++++++++++++++ fs/erofs/Kconfig | 10 + fs/erofs/Makefile | 1 + fs/erofs/data.c | 26 +- fs/erofs/fscache.c | 522 ++++++++++++++++++ fs/erofs/inode.c | 4 + fs/erofs/internal.h | 49 ++ fs/erofs/super.c | 105 +++- fs/erofs/sysfs.c | 4 +- include/linux/fscache.h | 1 + include/linux/netfs.h | 1 + include/trace/events/cachefiles.h | 176 ++++++ include/uapi/linux/cachefiles.h | 68 +++ 21 files changed, 1871 insertions(+), 79 deletions(-)create mode 100644
fs/cachefiles/ondemand.ccreate mode 100644fs/erofs/fscache.ccreate mode 100644include/uapi/linux/cachefiles.h