fscache,erofs: fscache-based demand-read semantics
Subsystems (5)
v2 cover fscache,erofs: fscache-based demand-read semantics
changes since v1:
- rebase to v5.17
- erofs: In chunk based layout, since the logical file offset has the same remainder over PAGE_SIZE with the corresponding physical address inside the data blob file, the file page cache can be directly transferred to netfs library to contain the data from data blob file. (patch 15) (Gao Xiang)
- netfs,cachefiles: manage logical/physical offset separately. (patch 2) (It is used by erofs_begin_cache_operation() in patch 15.)
- cachefiles: introduce a new devnode specificaly for on-demand reading. (patch 6)
- netfs,fscache,cachefiles: add new CONFIG_* for on-demand reading. (patch 3/5)
- You could start a quick test by
https://github.com/lostjeffle/demand-read-cachefilesd
- add more background information (mainly introduction to nydus) in the "Background" part of this cover letter
[Important Issues]
The following issues still need further discussion. Thanks for your time and patience.
1. I noticed that there's refactoring of netfs library[1], and patch 1 is not needed since [2].
2. The current implementation will severely conflict with the refactoring of netfs library[1][2]. The assumption of 'struct netfs_i_context' [2] is that, every file in the upper netfs will correspond to only one backing file. While in our scenario, one file in erofs can correspond to multiple backing files. That is, the content of one file can be divided into multiple chunks, and are distrubuted over multiple blob files, i.e. multiple backing files. Currently I have no good idea solving this conflic.
Besides there are still two quetions:
- What's the plan of [1]? When is it planned to be merged?
- It seems that all upper fs using fscache is going to use netfs API, while the APIs like fscache_read_or_alloc_page() are deprecated. Is that true?
[1] https://git.kernel.org/pub/scm/linux/kernel/git/dhowells/linux-fs.git/log/?h=netfs-lib
[2] https://git.kernel.org/pub/scm/linux/kernel/git/dhowells/linux-fs.git/commit/?h=netfs-lib&id=087d913752522fb9aa6d3effdb9a8c7908c779dd
RFC: https://lore.kernel.org/all/[email protected]/t/
v1: https://lore.kernel.org/lkml/[email protected]/T/
[Background]
============
Nydus is a remote container snapthotter specially optimised for container images distribution over network. It has recently been accepted as a sub-project of containerd[1]. Nydus is an excellent container image acceleration solution, since it only pulls data from remote when it's really needed, a.k.a. on-demand reading.
erofs (Enhanced Read-Only File System) is a filesystem specially optimised for read-only scenarios. (Documentation/filesystem/erofs.rst)
Recently we are focusing on erofs in container images distribution scenario [2], trying to combine it with nydus. In this case, erofs can be mounted from one bootstrap file (metadata) with (optional) multiple data blob files (data) stored on another local filesystem. (All these files are actually image files in erofs disk format.)
To accelerate the container startup (fetching container image from remote and then start the container), we do hope that the bootstrap blob file could support demand read. That is, erofs can be mounted and accessed even when the bootstrap/data blob files have not been fully downloaded.
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. Besides, the demand-read feature shall be general and it can benefit other using scenarios if it can be implemented in fscache level.
[1] https://d7y.io/en-us/blog/containerd_accepted_nydus-snapshotter.html
[2] https://sched.co/pcdL
[Overall Design]
================
The upper fs uses a backing file on the local fs as the local cache (exactly the "cachefiles" way), and relies on fscache to detect if data is ready or not (cache hit/miss). Since currently fscache detects cache hit/miss by detecting the hole of the backing files, our demand-read mechanism also relies on the hole detecting.
1. initial phase
On the first beginning, the user daemon will touch the backing files (bootstrap/data blob files) under corresponding directory (under <root>/cache/<volume>/<fan>/) in advance. These backing files are completely sparse files (with zero disk usage). Since these backing files are all read-only and the file size is known prior mounting, user daemon will set corresponding file size and thus create all these sparse backing files in advance.
2. cache miss
When a file range (of bootstrap/data blob file) is accessed for the first time, a cache miss will be triggered and then .issue_op() will be called to fetch the data somehow.
In the demand-read case, we relies on a user daemon to fetch the data from local/remote. In this case, .issue_op() just packages the file range into a message and informs the user daemon. User daemon needs to poll and wait on the devnode (/dev/cachefiles_demand). Once awaken, the user daemon will read the devnode to get the file range information, and then fetch the data corresponding to the file range somehow, e.g. download from remote through network. Once data ready, the user daemon will write the fetched data into the backing file and then inform cachefiles backend by writing to the devnode. Cachefiles backend getting blocked on the previous .issue_op() calling will be awaken then. By then the data has been ready in the backing file, and the netfs API will re-initiate a read request from the backing file.
3. cache hit
Once data is already ready in the backing file, netfs API will read from the backing file directly.
[Advantage of fscache-based demand-read]
========================================
1. Asynchronous Prefetch
In current mechanism, fscache is responsible for cache state management, while the data plane (fetch data from local/remote on cache miss) is done on the user daemon side.
If data has already been ready in the backing file, netfs API will read from the backing file directly and won't be trapped to user space anymore. Thus the user daemon could fetch data (from remote) asynchronously on the background, and thus accelerate the backing file accessing in some degree.
2. Support massive blob files
Besides this mechanism supports a large amount of backing files, and thus can benefit the densely employed scenario.
In our using scenario, one container image can correspond to one bootstrap file (required) and multiple data blob files (optional). For example, one container image for node.js will corresponds to ~20 files in total. In densely employed environment, there could be as many as hundreds of containers and thus thousands of backing files on one machine.
[Test]
You could start a quick test by
https://github.com/lostjeffle/demand-read-cachefilesd
Jeffle Xu (20):
netfs: make @file optional in netfs_alloc_read_request() netfs,cachefiles: manage logical/physical offset separately netfs,fscache: support on-demand reading
cachefiles: extract generic daemon write function
cachefiles: detect backing file size in on-demand read mode
cachefiles: introduce new devnode for on-demand read mode
erofs: use meta buffers for erofs_read_superblock()
erofs: export erofs_map_blocks()
erofs: add mode checking helper
erofs: register global fscache volume
erofs: add cookie context helper functions
erofs: add anonymous inode managing page cache of blob file
erofs: register cookie context for bootstrap blob
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: register cookie context for data blobs
erofs: implement fscache-based data read for data blobs
erofs: add 'uuid' mount option
erofs: support on-demand reading
fs/cachefiles/Kconfig | 8 + fs/cachefiles/daemon.c | 147 ++++++++++++++++- fs/cachefiles/internal.h | 23 +++ fs/cachefiles/io.c | 82 +++++++++- fs/cachefiles/main.c | 27 ++++ fs/cachefiles/namei.c | 60 ++++++- fs/erofs/Kconfig | 2 +- fs/erofs/Makefile | 3 +- fs/erofs/data.c | 18 ++- fs/erofs/fscache.c | 339 +++++++++++++++++++++++++++++++++++++++ fs/erofs/inode.c | 6 +- fs/erofs/internal.h | 30 ++++ fs/erofs/super.c | 101 +++++++++--- fs/fscache/Kconfig | 8 + fs/netfs/Kconfig | 8 + fs/netfs/read_helper.c | 65 ++++++-- include/linux/netfs.h | 10 ++ 17 files changed, 886 insertions(+), 51 deletions(-) create mode 100644 fs/erofs/fscache.c
Signature · 1 line
2.27.0
PATCH v2 1/20 netfs: make @file optional in netfs_alloc_read_request()
Make the @file parameter optional, and derive inode from the @folio parameter instead in order to support file system internal requests.
@file parameter can't be removed completely, since it also works as the private data of ops->init_rreq().
--- fs/netfs/read_helper.c | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-)
fs/netfs/read_helper.c +7 -5
@@ -39,7 +39,7 @@ static void netfs_put_subrequest(struct netfs_read_subrequest *subreq,static struct netfs_read_request *netfs_alloc_read_request (const struct netfs_read_request_ops *ops,void *netfs_priv, -struct file *file) +struct inode *inode,struct file *file) {static atomic_t debug_ids;struct netfs_read_request *rreq;
@@ -48,7 +48,7 @@ static struct netfs_read_request *netfs_alloc_read_request(if (rreq) { rreq->netfs_ops = ops; rreq->netfs_priv = netfs_priv; - rreq->inode =file_inode (file); + rreq->inode = inode; rreq->i_size =i_size_read (rreq->inode); rreq->debug_id =atomic_inc_return (&debug_ids);INIT_LIST_HEAD (&rreq->subrequests);
@@ -870,6 +870,7 @@ void netfs_readahead(struct readahead_control *ractl, void *netfs_priv) {struct netfs_read_request *rreq; +struct inode *inode =file_inode (ractl->file);unsigned int debug_index =0 ;int ret;
@@ -878,7 +879,7 @@ void netfs_readahead(struct readahead_control *ractl,if (readahead_count (ractl) ==0 ) goto cleanup; - rreq =netfs_alloc_read_request (ops, netfs_priv, ractl->file); + rreq =netfs_alloc_read_request (ops, netfs_priv, inode, ractl->file);if (!rreq) goto cleanup; rreq->mapping = ractl->mapping;
@@ -948,12 +949,13 @@ int netfs_readpage(struct file *file, void *netfs_priv) {struct netfs_read_request *rreq; +struct inode *inode =folio_file_mapping (folio)->host;unsigned int debug_index =0 ;int ret;_enter ("%lx" ,folio_index (folio)); - rreq =netfs_alloc_read_request (ops, netfs_priv, file); + rreq =netfs_alloc_read_request (ops, netfs_priv, inode, file);if (!rreq) {if (netfs_priv) ops->cleanup (folio_file_mapping (folio), netfs_priv);
@@ -1122,7 +1124,7 @@ int netfs_write_begin(struct file *file, struct address_space *mapping, } ret = -ENOMEM ; - rreq =netfs_alloc_read_request (ops, netfs_priv, file); + rreq =netfs_alloc_read_request (ops, netfs_priv, inode, file);if (!rreq) goto error; rreq->mapping =folio_file_mapping (folio);
Signature · 1 line
2.27.0
PATCH v2 2/20 netfs,cachefiles: manage logical/physical offset separately
In reply to Jeffle XuCurrently fscache is used in a style that every file in upper fs has a corresponding backing file in fscache, and the file offset in the upper file (logical) is always equal to that in the backing file (physical).
While upper fs may implement different backing strategy, the above assumption can no longer be valid, e.g. multiple upper files can be packed into one single backing file.
Thus this patch abstracts these two different offsets and manage them separately, so that upper fs can implement different backing strategy. For the original users where these two offsets are always equal, no change is needed. While for the scenario where these two offsets can be different, upper fs can set a separate logical/physical offset in ops->begin_cache_operation() if it's needed.
--- fs/cachefiles/io.c | 14 +++++++------- fs/netfs/read_helper.c | 16 ++++++++++++---- include/linux/netfs.h | 2 ++ 3 files changed, 21 insertions(+), 11 deletions(-)
fs/cachefiles/io.c +7 -7
@@ -370,7 +370,7 @@ static enum netfs_read_source cachefiles_prepare_read(struct netfs_read_subreque off =cachefiles_inject_read_error ();if (off ==0 ) - off =vfs_llseek (file, subreq->start,SEEK_DATA ); + off =vfs_llseek (file, subreq->p_start,SEEK_DATA );if (off <0 && off >= (loff_t)-MAX_ERRNO ) {if (off == (loff_t)-ENXIO ) { why = cachefiles_trace_read_seek_nxio;
@@ -382,21 +382,21 @@ static enum netfs_read_source cachefiles_prepare_read(struct netfs_read_subreque goto out; } -if (off >= subreq->start + subreq->len) { +if (off >= subreq->p_start + subreq->len) { why = cachefiles_trace_read_found_hole; goto download_and_store; } -if (off > subreq->start) { +if (off > subreq->p_start) { off =round_up (off, cache->bsize); - subreq->len = off - subreq->start; + subreq->len = off - subreq->p_start; why = cachefiles_trace_read_found_part; goto download_and_store; } to =cachefiles_inject_read_error ();if (to ==0 ) - to =vfs_llseek (file, subreq->start,SEEK_HOLE ); + to =vfs_llseek (file, subreq->p_start,SEEK_HOLE );if (to <0 && to >= (loff_t)-MAX_ERRNO ) {trace_cachefiles_io_error (object,file_inode (file), to, cachefiles_trace_seek_error);
@@ -404,12 +404,12 @@ static enum netfs_read_source cachefiles_prepare_read(struct netfs_read_subreque goto out; } -if (to < subreq->start + subreq->len) { +if (to < subreq->p_start + subreq->len) {if (subreq->start + subreq->len >= i_size) to =round_up (to, cache->bsize);else to =round_down (to, cache->bsize); - subreq->len = to - subreq->start; + subreq->len = to - subreq->p_start; } why = cachefiles_trace_read_have_data;
fs/netfs/read_helper.c +12 -4
@@ -181,7 +181,7 @@ static void netfs_read_from_cache(struct netfs_read_request *rreq, subreq->start + subreq->transferred, subreq->len - subreq->transferred); - cres->ops->read (cres, subreq->start, &iter, read_hole, + cres->ops->read (cres, subreq->p_start, &iter, read_hole, netfs_cache_read_terminated, subreq); }
@@ -323,7 +323,7 @@ static void netfs_rreq_do_write_to_cache(struct netfs_read_request *rreq)netfs_put_subrequest (next, false); } - ret = cres->ops->prepare_write (cres, &subreq->start, &subreq->len, + ret = cres->ops->prepare_write (cres, &subreq->p_start, &subreq->len, rreq->i_size, true);if (ret <0 ) {trace_netfs_failure (rreq, subreq, ret, netfs_fail_prepare_write);
@@ -338,7 +338,7 @@ static void netfs_rreq_do_write_to_cache(struct netfs_read_request *rreq)netfs_stat (&netfs_n_rh_write);netfs_get_read_subrequest (subreq);trace_netfs_sreq (subreq, netfs_sreq_trace_write); - cres->ops->write (cres, subreq->start, &iter, + cres->ops->write (cres, subreq->p_start, &iter, netfs_rreq_copy_terminated, subreq); }
@@ -760,6 +760,7 @@ static bool netfs_rreq_submit_slice(struct netfs_read_request *rreq, subreq->debug_index = (*_debug_index)++; subreq->start = rreq->start + rreq->submitted; + subreq->p_start = rreq->p_start + rreq->submitted; subreq->len = rreq->len - rreq->submitted;_debug ("slice %llx,%zx,%zx" , subreq->start, subreq->len, rreq->submitted);
@@ -818,8 +819,12 @@ static void netfs_rreq_expand(struct netfs_read_request *rreq, {/* Give the cache a chance to change the request parameters. The * resultant request must contain the original region. +* Skip expanding if there may be multi-to-multi mapping between +* backing file and backed file. */ -netfs_cache_expand_readahead (rreq, &rreq->start, &rreq->len, rreq->i_size); +if (rreq->start == rreq->p_start) +netfs_cache_expand_readahead (rreq, &rreq->start, &rreq->len, + rreq->i_size);/* Give the netfs a chance to change the request parameters. The * resultant request must contain the original region.
@@ -884,6 +889,7 @@ void netfs_readahead(struct readahead_control *ractl, goto cleanup; rreq->mapping = ractl->mapping; rreq->start =readahead_pos (ractl); + rreq->p_start = rreq->start; rreq->len =readahead_length (ractl);if (ops->begin_cache_operation) {
@@ -964,6 +970,7 @@ int netfs_readpage(struct file *file, } rreq->mapping =folio_file_mapping (folio); rreq->start =folio_file_pos (folio); + rreq->p_start = rreq->start; rreq->len =folio_size (folio);if (ops->begin_cache_operation) {
@@ -1129,6 +1136,7 @@ int netfs_write_begin(struct file *file, struct address_space *mapping, goto error; rreq->mapping =folio_file_mapping (folio); rreq->start =folio_file_pos (folio); + rreq->p_start = rreq->start; rreq->len =folio_size (folio); rreq->no_unlock_folio =folio_index (folio);__set_bit (NETFS_RREQ_NO_UNLOCK_FOLIO , &rreq->flags);
include/linux/netfs.h +2 -0
@@ -134,6 +134,7 @@ struct netfs_read_subrequest {struct netfs_read_request *rreq;/* Supervising read request */ struct list_head rreq_link;/* Link in rreq->subrequests */ loff_t start;/* Where to start the I/O */ +loff_t p_start;/* Start position of backing file */ size_t len;/* Size of the I/O */ size_t transferred;/* Amount of data transferred */ refcount_t usage;
@@ -167,6 +168,7 @@ struct netfs_read_request {short error;/* 0 or error that occurred */ loff_t i_size;/* Size of the file */ loff_t start;/* Start position */ +loff_t p_start;/* Start position of backing file */ pgoff_t no_unlock_folio;/* Don't unlock this folio after read */ refcount_t usage;unsigned long flags;
Signature · 1 line
2.27.0
PATCH v2 3/20 netfs,fscache: support on-demand reading
In reply to Jeffle XuAdd ondemand_read() callback to netfs_cache_ops to implement on-demand reading.
The precondition for implementing on-demand reading semantic is that, all blob files have been placed under corresponding directory with correct file size (sparse files) on the first beginning. When upper fs starts to access the blob file, it will "cache miss" (hit the hole) and then .issue_op() callback will be called to prepare the data.
The following working flow is described as below. The .issue_op() callback could be implemented by netfs_ondemand_read() helper, which will in turn call .ondemand_read() callback of corresponding fscache backend to prepare the data.
The implementation of .ondemand_read() callback can be backend specific. The following patch will introduce an implementation of .ondemand_read() callback for cachefiles, which will notify user daemon the requested file range to read. The .ondemand_read() callback will get blocked until the user daemon has prepared the corresponding data.
Then once .ondemand_read() callback returns with 0, it is guaranteed that the requested data has been ready. In this case, transform this IO request to NETFS_READ_FROM_CACHE state, initiate an incomplete completion and then retry to read from backing file.
--- fs/fscache/Kconfig | 8 ++++++++ fs/netfs/Kconfig | 8 ++++++++ fs/netfs/read_helper.c | 37 +++++++++++++++++++++++++++++++++++++ include/linux/netfs.h | 8 ++++++++ 4 files changed, 61 insertions(+)
fs/fscache/Kconfig +8 -0
@@ -41,3 +41,11 @@ config FSCACHE_DEBUG config FSCACHE_OLD_API bool + +config FSCACHE_ONDEMAND + bool "Support for on-demand reading" + depends on FSCACHE + select NETFS_ONDEMAND + help + This permits on-demand reading with fscache. + If unsure, say N.
fs/netfs/Kconfig +8 -0
@@ -21,3 +21,11 @@ config NETFS_STATS multi-CPU system these may be on cachelines that keep bouncing between CPUs. On the other hand, the stats are very useful for debugging purposes. Saying 'Y' here is recommended. + +config NETFS_ONDEMAND + bool "Support for on-demand reading" + depends on NETFS_SUPPORT + default n + help + This enables on-demand reading with netfs API. + If unsure, say N.
fs/netfs/read_helper.c +37 -0
@@ -1013,6 +1013,43 @@ int netfs_readpage(struct file *file, }EXPORT_SYMBOL (netfs_readpage); +#ifdef CONFIG_NETFS_ONDEMAND +void netfs_ondemand_read (struct netfs_read_subrequest *subreq) +{ +struct netfs_read_request *rreq = subreq->rreq; +struct netfs_cache_resources *cres = &rreq->cache_resources; +loff_t start_pos; +size_t len; +int ret = -ENOBUFS ; + +/* The cache backend may not be accessible at this moment. */ +if (!cres->ops) + goto out; + +if (!cres->ops->ondemand_read) { + ret = -EOPNOTSUPP ; + goto out; + } + + start_pos = subreq->p_start + subreq->transferred; + len = subreq->len - subreq->transferred; + +/* +* In success case (ret == 0), user daemon has prepared data for +* us, thus transform to NETFS_READ_FROM_CACHE state and +* advertise that 0 byte readed, so that the request will enter +* into INCOMPLETE state and retry to read from backing file. +*/ + ret = cres->ops->ondemand_read (cres, start_pos, len); +if (!ret) { + subreq->source =NETFS_READ_FROM_CACHE ; +__clear_bit (NETFS_SREQ_WRITE_TO_CACHE , &subreq->flags); + } +out: +netfs_subreq_terminated (subreq, ret, false); +} +#endif +/* * Prepare a folio for writing without reading first * @folio: The folio being prepared
include/linux/netfs.h +8 -0
@@ -246,6 +246,11 @@ struct netfs_cache_ops {int (*prepare_write)(struct netfs_cache_resources *cres,loff_t *_start,size_t *_len,loff_t i_size,bool no_space_allocated_yet); + +#ifdef CONFIG_NETFS_ONDEMAND +int (*ondemand_read)(struct netfs_cache_resources *cres, +loff_t start_pos,size_t len); +#endif };struct readahead_control ;
@@ -261,6 +266,9 @@ extern int netfs_write_begin(struct file *, struct address_space *, void **, const struct netfs_read_request_ops *, void *); +#ifdef CONFIG_NETFS_ONDEMAND +extern void netfs_ondemand_read (struct netfs_read_subrequest *); +#endif extern void netfs_subreq_terminated (struct netfs_read_subrequest *,ssize_t ,bool );extern void netfs_stats_show (struct seq_file *);
Signature · 1 line
2.27.0
PATCH v2 4/20 cachefiles: extract generic daemon write function
In reply to Jeffle Xu... so that the following new devnode can reuse most of the code when implementing its .write() callback.
--- fs/cachefiles/daemon.c | 20 +++++++++++++++----- 1 file changed, 15 insertions(+), 5 deletions(-)
fs/cachefiles/daemon.c +15 -5
@@ -209,10 +209,11 @@ static ssize_t cachefiles_daemon_read(struct file *file, char __user *_buffer,/* * Take a command from cachefilesd, parse it and act on it. */ -static ssize_t cachefiles_daemon_write (struct file *file, -const char __user *_data, -size_t datalen, -loff_t *pos) +static ssize_t cachefiles_daemon_do_write (struct file *file, +const char __user *_data, +size_t datalen, +loff_t *pos, +const struct cachefiles_daemon_cmd *cmds) {const struct cachefiles_daemon_cmd *cmd;struct cachefiles_cache *cache = file->private_data;
@@ -261,7 +262,7 @@ static ssize_t cachefiles_daemon_write(struct file *file, }/* run the appropriate command handler */ -for (cmd = cachefiles_daemon_cmds; cmd->name[0 ]; cmd++) +for (cmd = cmds; cmd->name[0 ]; cmd++)if (strcmp (cmd->name, data) ==0 ) goto found_command;
@@ -284,6 +285,15 @@ static ssize_t cachefiles_daemon_write(struct file *file, goto error; } +static ssize_t cachefiles_daemon_write (struct file *file, +const char __user *_data, +size_t datalen, +loff_t *pos) +{ +return cachefiles_daemon_do_write (file, _data, datalen, pos, + cachefiles_daemon_cmds); +} +/* * Poll for culling state * - use EPOLLOUT to indicate culling state
Signature · 1 line
2.27.0
PATCH v2 5/20 cachefiles: detect backing file size in on-demand read mode
In reply to Jeffle Xufscache/cachefiles used to serve as a local cache for remote fs. The following patches will introduce a new use case, in which local read-only fs could implement on-demand reading with fscache. Then in this case, the upper read-only fs may has no idea on the size of the backed file.
Besides it is worth nothing that, in this scenario, user daemon is responsible for preparing all backing files with correct file size (backing files are all sparse files in this case). And since it's read-only, we can trust the backing file size as the backed file size.
With this precondition, cachefiles can detect the actual size of the backing file, and set it as the size of the backed file.
This patch also adds one flag bit to distinguish the new introduced on-demand read mode from the original mode. The following patch will make it configurable by users.
--- fs/cachefiles/Kconfig | 8 ++++++ fs/cachefiles/internal.h | 1 + fs/cachefiles/namei.c | 60 +++++++++++++++++++++++++++++++++++++++- 3 files changed, 68 insertions(+), 1 deletion(-)
fs/cachefiles/Kconfig +8 -0
@@ -26,3 +26,11 @@ 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 reading" + depends on CACHEFILES && FSCACHE_ONDEMAND + default n + help + This permits on-demand read mode of cachefiles. + If unsure, say N.
fs/cachefiles/internal.h +1 -0
@@ -98,6 +98,7 @@ struct cachefiles_cache {#define CACHEFILES_DEAD 1/* T if cache dead */ #define CACHEFILES_CULLING 2/* T if cull engaged */ #define CACHEFILES_STATE_CHANGED 3/* T if state changed (poll trigger) */ +#define CACHEFILES_ONDEMAND_MODE 4/* T if in on-demand read mode */ char *rootdirname;/* name of cache root directory */ char *secctx;/* LSM security context */ char *tag;/* cache binding tag */
fs/cachefiles/namei.c +59 -1
80 lines not on this page · raw message
Signature · 1 line
2.27.0
PATCH v2 6/20 cachefiles: introduce new devnode for on-demand read mode
In reply to Jeffle XuThis patch introduces a new devnode 'cachefiles_ondemand' to support the newly introduced on-demand read mode.
The precondition for on-demand reading semantic is that, all blob files have been placed under corresponding directory with correct file size (sparse files) on the first beginning. When upper fs starts to access the blob file, it will "cache miss" (hit the hole) and then turn to user daemon for preparing the data.
The interaction between kernel and user daemon is described as below.
1. Once cache miss, .ondemand_read() callback of corresponding fscache backend is called to prepare the data. As for cachefiles, it just packages related metadata (file range to read, etc.) into a pending read request, and then the process triggering cache miss will fall asleep until the corresponding data gets fetched later.
2. User daemon needs to poll on the devnode ('cachefiles_ondemand'), waiting for pending read request.
3. Once there's pending read request, user daemon will be notified and shall read the devnode ('cachefiles_ondemand') to fetch one pending read request to process.
4. For the fetched read request, user daemon need to somehow prepare the data (e.g. download from remote through network) and then write the fetched data into the backing file to fill the hole.
5. After that, user daemon need to notify cachefiles backend by writing a 'done' command to devnode ('cachefiles_ondemand'). It will also awake the previous asleep process triggering cache miss.
6. By the time the process gets awaken, the data has been ready in the backing file. Then fscache will re-initiate a read request from the backing file.
--- fs/cachefiles/daemon.c | 127 +++++++++++++++++++++++++++++++++++++++ fs/cachefiles/internal.h | 22 +++++++ fs/cachefiles/io.c | 68 +++++++++++++++++++++ fs/cachefiles/main.c | 27 +++++++++ 4 files changed, 244 insertions(+)
fs/cachefiles/daemon.c +127 -0
@@ -108,6 +108,10 @@ 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); +#ifdef CONFIG_CACHEFILES_ONDEMAND +idr_init (&cache->reqs); +set_bit (CACHEFILES_ONDEMAND_MODE , &cache->flags); +#endif /* set default caching limits * - limit at 1% free space and/or free files
@@ -142,6 +146,9 @@ static int cachefiles_daemon_release(struct inode *inode, struct file *file)cachefiles_daemon_unbind (cache);/* clean up the control file interface */ +#ifdef CONFIG_CACHEFILES_ONDEMAND +idr_destroy (&cache->reqs); +#endif cache->cachefilesd =NULL ; file->private_data =NULL ; cachefiles_open =0 ;
@@ -747,3 +754,123 @@ static void cachefiles_daemon_unbind(struct cachefiles_cache *cache)_leave ("" ); } + +#ifdef CONFIG_CACHEFILES_ONDEMAND +static ssize_t cachefiles_ondemand_write (struct file *,const char __user *, +size_t ,loff_t *); +static ssize_t cachefiles_ondemand_read (struct file *,char __user *,size_t , +loff_t *); +static __poll_t cachefiles_ondemand_poll (struct file *, +struct poll_table_struct *); +static int cachefiles_daemon_done (struct cachefiles_cache *,char *); + +const struct file_operations cachefiles_ondemand_fops = { + .owner =THIS_MODULE , + .open = cachefiles_daemon_open, + .release = cachefiles_daemon_release, + .read = cachefiles_ondemand_read, + .write = cachefiles_ondemand_write, + .poll = cachefiles_ondemand_poll, + .llseek = noop_llseek, +}; + +static const struct cachefiles_daemon_cmd cachefiles_ondemand_cmds[] = { + {"bind" , cachefiles_daemon_bind }, + {"brun" , cachefiles_daemon_brun }, + {"bcull" , cachefiles_daemon_bcull }, + {"bstop" , cachefiles_daemon_bstop }, + {"cull" , cachefiles_daemon_cull }, + {"debug" , cachefiles_daemon_debug }, + {"dir" , cachefiles_daemon_dir }, + {"frun" , cachefiles_daemon_frun }, + {"fcull" , cachefiles_daemon_fcull }, + {"fstop" , cachefiles_daemon_fstop }, + {"inuse" , cachefiles_daemon_inuse }, + {"secctx" , cachefiles_daemon_secctx }, + {"tag" , cachefiles_daemon_tag }, + {"done" , cachefiles_daemon_done }, + {"" ,NULL } +}; + +static ssize_t cachefiles_ondemand_write (struct file *file, +const char __user *_data, +size_t datalen, +loff_t *pos) +{ +return cachefiles_daemon_do_write (file, _data, datalen, pos, + cachefiles_ondemand_cmds); +} + +static ssize_t cachefiles_ondemand_read (struct file *file,char __user *_buffer, +size_t buflen,loff_t *pos) +{ +struct cachefiles_cache *cache = file->private_data; +struct cachefiles_req *req; +int n, id =0 ; + +if (!test_bit (CACHEFILES_READY , &cache->flags)) +return 0 ; + +idr_lock (&cache->reqs); + req =idr_get_next (&cache->reqs, &id); +idr_unlock (&cache->reqs); +if (!req) +return 0 ; + + n =sizeof (req->req_in); +if (n > buflen) +return -EMSGSIZE ; + +if (copy_to_user (_buffer, &req->req_in, n) !=0 ) +return -EFAULT ; + +return n; +} + +static __poll_t cachefiles_ondemand_poll (struct file *file, +struct poll_table_struct *poll) +{ +struct cachefiles_cache *cache = file->private_data; +__poll_t mask; + +poll_wait (file, &cache->daemon_pollwq, poll); + mask =0 ; + +if (!idr_is_empty (&cache->reqs)) + mask |=EPOLLIN ; + +return mask; +} + +/* +* Request completion +* - command: "done <id>" +*/ +static int cachefiles_daemon_done (struct cachefiles_cache *cache,char *args) +{ +unsigned long id; +int ret; +struct cachefiles_req *req; + +_enter (",%s" , args); + +if (!*args) { +pr_err ("Empty id specified\n" ); +return -EINVAL ; + } + + ret =kstrtoul (args,0 , &id); +if (ret) +return ret; + +idr_lock (&cache->reqs); + req =idr_remove (&cache->reqs, id); +idr_unlock (&cache->reqs); +if (!req) +return -EINVAL ; + +complete (&req->done); + +return 0 ; +} +#endif
fs/cachefiles/internal.h +22 -0
@@ -15,6 +15,7 @@#include <linux/fscache-cache.h> #include <linux/cred.h> #include <linux/security.h> +#include <linux/idr.h> #define CACHEFILES_DIO_BLOCK_SIZE 4096
@@ -60,6 +61,20 @@ struct cachefiles_object {#define CACHEFILES_OBJECT_USING_TMPFILE 0/* Have an unlinked tmpfile */ }; +#ifdef CONFIG_CACHEFILES_ONDEMAND +struct cachefiles_req_in { +uint64_t id; +uint64_t off; +uint64_t len; +char path[NAME_MAX ]; +}; + +struct cachefiles_req { +struct completion done; +struct cachefiles_req_in req_in; +}; +#endif +/* * Cache files cache definition */
@@ -102,6 +117,10 @@ struct cachefiles_cache {char *rootdirname;/* name of cache root directory */ char *secctx;/* LSM security context */ char *tag;/* cache binding tag */ + +#ifdef CONFIG_CACHEFILES_ONDEMAND +struct idr reqs; +#endif };#include <trace/events/cachefiles.h>
@@ -146,6 +165,9 @@ extern int cachefiles_has_space(struct cachefiles_cache *cache,* daemon.c */ extern const struct file_operations cachefiles_daemon_fops; +#ifdef CONFIG_CACHEFILES_ONDEMAND +extern const struct file_operations cachefiles_ondemand_fops; +#endif /* * error_inject.c
fs/cachefiles/io.c +68 -0
80 lines not on this page · raw message
fs/cachefiles/main.c +27 -0
@@ -39,6 +39,27 @@ static struct miscdevice cachefiles_dev = { .fops = &cachefiles_daemon_fops, }; +#ifdef CONFIG_CACHEFILES_ONDEMAND +static struct miscdevice cachefiles_ondemand_dev = { + .minor =MISC_DYNAMIC_MINOR , + .name ="cachefiles_ondemand" , + .fops = &cachefiles_ondemand_fops, +}; + +static inline int cachefiles_init_ondemand (void ) +{ +return misc_register (&cachefiles_ondemand_dev); +} + +static inline void cachefiles_exit_ondemand (void ) +{ +misc_deregister (&cachefiles_ondemand_dev); +} +#else +static inline int cachefiles_init_ondemand (void ) {return 0 ; } +static inline void cachefiles_exit_ondemand (void ) {} +#endif +/* * initialise the fs caching module */
@@ -52,6 +73,9 @@ static int __init cachefiles_init(void) ret =misc_register (&cachefiles_dev);if (ret <0 ) goto error_dev; + ret =cachefiles_init_ondemand (); +if (ret <0 ) + goto error_ondemand_dev;/* create an object jar */ ret = -ENOMEM ;
@@ -68,6 +92,8 @@ static int __init cachefiles_init(void)return 0 ; error_object_jar: +cachefiles_exit_ondemand (); +error_ondemand_dev:misc_deregister (&cachefiles_dev); error_dev:cachefiles_unregister_error_injection ();
@@ -86,6 +112,7 @@ static void __exit cachefiles_exit(void)pr_info ("Unloading\n" );kmem_cache_destroy (cachefiles_object_jar); +cachefiles_exit_ondemand ();misc_deregister (&cachefiles_dev);cachefiles_unregister_error_injection (); }
Signature · 1 line
2.27.0
PATCH v2 7/20 erofs: use meta buffers for erofs_read_superblock()
In reply to Jeffle XuThe only change is that, meta buffers read cache page without __GFP_FS flag, which shall not matter.
--- fs/erofs/super.c | 13 +++++-------- 1 file changed, 5 insertions(+), 8 deletions(-)
fs/erofs/super.c +5 -8
@@ -281,21 +281,19 @@ static int erofs_init_devices(struct super_block *sb,static int erofs_read_superblock (struct super_block *sb) {struct erofs_sb_info *sbi; -struct page *page; +struct erofs_buf buf = __EROFS_BUF_INITIALIZER;struct erofs_super_block *dsb;unsigned int blkszbits;void *data;int ret; - page =read_mapping_page (sb->s_bdev->bd_inode->i_mapping,0 ,NULL ); -if (IS_ERR (page)) { + data =erofs_read_metabuf (&buf, sb,0 ,EROFS_KMAP ); +if (IS_ERR (data)) {erofs_err (sb,"cannot read erofs superblock" ); -return PTR_ERR (page); +return PTR_ERR (data); } sbi =EROFS_SB (sb); - - data =kmap (page); dsb = (struct erofs_super_block *)(data +EROFS_SUPER_OFFSET ); ret = -EINVAL ;
@@ -365,8 +363,7 @@ 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!" ); out: -kunmap (page); -put_page (page); +erofs_put_metabuf (&buf);return ret; }
Signature · 1 line
2.27.0
PATCH v2 8/20 erofs: export erofs_map_blocks()
In reply to Jeffle Xu... so that it can be used in the following introduced fs/erofs/fscache.c.
--- fs/erofs/data.c | 4 ++-- fs/erofs/internal.h | 2 ++ 2 files changed, 4 insertions(+), 2 deletions(-)
fs/erofs/data.c +2 -2
@@ -104,8 +104,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
@@ -484,6 +484,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 v2 9/20 erofs: add mode checking helper
In reply to Jeffle XuUntil then erofs is exactly blockdev based filesystem. In other using scenarios (e.g. container image), erofs needs to run upon files.
This patch set is going to introduces a new nodev mode, in which erofs could be mounted from a bootstrap blob file containing complete erofs image.
Add a helper checking which mode erofs works in.
--- fs/erofs/internal.h | 5 +++++ 1 file changed, 5 insertions(+)
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_bdev_mode (struct super_block *sb) +{ +return sb->s_bdev; +} +enum {EROFS_ZIP_CACHE_DISABLED ,EROFS_ZIP_CACHE_READAHEAD ,
Signature · 1 line
2.27.0
PATCH v2 10/20 erofs: register global fscache volume
In reply to Jeffle XuAll erofs instances will share one global fscache volume.
In this using scenario, one erofs instance could be mounted from one (or multiple) blob files instead of blkdev. The number of blob files that each erofs instance could correspond to is limited, since these blob files are quite large in size. For example, when used for container image distribution, one erofs instance used for container image for node.js will correspond to ~20 blob files in total. Thus in densely employed environment, there could be as many as hundreds of containers and thus thousands of fscache cookies under one fscache volume.
Then as for cachefiles backend, the hash table managing all cookies under one volume contains 32K slots. Thus the hashing functionality shall scale well in this case. Besides, cachefiles backend will scatter backing files under 256 fan sub-directoris, and thus the scalability of looking up backing files shall also not be an issue.
--- fs/erofs/Makefile | 3 ++- fs/erofs/fscache.c | 21 +++++++++++++++++++++ fs/erofs/internal.h | 5 +++++ fs/erofs/super.c | 7 +++++++ 4 files changed, 35 insertions(+), 1 deletion(-) create mode 100644 fs/erofs/fscache.c
fs/erofs/Makefile +2 -1
@@ -1,7 +1,8 @@ # SPDX-License-Identifier: GPL-2.0-only obj-$(CONFIG_EROFS_FS) += erofs.o -erofs-objs := super.o inode.o data.o namei.o dir.o utils.o pcpubuf.o sysfs.o +erofs-objs := super.o inode.o data.o namei.o dir.o utils.o pcpubuf.o sysfs.o \ + fscache.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
fs/erofs/fscache.c new file +21 -0
@@ -0,0 +1,21 @@ +// SPDX-License-Identifier: GPL-2.0-or-later +/* +* Copyright (C) 2021, Alibaba Cloud +*/ +#include "internal.h" + +static struct fscache_volume *volume; + +int __init erofs_init_fscache (void ) +{ + volume =fscache_acquire_volume ("erofs" ,NULL ,NULL ,0 ); +if (!volume) +return -EINVAL ; + +return 0 ; +} + +void erofs_exit_fscache (void ) +{ +fscache_relinquish_volume (volume,NULL , false); +}
fs/erofs/internal.h +5 -0
@@ -17,6 +17,7 @@#include <linux/slab.h> #include <linux/vmalloc.h> #include <linux/iomap.h> +#include <linux/fscache.h> #include "erofs_fs.h" /* redefine pr_fmt "erofs: " */
@@ -616,6 +617,10 @@ static inline int z_erofs_load_lzma_config(struct super_block *sb, }#endif /* !CONFIG_EROFS_FS_ZIP */ +/* fscache.c */ +int erofs_init_fscache (void ); +void erofs_exit_fscache (void ); +#define EFSCORRUPTED EUCLEAN/* Filesystem is corrupted */ #endif /* __EROFS_INTERNAL_H */
fs/erofs/super.c +7 -0
@@ -814,6 +814,10 @@ static int __init erofs_module_init(void)if (err) goto sysfs_err; + err =erofs_init_fscache (); +if (err) + goto fscache_err; + err =register_filesystem (&erofs_fs_type);if (err) goto fs_err;
@@ -821,6 +825,8 @@ static int __init erofs_module_init(void)return 0 ; fs_err: +erofs_exit_fscache (); +fscache_err:erofs_exit_sysfs (); sysfs_err:z_erofs_exit_zip_subsystem ();
@@ -841,6 +847,7 @@ static void __exit erofs_module_exit(void)/* Ensure all RCU free inodes / pclusters are safe to be destroyed. */ rcu_barrier (); +erofs_exit_fscache ();erofs_exit_sysfs ();z_erofs_exit_zip_subsystem ();z_erofs_lzma_exit ();
Signature · 1 line
2.27.0
PATCH v2 11/20 erofs: add cookie context helper functions
In reply to Jeffle XuIntroduce 'struct erofs_cookie_ctx' for managing cookie for backing file, and the following introduced API for reading from backing file.
Besides, introduce two helper functions for initializing and cleaning up erofs_cookie_ctx.
struct erofs_cookie_ctx * erofs_fscache_get_ctx(struct super_block *sb, char *path);
void erofs_fscache_put_ctx(struct erofs_cookie_ctx *ctx);
--- fs/erofs/fscache.c | 78 +++++++++++++++++++++++++++++++++++++++++++++ fs/erofs/internal.h | 8 +++++ 2 files changed, 86 insertions(+)
fs/erofs/fscache.c +78 -0
@@ -6,6 +6,84 @@static struct fscache_volume *volume; +static int erofs_fscache_init_cookie (struct erofs_fscache_context *ctx, +char *path) +{ +struct fscache_cookie *cookie; + +/* +* @object_size shall be non-zero to avoid +* FSCACHE_COOKIE_NO_DATA_TO_READ. +*/ + cookie =fscache_acquire_cookie (volume,0 , + path,strlen (path), +NULL ,0 ,-1 ); +if (!cookie) +return -EINVAL ; + +fscache_use_cookie (cookie, false); + ctx->cookie = cookie; +return 0 ; +} + +static inline +void erofs_fscache_cleanup_cookie (struct erofs_fscache_context *ctx) +{ +struct fscache_cookie *cookie = ctx->cookie; + +fscache_unuse_cookie (cookie,NULL ,NULL ); +fscache_relinquish_cookie (cookie, false); + ctx->cookie =NULL ; +} + +static int erofs_fscahce_init_ctx (struct erofs_fscache_context *ctx, +struct super_block *sb,char *path) +{ +int ret; + + ret =erofs_fscache_init_cookie (ctx, path); +if (ret) { +erofs_err (sb,"failed to init cookie" ); +return ret; + } + +return 0 ; +} + +static inline +void erofs_fscache_cleanup_ctx (struct erofs_fscache_context *ctx) +{ +erofs_fscache_cleanup_cookie (ctx); +} + +struct erofs_fscache_context *erofs_fscache_get_ctx (struct super_block *sb, +char *path) +{ +struct erofs_fscache_context *ctx; +int ret; + + ctx =kzalloc (sizeof (*ctx),GFP_KERNEL ); +if (!ctx) +return ERR_PTR (-ENOMEM ); + + ret =erofs_fscahce_init_ctx (ctx, sb, path); +if (ret) { +kfree (ctx); +return ERR_PTR (ret); + } + +return ctx; +} + +void erofs_fscache_put_ctx (struct erofs_fscache_context *ctx) +{ +if (!ctx) +return ; + +erofs_fscache_cleanup_ctx (ctx); +kfree (ctx); +} +int __init erofs_init_fscache (void ) { volume =fscache_acquire_volume ("erofs" ,NULL ,NULL ,0 );
fs/erofs/internal.h +8 -0
@@ -97,6 +97,10 @@ struct erofs_sb_lz4_info {u16 max_pclusterblks; }; +struct erofs_fscache_context { +struct fscache_cookie *cookie; +}; +struct erofs_sb_info {struct erofs_mount_opts opt;/* options */ #ifdef CONFIG_EROFS_FS_ZIP
@@ -621,6 +625,10 @@ static inline int z_erofs_load_lzma_config(struct super_block *sb,int erofs_init_fscache (void );void erofs_exit_fscache (void ); +struct erofs_fscache_context *erofs_fscache_get_ctx (struct super_block *sb, +char *path); +void erofs_fscache_put_ctx (struct erofs_fscache_context *ctx); +#define EFSCORRUPTED EUCLEAN/* Filesystem is corrupted */ #endif /* __EROFS_INTERNAL_H */
Signature · 1 line
2.27.0
David Howells
maintainerRe: [PATCH v2 11/20] erofs: add cookie context helper functions
Quoted 1 line · Jeffle Xu
+static int erofs_fscahce_init_ctx(struct
erofs_fscache_context*ctx,
fscahce => fscache?
David
Re: [PATCH v2 11/20] erofs: add cookie context helper functions
Quoted 6 lines · David Howells
Quoted 1 line · Jeffle Xu
+static int erofs_fscahce_init_ctx(struct
erofs_fscache_context*ctx,fscahce => fscache?
Right. Thanks.
Signature · 2 lines
Thanks, Jeffle
PATCH v2 12/20 erofs: add anonymous inode managing page cache of blob file
In reply to Jeffle XuIntroduce one anonymous inode for managing page cache of corresponding blob file. Then erofs could read directly from the address space of the anonymous inode when cache hit.
--- fs/erofs/fscache.c | 45 ++++++++++++++++++++++++++++++++++++++++++--- fs/erofs/internal.h | 3 ++- 2 files changed, 44 insertions(+), 4 deletions(-)
fs/erofs/fscache.c +42 -3
@@ -6,6 +6,9 @@static struct fscache_volume *volume; +static const struct address_space_operations erofs_fscache_blob_aops = { +}; +static int erofs_fscache_init_cookie (struct erofs_fscache_context *ctx,char *path) {
@@ -36,8 +39,34 @@ void erofs_fscache_cleanup_cookie(struct erofs_fscache_context *ctx) ctx->cookie =NULL ; } +static int erofs_fscache_get_inode (struct erofs_fscache_context *ctx, +struct super_block *sb) +{ +struct inode *const inode =new_inode (sb); + +if (!inode) +return -ENOMEM ; + +set_nlink (inode,1 ); + inode->i_size =OFFSET_MAX ; + + inode->i_mapping->a_ops = &erofs_fscache_blob_aops; +mapping_set_gfp_mask (inode->i_mapping, +GFP_NOFS | __GFP_HIGHMEM | __GFP_MOVABLE); + ctx->inode = inode; +return 0 ; +} + +static inline +void erofs_fscache_put_inode (struct erofs_fscache_context *ctx) +{ +iput (ctx->inode); + ctx->inode =NULL ; +} +static int erofs_fscahce_init_ctx (struct erofs_fscache_context *ctx, -struct super_block *sb,char *path) +struct super_block *sb,char *path, +bool need_inode) {int ret;
@@ -47,6 +76,15 @@ static int erofs_fscahce_init_ctx(struct erofs_fscache_context *ctx,return ret; } +if (need_inode) { + ret =erofs_fscache_get_inode (ctx, sb); +if (ret) { +erofs_err (sb,"failed to get anonymous inode" ); +erofs_fscache_cleanup_cookie (ctx); +return ret; + } + } +return 0 ; }
@@ -54,10 +92,11 @@ static inlinevoid erofs_fscache_cleanup_ctx (struct erofs_fscache_context *ctx) {erofs_fscache_cleanup_cookie (ctx); +erofs_fscache_put_inode (ctx); }struct erofs_fscache_context *erofs_fscache_get_ctx (struct super_block *sb, -char *path) +char *path,bool need_inode) {struct erofs_fscache_context *ctx;int ret;
@@ -66,7 +105,7 @@ struct erofs_fscache_context *erofs_fscache_get_ctx(struct super_block *sb,if (!ctx)return ERR_PTR (-ENOMEM ); - ret =erofs_fscahce_init_ctx (ctx, sb, path); + ret =erofs_fscahce_init_ctx (ctx, sb, path, need_inode);if (ret) {kfree (ctx);return ERR_PTR (ret);
fs/erofs/internal.h +2 -1
@@ -99,6 +99,7 @@ struct erofs_sb_lz4_info {struct erofs_fscache_context {struct fscache_cookie *cookie; +struct inode *inode; };struct erofs_sb_info {
@@ -626,7 +627,7 @@ int erofs_init_fscache(void);void erofs_exit_fscache (void );struct erofs_fscache_context *erofs_fscache_get_ctx (struct super_block *sb, -char *path); +char *path,bool need_inode);void erofs_fscache_put_ctx (struct erofs_fscache_context *ctx);#define EFSCORRUPTED EUCLEAN/* Filesystem is corrupted */
Signature · 1 line
2.27.0
PATCH v2 13/20 erofs: register cookie context for bootstrap blob
In reply to Jeffle XuRegisters fscache_cookie for the bootstrap blob file. The bootstrap blob file can be specified by a new mount option, which is going to be introduced by a following patch.
Something worth mentioning about the cleanup routine.
1. The init routine is prior to when the root inode gets initialized, and thus the corresponding cleanup routine shall be placed under .kill_sb() callback.
2. The init routine will instantiate anonymous inodes under the super_block, and thus .put_super() callback shall also contain the cleanup routine. Or we'll get "VFS: Busy inodes after unmount." warning.
--- fs/erofs/internal.h | 3 +++ fs/erofs/super.c | 13 +++++++++++++ 2 files changed, 16 insertions(+)
fs/erofs/internal.h +3 -0
@@ -75,6 +75,7 @@ struct erofs_mount_opts {unsigned int max_sync_decompress_pages;#endif unsigned int mount_opt; +char *uuid; };struct erofs_dev_context {
@@ -152,6 +153,8 @@ struct erofs_sb_info {/* sysfs support */ struct kobject s_kobj;/* /sys/fs/erofs/<devname> */ struct completion s_kobj_unregister; + +struct erofs_fscache_context *bootstrap; };#define EROFS_SB (sb) ((struct erofs_sb_info *)(sb)->s_fs_info)
fs/erofs/super.c +13 -0
@@ -598,6 +598,16 @@ static int erofs_fc_fill_super(struct super_block *sb, struct fs_context *fc) sbi->devs = ctx->devs; ctx->devs =NULL ; +if (!erofs_bdev_mode (sb)) { +struct erofs_fscache_context *bootstrap; + + bootstrap =erofs_fscache_get_ctx (sb, ctx->opt.uuid, true); +if (IS_ERR (bootstrap)) +return PTR_ERR (bootstrap); + + sbi->bootstrap = bootstrap; + } + err =erofs_read_superblock (sb);if (err)return err;
@@ -753,6 +763,7 @@ static void erofs_kill_sb(struct super_block *sb)return ;erofs_free_dev_context (sbi->devs); +erofs_fscache_put_ctx (sbi->bootstrap);fs_put_dax (sbi->dax_dev);kfree (sbi); sb->s_fs_info =NULL ;
@@ -771,6 +782,8 @@ static void erofs_put_super(struct super_block *sb)iput (sbi->managed_cache); sbi->managed_cache =NULL ;#endif +erofs_fscache_put_ctx (sbi->bootstrap); + sbi->bootstrap =NULL ; }static struct file_system_type erofs_fs_type = {
Signature · 1 line
2.27.0
PATCH v2 14/20 erofs: implement fscache-based metadata read
In reply to Jeffle XuThis patch implements the data plane of reading metadata from bootstrap blob file over fscache.
Be noted that currently it only supports the scenario where the backing file has no hole. Once it hits a hole of the backing file, erofs will fail the IO with -EOPNOTSUPP for now. The following patch will fix this issue, i.e. implementing the demand reading mode.
--- fs/erofs/data.c | 11 +++++++++-- fs/erofs/fscache.c | 33 +++++++++++++++++++++++++++++++++ fs/erofs/internal.h | 3 +++ 3 files changed, 45 insertions(+), 2 deletions(-)
fs/erofs/data.c +9 -2
@@ -31,15 +31,22 @@ void erofs_put_metabuf(struct erofs_buf *buf)void *erofs_read_metabuf (struct erofs_buf *buf,struct super_block *sb,erofs_blk_t blkaddr,enum erofs_kmap_type type) { -struct address_space *const mapping = sb->s_bdev->bd_inode->i_mapping; +struct address_space *mapping; +struct erofs_sb_info *sbi =EROFS_SB (sb);erofs_off_t offset =blknr_to_addr (blkaddr);pgoff_t index = offset >>PAGE_SHIFT ;struct page *page = buf->page;if (!page || page->index != index) {erofs_put_metabuf (buf); - page =read_cache_page_gfp (mapping, index, +if (erofs_bdev_mode (sb)) { + mapping = sb->s_bdev->bd_inode->i_mapping; + page =read_cache_page_gfp (mapping, index,mapping_gfp_constraint (mapping, ~__GFP_FS)); + }else { + page =erofs_fscache_read_cache_page (sbi->bootstrap, + index); + }if (IS_ERR (page))return page;/* should already be PageUptodate, no need to lock page */
fs/erofs/fscache.c +33 -0
@@ -6,9 +6,42 @@static struct fscache_volume *volume; +static int erofs_blob_begin_cache_operation (struct netfs_read_request *rreq) +{ +return fscache_begin_read_operation (&rreq->cache_resources, + rreq->netfs_priv); +} + +/* .cleanup() is needed if rreq->netfs_priv is non-NULL */ +static void erofs_noop_cleanup (struct address_space *mapping,void *netfs_priv) +{ +} + +static const struct netfs_read_request_ops erofs_blob_req_ops = { + .begin_cache_operation = erofs_blob_begin_cache_operation, + .cleanup = erofs_noop_cleanup, +}; + +static int erofs_fscache_blob_readpage (struct file *data,struct page *page) +{ +struct folio *folio =page_folio (page); +struct erofs_fscache_context *ctx = + (struct erofs_fscache_context *)data; + +return netfs_readpage (NULL , folio, &erofs_blob_req_ops, ctx->cookie); +} +static const struct address_space_operations erofs_fscache_blob_aops = { + .readpage = erofs_fscache_blob_readpage, }; +struct page *erofs_fscache_read_cache_page (struct erofs_fscache_context *ctx, +pgoff_t index) +{ +DBG_BUGON (!ctx->inode); +return read_mapping_page (ctx->inode->i_mapping, index, ctx); +} +static int erofs_fscache_init_cookie (struct erofs_fscache_context *ctx,char *path) {
fs/erofs/internal.h +3 -0
@@ -633,6 +633,9 @@ struct erofs_fscache_context *erofs_fscache_get_ctx(struct super_block *sb, char *path, bool need_inode);void erofs_fscache_put_ctx (struct erofs_fscache_context *ctx); +struct page *erofs_fscache_read_cache_page (struct erofs_fscache_context *ctx, +pgoff_t index); +#define EFSCORRUPTED EUCLEAN/* Filesystem is corrupted */ #endif /* __EROFS_INTERNAL_H */
Signature · 1 line
2.27.0
PATCH v2 15/20 erofs: implement fscache-based data read for non-inline layout
In reply to Jeffle XuThis patch implements the data plane of reading data from bootstrap blob file over fscache for non-inline layout.
Be noted that compressed layout is not supported yet.
--- fs/erofs/fscache.c | 111 ++++++++++++++++++++++++++++++++++++++++++++ fs/erofs/inode.c | 6 ++- fs/erofs/internal.h | 1 + 3 files changed, 117 insertions(+), 1 deletion(-)
fs/erofs/fscache.c +111 -0
@@ -4,6 +4,17 @@*/ #include "internal.h" +struct erofs_fscache_map { +struct erofs_fscache_context *m_ctx; +erofs_off_t m_pa, m_la, o_la; +u64 m_llen; +}; + +struct erofs_fscache_priv { +struct fscache_cookie *cookie; +loff_t offset; +}; +static struct fscache_volume *volume;static int erofs_blob_begin_cache_operation (struct netfs_read_request *rreq)
@@ -22,6 +33,33 @@ static const struct netfs_read_request_ops erofs_blob_req_ops = { .cleanup = erofs_noop_cleanup, }; +static int erofs_begin_cache_operation (struct netfs_read_request *rreq) +{ +struct erofs_fscache_priv *priv = rreq->netfs_priv; + + rreq->p_start = priv->offset; +return fscache_begin_read_operation (&rreq->cache_resources, + priv->cookie); +} + +static bool erofs_clamp_length (struct netfs_read_subrequest *subreq) +{ +/* +* For non-inline layout, rreq->i_size is actually the size of upper +* file in erofs rather than that of blob file. Thus when cache miss, +* subreq->len can be restricted to the upper file size, while we hope +* blob file can be filled in a EROFS_BLKSIZ granule. +*/ + subreq->len =round_up (subreq->len,EROFS_BLKSIZ ); +return true; +} + +static const struct netfs_read_request_ops erofs_req_ops = { + .begin_cache_operation = erofs_begin_cache_operation, + .cleanup = erofs_noop_cleanup, + .clamp_length = erofs_clamp_length, +}; +static int erofs_fscache_blob_readpage (struct file *data,struct page *page) {struct folio *folio =page_folio (page);
@@ -42,6 +80,79 @@ struct page *erofs_fscache_read_cache_page(struct erofs_fscache_context *ctx,return read_mapping_page (ctx->inode->i_mapping, index, ctx); } +static int erofs_fscache_readpage_noinline (struct page *page, +struct erofs_fscache_map *fsmap) +{ +struct folio *folio =page_folio (page); +struct erofs_fscache_priv priv; + +/* +* 1) For FLAT_PLAIN layout, the output map.m_la shall be equal to o_la, +* and the output map.m_pa is exactly the physical address of o_la. +* 2) For CHUNK_BASED layout, the output map.m_la is rounded down to the +* nearest chunk boundary, and the output map.m_pa is actually the +* physical address of this chunk boundary. So we need to recalculate +* the actual physical address of o_la. +*/ + priv.offset = fsmap->m_pa + fsmap->o_la - fsmap->m_la; + priv.cookie = fsmap->m_ctx->cookie; + +return netfs_readpage (NULL , folio, &erofs_req_ops, &priv); +} + +static int erofs_fscache_readpage (struct file *file,struct page *page) +{ +struct inode *inode = page->mapping->host; +struct erofs_inode *vi =EROFS_I (inode); +struct super_block *sb = inode->i_sb; +struct erofs_sb_info *sbi =EROFS_SB (sb); +struct erofs_map_blocks map; +struct erofs_fscache_map fsmap; +int ret; + +if (erofs_inode_is_data_compressed (vi->datalayout)) { +erofs_info (sb,"compressed layout not supported yet" ); + ret = -EOPNOTSUPP ; + goto err_out; + } + + map.m_la = fsmap.o_la =page_offset (page); + + ret =erofs_map_blocks (inode, &map,EROFS_GET_BLOCKS_RAW ); +if (ret) + goto err_out; + +if (!(map.m_flags &EROFS_MAP_MAPPED )) { +zero_user (page,0 ,PAGE_SIZE ); +SetPageUptodate (page); +unlock_page (page); +return 0 ; + } + + fsmap.m_ctx = sbi->bootstrap; + fsmap.m_la = map.m_la; + fsmap.m_pa = map.m_pa; + fsmap.m_llen = map.m_llen; + +switch (vi->datalayout) { +case EROFS_INODE_FLAT_PLAIN : +case EROFS_INODE_CHUNK_BASED : +return erofs_fscache_readpage_noinline (page, &fsmap); +default : +DBG_BUGON (1 ); + ret = -EOPNOTSUPP ; + } + +err_out: +SetPageError (page); +unlock_page (page); +return ret; +} + +const struct address_space_operations erofs_fscache_access_aops = { + .readpage = erofs_fscache_readpage, +}; +static int erofs_fscache_init_cookie (struct erofs_fscache_context *ctx,char *path) {
fs/erofs/inode.c +5 -1
@@ -296,7 +296,11 @@ static int erofs_fill_inode(struct inode *inode, int isdir) err =z_erofs_fill_inode (inode); goto out_unlock; } - inode->i_mapping->a_ops = &erofs_raw_access_aops; + +if (erofs_bdev_mode (inode->i_sb)) + inode->i_mapping->a_ops = &erofs_raw_access_aops; +else + inode->i_mapping->a_ops = &erofs_fscache_access_aops; out_unlock:erofs_put_metabuf (&buf);
fs/erofs/internal.h +1 -0
@@ -393,6 +393,7 @@ struct page *erofs_grab_cache_page_nowait(struct address_space *mapping,extern const struct super_operations erofs_sops;extern const struct address_space_operations erofs_raw_access_aops; +extern const struct address_space_operations erofs_fscache_access_aops;extern const struct address_space_operations z_erofs_aops;/*
Signature · 1 line
2.27.0
PATCH v2 16/20 erofs: implement fscache-based data read for inline layout
In reply to Jeffle XuThis patch implements the data plane of reading data from bootstrap blob file over fscache for inline layout.
--- fs/erofs/fscache.c | 41 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 41 insertions(+)
fs/erofs/fscache.c +41 -0
@@ -100,6 +100,45 @@ static int erofs_fscache_readpage_noinline(struct page *page,return netfs_readpage (NULL , folio, &erofs_req_ops, &priv); } +static int erofs_fscache_readpage_inline (struct page *page, +struct erofs_fscache_map *fsmap) +{ +struct inode *inode = page->mapping->host; +struct super_block *sb = inode->i_sb; +struct erofs_buf buf = __EROFS_BUF_INITIALIZER; +erofs_blk_t blknr; +size_t offset, len; +void *src, *dst; + +/* +* For inline (tail packing) layout, the offset may be non-zero, while +* the offset can be calculated from corresponding physical address +* directly. +* Currently only flat layout supports inline (FLAT_INLINE), and the +* output map.m_pa is exactly the physical address of o_la in this case. +*/ + offset =erofs_blkoff (fsmap->m_pa); + blknr =erofs_blknr (fsmap->m_pa); + len = fsmap->m_llen; + + src =erofs_read_metabuf (&buf, sb, blknr,EROFS_KMAP ); +if (IS_ERR (src)) { +SetPageError (page); +unlock_page (page); +return PTR_ERR (src); + } + + dst =kmap (page); +memcpy (dst, src + offset, len); +kunmap (page); + +erofs_put_metabuf (&buf); + +SetPageUptodate (page); +unlock_page (page); +return 0 ; +} +static int erofs_fscache_readpage (struct file *file,struct page *page) {struct inode *inode = page->mapping->host;
@@ -138,6 +177,8 @@ static int erofs_fscache_readpage(struct file *file, struct page *page)case EROFS_INODE_FLAT_PLAIN :case EROFS_INODE_CHUNK_BASED :return erofs_fscache_readpage_noinline (page, &fsmap); +case EROFS_INODE_FLAT_INLINE : +return erofs_fscache_readpage_inline (page, &fsmap);default :DBG_BUGON (1 ); ret = -EOPNOTSUPP ;
Signature · 1 line
2.27.0
PATCH v2 17/20 erofs: register cookie context for data blobs
In reply to Jeffle XuSimilar to the multi device mode, erofs could be mounted from multiple blob files (one bootstrap blob file and optional multiple data blob files). In this case, each device slot contains the path of corresponding data blob file.
This patch registers corresponding cookie context for each data blob file.
--- fs/erofs/internal.h | 1 + fs/erofs/super.c | 27 +++++++++++++++++++-------- 2 files changed, 20 insertions(+), 8 deletions(-)
fs/erofs/internal.h +1 -0
@@ -53,6 +53,7 @@ struct erofs_device_info {struct block_device *bdev;struct dax_device *dax_dev;u64 dax_part_off; +struct erofs_fscache_context *ctx;u32 blocks;u32 mapped_blkaddr;
fs/erofs/super.c +19 -8
@@ -250,6 +250,7 @@ static int erofs_init_devices(struct super_block *sb,down_read (&sbi->devs->rwsem);idr_for_each_entry (&sbi->devs->tree, dif, id) {struct block_device *bdev; +struct erofs_fscache_context *ctx; ptr =erofs_read_metabuf (&buf, sb,erofs_blknr (pos),EROFS_KMAP );
@@ -259,15 +260,24 @@ static int erofs_init_devices(struct super_block *sb, } dis = ptr +erofs_blkoff (pos); - bdev =blkdev_get_by_path (dif->path, -FMODE_READ |FMODE_EXCL , - sb->s_type); -if (IS_ERR (bdev)) { - err =PTR_ERR (bdev); -break ; +if (erofs_bdev_mode (sb)) { + bdev =blkdev_get_by_path (dif->path, +FMODE_READ |FMODE_EXCL , + sb->s_type); +if (IS_ERR (bdev)) { + err =PTR_ERR (bdev); +break ; + } + dif->bdev = bdev; + dif->dax_dev =fs_dax_get_by_bdev (bdev, &dif->dax_part_off); + }else { + ctx =erofs_fscache_get_ctx (sb, dif->path, false); +if (IS_ERR (ctx)) { + err =PTR_ERR (ctx); +break ; + } + dif->ctx = ctx; } - dif->bdev = bdev; - dif->dax_dev =fs_dax_get_by_bdev (bdev, &dif->dax_part_off); dif->blocks =le32_to_cpu (dis->blocks); dif->mapped_blkaddr =le32_to_cpu (dis->mapped_blkaddr); sbi->total_blocks += dif->blocks;
@@ -694,6 +704,7 @@ static int erofs_release_device_info(int id, void *ptr, void *data) {struct erofs_device_info *dif = ptr; +erofs_fscache_put_ctx (dif->ctx);fs_put_dax (dif->dax_dev);if (dif->bdev)blkdev_put (dif->bdev,FMODE_READ |FMODE_EXCL );
Signature · 1 line
2.27.0
PATCH v2 18/20 erofs: implement fscache-based data read for data blobs
In reply to Jeffle XuThis patch implements the data plane of reading data from data blob file over fscache.
--- fs/erofs/data.c | 3 +++ fs/erofs/fscache.c | 15 ++++++++++++--- fs/erofs/internal.h | 1 + 3 files changed, 16 insertions(+), 3 deletions(-)
fs/erofs/data.c +3 -0
@@ -200,6 +200,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_ctx =EROFS_SB (sb)->bootstrap;if (map->m_deviceid) {down_read (&devs->rwsem);
@@ -211,6 +212,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_ctx = dif->ctx;up_read (&devs->rwsem); }else if (devs->extra_devices) {down_read (&devs->rwsem);
@@ -228,6 +230,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_ctx = dif->ctx;break ; } }
fs/erofs/fscache.c +12 -3
@@ -144,8 +144,8 @@ static int erofs_fscache_readpage(struct file *file, struct page *page)struct inode *inode = page->mapping->host;struct erofs_inode *vi =EROFS_I (inode);struct super_block *sb = inode->i_sb; -struct erofs_sb_info *sbi =EROFS_SB (sb);struct erofs_map_blocks map; +struct erofs_map_dev mdev;struct erofs_fscache_map fsmap;int ret;
@@ -168,9 +168,18 @@ static int erofs_fscache_readpage(struct file *file, struct page *page)return 0 ; } - fsmap.m_ctx = sbi->bootstrap; + 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; + + fsmap.m_ctx = mdev.m_ctx; fsmap.m_la = map.m_la; - fsmap.m_pa = map.m_pa; + fsmap.m_pa = mdev.m_pa; fsmap.m_llen = map.m_llen;switch (vi->datalayout) {
fs/erofs/internal.h +1 -0
@@ -486,6 +486,7 @@ struct erofs_map_dev {struct block_device *m_bdev;struct dax_device *m_daxdev;u64 m_dax_part_off; +struct erofs_fscache_context *m_ctx;erofs_off_t m_pa;unsigned int m_deviceid;
Signature · 1 line
2.27.0
PATCH v2 19/20 erofs: add 'uuid' mount option
In reply to Jeffle XuIntroduce 'uuid' mount option to enable the nodev mode, in which erofs could be mounted from blob files instead of blkdev. By then users could specify the path of bootstrap blob file containing the complete erofs image.
--- fs/erofs/Kconfig | 2 +- fs/erofs/super.c | 43 ++++++++++++++++++++++++++++++++++++------- 2 files changed, 37 insertions(+), 8 deletions(-)
fs/erofs/Kconfig +1 -1
@@ -2,7 +2,7 @@ config EROFS_FS tristate "EROFS filesystem support" - depends on BLOCK + depends on BLOCK && FSCACHE_ONDEMAND select FS_IOMAP select LIBCRC32C help
fs/erofs/super.c +36 -7
106 lines not on this page · raw message
Signature · 1 line
2.27.0
PATCH v2 20/20 erofs: support on-demand reading
In reply to Jeffle XuImplement the .issue_op() callback, and all work is done by netfs_ondemand_read().
--- fs/erofs/fscache.c | 7 +++++++ 1 file changed, 7 insertions(+)
fs/erofs/fscache.c +7 -0
@@ -28,9 +28,15 @@ static void erofs_noop_cleanup(struct address_space *mapping, void *netfs_priv) { } +static void erofs_issue_op (struct netfs_read_subrequest *subreq) +{ +netfs_ondemand_read (subreq); +} +static const struct netfs_read_request_ops erofs_blob_req_ops = { .begin_cache_operation = erofs_blob_begin_cache_operation, .cleanup = erofs_noop_cleanup, + .issue_op = erofs_issue_op, };static int erofs_begin_cache_operation (struct netfs_read_request *rreq)
@@ -58,6 +64,7 @@ static const struct netfs_read_request_ops erofs_req_ops = { .begin_cache_operation = erofs_begin_cache_operation, .cleanup = erofs_noop_cleanup, .clamp_length = erofs_clamp_length, + .issue_op = erofs_issue_op, };static int erofs_fscache_blob_readpage (struct file *data,struct page *page)
Signature · 1 line
2.27.0
Re: [Linux-cachefs] [PATCH v2 00/20] fscache, erofs: fscache-based demand-read semantics
In reply to Jeffle XuHi David,
Quoted 25 lines · Jeffle Xu
changes since v1:
- rebase to v5.17
- erofs: In chunk based layout, since the logical file offset has the same remainder overPAGE_SIZEwith the corresponding physical address inside the data blob file, the file page cache can be directly transferred to netfs library to contain the data from data blob file. (patch 15) (Gao Xiang)
- netfs,cachefiles: manage logical/physical offset separately. (patch 2) (It is used byerofs_begin_cache_operation()in patch 15.)
- cachefiles: introduce a new devnode specificaly for on-demand reading. (patch 6)
- netfs,fscache,cachefiles: add new CONFIG_* for on-demand reading. (patch 3/5)
- You could start a quick test by
https://github.com/lostjeffle/demand-read-cachefilesd
- add more background information (mainly introduction to nydus) in the "Background" part of this cover letter[Important Issues]
The following issues still need further discussion. Thanks for your time and patience.1. I noticed that there's refactoring of netfs library[1], and patch 1 is not needed since [2].
Quoted 9 lines
2. The current implementation will severely conflict with the refactoring of netfs library[1][2]. The assumption of 'struct
netfs_i_context' [2] is that, every file in the upper netfs will correspond to only one backing file. While in our scenario, one file in erofs can correspond to multiple backing files. That is, the content of one file can be divided into multiple chunks, and are distrubuted over multiple blob files, i.e. multiple backing files. Currently I have no good idea solving this conflic.
Would you mind give more hints on this? Personally, I still think fscache is useful and clean way for image distribution on-demand load use cases in addition to cache network fs data as a more generic in-kernel caching framework. From the point view of current codestat, it has slight modification of netfslib and cachefiles (except for a new daemon):
fs/netfs/Kconfig | 8 + fs/netfs/read_helper.c | 65 ++++++-- include/linux/netfs.h | 10 ++
fs/cachefiles/Kconfig | 8 + fs/cachefiles/daemon.c | 147 ++++++++++++++++- fs/cachefiles/internal.h | 23 +++ fs/cachefiles/io.c | 82 +++++++++- fs/cachefiles/main.c | 27 ++++ fs/cachefiles/namei.c | 60 ++++++-
Besides, I think that cookies can be set according to data mapping (instead of fixed per file) will benefit the following scenario in addition to our on-demand load use cases:
It will benefit file cache data deduplication. What I can see is that
netfslib may have some follow-on development in order to support encryption and compression. However, I think cache data deduplication is also potentially useful to minimize cache storage since many local fses already support reflink. However, I'm not sure if it's a great idea that cachefile relies on underlayfs abilities for cache deduplication. So for cache deduplication scenarios, I'm not sure per-file cookie is still a good idea for us (or alternatively, maintain more complicated mapping per cookie inside fscache besides filesystem mapping, too unnecessary IMO).
By the way, in general, I'm not sure if it's a great idea to cache in per-file basis (especially for too many small files), that is why we introduced data deduplicated blobs. At least, it's simpler for read-only fses. Recently, I found another good article to summarize this:
http://0pointer.net/blog/casync-a-tool-for-distributing-file-system-images.html
Thanks,
Gao Xiang
Re: [Linux-cachefs] [PATCH v2 00/20] fscache, erofs: fscache-based demand-read semantics
Hi David,
Sincerely would you mind sharing if you like this patch set or not? It seems that the use case of file-based on-demand load is quite general. And as Gao Xaing noted, we still prefer fscache to implement this scenario, whilst fscache has well worked as the local cache for remote netfs.
Humbly I'd like to know if this potential new requirement for fscache meets your expectation or future plan for fscache. If it is, then we can improve the patch set in the later versions. Besides let me know if it indeed deviates from the roadmap of fscache.
Thanks,
Jeffle
Quoted 77 lines · Gao Xiang
Hi David,
Quoted 25 lines · Jeffle Xu
changes since v1:
- rebase to v5.17
- erofs: In chunk based layout, since the logical file offset has the same remainder overPAGE_SIZEwith the corresponding physical address inside the data blob file, the file page cache can be directly transferred to netfs library to contain the data from data blob file. (patch 15) (Gao Xiang)
- netfs,cachefiles: manage logical/physical offset separately. (patch 2) (It is used byerofs_begin_cache_operation()in patch 15.)
- cachefiles: introduce a new devnode specificaly for on-demand reading. (patch 6)
- netfs,fscache,cachefiles: add new CONFIG_* for on-demand reading. (patch 3/5)
- You could start a quick test by
https://github.com/lostjeffle/demand-read-cachefilesd
- add more background information (mainly introduction to nydus) in the "Background" part of this cover letter[Important Issues]
The following issues still need further discussion. Thanks for your time and patience.1. I noticed that there's refactoring of netfs library[1], and patch 1 is not needed since [2].
Quoted 9 lines
2. The current implementation will severely conflict with the refactoring of netfs library[1][2]. The assumption of 'struct
netfs_i_context' [2] is that, every file in the upper netfs will correspond to only one backing file. While in our scenario, one file in erofs can correspond to multiple backing files. That is, the content of one file can be divided into multiple chunks, and are distrubuted over multiple blob files, i.e. multiple backing files. Currently I have no good idea solving this conflic.Would you mind give more hints on this? Personally, I still think fscache is useful and clean way for image distribution on-demand load use cases in addition to cache network fs data as a more generic in-kernel caching framework. From the point view of current codestat, it has slight modification of netfslib and cachefiles (except for a new daemon):
fs/netfs/Kconfig | 8 + fs/netfs/read_helper.c | 65 ++++++-- include/linux/netfs.h | 10 ++fs/cachefiles/Kconfig | 8 + fs/cachefiles/daemon.c | 147 ++++++++++++++++- fs/cachefiles/internal.h | 23 +++ fs/cachefiles/io.c | 82 +++++++++- fs/cachefiles/main.c | 27 ++++ fs/cachefiles/namei.c | 60 ++++++-Besides, I think that cookies can be set according to data mapping (instead of fixed per file) will benefit the following scenario in addition to our on-demand load use cases:
It will benefit file cache data deduplication. What I can see is that
netfslib may have some follow-on development in order to support encryption and compression. However, I think cache data deduplication is also potentially useful to minimize cache storage since many local fses already support reflink. However, I'm not sure if it's a great idea that cachefile relies on underlayfs abilities for cache deduplication. So for cache deduplication scenarios, I'm not sure per-file cookie is still a good idea for us (or alternatively, maintain more complicated mapping per cookie inside fscache besides filesystem mapping, too unnecessary IMO).By the way, in general, I'm not sure if it's a great idea to cache in per-file basis (especially for too many small files), that is why we introduced data deduplicated blobs. At least, it's simpler for read-only fses. Recently, I found another good article to summarize this:
http://0pointer.net/blog/casync-a-tool-for-distributing-file-system-images.htmlThanks,
Gao Xiang
Signature · 2 lines
Thanks, Jeffle
David Howells
maintainerQuoted 2 lines · Jeffle Xu
You could start a quick test by
https://github.com/lostjeffle/demand-read-cachefilesd
Can you pull this up to v5.17-rc1 or my netfs-lib branch?
https://git.kernel.org/pub/scm/linux/kernel/git/dhowells/linux-fs.git/log/?h=netfs-lib
I'll do my best to have a look at it tomorrow.
Thanks,
David
Quoted 4 lines · David Howells
Quoted 2 lines · Jeffle Xu
You could start a quick test by
https://github.com/lostjeffle/demand-read-cachefilesd
There is a quick test script in this repo in addition to the daemon (temporarily named with cachefilesd2).
Quoted 4 lines
Can you pull this up to v5.17-rc1 or my netfs-lib branch?
https://git.kernel.org/pub/scm/linux/kernel/git/dhowells/linux-fs.git/log/?h=netfs-lib
While this kernel patch set is basically rebased to v5.17-rc1, rather than netfs-lib branch. I can see there's quite many refactoring for netfs lib in netfs-lib branch.
Quoted 3 lines
I'll do my best to have a look at it tomorrow.
Thanks a lot.
Signature · 2 lines
Thanks, Jeffle
Quoted 9 lines · David Howells
Quoted 2 lines · Jeffle Xu
You could start a quick test by
https://github.com/lostjeffle/demand-read-cachefilesdCan you pull this up to v5.17-rc1 or my netfs-lib branch?
https://git.kernel.org/pub/scm/linux/kernel/git/dhowells/linux-fs.git/log/?h=netfs-lib
Hi, you can check this kernel patch set on the following branch, which has been rebased to v5.17-rc1.
https://github.com/lostjeffle/linux/commits/master
Signature · 2 lines
Thanks, Jeffle
David Howells
maintainerQuoted 6 lines · Jeffle Xu
The following issues still need further discussion. Thanks for your time and patience.
1. I noticed that there's refactoring of netfs library[1],
...
[1] https://git.kernel.org/pub/scm/linux/kernel/git/dhowells/linux-fs.git/log/?h=netfs-lib
Yes. I'm working towards getting netfslib to do handling writes and dio as well as reads, along with content crypto/compression, and the idea I'm aiming towards is that you just point your address_space_ops at netfs directly if possible - but it's going to require its own context now to manage pending writes.
See my netfs-experimental branch for more of that - it's still a work in progress, though.
Btw, you could set rreq->netfs_priv in ->init_rreq() rather than passing it in to netfs_readpage().
Quoted 8 lines
2. The current implementation will severely conflict with the refactoring of netfs library[1][2]. The assumption of 'struct
netfs_i_context' [2] is that, every file in the upper netfs will correspond to only one backing file. While in our scenario, one file in erofs can correspond to multiple backing files. That is, the content of one file can be divided into multiple chunks, and are distrubuted over multiple blob files, i.e. multiple backing files. Currently I have no good idea solving this conflic.
I can think of a couple of options to explore:
(1) Duplicate the cachefiles backend. You can discard a lot of it, since a much of it is concerned with managing local modifications - which you're not going to do since you have a R/O filesystem and you're looking at importing files into the cache externally to the kernel.
I would suggest looking to see if you can do the blob mapping in the backend rather than passing the offset down. Maybe make the cookie index key hold the index too, e.g. "/path/to/file+offset".
Btw, do you still need cachefilesd for its culling duties?
(2) Do you actually need to go through netfslib? Might it be easier to call fscache_read() directly? Have a look at fs/nfs/fscache.c
Quoted 2 lines
Besides there are still two quetions:
- What's the plan of [1]? When is it planned to be merged?
Hopefully next merge window, but that's going to depend on a number of things.
Quoted 3 lines
- It seems that all upper fs using fscache is going to use netfs API, while the APIs like
fscache_read_or_alloc_page()are deprecated. Is that true?
fscache_read_or_alloc_page() is gone completely.
You don't have to use the netfs API. You can talk to fscache directly, doing DIO from the cache to an xarray-class iov_iter constructed from your inode's pagecache.
netfslib provides/will provide a number of services, such as multipage folios, transparent caching, crypto, compression and hiding the existence of pages/folios from the filesystem as entirely as possible. However, you already have some of these implemented on top of iomap for the blockdev interface, it would appear.
David
David Howells
maintainerQuoted 4 lines · David Howells
(1) Duplicate the cachefiles backend. You can discard a lot of it, since a much of it is concerned with managing local modifications - which you're not going to do since you have a R/O filesystem and you're looking at importing files into the cache externally to the kernel.
Take the attached as a start. It's completely untested. I've stripped out anything to do with writing to the cache, making directories, etc. as that can probably be delegated to the on-demand creation. You could drive on-demand creation from the points where it would create files. I've put some "TODO" comments in there as markers.
You could also strip out everything to do with invalidation and also make it just fail if it encounters a file type that it doesn't like or a file that is not correctly labelled for a coherency attribute.
Also, since you aren't intending to write anything or create new files here, there's no need to do the space checking - so I've got rid of all that too.
I've also made it open the backing files read only and got rid of the trimming to I/O blocksize for DIO purposes. The userspace side can take care of that - and, besides, you want to have multiple files within a backing file, right?
You might want to stop it from marking cache *files* in use (but only mark directories). It doesn't matter so much as you aren't going to get coherency issues from having multiple writers to the same file.
You then need to add a file offset member to the erofscache_object struct, set that when the backing file is looked up and add it to the file position in erofscache_read(). You also need to look at erofscache_prepare_read(). If your files are contiguous complete blobs, that can be a lot simpler.
Also, you might want to rename erofscache to something more suitable.
David
---
commit 6fb0e557451e1cd909679fea183822ae92eed67f
Author: David Howells <[email protected]> Date: Tue Jan 25 16:44:10 2022 +0000
erofs: Create specialised cache backend
fs/Kconfig +1 -0
@@ -133,6 +133,7 @@ menu "Caches" source "fs/netfs/Kconfig" source "fs/fscache/Kconfig" source "fs/cachefiles/Kconfig" +source "fs/erofscache/Kconfig" endmenu
fs/Makefile +1 -0
@@ -128,6 +128,7 @@ obj-$(CONFIG_NILFS2_FS) += nilfs2/ obj-$(CONFIG_BEFS_FS) += befs/ obj-$(CONFIG_HOSTFS) += hostfs/ obj-$(CONFIG_CACHEFILES) += cachefiles/ +obj-$(CONFIG_EROFSCACHE) += erofscache/ obj-$(CONFIG_DEBUG_FS) += debugfs/ obj-$(CONFIG_TRACING) += tracefs/ obj-$(CONFIG_OCFS2_FS) += ocfs2/
fs/erofscache/Kconfig new file +28 -0
@@ -0,0 +1,28 @@ +# SPDX-License-Identifier: GPL-2.0-only + +config EROFSCACHE + tristate "Filesystem caching on files" + depends on FSCACHE && BLOCK + help + This permits use of a mounted filesystem as a cache for other + filesystems - primarily networking filesystems - thus allowing fast + local disk to enhance the speed of slower devices. + + See Documentation/filesystems/caching/erofscache.rst for more + information. + +config EROFSCACHE_DEBUG + bool "Debug Erofscache" + depends on EROFSCACHE + help + This permits debugging to be dynamically enabled in the filesystem + caching on files module. If this is set, the debugging output may be + enabled by setting bits in /sys/modules/erofscache/parameter/debug or + by including a debugging specifier in /etc/erofscached.conf. + +config EROFSCACHE_ERROR_INJECTION + bool "Provide error injection for erofscache" + depends on EROFSCACHE && SYSCTL + help + This permits error injection to be enabled in erofscache whilst a + cache is in service.
fs/erofscache/Makefile new file +20 -0
@@ -0,0 +1,20 @@ +# SPDX-License-Identifier: GPL-2.0 +# +# Makefile for on-demand caching for erofs +# + +erofscache-y := \ + cache.o \ + daemon.o \ + interface.o \ + io.o \ + key.o \ + main.o \ + namei.o \ + security.o \ + volume.o \ + xattr.o + +erofscache-$(CONFIG_EROFSCACHE_ERROR_INJECTION) += error_inject.o + +obj-$(CONFIG_EROFSCACHE) := erofscache.o
fs/erofscache/cache.c new file +199 -0
199 lines not on this page · raw message
fs/erofscache/daemon.c new file +525 -0
525 lines not on this page · raw message
fs/erofscache/error_inject.c new file +46 -0
46 lines not on this page · raw message
fs/erofscache/interface.c new file +254 -0
254 lines not on this page · raw message
fs/erofscache/internal.h new file +349 -0
349 lines not on this page · raw message
fs/erofscache/io.c new file +339 -0
339 lines not on this page · raw message
fs/erofscache/key.c new file +138 -0
138 lines not on this page · raw message
fs/erofscache/main.c new file +93 -0
93 lines not on this page · raw message
fs/erofscache/namei.c new file +635 -0
635 lines not on this page · raw message
fs/erofscache/security.c new file +85 -0
85 lines not on this page · raw message
fs/erofscache/volume.c new file +129 -0
129 lines not on this page · raw message
fs/erofscache/xattr.c new file +146 -0
146 lines not on this page · raw message
include/trace/events/erofscache.h new file +515 -0
515 lines not on this page · raw message
The rest of this diff is not shown. View the raw message.
Part of the diff is not shown here. View the raw message.
Quoted 7 lines · David Howells
Quoted 4 lines · David Howells
(1) Duplicate the cachefiles backend. You can discard a lot of it, since a much of it is concerned with managing local modifications - which you're not going to do since you have a R/O filesystem and you're looking at importing files into the cache externally to the kernel.
Quoted 5 lines
Take the attached as a start. It's completely untested. I've stripped out anything to do with writing to the cache, making directories, etc. as that can probably be delegated to the on-demand creation. You could drive on-demand creation from the points where it would create files. I've put some "TODO" comments in there as markers.
Thanks for your inspiring work. Some questions below.
Quoted 18 lines
You could also strip out everything to do with invalidation and also make it just fail if it encounters a file type that it doesn't like or a file that is not correctly labelled for a coherency attribute.
Also, since you aren't intending to write anything or create new files here, there's no need to do the space checking - so I've got rid of all that too.
I've also made it open the backing files read only and got rid of the trimming to I/O blocksize for DIO purposes. The userspace side can take care of that - and, besides, you want to have multiple files within a backing file, right?
You might want to stop it from marking cache *files* in use (but only mark directories). It doesn't matter so much as you aren't going to get coherency issues from having multiple writers to the same file.
Quoted 4 lines
You then need to add a file offset member to the
erofscache_objectstruct, set that when the backing file is looked up and add it to the file position inerofscache_read(). You also need to look aterofscache_prepare_read(). If your files are contiguous complete blobs, that can be a lot simpler.
To be honest, I'm not sure if I get your points correctly. Do you mean each file in erofs has only one chunk (and thus corresponds to only one backing blob file), so that netfs lib can work well while given the only cookie associated with the netfs file?
By the way, let me explain the blob mapping in erofs further. To implement deduplication, one erofs file can be divided into multiple chunks, while these chunks can be distributed over several backing blob files quite randomly (rather than a round-robin style). Each erofs file maintains an on-disk map describing the mapping relationship between chunks and backing blob files. Something like the extent map. Thus there's a multi-to-multi relationship between erofs file and backing blob file.
Thus each erofs file can correspond to multiple cookies in this way, i.e. one 'struct netfs_i_context' can correspond to multiple cookies. Besides, the mapping relationship between chunks and backing blob files is totally implemented in upper fs (i.e. erofs), I have no idea how we can "do the blob mapping in the backend" [1]. So I don't think we can use netfs lib **directly** even with this R/O fscache backend implemented. Please correct me if I misunderstand it.
[1]
https://lore.kernel.org/lkml/[email protected]/T/#mfbb2053476760d8fac723c57dad529192a5084c6
Besides, IMHO it may suffer great challenges when implementing a new R/O backend, since there's quite many code duplication. I know it's just a starting version from scratch, but I'm not sure if it's worth it.
Signature · 2 lines
Thanks, Jeffle
Quoted 9 lines · David Howells
Quoted 6 lines · Jeffle Xu
The following issues still need further discussion. Thanks for your time and patience.
1. I noticed that there's refactoring of netfs library[1],
...
[1] https://git.kernel.org/pub/scm/linux/kernel/git/dhowells/linux-fs.git/log/?h=netfs-lib
Quoted 8 lines
Yes. I'm working towards getting netfslib to do handling writes and dio as well as reads, along with content
crypto/compression, and the idea I'm aiming towards is that you just point youraddress_space_opsat netfs directly if possible - but it's going to require its own context now to manage pending writes.See my netfs-experimental branch for more of that - it's still a work in progress, though.
Got it.
Quoted 22 lines
Btw, you could set
rreq->netfs_privin ->init_rreq() rather than passing it in tonetfs_readpage().Quoted 8 lines
2. The current implementation will severely conflict with the refactoring of netfs library[1][2]. The assumption of 'struct
netfs_i_context' [2] is that, every file in the upper netfs will correspond to only one backing file. While in our scenario, one file in erofs can correspond to multiple backing files. That is, the content of one file can be divided into multiple chunks, and are distrubuted over multiple blob files, i.e. multiple backing files. Currently I have no good idea solving this conflic.I can think of a couple of options to explore:
(1) Duplicate the cachefiles backend. You can discard a lot of it, since a much of it is concerned with managing local modifications - which you're not going to do since you have a R/O filesystem and you're looking at importing files into the cache externally to the kernel.
Quoted 3 lines
I would suggest looking to see if you can do the blob mapping in the backend rather than passing the offset down. Maybe make the cookie index key hold the index too, e.g. "/path/to/file+offset".
Have been discussed in [1].
[1]
https://lore.kernel.org/lkml/[email protected]/T/#m25b1229f96bf24929fb73746a07e9996e8222ac6
"/path/to/file+offset"
^
Besides, what does the 'offset' mean?
Quoted 2 lines
Btw, do you still need cachefilesd for its culling duties?
Yes we still need cache management in this on-demand scenario, in case of backing files exhausting the available blocks. (Though these backing files are prepared by daemon in advance, these files can all be sparse files.) And similarly the actual culling work should be done under protection of S_KERNEL_FILE, so that the culled backing file can't be picked back up.
Quoted 3 lines
(2) Do you actually need to go through netfslib? Might it be easier to call
fscache_read()directly? Have a look atfs/nfs/fscache.c
It would be great if we can use fscache_read() directly.
Quoted 16 lines
Quoted 3 lines
Besides there are still two quetions:
- What's the plan of [1]? When is it planned to be merged?Hopefully next merge window, but that's going to depend on a number of things.
Quoted 3 lines
- It seems that all upper fs using fscache is going to use netfs API, while the APIs like
fscache_read_or_alloc_page()are deprecated. Is that true?
fscache_read_or_alloc_page()is gone completely.You don't have to use the netfs API. You can talk to fscache directly, doing DIO from the cache to an xarray-class
iov_iterconstructed from your inode's pagecache.
Quoted 6 lines
netfslib provides/will provide a number of services, such as multipage folios, transparent caching, crypto, compression and hiding the existence of pages/folios from the filesystem as entirely as possible. However, you already have some of these implemented on top of iomap for the blockdev interface, it would appear.
Got it.
In summary,
1) I prefer option 2, i.e. calling fscache_read() directly, as the one at hand. In this case, the conflict with the netfs lib refactoring can be avoided. Besides, there will be less modification needed to cachefiles/netfs. Patch 1~3 are no longer required, while patch 4~6 are still needed, which mainly introduce the new devnode.
2) Later we can change to option 1, i.e. calling netfs lib and also a potential new R/O backend, if the issues in [1] can be clarified or solved.
[1]
https://lore.kernel.org/lkml/[email protected]/T/#m25b1229f96bf24929fb73746a07e9996e8222ac6
Signature · 2 lines
Thanks, Jeffle
David Howells
maintainerQuoted 4 lines · JeffleXu
"/path/to/file+offset"
^Besides, what does the 'offset' mean?
Assuming you're storing multiple erofs files within the same backend file, you need to tell the the cache backend how to find the data. Assuming the erofs data is arranged such that each erofs file is a single contiguous region, then you need a pathname and a file offset to find one of them.
David
Quoted 5 lines · David Howells
Quoted 3 lines · JeffleXu
"/path/to/file+offset"
^
Quoted 7 lines
Quoted 1 line
Besides, what does the 'offset' mean?
Assuming you're storing multiple erofs files within the same backend file, you need to tell the the cache backend how to find the data. Assuming the erofs data is arranged such that each erofs file is a single contiguous region, then you need a pathname and a file offset to find one of them.
Alright. In fact one erofs file can contain multiple chunks, which can correspond to different backing blob files. Thus currently I will use fscache_read() directly, to push this feature forward.
Thanks a lot.
Signature · 2 lines
Thanks, Jeffle