1 /* SPDX-License-Identifier: GPL-2.0-or-later */
2 /* General netfs cache on cache files internal defs
3 *
4 * Copyright (C) 2021 Red Hat, Inc. All Rights Reserved.
5 * Written by David Howells (dhowells@redhat.com)
6 */
7
8 #ifdef pr_fmt
9 #undef pr_fmt
10 #endif
11
12 #define pr_fmt(fmt) "CacheFiles: " fmt
13
14
15 #include <linux/fscache-cache.h>
16 #include <linux/cred.h>
17 #include <linux/security.h>
18
19 struct cachefiles_cache;
20 struct cachefiles_object;
21
22 enum cachefiles_content {
23 /* These values are saved on disk */
24 CACHEFILES_CONTENT_NO_DATA = 0, /* No content stored */
25 CACHEFILES_CONTENT_SINGLE = 1, /* Content is monolithic, all is present */
26 CACHEFILES_CONTENT_ALL = 2, /* Content is all present, no map */
27 CACHEFILES_CONTENT_BACKFS_MAP = 3, /* Content is piecemeal, mapped through backing fs */
28 CACHEFILES_CONTENT_DIRTY = 4, /* Content is dirty (only seen on disk) */
29 nr__cachefiles_content
30 };
31
32 /*
33 * Cached volume representation.
34 */
35 struct cachefiles_volume {
36 struct cachefiles_cache *cache;
37 struct list_head cache_link; /* Link in cache->volumes */
38 struct fscache_volume *vcookie; /* The netfs's representation */
39 struct dentry *dentry; /* The volume dentry */
40 struct dentry *fanout[256]; /* Fanout subdirs */
41 };
42
43 /*
44 * Backing file state.
45 */
46 struct cachefiles_object {
47 struct fscache_cookie *cookie; /* Netfs data storage object cookie */
48 struct cachefiles_volume *volume; /* Cache volume that holds this object */
49 struct list_head cache_link; /* Link in cache->*_list */
50 struct file *file; /* The file representing this object */
51 char *d_name; /* Backing file name */
52 unsigned long flags;
53 #define CACHEFILES_OBJECT_USING_TMPFILE 0 /* Have an unlinked tmpfile */
54 uoff_t object_size; /* Size of the object stored
55 * (independent of cookie->object_size for
56 * coherency reasons)
57 */
58 atomic64_t read_limit; /* Point beyond which uncommitted writes */
59 int debug_id;
60 spinlock_t lock;
61 refcount_t ref;
62 enum cachefiles_content content_info; /* Info about content presence */
63 };
64
65 /*
66 * Cache files cache definition
67 */
68 struct cachefiles_cache {
69 struct fscache_cache *cache; /* Cache cookie */
70 struct vfsmount *mnt; /* mountpoint holding the cache */
71 struct dentry *store; /* Directory into which live objects go */
72 struct dentry *graveyard; /* directory into which dead objects go */
73 struct file *cachefilesd; /* manager daemon handle */
74 struct list_head volumes; /* List of volume objects */
75 struct list_head object_list; /* List of active objects */
76 spinlock_t object_list_lock; /* Lock for volumes and object_list */
77 const struct cred *cache_cred; /* security override for accessing cache */
78 struct mutex daemon_mutex; /* command serialisation mutex */
79 wait_queue_head_t daemon_pollwq; /* poll waitqueue for daemon */
80 atomic_t gravecounter; /* graveyard uniquifier */
81 atomic_t f_released; /* number of objects released lately */
82 atomic_long_t b_released; /* number of blocks released lately */
83 atomic_long_t b_writing; /* Number of blocks being written */
84 unsigned frun_percent; /* when to stop culling (% files) */
85 unsigned fcull_percent; /* when to start culling (% files) */
86 unsigned fstop_percent; /* when to stop allocating (% files) */
87 unsigned brun_percent; /* when to stop culling (% blocks) */
88 unsigned bcull_percent; /* when to start culling (% blocks) */
89 unsigned bstop_percent; /* when to stop allocating (% blocks) */
90 unsigned bsize; /* cache's block size */
91 unsigned bshift; /* ilog2(bsize) */
92 uint64_t frun; /* when to stop culling */
93 uint64_t fcull; /* when to start culling */
94 uint64_t fstop; /* when to stop allocating */
95 sector_t brun; /* when to stop culling */
96 sector_t bcull; /* when to start culling */
97 sector_t bstop; /* when to stop allocating */
98 unsigned long flags;
99 #define CACHEFILES_READY 0 /* T if cache prepared */
100 #define CACHEFILES_DEAD 1 /* T if cache dead */
101 #define CACHEFILES_CULLING 2 /* T if cull engaged */
102 #define CACHEFILES_STATE_CHANGED 3 /* T if state changed (poll trigger) */
103 char *rootdirname; /* name of cache root directory */
104 char *tag; /* cache binding tag */
105 u32 secid; /* LSM security id */
106 bool have_secid; /* whether "secid" was set */
107 };
108
109 #include <trace/events/cachefiles.h>
110
111 static inline
cachefiles_cres_file(struct netfs_cache_resources * cres)112 struct file *cachefiles_cres_file(struct netfs_cache_resources *cres)
113 {
114 return cres->cache_priv2;
115 }
116
117 static inline
cachefiles_cres_object(struct netfs_cache_resources * cres)118 struct cachefiles_object *cachefiles_cres_object(struct netfs_cache_resources *cres)
119 {
120 return fscache_cres_cookie(cres)->cache_priv;
121 }
122
123 /*
124 * note change of state for daemon
125 */
cachefiles_state_changed(struct cachefiles_cache * cache)126 static inline void cachefiles_state_changed(struct cachefiles_cache *cache)
127 {
128 set_bit(CACHEFILES_STATE_CHANGED, &cache->flags);
129 wake_up_all(&cache->daemon_pollwq);
130 }
131
132 /*
133 * cache.c
134 */
135 extern int cachefiles_add_cache(struct cachefiles_cache *cache);
136 extern void cachefiles_withdraw_cache(struct cachefiles_cache *cache);
137
138 enum cachefiles_has_space_for {
139 cachefiles_has_space_check,
140 cachefiles_has_space_for_write,
141 cachefiles_has_space_for_create,
142 };
143 extern int cachefiles_has_space(struct cachefiles_cache *cache,
144 unsigned fnr, unsigned bnr,
145 enum cachefiles_has_space_for reason);
146
147 /*
148 * daemon.c
149 */
150 extern const struct file_operations cachefiles_daemon_fops;
151
152 /*
153 * error_inject.c
154 */
155 #ifdef CONFIG_CACHEFILES_ERROR_INJECTION
156 extern unsigned int cachefiles_error_injection_state;
157 extern int cachefiles_register_error_injection(void);
158 extern void cachefiles_unregister_error_injection(void);
159
160 #else
161 #define cachefiles_error_injection_state 0
162
cachefiles_register_error_injection(void)163 static inline int cachefiles_register_error_injection(void)
164 {
165 return 0;
166 }
167
cachefiles_unregister_error_injection(void)168 static inline void cachefiles_unregister_error_injection(void)
169 {
170 }
171 #endif
172
173
cachefiles_inject_read_error(void)174 static inline int cachefiles_inject_read_error(void)
175 {
176 return cachefiles_error_injection_state & 2 ? -EIO : 0;
177 }
178
cachefiles_inject_write_error(void)179 static inline int cachefiles_inject_write_error(void)
180 {
181 return cachefiles_error_injection_state & 2 ? -EIO :
182 cachefiles_error_injection_state & 1 ? -ENOSPC :
183 0;
184 }
185
cachefiles_inject_remove_error(void)186 static inline int cachefiles_inject_remove_error(void)
187 {
188 return cachefiles_error_injection_state & 2 ? -EIO : 0;
189 }
190
191 /*
192 * interface.c
193 */
194 extern const struct fscache_cache_ops cachefiles_cache_ops;
195 extern void cachefiles_see_object(struct cachefiles_object *object,
196 enum cachefiles_obj_ref_trace why);
197 extern struct cachefiles_object *cachefiles_grab_object(struct cachefiles_object *object,
198 enum cachefiles_obj_ref_trace why);
199 extern void cachefiles_put_object(struct cachefiles_object *object,
200 enum cachefiles_obj_ref_trace why);
201
202 /*
203 * io.c
204 */
205 extern bool cachefiles_begin_operation(struct netfs_cache_resources *cres,
206 enum fscache_want_state want_state);
207 extern int __cachefiles_prepare_write(struct cachefiles_object *object,
208 struct file *file,
209 uoff_t *_start, size_t *_len, size_t upper_len,
210 bool no_space_allocated_yet);
211 extern int __cachefiles_write(struct cachefiles_object *object,
212 struct file *file,
213 uoff_t start_pos,
214 struct iov_iter *iter,
215 netfs_io_terminated_t term_func,
216 void *term_func_priv);
217
218 /*
219 * key.c
220 */
221 extern bool cachefiles_cook_key(struct cachefiles_object *object);
222
223 /*
224 * main.c
225 */
226 extern struct kmem_cache *cachefiles_object_jar;
227
228 /*
229 * namei.c
230 */
231 extern void cachefiles_unmark_inode_in_use(struct cachefiles_object *object,
232 struct file *file);
233 extern int cachefiles_bury_object(struct cachefiles_cache *cache,
234 struct cachefiles_object *object,
235 struct dentry *dir,
236 struct dentry *rep,
237 enum fscache_why_object_killed why);
238 extern int cachefiles_delete_object(struct cachefiles_object *object,
239 enum fscache_why_object_killed why);
240 extern bool cachefiles_look_up_object(struct cachefiles_object *object);
241 extern struct dentry *cachefiles_get_directory(struct cachefiles_cache *cache,
242 struct dentry *dir,
243 const char *name,
244 bool *_is_new);
245 extern void cachefiles_put_directory(struct dentry *dir);
246
247 extern int cachefiles_cull(struct cachefiles_cache *cache, struct dentry *dir,
248 char *filename);
249
250 extern int cachefiles_check_in_use(struct cachefiles_cache *cache,
251 struct dentry *dir, char *filename);
252 extern struct file *cachefiles_create_tmpfile(struct cachefiles_object *object);
253 extern bool cachefiles_commit_tmpfile(struct cachefiles_cache *cache,
254 struct cachefiles_object *object);
255
256 /*
257 * security.c
258 */
259 extern int cachefiles_get_security_ID(struct cachefiles_cache *cache);
260 extern int cachefiles_determine_cache_security(struct cachefiles_cache *cache,
261 struct dentry *root,
262 const struct cred **_saved_cred);
263
cachefiles_begin_secure(struct cachefiles_cache * cache,const struct cred ** _saved_cred)264 static inline void cachefiles_begin_secure(struct cachefiles_cache *cache,
265 const struct cred **_saved_cred)
266 {
267 *_saved_cred = override_creds(cache->cache_cred);
268 }
269
cachefiles_end_secure(struct cachefiles_cache * cache,const struct cred * saved_cred)270 static inline void cachefiles_end_secure(struct cachefiles_cache *cache,
271 const struct cred *saved_cred)
272 {
273 revert_creds(saved_cred);
274 }
275
276 /*
277 * volume.c
278 */
279 void cachefiles_acquire_volume(struct fscache_volume *volume);
280 void cachefiles_free_volume(struct fscache_volume *volume);
281 void cachefiles_withdraw_volume(struct cachefiles_volume *volume);
282
283 /*
284 * xattr.c
285 */
286 int cachefiles_preset_object_xattr(struct cachefiles_object *object, struct file *file);
287 extern int cachefiles_set_object_xattr(struct cachefiles_object *object);
288 extern int cachefiles_check_auxdata(struct cachefiles_object *object,
289 struct file *file);
290 extern int cachefiles_remove_object_xattr(struct cachefiles_cache *cache,
291 struct cachefiles_object *object,
292 struct dentry *dentry);
293 extern void cachefiles_prepare_to_write(struct fscache_cookie *cookie);
294 extern bool cachefiles_set_volume_xattr(struct cachefiles_volume *volume);
295 extern int cachefiles_check_volume_xattr(struct cachefiles_volume *volume);
296
297 /*
298 * Error handling
299 */
300 #define cachefiles_io_error(___cache, FMT, ...) \
301 do { \
302 pr_err("I/O Error: " FMT"\n", ##__VA_ARGS__); \
303 fscache_io_error((___cache)->cache); \
304 set_bit(CACHEFILES_DEAD, &(___cache)->flags); \
305 } while (0)
306
307 #define cachefiles_io_error_obj(object, FMT, ...) \
308 do { \
309 struct cachefiles_cache *___cache; \
310 \
311 ___cache = (object)->volume->cache; \
312 cachefiles_io_error(___cache, FMT " [o=%08x]", ##__VA_ARGS__, \
313 (object)->debug_id); \
314 } while (0)
315
316
317 /*
318 * Debug tracing
319 */
320 extern unsigned cachefiles_debug;
321 #define CACHEFILES_DEBUG_KENTER 1
322 #define CACHEFILES_DEBUG_KLEAVE 2
323 #define CACHEFILES_DEBUG_KDEBUG 4
324
325 #define dbgprintk(FMT, ...) \
326 printk(KERN_DEBUG "[%-6.6s] "FMT"\n", current->comm, ##__VA_ARGS__)
327
328 #define kenter(FMT, ...) dbgprintk("==> %s("FMT")", __func__, ##__VA_ARGS__)
329 #define kleave(FMT, ...) dbgprintk("<== %s()"FMT"", __func__, ##__VA_ARGS__)
330 #define kdebug(FMT, ...) dbgprintk(FMT, ##__VA_ARGS__)
331
332
333 #if defined(__KDEBUG)
334 #define _enter(FMT, ...) kenter(FMT, ##__VA_ARGS__)
335 #define _leave(FMT, ...) kleave(FMT, ##__VA_ARGS__)
336 #define _debug(FMT, ...) kdebug(FMT, ##__VA_ARGS__)
337
338 #elif defined(CONFIG_CACHEFILES_DEBUG)
339 #define _enter(FMT, ...) \
340 do { \
341 if (cachefiles_debug & CACHEFILES_DEBUG_KENTER) \
342 kenter(FMT, ##__VA_ARGS__); \
343 } while (0)
344
345 #define _leave(FMT, ...) \
346 do { \
347 if (cachefiles_debug & CACHEFILES_DEBUG_KLEAVE) \
348 kleave(FMT, ##__VA_ARGS__); \
349 } while (0)
350
351 #define _debug(FMT, ...) \
352 do { \
353 if (cachefiles_debug & CACHEFILES_DEBUG_KDEBUG) \
354 kdebug(FMT, ##__VA_ARGS__); \
355 } while (0)
356
357 #else
358 #define _enter(FMT, ...) no_printk("==> %s("FMT")", __func__, ##__VA_ARGS__)
359 #define _leave(FMT, ...) no_printk("<== %s()"FMT"", __func__, ##__VA_ARGS__)
360 #define _debug(FMT, ...) no_printk(FMT, ##__VA_ARGS__)
361 #endif
362
363 #if 1 /* defined(__KDEBUGALL) */
364
365 #define ASSERT(X) \
366 do { \
367 if (unlikely(!(X))) { \
368 pr_err("\n"); \
369 pr_err("Assertion failed\n"); \
370 BUG(); \
371 } \
372 } while (0)
373
374 #define ASSERTCMP(X, OP, Y) \
375 do { \
376 if (unlikely(!((X) OP (Y)))) { \
377 pr_err("\n"); \
378 pr_err("Assertion failed\n"); \
379 pr_err("%lx " #OP " %lx is false\n", \
380 (unsigned long)(X), (unsigned long)(Y)); \
381 BUG(); \
382 } \
383 } while (0)
384
385 #define ASSERTIF(C, X) \
386 do { \
387 if (unlikely((C) && !(X))) { \
388 pr_err("\n"); \
389 pr_err("Assertion failed\n"); \
390 BUG(); \
391 } \
392 } while (0)
393
394 #define ASSERTIFCMP(C, X, OP, Y) \
395 do { \
396 if (unlikely((C) && !((X) OP (Y)))) { \
397 pr_err("\n"); \
398 pr_err("Assertion failed\n"); \
399 pr_err("%lx " #OP " %lx is false\n", \
400 (unsigned long)(X), (unsigned long)(Y)); \
401 BUG(); \
402 } \
403 } while (0)
404
405 #else
406
407 #define ASSERT(X) do {} while (0)
408 #define ASSERTCMP(X, OP, Y) do {} while (0)
409 #define ASSERTIF(C, X) do {} while (0)
410 #define ASSERTIFCMP(C, X, OP, Y) do {} while (0)
411
412 #endif
413