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.
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 */
47 enum cachefiles_object_state {
48 CACHEFILES_ONDEMAND_OBJSTATE_CLOSE, /* Anonymous fd closed by daemon or initial state */
49 CACHEFILES_ONDEMAND_OBJSTATE_OPEN, /* Anonymous fd associated with object is available */
50 CACHEFILES_ONDEMAND_OBJSTATE_REOPENING, /* Object that was closed and is being reopened. */
53 struct cachefiles_ondemand_info {
54 struct work_struct ondemand_work;
56 enum cachefiles_object_state state;
57 struct cachefiles_object *object;
63 struct cachefiles_object {
64 struct fscache_cookie *cookie; /* Netfs data storage object cookie */
65 struct cachefiles_volume *volume; /* Cache volume that holds this object */
66 struct list_head cache_link; /* Link in cache->*_list */
67 struct file *file; /* The file representing this object */
68 char *d_name; /* Backing file name */
72 u8 d_name_len; /* Length of filename */
73 enum cachefiles_content content_info:8; /* Info about content presence */
75 #define CACHEFILES_OBJECT_USING_TMPFILE 0 /* Have an unlinked tmpfile */
76 #ifdef CONFIG_CACHEFILES_ONDEMAND
77 struct cachefiles_ondemand_info *ondemand;
81 #define CACHEFILES_ONDEMAND_ID_CLOSED -1
84 * Cache files cache definition
86 struct cachefiles_cache {
87 struct fscache_cache *cache; /* Cache cookie */
88 struct vfsmount *mnt; /* mountpoint holding the cache */
89 struct dentry *store; /* Directory into which live objects go */
90 struct dentry *graveyard; /* directory into which dead objects go */
91 struct file *cachefilesd; /* manager daemon handle */
92 struct list_head volumes; /* List of volume objects */
93 struct list_head object_list; /* List of active objects */
94 spinlock_t object_list_lock; /* Lock for volumes and object_list */
95 const struct cred *cache_cred; /* security override for accessing cache */
96 struct mutex daemon_mutex; /* command serialisation mutex */
97 wait_queue_head_t daemon_pollwq; /* poll waitqueue for daemon */
98 atomic_t gravecounter; /* graveyard uniquifier */
99 atomic_t f_released; /* number of objects released lately */
100 atomic_long_t b_released; /* number of blocks released lately */
101 atomic_long_t b_writing; /* Number of blocks being written */
102 unsigned frun_percent; /* when to stop culling (% files) */
103 unsigned fcull_percent; /* when to start culling (% files) */
104 unsigned fstop_percent; /* when to stop allocating (% files) */
105 unsigned brun_percent; /* when to stop culling (% blocks) */
106 unsigned bcull_percent; /* when to start culling (% blocks) */
107 unsigned bstop_percent; /* when to stop allocating (% blocks) */
108 unsigned bsize; /* cache's block size */
109 unsigned bshift; /* ilog2(bsize) */
110 uint64_t frun; /* when to stop culling */
111 uint64_t fcull; /* when to start culling */
112 uint64_t fstop; /* when to stop allocating */
113 sector_t brun; /* when to stop culling */
114 sector_t bcull; /* when to start culling */
115 sector_t bstop; /* when to stop allocating */
117 #define CACHEFILES_READY 0 /* T if cache prepared */
118 #define CACHEFILES_DEAD 1 /* T if cache dead */
119 #define CACHEFILES_CULLING 2 /* T if cull engaged */
120 #define CACHEFILES_STATE_CHANGED 3 /* T if state changed (poll trigger) */
121 #define CACHEFILES_ONDEMAND_MODE 4 /* T if in on-demand read mode */
122 char *rootdirname; /* name of cache root directory */
123 char *secctx; /* LSM security context */
124 char *tag; /* cache binding tag */
125 refcount_t unbind_pincount;/* refcount to do daemon unbind */
126 struct xarray reqs; /* xarray of pending on-demand requests */
127 unsigned long req_id_next;
128 struct xarray ondemand_ids; /* xarray for ondemand_id allocation */
129 u32 ondemand_id_next;
132 static inline bool cachefiles_in_ondemand_mode(struct cachefiles_cache *cache)
134 return IS_ENABLED(CONFIG_CACHEFILES_ONDEMAND) &&
135 test_bit(CACHEFILES_ONDEMAND_MODE, &cache->flags);
138 struct cachefiles_req {
139 struct cachefiles_object *object;
140 struct completion done;
142 struct cachefiles_msg msg;
145 #define CACHEFILES_REQ_NEW XA_MARK_1
147 #include <trace/events/cachefiles.h>
150 struct file *cachefiles_cres_file(struct netfs_cache_resources *cres)
152 return cres->cache_priv2;
156 struct cachefiles_object *cachefiles_cres_object(struct netfs_cache_resources *cres)
158 return fscache_cres_cookie(cres)->cache_priv;
162 * note change of state for daemon
164 static inline void cachefiles_state_changed(struct cachefiles_cache *cache)
166 set_bit(CACHEFILES_STATE_CHANGED, &cache->flags);
167 wake_up_all(&cache->daemon_pollwq);
173 extern int cachefiles_add_cache(struct cachefiles_cache *cache);
174 extern void cachefiles_withdraw_cache(struct cachefiles_cache *cache);
176 enum cachefiles_has_space_for {
177 cachefiles_has_space_check,
178 cachefiles_has_space_for_write,
179 cachefiles_has_space_for_create,
181 extern int cachefiles_has_space(struct cachefiles_cache *cache,
182 unsigned fnr, unsigned bnr,
183 enum cachefiles_has_space_for reason);
188 extern const struct file_operations cachefiles_daemon_fops;
189 extern void cachefiles_get_unbind_pincount(struct cachefiles_cache *cache);
190 extern void cachefiles_put_unbind_pincount(struct cachefiles_cache *cache);
195 #ifdef CONFIG_CACHEFILES_ERROR_INJECTION
196 extern unsigned int cachefiles_error_injection_state;
197 extern int cachefiles_register_error_injection(void);
198 extern void cachefiles_unregister_error_injection(void);
201 #define cachefiles_error_injection_state 0
203 static inline int cachefiles_register_error_injection(void)
208 static inline void cachefiles_unregister_error_injection(void)
214 static inline int cachefiles_inject_read_error(void)
216 return cachefiles_error_injection_state & 2 ? -EIO : 0;
219 static inline int cachefiles_inject_write_error(void)
221 return cachefiles_error_injection_state & 2 ? -EIO :
222 cachefiles_error_injection_state & 1 ? -ENOSPC :
226 static inline int cachefiles_inject_remove_error(void)
228 return cachefiles_error_injection_state & 2 ? -EIO : 0;
234 extern const struct fscache_cache_ops cachefiles_cache_ops;
235 extern void cachefiles_see_object(struct cachefiles_object *object,
236 enum cachefiles_obj_ref_trace why);
237 extern struct cachefiles_object *cachefiles_grab_object(struct cachefiles_object *object,
238 enum cachefiles_obj_ref_trace why);
239 extern void cachefiles_put_object(struct cachefiles_object *object,
240 enum cachefiles_obj_ref_trace why);
245 extern bool cachefiles_begin_operation(struct netfs_cache_resources *cres,
246 enum fscache_want_state want_state);
247 extern int __cachefiles_prepare_write(struct cachefiles_object *object,
249 loff_t *_start, size_t *_len, size_t upper_len,
250 bool no_space_allocated_yet);
251 extern int __cachefiles_write(struct cachefiles_object *object,
254 struct iov_iter *iter,
255 netfs_io_terminated_t term_func,
256 void *term_func_priv);
261 extern bool cachefiles_cook_key(struct cachefiles_object *object);
266 extern struct kmem_cache *cachefiles_object_jar;
271 extern void cachefiles_unmark_inode_in_use(struct cachefiles_object *object,
273 extern int cachefiles_bury_object(struct cachefiles_cache *cache,
274 struct cachefiles_object *object,
277 enum fscache_why_object_killed why);
278 extern int cachefiles_delete_object(struct cachefiles_object *object,
279 enum fscache_why_object_killed why);
280 extern bool cachefiles_look_up_object(struct cachefiles_object *object);
281 extern struct dentry *cachefiles_get_directory(struct cachefiles_cache *cache,
285 extern void cachefiles_put_directory(struct dentry *dir);
287 extern int cachefiles_cull(struct cachefiles_cache *cache, struct dentry *dir,
290 extern int cachefiles_check_in_use(struct cachefiles_cache *cache,
291 struct dentry *dir, char *filename);
292 extern struct file *cachefiles_create_tmpfile(struct cachefiles_object *object);
293 extern bool cachefiles_commit_tmpfile(struct cachefiles_cache *cache,
294 struct cachefiles_object *object);
299 #ifdef CONFIG_CACHEFILES_ONDEMAND
300 extern ssize_t cachefiles_ondemand_daemon_read(struct cachefiles_cache *cache,
301 char __user *_buffer, size_t buflen);
303 extern int cachefiles_ondemand_copen(struct cachefiles_cache *cache,
306 extern int cachefiles_ondemand_restore(struct cachefiles_cache *cache,
309 extern int cachefiles_ondemand_init_object(struct cachefiles_object *object);
310 extern void cachefiles_ondemand_clean_object(struct cachefiles_object *object);
312 extern int cachefiles_ondemand_read(struct cachefiles_object *object,
313 loff_t pos, size_t len);
315 extern int cachefiles_ondemand_init_obj_info(struct cachefiles_object *obj,
316 struct cachefiles_volume *volume);
317 extern void cachefiles_ondemand_deinit_obj_info(struct cachefiles_object *obj);
319 #define CACHEFILES_OBJECT_STATE_FUNCS(_state, _STATE) \
321 cachefiles_ondemand_object_is_##_state(const struct cachefiles_object *object) \
323 return object->ondemand->state == CACHEFILES_ONDEMAND_OBJSTATE_##_STATE; \
327 cachefiles_ondemand_set_object_##_state(struct cachefiles_object *object) \
329 object->ondemand->state = CACHEFILES_ONDEMAND_OBJSTATE_##_STATE; \
332 CACHEFILES_OBJECT_STATE_FUNCS(open, OPEN);
333 CACHEFILES_OBJECT_STATE_FUNCS(close, CLOSE);
334 CACHEFILES_OBJECT_STATE_FUNCS(reopening, REOPENING);
336 static inline bool cachefiles_ondemand_is_reopening_read(struct cachefiles_req *req)
338 return cachefiles_ondemand_object_is_reopening(req->object) &&
339 req->msg.opcode == CACHEFILES_OP_READ;
343 static inline ssize_t cachefiles_ondemand_daemon_read(struct cachefiles_cache *cache,
344 char __user *_buffer, size_t buflen)
349 static inline int cachefiles_ondemand_init_object(struct cachefiles_object *object)
354 static inline void cachefiles_ondemand_clean_object(struct cachefiles_object *object)
358 static inline int cachefiles_ondemand_read(struct cachefiles_object *object,
359 loff_t pos, size_t len)
364 static inline int cachefiles_ondemand_init_obj_info(struct cachefiles_object *obj,
365 struct cachefiles_volume *volume)
369 static inline void cachefiles_ondemand_deinit_obj_info(struct cachefiles_object *obj)
373 static inline bool cachefiles_ondemand_is_reopening_read(struct cachefiles_req *req)
382 extern int cachefiles_get_security_ID(struct cachefiles_cache *cache);
383 extern int cachefiles_determine_cache_security(struct cachefiles_cache *cache,
385 const struct cred **_saved_cred);
387 static inline void cachefiles_begin_secure(struct cachefiles_cache *cache,
388 const struct cred **_saved_cred)
390 *_saved_cred = override_creds(cache->cache_cred);
393 static inline void cachefiles_end_secure(struct cachefiles_cache *cache,
394 const struct cred *saved_cred)
396 revert_creds(saved_cred);
402 void cachefiles_acquire_volume(struct fscache_volume *volume);
403 void cachefiles_free_volume(struct fscache_volume *volume);
404 void cachefiles_withdraw_volume(struct cachefiles_volume *volume);
409 extern int cachefiles_set_object_xattr(struct cachefiles_object *object);
410 extern int cachefiles_check_auxdata(struct cachefiles_object *object,
412 extern int cachefiles_remove_object_xattr(struct cachefiles_cache *cache,
413 struct cachefiles_object *object,
414 struct dentry *dentry);
415 extern void cachefiles_prepare_to_write(struct fscache_cookie *cookie);
416 extern bool cachefiles_set_volume_xattr(struct cachefiles_volume *volume);
417 extern int cachefiles_check_volume_xattr(struct cachefiles_volume *volume);
422 #define cachefiles_io_error(___cache, FMT, ...) \
424 pr_err("I/O Error: " FMT"\n", ##__VA_ARGS__); \
425 fscache_io_error((___cache)->cache); \
426 set_bit(CACHEFILES_DEAD, &(___cache)->flags); \
429 #define cachefiles_io_error_obj(object, FMT, ...) \
431 struct cachefiles_cache *___cache; \
433 ___cache = (object)->volume->cache; \
434 cachefiles_io_error(___cache, FMT " [o=%08x]", ##__VA_ARGS__, \
435 (object)->debug_id); \
442 extern unsigned cachefiles_debug;
443 #define CACHEFILES_DEBUG_KENTER 1
444 #define CACHEFILES_DEBUG_KLEAVE 2
445 #define CACHEFILES_DEBUG_KDEBUG 4
447 #define dbgprintk(FMT, ...) \
448 printk(KERN_DEBUG "[%-6.6s] "FMT"\n", current->comm, ##__VA_ARGS__)
450 #define kenter(FMT, ...) dbgprintk("==> %s("FMT")", __func__, ##__VA_ARGS__)
451 #define kleave(FMT, ...) dbgprintk("<== %s()"FMT"", __func__, ##__VA_ARGS__)
452 #define kdebug(FMT, ...) dbgprintk(FMT, ##__VA_ARGS__)
455 #if defined(__KDEBUG)
456 #define _enter(FMT, ...) kenter(FMT, ##__VA_ARGS__)
457 #define _leave(FMT, ...) kleave(FMT, ##__VA_ARGS__)
458 #define _debug(FMT, ...) kdebug(FMT, ##__VA_ARGS__)
460 #elif defined(CONFIG_CACHEFILES_DEBUG)
461 #define _enter(FMT, ...) \
463 if (cachefiles_debug & CACHEFILES_DEBUG_KENTER) \
464 kenter(FMT, ##__VA_ARGS__); \
467 #define _leave(FMT, ...) \
469 if (cachefiles_debug & CACHEFILES_DEBUG_KLEAVE) \
470 kleave(FMT, ##__VA_ARGS__); \
473 #define _debug(FMT, ...) \
475 if (cachefiles_debug & CACHEFILES_DEBUG_KDEBUG) \
476 kdebug(FMT, ##__VA_ARGS__); \
480 #define _enter(FMT, ...) no_printk("==> %s("FMT")", __func__, ##__VA_ARGS__)
481 #define _leave(FMT, ...) no_printk("<== %s()"FMT"", __func__, ##__VA_ARGS__)
482 #define _debug(FMT, ...) no_printk(FMT, ##__VA_ARGS__)
485 #if 1 /* defined(__KDEBUGALL) */
489 if (unlikely(!(X))) { \
491 pr_err("Assertion failed\n"); \
496 #define ASSERTCMP(X, OP, Y) \
498 if (unlikely(!((X) OP (Y)))) { \
500 pr_err("Assertion failed\n"); \
501 pr_err("%lx " #OP " %lx is false\n", \
502 (unsigned long)(X), (unsigned long)(Y)); \
507 #define ASSERTIF(C, X) \
509 if (unlikely((C) && !(X))) { \
511 pr_err("Assertion failed\n"); \
516 #define ASSERTIFCMP(C, X, OP, Y) \
518 if (unlikely((C) && !((X) OP (Y)))) { \
520 pr_err("Assertion failed\n"); \
521 pr_err("%lx " #OP " %lx is false\n", \
522 (unsigned long)(X), (unsigned long)(Y)); \
529 #define ASSERT(X) do {} while (0)
530 #define ASSERTCMP(X, OP, Y) do {} while (0)
531 #define ASSERTIF(C, X) do {} while (0)
532 #define ASSERTIFCMP(C, X, OP, Y) do {} while (0)