Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
torvalds
GitHub Repository: torvalds/linux
Path: blob/master/fs/cachefiles/internal.h
26278 views
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 ([email protected])
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
#include <linux/xarray.h>
19
#include <linux/cachefiles.h>
20
21
#define CACHEFILES_DIO_BLOCK_SIZE 4096
22
23
struct cachefiles_cache;
24
struct cachefiles_object;
25
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
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
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. */
51
CACHEFILES_ONDEMAND_OBJSTATE_DROPPING, /* Object is being dropped. */
52
};
53
54
struct cachefiles_ondemand_info {
55
struct work_struct ondemand_work;
56
int ondemand_id;
57
enum cachefiles_object_state state;
58
struct cachefiles_object *object;
59
spinlock_t lock;
60
};
61
62
/*
63
* Backing file state.
64
*/
65
struct cachefiles_object {
66
struct fscache_cookie *cookie; /* Netfs data storage object cookie */
67
struct cachefiles_volume *volume; /* Cache volume that holds this object */
68
struct list_head cache_link; /* Link in cache->*_list */
69
struct file *file; /* The file representing this object */
70
char *d_name; /* Backing file name */
71
int debug_id;
72
spinlock_t lock;
73
refcount_t ref;
74
enum cachefiles_content content_info:8; /* Info about content presence */
75
unsigned long flags;
76
#define CACHEFILES_OBJECT_USING_TMPFILE 0 /* Have an unlinked tmpfile */
77
#ifdef CONFIG_CACHEFILES_ONDEMAND
78
struct cachefiles_ondemand_info *ondemand;
79
#endif
80
};
81
82
#define CACHEFILES_ONDEMAND_ID_CLOSED -1
83
84
/*
85
* Cache files cache definition
86
*/
87
struct cachefiles_cache {
88
struct fscache_cache *cache; /* Cache cookie */
89
struct vfsmount *mnt; /* mountpoint holding the cache */
90
struct dentry *store; /* Directory into which live objects go */
91
struct dentry *graveyard; /* directory into which dead objects go */
92
struct file *cachefilesd; /* manager daemon handle */
93
struct list_head volumes; /* List of volume objects */
94
struct list_head object_list; /* List of active objects */
95
spinlock_t object_list_lock; /* Lock for volumes and object_list */
96
const struct cred *cache_cred; /* security override for accessing cache */
97
struct mutex daemon_mutex; /* command serialisation mutex */
98
wait_queue_head_t daemon_pollwq; /* poll waitqueue for daemon */
99
atomic_t gravecounter; /* graveyard uniquifier */
100
atomic_t f_released; /* number of objects released lately */
101
atomic_long_t b_released; /* number of blocks released lately */
102
atomic_long_t b_writing; /* Number of blocks being written */
103
unsigned frun_percent; /* when to stop culling (% files) */
104
unsigned fcull_percent; /* when to start culling (% files) */
105
unsigned fstop_percent; /* when to stop allocating (% files) */
106
unsigned brun_percent; /* when to stop culling (% blocks) */
107
unsigned bcull_percent; /* when to start culling (% blocks) */
108
unsigned bstop_percent; /* when to stop allocating (% blocks) */
109
unsigned bsize; /* cache's block size */
110
unsigned bshift; /* ilog2(bsize) */
111
uint64_t frun; /* when to stop culling */
112
uint64_t fcull; /* when to start culling */
113
uint64_t fstop; /* when to stop allocating */
114
sector_t brun; /* when to stop culling */
115
sector_t bcull; /* when to start culling */
116
sector_t bstop; /* when to stop allocating */
117
unsigned long flags;
118
#define CACHEFILES_READY 0 /* T if cache prepared */
119
#define CACHEFILES_DEAD 1 /* T if cache dead */
120
#define CACHEFILES_CULLING 2 /* T if cull engaged */
121
#define CACHEFILES_STATE_CHANGED 3 /* T if state changed (poll trigger) */
122
#define CACHEFILES_ONDEMAND_MODE 4 /* T if in on-demand read mode */
123
char *rootdirname; /* name of cache root directory */
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;
130
u32 msg_id_next;
131
u32 secid; /* LSM security id */
132
bool have_secid; /* whether "secid" was set */
133
};
134
135
static inline bool cachefiles_in_ondemand_mode(struct cachefiles_cache *cache)
136
{
137
return IS_ENABLED(CONFIG_CACHEFILES_ONDEMAND) &&
138
test_bit(CACHEFILES_ONDEMAND_MODE, &cache->flags);
139
}
140
141
struct cachefiles_req {
142
struct cachefiles_object *object;
143
struct completion done;
144
refcount_t ref;
145
int error;
146
struct cachefiles_msg msg;
147
};
148
149
#define CACHEFILES_REQ_NEW XA_MARK_1
150
151
#include <trace/events/cachefiles.h>
152
153
static inline
154
struct file *cachefiles_cres_file(struct netfs_cache_resources *cres)
155
{
156
return cres->cache_priv2;
157
}
158
159
static inline
160
struct cachefiles_object *cachefiles_cres_object(struct netfs_cache_resources *cres)
161
{
162
return fscache_cres_cookie(cres)->cache_priv;
163
}
164
165
/*
166
* note change of state for daemon
167
*/
168
static inline void cachefiles_state_changed(struct cachefiles_cache *cache)
169
{
170
set_bit(CACHEFILES_STATE_CHANGED, &cache->flags);
171
wake_up_all(&cache->daemon_pollwq);
172
}
173
174
/*
175
* cache.c
176
*/
177
extern int cachefiles_add_cache(struct cachefiles_cache *cache);
178
extern void cachefiles_withdraw_cache(struct cachefiles_cache *cache);
179
180
enum cachefiles_has_space_for {
181
cachefiles_has_space_check,
182
cachefiles_has_space_for_write,
183
cachefiles_has_space_for_create,
184
};
185
extern int cachefiles_has_space(struct cachefiles_cache *cache,
186
unsigned fnr, unsigned bnr,
187
enum cachefiles_has_space_for reason);
188
189
/*
190
* daemon.c
191
*/
192
extern const struct file_operations cachefiles_daemon_fops;
193
extern void cachefiles_flush_reqs(struct cachefiles_cache *cache);
194
extern void cachefiles_get_unbind_pincount(struct cachefiles_cache *cache);
195
extern void cachefiles_put_unbind_pincount(struct cachefiles_cache *cache);
196
197
/*
198
* error_inject.c
199
*/
200
#ifdef CONFIG_CACHEFILES_ERROR_INJECTION
201
extern unsigned int cachefiles_error_injection_state;
202
extern int cachefiles_register_error_injection(void);
203
extern void cachefiles_unregister_error_injection(void);
204
205
#else
206
#define cachefiles_error_injection_state 0
207
208
static inline int cachefiles_register_error_injection(void)
209
{
210
return 0;
211
}
212
213
static inline void cachefiles_unregister_error_injection(void)
214
{
215
}
216
#endif
217
218
219
static inline int cachefiles_inject_read_error(void)
220
{
221
return cachefiles_error_injection_state & 2 ? -EIO : 0;
222
}
223
224
static inline int cachefiles_inject_write_error(void)
225
{
226
return cachefiles_error_injection_state & 2 ? -EIO :
227
cachefiles_error_injection_state & 1 ? -ENOSPC :
228
0;
229
}
230
231
static inline int cachefiles_inject_remove_error(void)
232
{
233
return cachefiles_error_injection_state & 2 ? -EIO : 0;
234
}
235
236
/*
237
* interface.c
238
*/
239
extern const struct fscache_cache_ops cachefiles_cache_ops;
240
extern void cachefiles_see_object(struct cachefiles_object *object,
241
enum cachefiles_obj_ref_trace why);
242
extern struct cachefiles_object *cachefiles_grab_object(struct cachefiles_object *object,
243
enum cachefiles_obj_ref_trace why);
244
extern void cachefiles_put_object(struct cachefiles_object *object,
245
enum cachefiles_obj_ref_trace why);
246
247
/*
248
* io.c
249
*/
250
extern bool cachefiles_begin_operation(struct netfs_cache_resources *cres,
251
enum fscache_want_state want_state);
252
extern int __cachefiles_prepare_write(struct cachefiles_object *object,
253
struct file *file,
254
loff_t *_start, size_t *_len, size_t upper_len,
255
bool no_space_allocated_yet);
256
extern int __cachefiles_write(struct cachefiles_object *object,
257
struct file *file,
258
loff_t start_pos,
259
struct iov_iter *iter,
260
netfs_io_terminated_t term_func,
261
void *term_func_priv);
262
263
/*
264
* key.c
265
*/
266
extern bool cachefiles_cook_key(struct cachefiles_object *object);
267
268
/*
269
* main.c
270
*/
271
extern struct kmem_cache *cachefiles_object_jar;
272
273
/*
274
* namei.c
275
*/
276
extern void cachefiles_unmark_inode_in_use(struct cachefiles_object *object,
277
struct file *file);
278
extern int cachefiles_bury_object(struct cachefiles_cache *cache,
279
struct cachefiles_object *object,
280
struct dentry *dir,
281
struct dentry *rep,
282
enum fscache_why_object_killed why);
283
extern int cachefiles_delete_object(struct cachefiles_object *object,
284
enum fscache_why_object_killed why);
285
extern bool cachefiles_look_up_object(struct cachefiles_object *object);
286
extern struct dentry *cachefiles_get_directory(struct cachefiles_cache *cache,
287
struct dentry *dir,
288
const char *name,
289
bool *_is_new);
290
extern void cachefiles_put_directory(struct dentry *dir);
291
292
extern int cachefiles_cull(struct cachefiles_cache *cache, struct dentry *dir,
293
char *filename);
294
295
extern int cachefiles_check_in_use(struct cachefiles_cache *cache,
296
struct dentry *dir, char *filename);
297
extern struct file *cachefiles_create_tmpfile(struct cachefiles_object *object);
298
extern bool cachefiles_commit_tmpfile(struct cachefiles_cache *cache,
299
struct cachefiles_object *object);
300
301
/*
302
* ondemand.c
303
*/
304
#ifdef CONFIG_CACHEFILES_ONDEMAND
305
extern ssize_t cachefiles_ondemand_daemon_read(struct cachefiles_cache *cache,
306
char __user *_buffer, size_t buflen);
307
308
extern int cachefiles_ondemand_copen(struct cachefiles_cache *cache,
309
char *args);
310
311
extern int cachefiles_ondemand_restore(struct cachefiles_cache *cache,
312
char *args);
313
314
extern int cachefiles_ondemand_init_object(struct cachefiles_object *object);
315
extern void cachefiles_ondemand_clean_object(struct cachefiles_object *object);
316
317
extern int cachefiles_ondemand_read(struct cachefiles_object *object,
318
loff_t pos, size_t len);
319
320
extern int cachefiles_ondemand_init_obj_info(struct cachefiles_object *obj,
321
struct cachefiles_volume *volume);
322
extern void cachefiles_ondemand_deinit_obj_info(struct cachefiles_object *obj);
323
324
#define CACHEFILES_OBJECT_STATE_FUNCS(_state, _STATE) \
325
static inline bool \
326
cachefiles_ondemand_object_is_##_state(const struct cachefiles_object *object) \
327
{ \
328
return object->ondemand->state == CACHEFILES_ONDEMAND_OBJSTATE_##_STATE; \
329
} \
330
\
331
static inline void \
332
cachefiles_ondemand_set_object_##_state(struct cachefiles_object *object) \
333
{ \
334
object->ondemand->state = CACHEFILES_ONDEMAND_OBJSTATE_##_STATE; \
335
}
336
337
CACHEFILES_OBJECT_STATE_FUNCS(open, OPEN);
338
CACHEFILES_OBJECT_STATE_FUNCS(close, CLOSE);
339
CACHEFILES_OBJECT_STATE_FUNCS(reopening, REOPENING);
340
CACHEFILES_OBJECT_STATE_FUNCS(dropping, DROPPING);
341
342
static inline bool cachefiles_ondemand_is_reopening_read(struct cachefiles_req *req)
343
{
344
return cachefiles_ondemand_object_is_reopening(req->object) &&
345
req->msg.opcode == CACHEFILES_OP_READ;
346
}
347
348
#else
349
static inline ssize_t cachefiles_ondemand_daemon_read(struct cachefiles_cache *cache,
350
char __user *_buffer, size_t buflen)
351
{
352
return -EOPNOTSUPP;
353
}
354
355
static inline int cachefiles_ondemand_init_object(struct cachefiles_object *object)
356
{
357
return 0;
358
}
359
360
static inline void cachefiles_ondemand_clean_object(struct cachefiles_object *object)
361
{
362
}
363
364
static inline int cachefiles_ondemand_read(struct cachefiles_object *object,
365
loff_t pos, size_t len)
366
{
367
return -EOPNOTSUPP;
368
}
369
370
static inline int cachefiles_ondemand_init_obj_info(struct cachefiles_object *obj,
371
struct cachefiles_volume *volume)
372
{
373
return 0;
374
}
375
static inline void cachefiles_ondemand_deinit_obj_info(struct cachefiles_object *obj)
376
{
377
}
378
379
static inline bool cachefiles_ondemand_is_reopening_read(struct cachefiles_req *req)
380
{
381
return false;
382
}
383
#endif
384
385
/*
386
* security.c
387
*/
388
extern int cachefiles_get_security_ID(struct cachefiles_cache *cache);
389
extern int cachefiles_determine_cache_security(struct cachefiles_cache *cache,
390
struct dentry *root,
391
const struct cred **_saved_cred);
392
393
static inline void cachefiles_begin_secure(struct cachefiles_cache *cache,
394
const struct cred **_saved_cred)
395
{
396
*_saved_cred = override_creds(cache->cache_cred);
397
}
398
399
static inline void cachefiles_end_secure(struct cachefiles_cache *cache,
400
const struct cred *saved_cred)
401
{
402
revert_creds(saved_cred);
403
}
404
405
/*
406
* volume.c
407
*/
408
void cachefiles_acquire_volume(struct fscache_volume *volume);
409
void cachefiles_free_volume(struct fscache_volume *volume);
410
void cachefiles_withdraw_volume(struct cachefiles_volume *volume);
411
412
/*
413
* xattr.c
414
*/
415
extern int cachefiles_set_object_xattr(struct cachefiles_object *object);
416
extern int cachefiles_check_auxdata(struct cachefiles_object *object,
417
struct file *file);
418
extern int cachefiles_remove_object_xattr(struct cachefiles_cache *cache,
419
struct cachefiles_object *object,
420
struct dentry *dentry);
421
extern void cachefiles_prepare_to_write(struct fscache_cookie *cookie);
422
extern bool cachefiles_set_volume_xattr(struct cachefiles_volume *volume);
423
extern int cachefiles_check_volume_xattr(struct cachefiles_volume *volume);
424
425
/*
426
* Error handling
427
*/
428
#define cachefiles_io_error(___cache, FMT, ...) \
429
do { \
430
pr_err("I/O Error: " FMT"\n", ##__VA_ARGS__); \
431
fscache_io_error((___cache)->cache); \
432
set_bit(CACHEFILES_DEAD, &(___cache)->flags); \
433
if (cachefiles_in_ondemand_mode(___cache)) \
434
cachefiles_flush_reqs(___cache); \
435
} while (0)
436
437
#define cachefiles_io_error_obj(object, FMT, ...) \
438
do { \
439
struct cachefiles_cache *___cache; \
440
\
441
___cache = (object)->volume->cache; \
442
cachefiles_io_error(___cache, FMT " [o=%08x]", ##__VA_ARGS__, \
443
(object)->debug_id); \
444
} while (0)
445
446
447
/*
448
* Debug tracing
449
*/
450
extern unsigned cachefiles_debug;
451
#define CACHEFILES_DEBUG_KENTER 1
452
#define CACHEFILES_DEBUG_KLEAVE 2
453
#define CACHEFILES_DEBUG_KDEBUG 4
454
455
#define dbgprintk(FMT, ...) \
456
printk(KERN_DEBUG "[%-6.6s] "FMT"\n", current->comm, ##__VA_ARGS__)
457
458
#define kenter(FMT, ...) dbgprintk("==> %s("FMT")", __func__, ##__VA_ARGS__)
459
#define kleave(FMT, ...) dbgprintk("<== %s()"FMT"", __func__, ##__VA_ARGS__)
460
#define kdebug(FMT, ...) dbgprintk(FMT, ##__VA_ARGS__)
461
462
463
#if defined(__KDEBUG)
464
#define _enter(FMT, ...) kenter(FMT, ##__VA_ARGS__)
465
#define _leave(FMT, ...) kleave(FMT, ##__VA_ARGS__)
466
#define _debug(FMT, ...) kdebug(FMT, ##__VA_ARGS__)
467
468
#elif defined(CONFIG_CACHEFILES_DEBUG)
469
#define _enter(FMT, ...) \
470
do { \
471
if (cachefiles_debug & CACHEFILES_DEBUG_KENTER) \
472
kenter(FMT, ##__VA_ARGS__); \
473
} while (0)
474
475
#define _leave(FMT, ...) \
476
do { \
477
if (cachefiles_debug & CACHEFILES_DEBUG_KLEAVE) \
478
kleave(FMT, ##__VA_ARGS__); \
479
} while (0)
480
481
#define _debug(FMT, ...) \
482
do { \
483
if (cachefiles_debug & CACHEFILES_DEBUG_KDEBUG) \
484
kdebug(FMT, ##__VA_ARGS__); \
485
} while (0)
486
487
#else
488
#define _enter(FMT, ...) no_printk("==> %s("FMT")", __func__, ##__VA_ARGS__)
489
#define _leave(FMT, ...) no_printk("<== %s()"FMT"", __func__, ##__VA_ARGS__)
490
#define _debug(FMT, ...) no_printk(FMT, ##__VA_ARGS__)
491
#endif
492
493
#if 1 /* defined(__KDEBUGALL) */
494
495
#define ASSERT(X) \
496
do { \
497
if (unlikely(!(X))) { \
498
pr_err("\n"); \
499
pr_err("Assertion failed\n"); \
500
BUG(); \
501
} \
502
} while (0)
503
504
#define ASSERTCMP(X, OP, Y) \
505
do { \
506
if (unlikely(!((X) OP (Y)))) { \
507
pr_err("\n"); \
508
pr_err("Assertion failed\n"); \
509
pr_err("%lx " #OP " %lx is false\n", \
510
(unsigned long)(X), (unsigned long)(Y)); \
511
BUG(); \
512
} \
513
} while (0)
514
515
#define ASSERTIF(C, X) \
516
do { \
517
if (unlikely((C) && !(X))) { \
518
pr_err("\n"); \
519
pr_err("Assertion failed\n"); \
520
BUG(); \
521
} \
522
} while (0)
523
524
#define ASSERTIFCMP(C, X, OP, Y) \
525
do { \
526
if (unlikely((C) && !((X) OP (Y)))) { \
527
pr_err("\n"); \
528
pr_err("Assertion failed\n"); \
529
pr_err("%lx " #OP " %lx is false\n", \
530
(unsigned long)(X), (unsigned long)(Y)); \
531
BUG(); \
532
} \
533
} while (0)
534
535
#else
536
537
#define ASSERT(X) do {} while (0)
538
#define ASSERTCMP(X, OP, Y) do {} while (0)
539
#define ASSERTIF(C, X) do {} while (0)
540
#define ASSERTIFCMP(C, X, OP, Y) do {} while (0)
541
542
#endif
543
544