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