1 /* SPDX-License-Identifier: GPL-2.0-or-later */
2 /* General netfs cache on cache files internal defs
4 * Copyright (C) 2021 Red Hat, Inc. All Rights Reserved.
5 * Written by David Howells (dhowells@redhat.com)
12 #define pr_fmt(fmt) "CacheFiles: " fmt
15 #include <linux/fscache-cache.h>
16 #include <linux/cred.h>
17 #include <linux/security.h>
18 #include <linux/xarray.h>
19 #include <linux/cachefiles.h>
21 #define CACHEFILES_DIO_BLOCK_SIZE 4096
23 struct cachefiles_cache;
24 struct cachefiles_object;
26 enum cachefiles_content {
27 /* These values are saved on disk */
28 CACHEFILES_CONTENT_NO_DATA = 0, /* No content stored */
29 CACHEFILES_CONTENT_SINGLE = 1, /* Content is monolithic, all is present */
30 CACHEFILES_CONTENT_ALL = 2, /* Content is all present, no map */
31 CACHEFILES_CONTENT_BACKFS_MAP = 3, /* Content is piecemeal, mapped through backing fs */
32 CACHEFILES_CONTENT_DIRTY = 4, /* Content is dirty (only seen on disk) */
33 nr__cachefiles_content
37 * Cached volume representation.
39 struct cachefiles_volume {
40 struct cachefiles_cache *cache;
41 struct list_head cache_link; /* Link in cache->volumes */
42 struct fscache_volume *vcookie; /* The netfs's representation */
43 struct dentry *dentry; /* The volume dentry */
44 struct dentry *fanout[256]; /* Fanout subdirs */
50 struct cachefiles_object {
51 struct fscache_cookie *cookie; /* Netfs data storage object cookie */
52 struct cachefiles_volume *volume; /* Cache volume that holds this object */
53 struct list_head cache_link; /* Link in cache->*_list */
54 struct file *file; /* The file representing this object */
55 char *d_name; /* Backing file name */
59 u8 d_name_len; /* Length of filename */
60 enum cachefiles_content content_info:8; /* Info about content presence */
62 #define CACHEFILES_OBJECT_USING_TMPFILE 0 /* Have an unlinked tmpfile */
63 #ifdef CONFIG_CACHEFILES_ONDEMAND
68 #define CACHEFILES_ONDEMAND_ID_CLOSED -1
71 * Cache files cache definition
73 struct cachefiles_cache {
74 struct fscache_cache *cache; /* Cache cookie */
75 struct vfsmount *mnt; /* mountpoint holding the cache */
76 struct dentry *store; /* Directory into which live objects go */
77 struct dentry *graveyard; /* directory into which dead objects go */
78 struct file *cachefilesd; /* manager daemon handle */
79 struct list_head volumes; /* List of volume objects */
80 struct list_head object_list; /* List of active objects */
81 spinlock_t object_list_lock; /* Lock for volumes and object_list */
82 const struct cred *cache_cred; /* security override for accessing cache */
83 struct mutex daemon_mutex; /* command serialisation mutex */
84 wait_queue_head_t daemon_pollwq; /* poll waitqueue for daemon */
85 atomic_t gravecounter; /* graveyard uniquifier */
86 atomic_t f_released; /* number of objects released lately */
87 atomic_long_t b_released; /* number of blocks released lately */
88 atomic_long_t b_writing; /* Number of blocks being written */
89 unsigned frun_percent; /* when to stop culling (% files) */
90 unsigned fcull_percent; /* when to start culling (% files) */
91 unsigned fstop_percent; /* when to stop allocating (% files) */
92 unsigned brun_percent; /* when to stop culling (% blocks) */
93 unsigned bcull_percent; /* when to start culling (% blocks) */
94 unsigned bstop_percent; /* when to stop allocating (% blocks) */
95 unsigned bsize; /* cache's block size */
96 unsigned bshift; /* ilog2(bsize) */
97 uint64_t frun; /* when to stop culling */
98 uint64_t fcull; /* when to start culling */
99 uint64_t fstop; /* when to stop allocating */
100 sector_t brun; /* when to stop culling */
101 sector_t bcull; /* when to start culling */
102 sector_t bstop; /* when to stop allocating */
104 #define CACHEFILES_READY 0 /* T if cache prepared */
105 #define CACHEFILES_DEAD 1 /* T if cache dead */
106 #define CACHEFILES_CULLING 2 /* T if cull engaged */
107 #define CACHEFILES_STATE_CHANGED 3 /* T if state changed (poll trigger) */
108 #define CACHEFILES_ONDEMAND_MODE 4 /* T if in on-demand read mode */
109 char *rootdirname; /* name of cache root directory */
110 char *secctx; /* LSM security context */
111 char *tag; /* cache binding tag */
112 refcount_t unbind_pincount;/* refcount to do daemon unbind */
113 struct xarray reqs; /* xarray of pending on-demand requests */
114 struct xarray ondemand_ids; /* xarray for ondemand_id allocation */
115 u32 ondemand_id_next;
118 static inline bool cachefiles_in_ondemand_mode(struct cachefiles_cache *cache)
120 return IS_ENABLED(CONFIG_CACHEFILES_ONDEMAND) &&
121 test_bit(CACHEFILES_ONDEMAND_MODE, &cache->flags);
124 struct cachefiles_req {
125 struct cachefiles_object *object;
126 struct completion done;
128 struct cachefiles_msg msg;
131 #define CACHEFILES_REQ_NEW XA_MARK_1
133 #include <trace/events/cachefiles.h>
136 struct file *cachefiles_cres_file(struct netfs_cache_resources *cres)
138 return cres->cache_priv2;
142 struct cachefiles_object *cachefiles_cres_object(struct netfs_cache_resources *cres)
144 return fscache_cres_cookie(cres)->cache_priv;
148 * note change of state for daemon
150 static inline void cachefiles_state_changed(struct cachefiles_cache *cache)
152 set_bit(CACHEFILES_STATE_CHANGED, &cache->flags);
153 wake_up_all(&cache->daemon_pollwq);
159 extern int cachefiles_add_cache(struct cachefiles_cache *cache);
160 extern void cachefiles_withdraw_cache(struct cachefiles_cache *cache);
162 enum cachefiles_has_space_for {
163 cachefiles_has_space_check,
164 cachefiles_has_space_for_write,
165 cachefiles_has_space_for_create,
167 extern int cachefiles_has_space(struct cachefiles_cache *cache,
168 unsigned fnr, unsigned bnr,
169 enum cachefiles_has_space_for reason);
174 extern const struct file_operations cachefiles_daemon_fops;
175 extern void cachefiles_get_unbind_pincount(struct cachefiles_cache *cache);
176 extern void cachefiles_put_unbind_pincount(struct cachefiles_cache *cache);
181 #ifdef CONFIG_CACHEFILES_ERROR_INJECTION
182 extern unsigned int cachefiles_error_injection_state;
183 extern int cachefiles_register_error_injection(void);
184 extern void cachefiles_unregister_error_injection(void);
187 #define cachefiles_error_injection_state 0
189 static inline int cachefiles_register_error_injection(void)
194 static inline void cachefiles_unregister_error_injection(void)
200 static inline int cachefiles_inject_read_error(void)
202 return cachefiles_error_injection_state & 2 ? -EIO : 0;
205 static inline int cachefiles_inject_write_error(void)
207 return cachefiles_error_injection_state & 2 ? -EIO :
208 cachefiles_error_injection_state & 1 ? -ENOSPC :
212 static inline int cachefiles_inject_remove_error(void)
214 return cachefiles_error_injection_state & 2 ? -EIO : 0;
220 extern const struct fscache_cache_ops cachefiles_cache_ops;
221 extern void cachefiles_see_object(struct cachefiles_object *object,
222 enum cachefiles_obj_ref_trace why);
223 extern struct cachefiles_object *cachefiles_grab_object(struct cachefiles_object *object,
224 enum cachefiles_obj_ref_trace why);
225 extern void cachefiles_put_object(struct cachefiles_object *object,
226 enum cachefiles_obj_ref_trace why);
231 extern bool cachefiles_begin_operation(struct netfs_cache_resources *cres,
232 enum fscache_want_state want_state);
233 extern int __cachefiles_prepare_write(struct cachefiles_object *object,
235 loff_t *_start, size_t *_len,
236 bool no_space_allocated_yet);
237 extern int __cachefiles_write(struct cachefiles_object *object,
240 struct iov_iter *iter,
241 netfs_io_terminated_t term_func,
242 void *term_func_priv);
247 extern bool cachefiles_cook_key(struct cachefiles_object *object);
252 extern struct kmem_cache *cachefiles_object_jar;
257 extern void cachefiles_unmark_inode_in_use(struct cachefiles_object *object,
259 extern int cachefiles_bury_object(struct cachefiles_cache *cache,
260 struct cachefiles_object *object,
263 enum fscache_why_object_killed why);
264 extern int cachefiles_delete_object(struct cachefiles_object *object,
265 enum fscache_why_object_killed why);
266 extern bool cachefiles_look_up_object(struct cachefiles_object *object);
267 extern struct dentry *cachefiles_get_directory(struct cachefiles_cache *cache,
271 extern void cachefiles_put_directory(struct dentry *dir);
273 extern int cachefiles_cull(struct cachefiles_cache *cache, struct dentry *dir,
276 extern int cachefiles_check_in_use(struct cachefiles_cache *cache,
277 struct dentry *dir, char *filename);
278 extern struct file *cachefiles_create_tmpfile(struct cachefiles_object *object);
279 extern bool cachefiles_commit_tmpfile(struct cachefiles_cache *cache,
280 struct cachefiles_object *object);
285 #ifdef CONFIG_CACHEFILES_ONDEMAND
286 extern ssize_t cachefiles_ondemand_daemon_read(struct cachefiles_cache *cache,
287 char __user *_buffer, size_t buflen);
289 extern int cachefiles_ondemand_copen(struct cachefiles_cache *cache,
292 extern int cachefiles_ondemand_init_object(struct cachefiles_object *object);
293 extern void cachefiles_ondemand_clean_object(struct cachefiles_object *object);
295 extern int cachefiles_ondemand_read(struct cachefiles_object *object,
296 loff_t pos, size_t len);
299 static inline ssize_t cachefiles_ondemand_daemon_read(struct cachefiles_cache *cache,
300 char __user *_buffer, size_t buflen)
305 static inline int cachefiles_ondemand_init_object(struct cachefiles_object *object)
310 static inline void cachefiles_ondemand_clean_object(struct cachefiles_object *object)
314 static inline int cachefiles_ondemand_read(struct cachefiles_object *object,
315 loff_t pos, size_t len)
324 extern int cachefiles_get_security_ID(struct cachefiles_cache *cache);
325 extern int cachefiles_determine_cache_security(struct cachefiles_cache *cache,
327 const struct cred **_saved_cred);
329 static inline void cachefiles_begin_secure(struct cachefiles_cache *cache,
330 const struct cred **_saved_cred)
332 *_saved_cred = override_creds(cache->cache_cred);
335 static inline void cachefiles_end_secure(struct cachefiles_cache *cache,
336 const struct cred *saved_cred)
338 revert_creds(saved_cred);
344 void cachefiles_acquire_volume(struct fscache_volume *volume);
345 void cachefiles_free_volume(struct fscache_volume *volume);
346 void cachefiles_withdraw_volume(struct cachefiles_volume *volume);
351 extern int cachefiles_set_object_xattr(struct cachefiles_object *object);
352 extern int cachefiles_check_auxdata(struct cachefiles_object *object,
354 extern int cachefiles_remove_object_xattr(struct cachefiles_cache *cache,
355 struct cachefiles_object *object,
356 struct dentry *dentry);
357 extern void cachefiles_prepare_to_write(struct fscache_cookie *cookie);
358 extern bool cachefiles_set_volume_xattr(struct cachefiles_volume *volume);
359 extern int cachefiles_check_volume_xattr(struct cachefiles_volume *volume);
364 #define cachefiles_io_error(___cache, FMT, ...) \
366 pr_err("I/O Error: " FMT"\n", ##__VA_ARGS__); \
367 fscache_io_error((___cache)->cache); \
368 set_bit(CACHEFILES_DEAD, &(___cache)->flags); \
371 #define cachefiles_io_error_obj(object, FMT, ...) \
373 struct cachefiles_cache *___cache; \
375 ___cache = (object)->volume->cache; \
376 cachefiles_io_error(___cache, FMT " [o=%08x]", ##__VA_ARGS__, \
377 (object)->debug_id); \
384 extern unsigned cachefiles_debug;
385 #define CACHEFILES_DEBUG_KENTER 1
386 #define CACHEFILES_DEBUG_KLEAVE 2
387 #define CACHEFILES_DEBUG_KDEBUG 4
389 #define dbgprintk(FMT, ...) \
390 printk(KERN_DEBUG "[%-6.6s] "FMT"\n", current->comm, ##__VA_ARGS__)
392 #define kenter(FMT, ...) dbgprintk("==> %s("FMT")", __func__, ##__VA_ARGS__)
393 #define kleave(FMT, ...) dbgprintk("<== %s()"FMT"", __func__, ##__VA_ARGS__)
394 #define kdebug(FMT, ...) dbgprintk(FMT, ##__VA_ARGS__)
397 #if defined(__KDEBUG)
398 #define _enter(FMT, ...) kenter(FMT, ##__VA_ARGS__)
399 #define _leave(FMT, ...) kleave(FMT, ##__VA_ARGS__)
400 #define _debug(FMT, ...) kdebug(FMT, ##__VA_ARGS__)
402 #elif defined(CONFIG_CACHEFILES_DEBUG)
403 #define _enter(FMT, ...) \
405 if (cachefiles_debug & CACHEFILES_DEBUG_KENTER) \
406 kenter(FMT, ##__VA_ARGS__); \
409 #define _leave(FMT, ...) \
411 if (cachefiles_debug & CACHEFILES_DEBUG_KLEAVE) \
412 kleave(FMT, ##__VA_ARGS__); \
415 #define _debug(FMT, ...) \
417 if (cachefiles_debug & CACHEFILES_DEBUG_KDEBUG) \
418 kdebug(FMT, ##__VA_ARGS__); \
422 #define _enter(FMT, ...) no_printk("==> %s("FMT")", __func__, ##__VA_ARGS__)
423 #define _leave(FMT, ...) no_printk("<== %s()"FMT"", __func__, ##__VA_ARGS__)
424 #define _debug(FMT, ...) no_printk(FMT, ##__VA_ARGS__)
427 #if 1 /* defined(__KDEBUGALL) */
431 if (unlikely(!(X))) { \
433 pr_err("Assertion failed\n"); \
438 #define ASSERTCMP(X, OP, Y) \
440 if (unlikely(!((X) OP (Y)))) { \
442 pr_err("Assertion failed\n"); \
443 pr_err("%lx " #OP " %lx is false\n", \
444 (unsigned long)(X), (unsigned long)(Y)); \
449 #define ASSERTIF(C, X) \
451 if (unlikely((C) && !(X))) { \
453 pr_err("Assertion failed\n"); \
458 #define ASSERTIFCMP(C, X, OP, Y) \
460 if (unlikely((C) && !((X) OP (Y)))) { \
462 pr_err("Assertion failed\n"); \
463 pr_err("%lx " #OP " %lx is false\n", \
464 (unsigned long)(X), (unsigned long)(Y)); \
471 #define ASSERT(X) do {} while (0)
472 #define ASSERTCMP(X, OP, Y) do {} while (0)
473 #define ASSERTIF(C, X) do {} while (0)
474 #define ASSERTIFCMP(C, X, OP, Y) do {} while (0)