fscache,erofs: fscache-based demand-read semantics (fscache part)
Subsystems (5)
v3 cover fscache,erofs: fscache-based demand-read semantics (fscache part)
Hi David,
This patchset is from [1], which is all fscache part related, in prep for the newly introduced on-demand read semantics.
The previous patch 5 [2] from [1] is still under modification according to the comments from Greg. Thus it is not included in this patchset yet, and will be sent out later.
Whilst these four patches in this patchset are basically trivial cleanup codes. I do appreciate if you coud give some hint on this work.
Thanks.
[1] https://lore.kernel.org/all/[email protected]/T/
[2] https://lore.kernel.org/all/[email protected]/T/#ma8898ba06bf66d815925a32d1d6689d71346e609
Jeffle Xu (4):
fscache: export fscache_end_operation()
fscache: add a method to support on-demand read semantics
cachefiles: extract generic function for daemon methods
cachefiles: detect backing file size in on-demand read mode
Documentation/filesystems/netfs_library.rst | 17 +++++ fs/cachefiles/Kconfig | 13 ++++ fs/cachefiles/daemon.c | 70 +++++++++++++-------- fs/cachefiles/internal.h | 1 + fs/cachefiles/namei.c | 60 +++++++++++++++++- fs/fscache/internal.h | 11 ---- fs/nfs/fscache.c | 8 --- include/linux/fscache.h | 39 ++++++++++++ include/linux/netfs.h | 4 ++ 9 files changed, 178 insertions(+), 45 deletions(-)
Signature · 1 line
2.27.0
PATCH RESEND v3 1/4 fscache: export fscache_end_operation()
Export fscache_end_operation() to avoid code duplication.
Besides, considering the paired fscache_begin_read_operation() is already exported, it shall make sense to also export fscache_end_operation().
--- fs/fscache/internal.h | 11 ----------- fs/nfs/fscache.c | 8 -------- include/linux/fscache.h | 14 ++++++++++++++ 3 files changed, 14 insertions(+), 19 deletions(-)
fs/fscache/internal.h +0 -11
@@ -70,17 +70,6 @@ static inline void fscache_see_cookie(struct fscache_cookie *cookie, where); } -/* -* io.c -*/ -static inline void fscache_end_operation (struct netfs_cache_resources *cres) -{ -const struct netfs_cache_ops *ops =fscache_operation_valid (cres); - -if (ops) - ops->end_operation (cres); -} -/* * main.c */
fs/nfs/fscache.c +0 -8
@@ -249,14 +249,6 @@ void nfs_fscache_release_file(struct inode *inode, struct file *filp) } } -static inline void fscache_end_operation (struct netfs_cache_resources *cres) -{ -const struct netfs_cache_ops *ops =fscache_operation_valid (cres); - -if (ops) - ops->end_operation (cres); -} -/* * Fallback page reading interface. */
include/linux/fscache.h +14 -0
@@ -456,6 +456,20 @@ int fscache_begin_read_operation(struct netfs_cache_resources *cres,return -ENOBUFS ; } +/** +* fscache_end_operation - End the read operation for the netfs lib +* @cres: The cache resources for the read operation +* +* Clean up the resources at the end of the read request. +*/ +static inline void fscache_end_operation (struct netfs_cache_resources *cres) +{ +const struct netfs_cache_ops *ops =fscache_operation_valid (cres); + +if (ops) + ops->end_operation (cres); +} +/** * fscache_read - Start a read from the cache. * @cres: The cache resources to use
Signature · 1 line
2.27.0
PATCH RESEND v3 2/4 fscache: add a method to support on-demand read semantics
In reply to Jeffle XuAdd .ondemand_read() callback to netfs_cache_ops to implement on-demand read.
The precondition for implementing on-demand read semantics 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). Then .ondemand_read() callback can be called to notify backend to prepare the data.
The implementation of .ondemand_read() callback can be backend specific. The following patch will introduce the implementation 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, users can retry to read from the backing file.
--- Documentation/filesystems/netfs_library.rst | 17 ++++++++++++++ include/linux/fscache.h | 25 +++++++++++++++++++++ include/linux/netfs.h | 4 ++++ 3 files changed, 46 insertions(+)
Documentation/filesystems/netfs_library.rst +17 -0
@@ -466,6 +466,8 @@ operation table looks like the following:: int (*query_occupancy)(struct netfs_cache_resources *cres, loff_t start, size_t len, size_t granularity, loff_t *_data_start, size_t *_data_len); + int (*ondemand_read)(struct netfs_cache_resources *cres, + loff_t start_pos, size_t len); }; With a termination handler function pointer::
@@ -552,6 +554,21 @@ The methods defined in the table are: It returns 0 if some data was found, -ENODATA if there was no usable data within the region or -ENOBUFS if there is no caching on this file. + * ``ondemand_read()`` + + [Optional] Called to prepare cache for the requested data. It shall be called + only when on-demand read semantics is required. It will be called when a cache + miss is encountered. The function will make the backend prepare the data + regarding to @start_pos/@len of the cache file. It may get blocked until the + backend finishes getting the requested data or returns errors. + + Once it returns with 0, it is guaranteed that the requested data has been + ready in the cache file. In this case, users can get the data with another + read request. + + It returns 0 if data has been ready in the cache file, or other error code + from the cache, such as -ENOMEM. + Note that these methods are passed a pointer to the cache resource structure, not the read request structure as they could be used in other situations where there isn't a read request structure as well, such as writing dirty data to the
include/linux/fscache.h +25 -0
@@ -514,6 +514,31 @@ int fscache_read(struct netfs_cache_resources *cres, term_func, term_func_priv); } +/** +* fscache_ondemand_read - Prepare cache for the requested data. +* @cres: The cache resources to use +* @start_pos: The beginning file offset in the cache file +* @len: The length of the file offset range in the cache file +* +* This shall only be called when a cache miss is encountered. It will make +* the backend prepare the data regarding to @start_pos/@len of the cache file. +* It may get blocked until the backend finishes getting the requested data or +* returns errors. +* +* Returns: +* * 0 - Success (Data is ready in the cache file) +* * Other error code from the cache, such as -ENOMEM. +*/ +static inline +int fscache_ondemand_read (struct netfs_cache_resources *cres, +loff_t start_pos,size_t len) +{ +const struct netfs_cache_ops *ops =fscache_operation_valid (cres); +if (ops->ondemand_read) +return ops->ondemand_read (cres, start_pos, len); +return -EOPNOTSUPP ; +} +/** * fscache_begin_write_operation - Begin a write operation for the netfs lib * @cres: The cache resources for the write being performed
include/linux/netfs.h +4 -0
@@ -251,6 +251,10 @@ struct netfs_cache_ops {int (*query_occupancy)(struct netfs_cache_resources *cres,loff_t start,size_t len,size_t granularity,loff_t *_data_start,size_t *_data_len); + +/* Prepare cache for the requested data */ +int (*ondemand_read)(struct netfs_cache_resources *cres, +loff_t start_pos,size_t len); };struct readahead_control ;
Signature · 1 line
2.27.0
PATCH RESEND v3 3/4 cachefiles: extract generic function for daemon methods
In reply to Jeffle Xu... so that the following new devnode can reuse most of the code when implementing its own methods.
--- fs/cachefiles/daemon.c | 70 +++++++++++++++++++++++++++--------------- 1 file changed, 45 insertions(+), 25 deletions(-)
fs/cachefiles/daemon.c +45 -25
@@ -78,6 +78,34 @@ static const struct cachefiles_daemon_cmd cachefiles_daemon_cmds[] = { { "", NULL } }; +static struct cachefiles_cache *cachefiles_daemon_open_cache (void ) +{ +struct cachefiles_cache *cache; + +/* allocate a cache record */ + cache =kzalloc (sizeof (struct cachefiles_cache ),GFP_KERNEL ); +if (cache) { +mutex_init (&cache->daemon_mutex); +init_waitqueue_head (&cache->daemon_pollwq); +INIT_LIST_HEAD (&cache->volumes); +INIT_LIST_HEAD (&cache->object_list); +spin_lock_init (&cache->object_list_lock); + +/* set default caching limits +* - limit at 1% free space and/or free files +* - cull below 5% free space and/or free files +* - cease culling above 7% free space and/or free files +*/ + cache->frun_percent =7 ; + cache->fcull_percent =5 ; + cache->fstop_percent =1 ; + cache->brun_percent =7 ; + cache->bcull_percent =5 ; + cache->bstop_percent =1 ; + } + +return cache; +}/* * Prepare a cache for caching.
@@ -96,31 +124,13 @@ static int cachefiles_daemon_open(struct inode *inode, struct file *file)if (xchg (&cachefiles_open,1 ) ==1 )return -EBUSY ; -/* allocate a cache record */ - cache =kzalloc (sizeof (struct cachefiles_cache ),GFP_KERNEL ); + + cache =cachefiles_daemon_open_cache ();if (!cache) { cachefiles_open =0 ;return -ENOMEM ; } -mutex_init (&cache->daemon_mutex); -init_waitqueue_head (&cache->daemon_pollwq); -INIT_LIST_HEAD (&cache->volumes); -INIT_LIST_HEAD (&cache->object_list); -spin_lock_init (&cache->object_list_lock); - -/* set default caching limits -* - limit at 1% free space and/or free files -* - cull below 5% free space and/or free files -* - cease culling above 7% free space and/or free files -*/ - cache->frun_percent =7 ; - cache->fcull_percent =5 ; - cache->fstop_percent =1 ; - cache->brun_percent =7 ; - cache->bcull_percent =5 ; - cache->bstop_percent =1 ; - file->private_data = cache; cache->cachefilesd = file;return 0 ;
@@ -209,10 +219,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 +272,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 +295,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 RESEND v3 4/4 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.
It is worth nothing that, in this scenario, user daemon is responsible for preparing all backing files with correct file size in the first beginning. (Backing files are all sparse files in this case). And since it's read-only, we can get the backing file size at runtime as the object size.
This patch also adds one flag bit to distinguish the new introduced on-demand read mode from the original mode. The following patch will introduce a user configures it.
--- fs/cachefiles/Kconfig | 13 +++++++++ fs/cachefiles/internal.h | 1 + fs/cachefiles/namei.c | 60 +++++++++++++++++++++++++++++++++++++++- 3 files changed, 73 insertions(+), 1 deletion(-)
fs/cachefiles/Kconfig +13 -0
@@ -26,3 +26,16 @@ 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 + default n + help + This permits on-demand read mode of cachefiles. In this mode, when + cache miss, the cachefiles backend instead of the upper fs using + fscache is responsible for fetching data, e.g. through user daemon. + Then after the data's ready, upper fs can reinitiate a read from the + cache. + + 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