Commit | Line | Data |
---|---|---|
77443f61 DH |
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> | |
c8383054 JX |
18 | #include <linux/xarray.h> |
19 | #include <linux/cachefiles.h> | |
77443f61 | 20 | |
1f08c925 DH |
21 | #define CACHEFILES_DIO_BLOCK_SIZE 4096 |
22 | ||
8390fbc4 DH |
23 | struct cachefiles_cache; |
24 | struct cachefiles_object; | |
25 | ||
df98e87f DH |
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 | |
34 | }; | |
35 | ||
fe2140e2 DH |
36 | /* |
37 | * Cached volume representation. | |
38 | */ | |
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 */ | |
45 | }; | |
46 | ||
8390fbc4 | 47 | /* |
df98e87f | 48 | * Backing file state. |
8390fbc4 DH |
49 | */ |
50 | struct cachefiles_object { | |
df98e87f DH |
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 */ | |
56 | int debug_id; | |
57 | spinlock_t lock; | |
58 | refcount_t ref; | |
59 | u8 d_name_len; /* Length of filename */ | |
60 | enum cachefiles_content content_info:8; /* Info about content presence */ | |
61 | unsigned long flags; | |
72b95785 | 62 | #define CACHEFILES_OBJECT_USING_TMPFILE 0 /* Have an unlinked tmpfile */ |
c8383054 JX |
63 | #ifdef CONFIG_CACHEFILES_ONDEMAND |
64 | int ondemand_id; | |
65 | #endif | |
8390fbc4 DH |
66 | }; |
67 | ||
c8383054 JX |
68 | #define CACHEFILES_ONDEMAND_ID_CLOSED -1 |
69 | ||
8390fbc4 DH |
70 | /* |
71 | * Cache files cache definition | |
72 | */ | |
73 | struct cachefiles_cache { | |
1493bf74 | 74 | struct fscache_cache *cache; /* Cache cookie */ |
8390fbc4 | 75 | struct vfsmount *mnt; /* mountpoint holding the cache */ |
d1065b0a DH |
76 | struct dentry *store; /* Directory into which live objects go */ |
77 | struct dentry *graveyard; /* directory into which dead objects go */ | |
8390fbc4 | 78 | struct file *cachefilesd; /* manager daemon handle */ |
fe2140e2 | 79 | struct list_head volumes; /* List of volume objects */ |
1f08c925 | 80 | struct list_head object_list; /* List of active objects */ |
fe2140e2 | 81 | spinlock_t object_list_lock; /* Lock for volumes and object_list */ |
8390fbc4 DH |
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 */ | |
80f94f29 | 88 | atomic_long_t b_writing; /* Number of blocks being written */ |
8390fbc4 DH |
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 */ | |
5638b067 | 96 | unsigned bshift; /* ilog2(bsize) */ |
8390fbc4 DH |
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 */ | |
103 | unsigned long flags; | |
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) */ | |
c8383054 | 108 | #define CACHEFILES_ONDEMAND_MODE 4 /* T if in on-demand read mode */ |
8390fbc4 DH |
109 | char *rootdirname; /* name of cache root directory */ |
110 | char *secctx; /* LSM security context */ | |
111 | char *tag; /* cache binding tag */ | |
d11b0b04 | 112 | refcount_t unbind_pincount;/* refcount to do daemon unbind */ |
c8383054 | 113 | struct xarray reqs; /* xarray of pending on-demand requests */ |
1122f400 | 114 | unsigned long req_id_next; |
c8383054 JX |
115 | struct xarray ondemand_ids; /* xarray for ondemand_id allocation */ |
116 | u32 ondemand_id_next; | |
8390fbc4 DH |
117 | }; |
118 | ||
c8383054 JX |
119 | static inline bool cachefiles_in_ondemand_mode(struct cachefiles_cache *cache) |
120 | { | |
121 | return IS_ENABLED(CONFIG_CACHEFILES_ONDEMAND) && | |
122 | test_bit(CACHEFILES_ONDEMAND_MODE, &cache->flags); | |
123 | } | |
124 | ||
125 | struct cachefiles_req { | |
126 | struct cachefiles_object *object; | |
127 | struct completion done; | |
128 | int error; | |
129 | struct cachefiles_msg msg; | |
130 | }; | |
131 | ||
132 | #define CACHEFILES_REQ_NEW XA_MARK_1 | |
133 | ||
ecf5a6ce | 134 | #include <trace/events/cachefiles.h> |
77443f61 | 135 | |
287fd611 DH |
136 | static inline |
137 | struct file *cachefiles_cres_file(struct netfs_cache_resources *cres) | |
138 | { | |
139 | return cres->cache_priv2; | |
140 | } | |
141 | ||
142 | static inline | |
143 | struct cachefiles_object *cachefiles_cres_object(struct netfs_cache_resources *cres) | |
144 | { | |
145 | return fscache_cres_cookie(cres)->cache_priv; | |
146 | } | |
147 | ||
8667d434 DH |
148 | /* |
149 | * note change of state for daemon | |
150 | */ | |
151 | static inline void cachefiles_state_changed(struct cachefiles_cache *cache) | |
152 | { | |
153 | set_bit(CACHEFILES_STATE_CHANGED, &cache->flags); | |
154 | wake_up_all(&cache->daemon_pollwq); | |
155 | } | |
156 | ||
80f94f29 DH |
157 | /* |
158 | * cache.c | |
159 | */ | |
d1065b0a | 160 | extern int cachefiles_add_cache(struct cachefiles_cache *cache); |
d1065b0a | 161 | extern void cachefiles_withdraw_cache(struct cachefiles_cache *cache); |
80f94f29 | 162 | |
3929eca7 DH |
163 | enum cachefiles_has_space_for { |
164 | cachefiles_has_space_check, | |
165 | cachefiles_has_space_for_write, | |
166 | cachefiles_has_space_for_create, | |
167 | }; | |
168 | extern int cachefiles_has_space(struct cachefiles_cache *cache, | |
169 | unsigned fnr, unsigned bnr, | |
170 | enum cachefiles_has_space_for reason); | |
171 | ||
8667d434 DH |
172 | /* |
173 | * daemon.c | |
174 | */ | |
175 | extern const struct file_operations cachefiles_daemon_fops; | |
d11b0b04 JX |
176 | extern void cachefiles_get_unbind_pincount(struct cachefiles_cache *cache); |
177 | extern void cachefiles_put_unbind_pincount(struct cachefiles_cache *cache); | |
8667d434 | 178 | |
77443f61 | 179 | /* |
a70f6526 DH |
180 | * error_inject.c |
181 | */ | |
182 | #ifdef CONFIG_CACHEFILES_ERROR_INJECTION | |
183 | extern unsigned int cachefiles_error_injection_state; | |
184 | extern int cachefiles_register_error_injection(void); | |
185 | extern void cachefiles_unregister_error_injection(void); | |
186 | ||
187 | #else | |
188 | #define cachefiles_error_injection_state 0 | |
189 | ||
190 | static inline int cachefiles_register_error_injection(void) | |
191 | { | |
192 | return 0; | |
193 | } | |
194 | ||
195 | static inline void cachefiles_unregister_error_injection(void) | |
196 | { | |
197 | } | |
198 | #endif | |
199 | ||
200 | ||
201 | static inline int cachefiles_inject_read_error(void) | |
202 | { | |
203 | return cachefiles_error_injection_state & 2 ? -EIO : 0; | |
204 | } | |
205 | ||
206 | static inline int cachefiles_inject_write_error(void) | |
207 | { | |
208 | return cachefiles_error_injection_state & 2 ? -EIO : | |
209 | cachefiles_error_injection_state & 1 ? -ENOSPC : | |
210 | 0; | |
211 | } | |
212 | ||
213 | static inline int cachefiles_inject_remove_error(void) | |
214 | { | |
215 | return cachefiles_error_injection_state & 2 ? -EIO : 0; | |
216 | } | |
217 | ||
d1065b0a DH |
218 | /* |
219 | * interface.c | |
220 | */ | |
221 | extern const struct fscache_cache_ops cachefiles_cache_ops; | |
df98e87f DH |
222 | extern void cachefiles_see_object(struct cachefiles_object *object, |
223 | enum cachefiles_obj_ref_trace why); | |
224 | extern struct cachefiles_object *cachefiles_grab_object(struct cachefiles_object *object, | |
225 | enum cachefiles_obj_ref_trace why); | |
226 | extern void cachefiles_put_object(struct cachefiles_object *object, | |
227 | enum cachefiles_obj_ref_trace why); | |
228 | ||
287fd611 DH |
229 | /* |
230 | * io.c | |
231 | */ | |
232 | extern bool cachefiles_begin_operation(struct netfs_cache_resources *cres, | |
233 | enum fscache_want_state want_state); | |
a06fac15 JX |
234 | extern int __cachefiles_prepare_write(struct cachefiles_object *object, |
235 | struct file *file, | |
236 | loff_t *_start, size_t *_len, | |
237 | bool no_space_allocated_yet); | |
238 | extern int __cachefiles_write(struct cachefiles_object *object, | |
239 | struct file *file, | |
240 | loff_t start_pos, | |
241 | struct iov_iter *iter, | |
242 | netfs_io_terminated_t term_func, | |
243 | void *term_func_priv); | |
287fd611 | 244 | |
5d439467 DH |
245 | /* |
246 | * key.c | |
247 | */ | |
248 | extern bool cachefiles_cook_key(struct cachefiles_object *object); | |
249 | ||
df98e87f DH |
250 | /* |
251 | * main.c | |
252 | */ | |
253 | extern struct kmem_cache *cachefiles_object_jar; | |
d1065b0a | 254 | |
32759f7d DH |
255 | /* |
256 | * namei.c | |
257 | */ | |
169379ea DH |
258 | extern void cachefiles_unmark_inode_in_use(struct cachefiles_object *object, |
259 | struct file *file); | |
07a90e97 DH |
260 | extern int cachefiles_bury_object(struct cachefiles_cache *cache, |
261 | struct cachefiles_object *object, | |
262 | struct dentry *dir, | |
263 | struct dentry *rep, | |
264 | enum fscache_why_object_killed why); | |
1f08c925 DH |
265 | extern int cachefiles_delete_object(struct cachefiles_object *object, |
266 | enum fscache_why_object_killed why); | |
267 | extern bool cachefiles_look_up_object(struct cachefiles_object *object); | |
32759f7d DH |
268 | extern struct dentry *cachefiles_get_directory(struct cachefiles_cache *cache, |
269 | struct dentry *dir, | |
270 | const char *name, | |
271 | bool *_is_new); | |
272 | extern void cachefiles_put_directory(struct dentry *dir); | |
273 | ||
07a90e97 DH |
274 | extern int cachefiles_cull(struct cachefiles_cache *cache, struct dentry *dir, |
275 | char *filename); | |
276 | ||
277 | extern int cachefiles_check_in_use(struct cachefiles_cache *cache, | |
278 | struct dentry *dir, char *filename); | |
1f08c925 DH |
279 | extern struct file *cachefiles_create_tmpfile(struct cachefiles_object *object); |
280 | extern bool cachefiles_commit_tmpfile(struct cachefiles_cache *cache, | |
281 | struct cachefiles_object *object); | |
07a90e97 | 282 | |
c8383054 JX |
283 | /* |
284 | * ondemand.c | |
285 | */ | |
286 | #ifdef CONFIG_CACHEFILES_ONDEMAND | |
287 | extern ssize_t cachefiles_ondemand_daemon_read(struct cachefiles_cache *cache, | |
288 | char __user *_buffer, size_t buflen); | |
289 | ||
290 | extern int cachefiles_ondemand_copen(struct cachefiles_cache *cache, | |
291 | char *args); | |
292 | ||
293 | extern int cachefiles_ondemand_init_object(struct cachefiles_object *object); | |
324b954a | 294 | extern void cachefiles_ondemand_clean_object(struct cachefiles_object *object); |
c8383054 | 295 | |
9032b6e8 JX |
296 | extern int cachefiles_ondemand_read(struct cachefiles_object *object, |
297 | loff_t pos, size_t len); | |
298 | ||
c8383054 JX |
299 | #else |
300 | static inline ssize_t cachefiles_ondemand_daemon_read(struct cachefiles_cache *cache, | |
301 | char __user *_buffer, size_t buflen) | |
302 | { | |
303 | return -EOPNOTSUPP; | |
304 | } | |
305 | ||
306 | static inline int cachefiles_ondemand_init_object(struct cachefiles_object *object) | |
307 | { | |
308 | return 0; | |
309 | } | |
324b954a JX |
310 | |
311 | static inline void cachefiles_ondemand_clean_object(struct cachefiles_object *object) | |
312 | { | |
313 | } | |
9032b6e8 JX |
314 | |
315 | static inline int cachefiles_ondemand_read(struct cachefiles_object *object, | |
316 | loff_t pos, size_t len) | |
317 | { | |
318 | return -EOPNOTSUPP; | |
319 | } | |
c8383054 JX |
320 | #endif |
321 | ||
254947d4 DH |
322 | /* |
323 | * security.c | |
324 | */ | |
325 | extern int cachefiles_get_security_ID(struct cachefiles_cache *cache); | |
326 | extern int cachefiles_determine_cache_security(struct cachefiles_cache *cache, | |
327 | struct dentry *root, | |
328 | const struct cred **_saved_cred); | |
329 | ||
330 | static inline void cachefiles_begin_secure(struct cachefiles_cache *cache, | |
331 | const struct cred **_saved_cred) | |
332 | { | |
333 | *_saved_cred = override_creds(cache->cache_cred); | |
334 | } | |
335 | ||
336 | static inline void cachefiles_end_secure(struct cachefiles_cache *cache, | |
337 | const struct cred *saved_cred) | |
338 | { | |
339 | revert_creds(saved_cred); | |
340 | } | |
341 | ||
fe2140e2 DH |
342 | /* |
343 | * volume.c | |
344 | */ | |
345 | void cachefiles_acquire_volume(struct fscache_volume *volume); | |
346 | void cachefiles_free_volume(struct fscache_volume *volume); | |
347 | void cachefiles_withdraw_volume(struct cachefiles_volume *volume); | |
348 | ||
72b95785 DH |
349 | /* |
350 | * xattr.c | |
351 | */ | |
352 | extern int cachefiles_set_object_xattr(struct cachefiles_object *object); | |
353 | extern int cachefiles_check_auxdata(struct cachefiles_object *object, | |
354 | struct file *file); | |
355 | extern int cachefiles_remove_object_xattr(struct cachefiles_cache *cache, | |
356 | struct cachefiles_object *object, | |
357 | struct dentry *dentry); | |
358 | extern void cachefiles_prepare_to_write(struct fscache_cookie *cookie); | |
32e15003 DH |
359 | extern bool cachefiles_set_volume_xattr(struct cachefiles_volume *volume); |
360 | extern int cachefiles_check_volume_xattr(struct cachefiles_volume *volume); | |
72b95785 | 361 | |
1493bf74 DH |
362 | /* |
363 | * Error handling | |
364 | */ | |
365 | #define cachefiles_io_error(___cache, FMT, ...) \ | |
366 | do { \ | |
367 | pr_err("I/O Error: " FMT"\n", ##__VA_ARGS__); \ | |
368 | fscache_io_error((___cache)->cache); \ | |
369 | set_bit(CACHEFILES_DEAD, &(___cache)->flags); \ | |
370 | } while (0) | |
371 | ||
72b95785 DH |
372 | #define cachefiles_io_error_obj(object, FMT, ...) \ |
373 | do { \ | |
374 | struct cachefiles_cache *___cache; \ | |
375 | \ | |
376 | ___cache = (object)->volume->cache; \ | |
377 | cachefiles_io_error(___cache, FMT " [o=%08x]", ##__VA_ARGS__, \ | |
378 | (object)->debug_id); \ | |
379 | } while (0) | |
380 | ||
a70f6526 DH |
381 | |
382 | /* | |
383 | * Debug tracing | |
77443f61 DH |
384 | */ |
385 | extern unsigned cachefiles_debug; | |
386 | #define CACHEFILES_DEBUG_KENTER 1 | |
387 | #define CACHEFILES_DEBUG_KLEAVE 2 | |
388 | #define CACHEFILES_DEBUG_KDEBUG 4 | |
389 | ||
390 | #define dbgprintk(FMT, ...) \ | |
391 | printk(KERN_DEBUG "[%-6.6s] "FMT"\n", current->comm, ##__VA_ARGS__) | |
392 | ||
393 | #define kenter(FMT, ...) dbgprintk("==> %s("FMT")", __func__, ##__VA_ARGS__) | |
394 | #define kleave(FMT, ...) dbgprintk("<== %s()"FMT"", __func__, ##__VA_ARGS__) | |
395 | #define kdebug(FMT, ...) dbgprintk(FMT, ##__VA_ARGS__) | |
396 | ||
397 | ||
398 | #if defined(__KDEBUG) | |
399 | #define _enter(FMT, ...) kenter(FMT, ##__VA_ARGS__) | |
400 | #define _leave(FMT, ...) kleave(FMT, ##__VA_ARGS__) | |
401 | #define _debug(FMT, ...) kdebug(FMT, ##__VA_ARGS__) | |
402 | ||
403 | #elif defined(CONFIG_CACHEFILES_DEBUG) | |
404 | #define _enter(FMT, ...) \ | |
405 | do { \ | |
406 | if (cachefiles_debug & CACHEFILES_DEBUG_KENTER) \ | |
407 | kenter(FMT, ##__VA_ARGS__); \ | |
408 | } while (0) | |
409 | ||
410 | #define _leave(FMT, ...) \ | |
411 | do { \ | |
412 | if (cachefiles_debug & CACHEFILES_DEBUG_KLEAVE) \ | |
413 | kleave(FMT, ##__VA_ARGS__); \ | |
414 | } while (0) | |
415 | ||
416 | #define _debug(FMT, ...) \ | |
417 | do { \ | |
418 | if (cachefiles_debug & CACHEFILES_DEBUG_KDEBUG) \ | |
419 | kdebug(FMT, ##__VA_ARGS__); \ | |
420 | } while (0) | |
421 | ||
422 | #else | |
423 | #define _enter(FMT, ...) no_printk("==> %s("FMT")", __func__, ##__VA_ARGS__) | |
424 | #define _leave(FMT, ...) no_printk("<== %s()"FMT"", __func__, ##__VA_ARGS__) | |
425 | #define _debug(FMT, ...) no_printk(FMT, ##__VA_ARGS__) | |
426 | #endif | |
427 | ||
428 | #if 1 /* defined(__KDEBUGALL) */ | |
429 | ||
430 | #define ASSERT(X) \ | |
431 | do { \ | |
432 | if (unlikely(!(X))) { \ | |
433 | pr_err("\n"); \ | |
434 | pr_err("Assertion failed\n"); \ | |
435 | BUG(); \ | |
436 | } \ | |
437 | } while (0) | |
438 | ||
439 | #define ASSERTCMP(X, OP, Y) \ | |
440 | do { \ | |
441 | if (unlikely(!((X) OP (Y)))) { \ | |
442 | pr_err("\n"); \ | |
443 | pr_err("Assertion failed\n"); \ | |
444 | pr_err("%lx " #OP " %lx is false\n", \ | |
445 | (unsigned long)(X), (unsigned long)(Y)); \ | |
446 | BUG(); \ | |
447 | } \ | |
448 | } while (0) | |
449 | ||
450 | #define ASSERTIF(C, X) \ | |
451 | do { \ | |
452 | if (unlikely((C) && !(X))) { \ | |
453 | pr_err("\n"); \ | |
454 | pr_err("Assertion failed\n"); \ | |
455 | BUG(); \ | |
456 | } \ | |
457 | } while (0) | |
458 | ||
459 | #define ASSERTIFCMP(C, X, OP, Y) \ | |
460 | do { \ | |
461 | if (unlikely((C) && !((X) OP (Y)))) { \ | |
462 | pr_err("\n"); \ | |
463 | pr_err("Assertion failed\n"); \ | |
464 | pr_err("%lx " #OP " %lx is false\n", \ | |
465 | (unsigned long)(X), (unsigned long)(Y)); \ | |
466 | BUG(); \ | |
467 | } \ | |
468 | } while (0) | |
469 | ||
470 | #else | |
471 | ||
472 | #define ASSERT(X) do {} while (0) | |
473 | #define ASSERTCMP(X, OP, Y) do {} while (0) | |
474 | #define ASSERTIF(C, X) do {} while (0) | |
475 | #define ASSERTIFCMP(C, X, OP, Y) do {} while (0) | |
476 | ||
477 | #endif |