[PATCH 11/23] cache: pass cache pointer to callbacks

Darrick J. Wong
2025-11-06T22:46:00Z

Pass the cache pointer to the cache node callbacks so that subsequent
patches don't have to waste memory putting pointers to struct fuse4fs in
the cached objects.

Signed-off-by: "Darrick J. Wong" <djwong@kernel.org>
---
lib/support/cache.h | 12 ++++++------
lib/support/cache.c | 21 +++++++++++----------
2 files changed, 17 insertions(+), 16 deletions(-)

diff --git a/lib/support/cache.h b/lib/support/cache.h
index 993f1385dedcee..0168fdca027896 100644
--- a/lib/support/cache.h
+++ b/lib/support/cache.h
@@ -56,16 +56,16 @@ struct cache_node;

typedef void *cache_key_t;

-typedef void (*cache_walk_t)(struct cache_node *);
-typedef struct cache_node * (*cache_node_alloc_t)(cache_key_t);
-typedef int (*cache_node_flush_t)(struct cache_node *);
-typedef void (*cache_node_relse_t)(struct cache_node *);
+typedef void (*cache_walk_t)(struct cache *c, struct cache_node *cn);
+typedef struct cache_node * (*cache_node_alloc_t)(struct cache *c, cache_key_t k);
+typedef int (*cache_node_flush_t)(struct cache *c, struct cache_node *cn);
+typedef void (*cache_node_relse_t)(struct cache *c, struct cache_node *cn);
typedef unsigned int (*cache_node_hash_t)(cache_key_t, unsigned int,
unsigned int);
typedef int (*cache_node_compare_t)(struct cache_node *, cache_key_t);
typedef unsigned int (*cache_bulk_relse_t)(struct cache *, struct list_head *);
-typedef int (*cache_node_get_t)(struct cache_node *);
-typedef void (*cache_node_put_t)(struct cache_node *);
+typedef int (*cache_node_get_t)(struct cache *c, struct cache_node *cn);
+typedef void (*cache_node_put_t)(struct cache *c, struct cache_node *cn);

struct cache_operations {
cache_node_hash_t hash;
diff --git a/lib/support/cache.c b/lib/support/cache.c
index 8b4f9f03c3899b..2e2e36ccc3ef78 100644
--- a/lib/support/cache.c
+++ b/lib/support/cache.c
@@ -111,7 +111,7 @@ cache_walk(
hash = &cache->c_hash[i];
pthread_mutex_lock(&hash->ch_mutex);
list_for_each_entry(pos, &hash->ch_list, cn_hash)
- visit(pos);
+ visit(cache, pos);
pthread_mutex_unlock(&hash->ch_mutex);
}
}
@@ -125,7 +125,8 @@ cache_walk(
#ifdef CACHE_DEBUG
static void
cache_zero_check(
- struct cache_node * node)
+ struct cache *cache,
+ struct cache_node *node)
{
if (node->cn_count > 0) {
fprintf(stderr, "%s: refcount is %u, not zero (node=%p)\n",
@@ -170,7 +171,7 @@ cache_generic_bulkrelse(
node = list_entry(list->next, struct cache_node, cn_mru);
pthread_mutex_destroy(&node->cn_mutex);
list_del_init(&node->cn_mru);
- cache->relse(node);
+ cache->relse(cache, node);
count++;
}

@@ -237,7 +238,7 @@ cache_shake(
continue;

/* memory pressure is not allowed to release dirty objects */
- if (cache->flush(node) && !purge) {
+ if (cache->flush(cache, node) && !purge) {
list_del(&node->cn_mru);
mru->cm_count--;
node->cn_priority = -1;
@@ -302,7 +303,7 @@ cache_node_allocate(
pthread_mutex_unlock(&cache->c_mutex);
if (!nodesfree)
return NULL;
- node = cache->alloc(key);
+ node = cache->alloc(cache, key);
if (node == NULL) { /* uh-oh */
pthread_mutex_lock(&cache->c_mutex);
cache->c_count--;
@@ -341,7 +342,7 @@ __cache_node_purge(
}

/* can't purge dirty objects */
- if (cache->flush(node)) {
+ if (cache->flush(cache, node)) {
pthread_mutex_unlock(&node->cn_mutex);
return 1;
}
@@ -355,7 +356,7 @@ __cache_node_purge(
pthread_mutex_unlock(&node->cn_mutex);
pthread_mutex_destroy(&node->cn_mutex);
list_del_init(&node->cn_hash);
- cache->relse(node);
+ cache->relse(cache, node);
return 0;
}

@@ -410,7 +411,7 @@ cache_node_get(
pthread_mutex_lock(&node->cn_mutex);

if (node->cn_count == 0 && cache->get) {
- int err = cache->get(node);
+ int err = cache->get(cache, node);
if (err) {
pthread_mutex_unlock(&node->cn_mutex);
goto next_object;
@@ -505,7 +506,7 @@ cache_node_put(
node->cn_count--;

if (node->cn_count == 0 && cache->put)
- cache->put(node);
+ cache->put(cache, node);
if (node->cn_count == 0) {
/* add unreferenced node to appropriate MRU for shaker */
mru = &cache->c_mrus[node->cn_priority];
@@ -640,7 +641,7 @@ cache_flush(
pthread_mutex_lock(&hash->ch_mutex);
list_for_each_entry(node, &hash->ch_list, cn_hash) {
pthread_mutex_lock(&node->cn_mutex);
- cache->flush(node);
+ cache->flush(cache, node);
pthread_mutex_unlock(&node->cn_mutex);
}
pthread_mutex_unlock(&hash->ch_mutex);