ocfs2: dlm: fix race between purge and get lock resource
[firefly-linux-kernel-4.4.55.git] / fs / cachefiles / internal.h
1 /* General netfs cache on cache files internal defs
2  *
3  * Copyright (C) 2007 Red Hat, Inc. All Rights Reserved.
4  * Written by David Howells (dhowells@redhat.com)
5  *
6  * This program is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU General Public Licence
8  * as published by the Free Software Foundation; either version
9  * 2 of the Licence, or (at your option) any later version.
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
18 struct cachefiles_cache;
19 struct cachefiles_object;
20
21 extern 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  * node records
30  */
31 struct cachefiles_object {
32         struct fscache_object           fscache;        /* fscache handle */
33         struct cachefiles_lookup_data   *lookup_data;   /* cached lookup data */
34         struct dentry                   *dentry;        /* the file/dir representing this object */
35         struct dentry                   *backer;        /* backing file */
36         loff_t                          i_size;         /* object size */
37         unsigned long                   flags;
38 #define CACHEFILES_OBJECT_ACTIVE        0               /* T if marked active */
39 #define CACHEFILES_OBJECT_BURIED        1               /* T if preemptively buried */
40         atomic_t                        usage;          /* object usage count */
41         uint8_t                         type;           /* object type */
42         uint8_t                         new;            /* T if object new */
43         spinlock_t                      work_lock;
44         struct rb_node                  active_node;    /* link in active tree (dentry is key) */
45 };
46
47 extern struct kmem_cache *cachefiles_object_jar;
48
49 /*
50  * Cache files cache definition
51  */
52 struct cachefiles_cache {
53         struct fscache_cache            cache;          /* FS-Cache record */
54         struct vfsmount                 *mnt;           /* mountpoint holding the cache */
55         struct dentry                   *graveyard;     /* directory into which dead objects go */
56         struct file                     *cachefilesd;   /* manager daemon handle */
57         const struct cred               *cache_cred;    /* security override for accessing cache */
58         struct mutex                    daemon_mutex;   /* command serialisation mutex */
59         wait_queue_head_t               daemon_pollwq;  /* poll waitqueue for daemon */
60         struct rb_root                  active_nodes;   /* active nodes (can't be culled) */
61         rwlock_t                        active_lock;    /* lock for active_nodes */
62         atomic_t                        gravecounter;   /* graveyard uniquifier */
63         unsigned                        frun_percent;   /* when to stop culling (% files) */
64         unsigned                        fcull_percent;  /* when to start culling (% files) */
65         unsigned                        fstop_percent;  /* when to stop allocating (% files) */
66         unsigned                        brun_percent;   /* when to stop culling (% blocks) */
67         unsigned                        bcull_percent;  /* when to start culling (% blocks) */
68         unsigned                        bstop_percent;  /* when to stop allocating (% blocks) */
69         unsigned                        bsize;          /* cache's block size */
70         unsigned                        bshift;         /* min(ilog2(PAGE_SIZE / bsize), 0) */
71         uint64_t                        frun;           /* when to stop culling */
72         uint64_t                        fcull;          /* when to start culling */
73         uint64_t                        fstop;          /* when to stop allocating */
74         sector_t                        brun;           /* when to stop culling */
75         sector_t                        bcull;          /* when to start culling */
76         sector_t                        bstop;          /* when to stop allocating */
77         unsigned long                   flags;
78 #define CACHEFILES_READY                0       /* T if cache prepared */
79 #define CACHEFILES_DEAD                 1       /* T if cache dead */
80 #define CACHEFILES_CULLING              2       /* T if cull engaged */
81 #define CACHEFILES_STATE_CHANGED        3       /* T if state changed (poll trigger) */
82         char                            *rootdirname;   /* name of cache root directory */
83         char                            *secctx;        /* LSM security context */
84         char                            *tag;           /* cache binding tag */
85 };
86
87 /*
88  * backing file read tracking
89  */
90 struct cachefiles_one_read {
91         wait_queue_t                    monitor;        /* link into monitored waitqueue */
92         struct page                     *back_page;     /* backing file page we're waiting for */
93         struct page                     *netfs_page;    /* netfs page we're going to fill */
94         struct fscache_retrieval        *op;            /* retrieval op covering this */
95         struct list_head                op_link;        /* link in op's todo list */
96 };
97
98 /*
99  * backing file write tracking
100  */
101 struct cachefiles_one_write {
102         struct page                     *netfs_page;    /* netfs page to copy */
103         struct cachefiles_object        *object;
104         struct list_head                obj_link;       /* link in object's lists */
105         fscache_rw_complete_t           end_io_func;
106         void                            *context;
107 };
108
109 /*
110  * auxiliary data xattr buffer
111  */
112 struct cachefiles_xattr {
113         uint16_t                        len;
114         uint8_t                         type;
115         uint8_t                         data[];
116 };
117
118 /*
119  * note change of state for daemon
120  */
121 static 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  * bind.c
129  */
130 extern int cachefiles_daemon_bind(struct cachefiles_cache *cache, char *args);
131 extern void cachefiles_daemon_unbind(struct cachefiles_cache *cache);
132
133 /*
134  * daemon.c
135  */
136 extern const struct file_operations cachefiles_daemon_fops;
137
138 extern int cachefiles_has_space(struct cachefiles_cache *cache,
139                                 unsigned fnr, unsigned bnr);
140
141 /*
142  * interface.c
143  */
144 extern const struct fscache_cache_ops cachefiles_cache_ops;
145
146 /*
147  * key.c
148  */
149 extern char *cachefiles_cook_key(const u8 *raw, int keylen, uint8_t type);
150
151 /*
152  * namei.c
153  */
154 extern int cachefiles_delete_object(struct cachefiles_cache *cache,
155                                     struct cachefiles_object *object);
156 extern int cachefiles_walk_to_object(struct cachefiles_object *parent,
157                                      struct cachefiles_object *object,
158                                      const char *key,
159                                      struct cachefiles_xattr *auxdata);
160 extern struct dentry *cachefiles_get_directory(struct cachefiles_cache *cache,
161                                                struct dentry *dir,
162                                                const char *name);
163
164 extern int cachefiles_cull(struct cachefiles_cache *cache, struct dentry *dir,
165                            char *filename);
166
167 extern int cachefiles_check_in_use(struct cachefiles_cache *cache,
168                                    struct dentry *dir, char *filename);
169
170 /*
171  * proc.c
172  */
173 #ifdef CONFIG_CACHEFILES_HISTOGRAM
174 extern atomic_t cachefiles_lookup_histogram[HZ];
175 extern atomic_t cachefiles_mkdir_histogram[HZ];
176 extern atomic_t cachefiles_create_histogram[HZ];
177
178 extern int __init cachefiles_proc_init(void);
179 extern void cachefiles_proc_cleanup(void);
180 static inline
181 void 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  * rdwr.c
197  */
198 extern int cachefiles_read_or_alloc_page(struct fscache_retrieval *,
199                                          struct page *, gfp_t);
200 extern int cachefiles_read_or_alloc_pages(struct fscache_retrieval *,
201                                           struct list_head *, unsigned *,
202                                           gfp_t);
203 extern int cachefiles_allocate_page(struct fscache_retrieval *, struct page *,
204                                     gfp_t);
205 extern int cachefiles_allocate_pages(struct fscache_retrieval *,
206                                      struct list_head *, unsigned *, gfp_t);
207 extern int cachefiles_write_page(struct fscache_storage *, struct page *);
208 extern void cachefiles_uncache_page(struct fscache_object *, struct page *);
209
210 /*
211  * security.c
212  */
213 extern int cachefiles_get_security_ID(struct cachefiles_cache *cache);
214 extern int cachefiles_determine_cache_security(struct cachefiles_cache *cache,
215                                                struct dentry *root,
216                                                const struct cred **_saved_cred);
217
218 static 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
224 static 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  * xattr.c
232  */
233 extern int cachefiles_check_object_type(struct cachefiles_object *object);
234 extern int cachefiles_set_object_xattr(struct cachefiles_object *object,
235                                        struct cachefiles_xattr *auxdata);
236 extern int cachefiles_update_object_xattr(struct cachefiles_object *object,
237                                           struct cachefiles_xattr *auxdata);
238 extern int cachefiles_check_object_xattr(struct cachefiles_object *object,
239                                          struct cachefiles_xattr *auxdata);
240 extern int cachefiles_remove_object_xattr(struct cachefiles_cache *cache,
241                                           struct dentry *dentry);
242
243
244 /*
245  * error handling
246  */
247 #define kerror(FMT, ...) printk(KERN_ERR "CacheFiles: "FMT"\n", ##__VA_ARGS__)
248
249 #define cachefiles_io_error(___cache, FMT, ...)         \
250 do {                                                    \
251         kerror("I/O Error: " FMT, ##__VA_ARGS__);       \
252         fscache_io_error(&(___cache)->cache);           \
253         set_bit(CACHEFILES_DEAD, &(___cache)->flags);   \
254 } while (0)
255
256 #define cachefiles_io_error_obj(object, FMT, ...)                       \
257 do {                                                                    \
258         struct cachefiles_cache *___cache;                              \
259                                                                         \
260         ___cache = container_of((object)->fscache.cache,                \
261                                 struct cachefiles_cache, cache);        \
262         cachefiles_io_error(___cache, FMT, ##__VA_ARGS__);              \
263 } while (0)
264
265
266 /*
267  * debug tracing
268  */
269 #define dbgprintk(FMT, ...) \
270         printk(KERN_DEBUG "[%-6.6s] "FMT"\n", current->comm, ##__VA_ARGS__)
271
272 #define kenter(FMT, ...) dbgprintk("==> %s("FMT")", __func__, ##__VA_ARGS__)
273 #define kleave(FMT, ...) dbgprintk("<== %s()"FMT"", __func__, ##__VA_ARGS__)
274 #define kdebug(FMT, ...) dbgprintk(FMT, ##__VA_ARGS__)
275
276
277 #if defined(__KDEBUG)
278 #define _enter(FMT, ...) kenter(FMT, ##__VA_ARGS__)
279 #define _leave(FMT, ...) kleave(FMT, ##__VA_ARGS__)
280 #define _debug(FMT, ...) kdebug(FMT, ##__VA_ARGS__)
281
282 #elif defined(CONFIG_CACHEFILES_DEBUG)
283 #define _enter(FMT, ...)                                \
284 do {                                                    \
285         if (cachefiles_debug & CACHEFILES_DEBUG_KENTER) \
286                 kenter(FMT, ##__VA_ARGS__);             \
287 } while (0)
288
289 #define _leave(FMT, ...)                                \
290 do {                                                    \
291         if (cachefiles_debug & CACHEFILES_DEBUG_KLEAVE) \
292                 kleave(FMT, ##__VA_ARGS__);             \
293 } while (0)
294
295 #define _debug(FMT, ...)                                \
296 do {                                                    \
297         if (cachefiles_debug & CACHEFILES_DEBUG_KDEBUG) \
298                 kdebug(FMT, ##__VA_ARGS__);             \
299 } while (0)
300
301 #else
302 #define _enter(FMT, ...) no_printk("==> %s("FMT")", __func__, ##__VA_ARGS__)
303 #define _leave(FMT, ...) no_printk("<== %s()"FMT"", __func__, ##__VA_ARGS__)
304 #define _debug(FMT, ...) no_printk(FMT, ##__VA_ARGS__)
305 #endif
306
307 #if 1 /* defined(__KDEBUGALL) */
308
309 #define ASSERT(X)                                                       \
310 do {                                                                    \
311         if (unlikely(!(X))) {                                           \
312                 printk(KERN_ERR "\n");                                  \
313                 printk(KERN_ERR "CacheFiles: Assertion failed\n");      \
314                 BUG();                                                  \
315         }                                                               \
316 } while (0)
317
318 #define ASSERTCMP(X, OP, Y)                                             \
319 do {                                                                    \
320         if (unlikely(!((X) OP (Y)))) {                                  \
321                 printk(KERN_ERR "\n");                                  \
322                 printk(KERN_ERR "CacheFiles: Assertion failed\n");      \
323                 printk(KERN_ERR "%lx " #OP " %lx is false\n",           \
324                        (unsigned long)(X), (unsigned long)(Y));         \
325                 BUG();                                                  \
326         }                                                               \
327 } while (0)
328
329 #define ASSERTIF(C, X)                                                  \
330 do {                                                                    \
331         if (unlikely((C) && !(X))) {                                    \
332                 printk(KERN_ERR "\n");                                  \
333                 printk(KERN_ERR "CacheFiles: Assertion failed\n");      \
334                 BUG();                                                  \
335         }                                                               \
336 } while (0)
337
338 #define ASSERTIFCMP(C, X, OP, Y)                                        \
339 do {                                                                    \
340         if (unlikely((C) && !((X) OP (Y)))) {                           \
341                 printk(KERN_ERR "\n");                                  \
342                 printk(KERN_ERR "CacheFiles: Assertion failed\n");      \
343                 printk(KERN_ERR "%lx " #OP " %lx is false\n",           \
344                        (unsigned long)(X), (unsigned long)(Y));         \
345                 BUG();                                                  \
346         }                                                               \
347 } while (0)
348
349 #else
350
351 #define ASSERT(X)                       do {} while (0)
352 #define ASSERTCMP(X, OP, Y)             do {} while (0)
353 #define ASSERTIF(C, X)                  do {} while (0)
354 #define ASSERTIFCMP(C, X, OP, Y)        do {} while (0)
355
356 #endif