[PATCH v3 03/22] cachefiles: extract generic function for daemon methods

From: Jeffle Xu
Date: Wed Feb 09 2022 - 01:05:36 EST


... so that the following new devnode can reuse most of the code when
implementing its own methods.

Signed-off-by: Jeffle Xu <jefflexu@xxxxxxxxxxxxxxxxx>
---
fs/cachefiles/daemon.c | 70 +++++++++++++++++++++++++++---------------
1 file changed, 45 insertions(+), 25 deletions(-)

diff --git a/fs/cachefiles/daemon.c b/fs/cachefiles/daemon.c
index 7ac04ee2c0a0..6b8d7c5bbe5d 100644
--- a/fs/cachefiles/daemon.c
+++ b/fs/cachefiles/daemon.c
@@ -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
--
2.27.0