xref: /linux-6.15/include/linux/fscache-cache.h (revision d64f4554)
1 /* SPDX-License-Identifier: GPL-2.0-or-later */
2 /* General filesystem caching backing cache interface
3  *
4  * Copyright (C) 2021 Red Hat, Inc. All Rights Reserved.
5  * Written by David Howells ([email protected])
6  *
7  * NOTE!!! See:
8  *
9  *	Documentation/filesystems/caching/backend-api.rst
10  *
11  * for a description of the cache backend interface declared here.
12  */
13 
14 #ifndef _LINUX_FSCACHE_CACHE_H
15 #define _LINUX_FSCACHE_CACHE_H
16 
17 #include <linux/fscache.h>
18 
19 enum fscache_cache_trace;
20 enum fscache_cookie_trace;
21 enum fscache_access_trace;
22 
23 enum fscache_cache_state {
24 	FSCACHE_CACHE_IS_NOT_PRESENT,	/* No cache is present for this name */
25 	FSCACHE_CACHE_IS_PREPARING,	/* A cache is preparing to come live */
26 	FSCACHE_CACHE_IS_ACTIVE,	/* Attached cache is active and can be used */
27 	FSCACHE_CACHE_GOT_IOERROR,	/* Attached cache stopped on I/O error */
28 	FSCACHE_CACHE_IS_WITHDRAWN,	/* Attached cache is being withdrawn */
29 #define NR__FSCACHE_CACHE_STATE (FSCACHE_CACHE_IS_WITHDRAWN + 1)
30 };
31 
32 /*
33  * Cache cookie.
34  */
35 struct fscache_cache {
36 	const struct fscache_cache_ops *ops;
37 	struct list_head	cache_link;	/* Link in cache list */
38 	void			*cache_priv;	/* Private cache data (or NULL) */
39 	refcount_t		ref;
40 	atomic_t		n_volumes;	/* Number of active volumes; */
41 	atomic_t		n_accesses;	/* Number of in-progress accesses on the cache */
42 	atomic_t		object_count;	/* no. of live objects in this cache */
43 	unsigned int		debug_id;
44 	enum fscache_cache_state state;
45 	char			*name;
46 };
47 
48 /*
49  * cache operations
50  */
51 struct fscache_cache_ops {
52 	/* name of cache provider */
53 	const char *name;
54 
55 	/* Acquire a volume */
56 	void (*acquire_volume)(struct fscache_volume *volume);
57 
58 	/* Free the cache's data attached to a volume */
59 	void (*free_volume)(struct fscache_volume *volume);
60 
61 	/* Look up a cookie in the cache */
62 	bool (*lookup_cookie)(struct fscache_cookie *cookie);
63 
64 	/* Withdraw an object without any cookie access counts held */
65 	void (*withdraw_cookie)(struct fscache_cookie *cookie);
66 
67 	/* Invalidate an object */
68 	bool (*invalidate_cookie)(struct fscache_cookie *cookie);
69 
70 	/* Begin an operation for the netfs lib */
71 	bool (*begin_operation)(struct netfs_cache_resources *cres,
72 				enum fscache_want_state want_state);
73 
74 	/* Prepare to write to a live cache object */
75 	void (*prepare_to_write)(struct fscache_cookie *cookie);
76 };
77 
78 extern struct workqueue_struct *fscache_wq;
79 
80 /*
81  * out-of-line cache backend functions
82  */
83 extern struct rw_semaphore fscache_addremove_sem;
84 extern struct fscache_cache *fscache_acquire_cache(const char *name);
85 extern void fscache_relinquish_cache(struct fscache_cache *cache);
86 extern int fscache_add_cache(struct fscache_cache *cache,
87 			     const struct fscache_cache_ops *ops,
88 			     void *cache_priv);
89 extern void fscache_withdraw_cache(struct fscache_cache *cache);
90 extern void fscache_withdraw_volume(struct fscache_volume *volume);
91 extern void fscache_withdraw_cookie(struct fscache_cookie *cookie);
92 
93 extern void fscache_io_error(struct fscache_cache *cache);
94 
95 extern void fscache_end_volume_access(struct fscache_volume *volume,
96 				      struct fscache_cookie *cookie,
97 				      enum fscache_access_trace why);
98 
99 extern struct fscache_cookie *fscache_get_cookie(struct fscache_cookie *cookie,
100 						 enum fscache_cookie_trace where);
101 extern void fscache_put_cookie(struct fscache_cookie *cookie,
102 			       enum fscache_cookie_trace where);
103 extern void fscache_end_cookie_access(struct fscache_cookie *cookie,
104 				      enum fscache_access_trace why);
105 extern void fscache_cookie_lookup_negative(struct fscache_cookie *cookie);
106 extern void fscache_resume_after_invalidation(struct fscache_cookie *cookie);
107 extern void fscache_caching_failed(struct fscache_cookie *cookie);
108 extern bool fscache_wait_for_operation(struct netfs_cache_resources *cred,
109 				       enum fscache_want_state state);
110 
111 /**
112  * fscache_cookie_state - Read the state of a cookie
113  * @cookie: The cookie to query
114  *
115  * Get the state of a cookie, imposing an ordering between the cookie contents
116  * and the state value.  Paired with fscache_set_cookie_state().
117  */
118 static inline
119 enum fscache_cookie_state fscache_cookie_state(struct fscache_cookie *cookie)
120 {
121 	return smp_load_acquire(&cookie->state);
122 }
123 
124 /**
125  * fscache_get_key - Get a pointer to the cookie key
126  * @cookie: The cookie to query
127  *
128  * Return a pointer to the where a cookie's key is stored.
129  */
130 static inline void *fscache_get_key(struct fscache_cookie *cookie)
131 {
132 	if (cookie->key_len <= sizeof(cookie->inline_key))
133 		return cookie->inline_key;
134 	else
135 		return cookie->key;
136 }
137 
138 static inline struct fscache_cookie *fscache_cres_cookie(struct netfs_cache_resources *cres)
139 {
140 	return cres->cache_priv;
141 }
142 
143 #endif /* _LINUX_FSCACHE_CACHE_H */
144