cachefiles: Provide a function to check how much space there is
authorDavid Howells <dhowells@redhat.com>
Thu, 21 Oct 2021 07:59:46 +0000 (08:59 +0100)
committerDavid Howells <dhowells@redhat.com>
Fri, 7 Jan 2022 13:41:26 +0000 (13:41 +0000)
Provide a function to check how much space there is.  This also flips the
state on the cache and will signal the daemon to inform it of the change
and to ask it to do some culling if necessary.

We will also need to subtract the amount of data currently being written to
the cache (cache->b_writing) from the amount of available space to avoid
hitting ENOSPC accidentally.

Signed-off-by: David Howells <dhowells@redhat.com>
Reviewed-by: Jeff Layton <jlayton@kernel.org>
cc: linux-cachefs@redhat.com
Link: https://lore.kernel.org/r/163819629322.215744.13457425294680841213.stgit@warthog.procyon.org.uk/
Link: https://lore.kernel.org/r/163906930100.143852.1681026700865762069.stgit@warthog.procyon.org.uk/
Link: https://lore.kernel.org/r/163967140058.1823006.7781243664702837128.stgit@warthog.procyon.org.uk/
Link: https://lore.kernel.org/r/164021539957.640689.12477177372616805706.stgit@warthog.procyon.org.uk/
fs/cachefiles/Makefile
fs/cachefiles/cache.c [new file with mode: 0644]
fs/cachefiles/daemon.c
fs/cachefiles/internal.h

index f008524..463e3d6 100644 (file)
@@ -4,6 +4,7 @@
 #
 
 cachefiles-y := \
+       cache.o \
        daemon.o \
        main.o \
        security.o
diff --git a/fs/cachefiles/cache.c b/fs/cachefiles/cache.c
new file mode 100644 (file)
index 0000000..73636f8
--- /dev/null
@@ -0,0 +1,103 @@
+// SPDX-License-Identifier: GPL-2.0-or-later
+/* Manage high-level VFS aspects of a cache.
+ *
+ * Copyright (C) 2007, 2021 Red Hat, Inc. All Rights Reserved.
+ * Written by David Howells (dhowells@redhat.com)
+ */
+
+#include <linux/slab.h>
+#include <linux/statfs.h>
+#include <linux/namei.h>
+#include "internal.h"
+
+/*
+ * See if we have space for a number of pages and/or a number of files in the
+ * cache
+ */
+int cachefiles_has_space(struct cachefiles_cache *cache,
+                        unsigned fnr, unsigned bnr)
+{
+       struct kstatfs stats;
+       u64 b_avail, b_writing;
+       int ret;
+
+       struct path path = {
+               .mnt    = cache->mnt,
+               .dentry = cache->mnt->mnt_root,
+       };
+
+       //_enter("{%llu,%llu,%llu,%llu,%llu,%llu},%u,%u",
+       //       (unsigned long long) cache->frun,
+       //       (unsigned long long) cache->fcull,
+       //       (unsigned long long) cache->fstop,
+       //       (unsigned long long) cache->brun,
+       //       (unsigned long long) cache->bcull,
+       //       (unsigned long long) cache->bstop,
+       //       fnr, bnr);
+
+       /* find out how many pages of blockdev are available */
+       memset(&stats, 0, sizeof(stats));
+
+       ret = vfs_statfs(&path, &stats);
+       if (ret < 0) {
+               trace_cachefiles_vfs_error(NULL, d_inode(path.dentry), ret,
+                                          cachefiles_trace_statfs_error);
+               if (ret == -EIO)
+                       cachefiles_io_error(cache, "statfs failed");
+               _leave(" = %d", ret);
+               return ret;
+       }
+
+       b_avail = stats.f_bavail >> cache->bshift;
+       b_writing = atomic_long_read(&cache->b_writing);
+       if (b_avail > b_writing)
+               b_avail -= b_writing;
+       else
+               b_avail = 0;
+
+       //_debug("avail %llu,%llu",
+       //       (unsigned long long)stats.f_ffree,
+       //       (unsigned long long)b_avail);
+
+       /* see if there is sufficient space */
+       if (stats.f_ffree > fnr)
+               stats.f_ffree -= fnr;
+       else
+               stats.f_ffree = 0;
+
+       if (b_avail > bnr)
+               b_avail -= bnr;
+       else
+               b_avail = 0;
+
+       ret = -ENOBUFS;
+       if (stats.f_ffree < cache->fstop ||
+           b_avail < cache->bstop)
+               goto begin_cull;
+
+       ret = 0;
+       if (stats.f_ffree < cache->fcull ||
+           b_avail < cache->bcull)
+               goto begin_cull;
+
+       if (test_bit(CACHEFILES_CULLING, &cache->flags) &&
+           stats.f_ffree >= cache->frun &&
+           b_avail >= cache->brun &&
+           test_and_clear_bit(CACHEFILES_CULLING, &cache->flags)
+           ) {
+               _debug("cease culling");
+               cachefiles_state_changed(cache);
+       }
+
+       //_leave(" = 0");
+       return 0;
+
+begin_cull:
+       if (!test_and_set_bit(CACHEFILES_CULLING, &cache->flags)) {
+               _debug("### CULL CACHE ###");
+               cachefiles_state_changed(cache);
+       }
+
+       _leave(" = %d", ret);
+       return ret;
+}
index 4cfb7c8..7d46916 100644 (file)
@@ -167,7 +167,7 @@ static ssize_t cachefiles_daemon_read(struct file *file, char __user *_buffer,
                return 0;
 
        /* check how much space the cache has */
-       // PLACEHOLDER: Check space
+       cachefiles_has_space(cache, 0, 0);
 
        /* summarise */
        f_released = atomic_xchg(&cache->f_released, 0);
index 7fd5429..3783a3e 100644 (file)
@@ -39,6 +39,7 @@ struct cachefiles_cache {
        atomic_t                        gravecounter;   /* graveyard uniquifier */
        atomic_t                        f_released;     /* number of objects released lately */
        atomic_long_t                   b_released;     /* number of blocks released lately */
+       atomic_long_t                   b_writing;      /* Number of blocks being written */
        unsigned                        frun_percent;   /* when to stop culling (% files) */
        unsigned                        fcull_percent;  /* when to start culling (% files) */
        unsigned                        fstop_percent;  /* when to stop allocating (% files) */
@@ -74,6 +75,12 @@ static inline void cachefiles_state_changed(struct cachefiles_cache *cache)
        wake_up_all(&cache->daemon_pollwq);
 }
 
+/*
+ * cache.c
+ */
+extern int cachefiles_has_space(struct cachefiles_cache *cache,
+                               unsigned fnr, unsigned bnr);
+
 /*
  * daemon.c
  */