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