1
2
3
4
5
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/timer.h>
17#include <linux/wait_bit.h>
18#include <linux/cred.h>
19#include <linux/workqueue.h>
20#include <linux/security.h>
21
22struct cachefiles_cache;
23struct cachefiles_object;
24
25extern unsigned cachefiles_debug;
26#define CACHEFILES_DEBUG_KENTER 1
27#define CACHEFILES_DEBUG_KLEAVE 2
28#define CACHEFILES_DEBUG_KDEBUG 4
29
30#define cachefiles_gfp (__GFP_RECLAIM | __GFP_NORETRY | __GFP_NOMEMALLOC)
31
32
33
34
35struct cachefiles_object {
36 struct fscache_object fscache;
37 struct cachefiles_lookup_data *lookup_data;
38 struct dentry *dentry;
39 struct dentry *backer;
40 loff_t i_size;
41 unsigned long flags;
42#define CACHEFILES_OBJECT_ACTIVE 0
43 atomic_t usage;
44 uint8_t type;
45 uint8_t new;
46 spinlock_t work_lock;
47 struct rb_node active_node;
48};
49
50extern struct kmem_cache *cachefiles_object_jar;
51
52
53
54
55struct cachefiles_cache {
56 struct fscache_cache cache;
57 struct vfsmount *mnt;
58 struct dentry *graveyard;
59 struct file *cachefilesd;
60 const struct cred *cache_cred;
61 struct mutex daemon_mutex;
62 wait_queue_head_t daemon_pollwq;
63 struct rb_root active_nodes;
64 rwlock_t active_lock;
65 atomic_t gravecounter;
66 atomic_t f_released;
67 atomic_long_t b_released;
68 unsigned frun_percent;
69 unsigned fcull_percent;
70 unsigned fstop_percent;
71 unsigned brun_percent;
72 unsigned bcull_percent;
73 unsigned bstop_percent;
74 unsigned bsize;
75 unsigned bshift;
76 uint64_t frun;
77 uint64_t fcull;
78 uint64_t fstop;
79 sector_t brun;
80 sector_t bcull;
81 sector_t bstop;
82 unsigned long flags;
83#define CACHEFILES_READY 0
84#define CACHEFILES_DEAD 1
85#define CACHEFILES_CULLING 2
86#define CACHEFILES_STATE_CHANGED 3
87 char *rootdirname;
88 char *secctx;
89 char *tag;
90};
91
92
93
94
95struct cachefiles_one_read {
96 wait_queue_entry_t monitor;
97 struct page *back_page;
98 struct page *netfs_page;
99 struct fscache_retrieval *op;
100 struct list_head op_link;
101};
102
103
104
105
106struct cachefiles_one_write {
107 struct page *netfs_page;
108 struct cachefiles_object *object;
109 struct list_head obj_link;
110 fscache_rw_complete_t end_io_func;
111 void *context;
112};
113
114
115
116
117struct cachefiles_xattr {
118 uint16_t len;
119 uint8_t type;
120 uint8_t data[];
121};
122
123#include <trace/events/cachefiles.h>
124
125
126
127
128static inline void cachefiles_state_changed(struct cachefiles_cache *cache)
129{
130 set_bit(CACHEFILES_STATE_CHANGED, &cache->flags);
131 wake_up_all(&cache->daemon_pollwq);
132}
133
134
135
136
137extern int cachefiles_daemon_bind(struct cachefiles_cache *cache, char *args);
138extern void cachefiles_daemon_unbind(struct cachefiles_cache *cache);
139
140
141
142
143extern const struct file_operations cachefiles_daemon_fops;
144
145extern int cachefiles_has_space(struct cachefiles_cache *cache,
146 unsigned fnr, unsigned bnr);
147
148
149
150
151extern const struct fscache_cache_ops cachefiles_cache_ops;
152
153void cachefiles_put_object(struct fscache_object *_object,
154 enum fscache_obj_ref_trace why);
155
156
157
158
159extern char *cachefiles_cook_key(const u8 *raw, int keylen, uint8_t type);
160
161
162
163
164extern void cachefiles_mark_object_inactive(struct cachefiles_cache *cache,
165 struct cachefiles_object *object,
166 blkcnt_t i_blocks);
167extern int cachefiles_delete_object(struct cachefiles_cache *cache,
168 struct cachefiles_object *object);
169extern int cachefiles_walk_to_object(struct cachefiles_object *parent,
170 struct cachefiles_object *object,
171 const char *key,
172 struct cachefiles_xattr *auxdata);
173extern struct dentry *cachefiles_get_directory(struct cachefiles_cache *cache,
174 struct dentry *dir,
175 const char *name);
176
177extern int cachefiles_cull(struct cachefiles_cache *cache, struct dentry *dir,
178 char *filename);
179
180extern int cachefiles_check_in_use(struct cachefiles_cache *cache,
181 struct dentry *dir, char *filename);
182
183
184
185
186extern int cachefiles_read_or_alloc_page(struct fscache_retrieval *,
187 struct page *, gfp_t);
188extern int cachefiles_read_or_alloc_pages(struct fscache_retrieval *,
189 struct list_head *, unsigned *,
190 gfp_t);
191extern int cachefiles_allocate_page(struct fscache_retrieval *, struct page *,
192 gfp_t);
193extern int cachefiles_allocate_pages(struct fscache_retrieval *,
194 struct list_head *, unsigned *, gfp_t);
195extern int cachefiles_write_page(struct fscache_storage *, struct page *);
196extern void cachefiles_uncache_page(struct fscache_object *, struct page *);
197
198
199
200
201extern int cachefiles_begin_read_operation(struct netfs_read_request *,
202 struct fscache_retrieval *);
203
204
205
206
207extern int cachefiles_get_security_ID(struct cachefiles_cache *cache);
208extern int cachefiles_determine_cache_security(struct cachefiles_cache *cache,
209 struct dentry *root,
210 const struct cred **_saved_cred);
211
212static inline void cachefiles_begin_secure(struct cachefiles_cache *cache,
213 const struct cred **_saved_cred)
214{
215 *_saved_cred = override_creds(cache->cache_cred);
216}
217
218static inline void cachefiles_end_secure(struct cachefiles_cache *cache,
219 const struct cred *saved_cred)
220{
221 revert_creds(saved_cred);
222}
223
224
225
226
227extern int cachefiles_check_object_type(struct cachefiles_object *object);
228extern int cachefiles_set_object_xattr(struct cachefiles_object *object,
229 struct cachefiles_xattr *auxdata);
230extern int cachefiles_update_object_xattr(struct cachefiles_object *object,
231 struct cachefiles_xattr *auxdata);
232extern int cachefiles_check_auxdata(struct cachefiles_object *object);
233extern int cachefiles_check_object_xattr(struct cachefiles_object *object,
234 struct cachefiles_xattr *auxdata);
235extern int cachefiles_remove_object_xattr(struct cachefiles_cache *cache,
236 struct dentry *dentry);
237
238
239
240
241
242
243#define cachefiles_io_error(___cache, FMT, ...) \
244do { \
245 pr_err("I/O Error: " FMT"\n", ##__VA_ARGS__); \
246 fscache_io_error(&(___cache)->cache); \
247 set_bit(CACHEFILES_DEAD, &(___cache)->flags); \
248} while (0)
249
250#define cachefiles_io_error_obj(object, FMT, ...) \
251do { \
252 struct cachefiles_cache *___cache; \
253 \
254 ___cache = container_of((object)->fscache.cache, \
255 struct cachefiles_cache, cache); \
256 cachefiles_io_error(___cache, FMT, ##__VA_ARGS__); \
257} while (0)
258
259
260
261
262
263#define dbgprintk(FMT, ...) \
264 printk(KERN_DEBUG "[%-6.6s] "FMT"\n", current->comm, ##__VA_ARGS__)
265
266#define kenter(FMT, ...) dbgprintk("==> %s("FMT")", __func__, ##__VA_ARGS__)
267#define kleave(FMT, ...) dbgprintk("<== %s()"FMT"", __func__, ##__VA_ARGS__)
268#define kdebug(FMT, ...) dbgprintk(FMT, ##__VA_ARGS__)
269
270
271#if defined(__KDEBUG)
272#define _enter(FMT, ...) kenter(FMT, ##__VA_ARGS__)
273#define _leave(FMT, ...) kleave(FMT, ##__VA_ARGS__)
274#define _debug(FMT, ...) kdebug(FMT, ##__VA_ARGS__)
275
276#elif defined(CONFIG_CACHEFILES_DEBUG)
277#define _enter(FMT, ...) \
278do { \
279 if (cachefiles_debug & CACHEFILES_DEBUG_KENTER) \
280 kenter(FMT, ##__VA_ARGS__); \
281} while (0)
282
283#define _leave(FMT, ...) \
284do { \
285 if (cachefiles_debug & CACHEFILES_DEBUG_KLEAVE) \
286 kleave(FMT, ##__VA_ARGS__); \
287} while (0)
288
289#define _debug(FMT, ...) \
290do { \
291 if (cachefiles_debug & CACHEFILES_DEBUG_KDEBUG) \
292 kdebug(FMT, ##__VA_ARGS__); \
293} while (0)
294
295#else
296#define _enter(FMT, ...) no_printk("==> %s("FMT")", __func__, ##__VA_ARGS__)
297#define _leave(FMT, ...) no_printk("<== %s()"FMT"", __func__, ##__VA_ARGS__)
298#define _debug(FMT, ...) no_printk(FMT, ##__VA_ARGS__)
299#endif
300
301#if 1
302
303#define ASSERT(X) \
304do { \
305 if (unlikely(!(X))) { \
306 pr_err("\n"); \
307 pr_err("Assertion failed\n"); \
308 BUG(); \
309 } \
310} while (0)
311
312#define ASSERTCMP(X, OP, Y) \
313do { \
314 if (unlikely(!((X) OP (Y)))) { \
315 pr_err("\n"); \
316 pr_err("Assertion failed\n"); \
317 pr_err("%lx " #OP " %lx is false\n", \
318 (unsigned long)(X), (unsigned long)(Y)); \
319 BUG(); \
320 } \
321} while (0)
322
323#define ASSERTIF(C, X) \
324do { \
325 if (unlikely((C) && !(X))) { \
326 pr_err("\n"); \
327 pr_err("Assertion failed\n"); \
328 BUG(); \
329 } \
330} while (0)
331
332#define ASSERTIFCMP(C, X, OP, Y) \
333do { \
334 if (unlikely((C) && !((X) OP (Y)))) { \
335 pr_err("\n"); \
336 pr_err("Assertion failed\n"); \
337 pr_err("%lx " #OP " %lx is false\n", \
338 (unsigned long)(X), (unsigned long)(Y)); \
339 BUG(); \
340 } \
341} while (0)
342
343#else
344
345#define ASSERT(X) do {} while (0)
346#define ASSERTCMP(X, OP, Y) do {} while (0)
347#define ASSERTIF(C, X) do {} while (0)
348#define ASSERTIFCMP(C, X, OP, Y) do {} while (0)
349
350#endif
351