Merge tag 'ext4_for_linus_stable' of git://git.kernel.org/pub/scm/linux/kernel/git...
[linux-2.6-microblaze.git] / fs / cachefiles / internal.h
1 /* SPDX-License-Identifier: GPL-2.0-or-later */
2 /* General netfs cache on cache files internal defs
3  *
4  * Copyright (C) 2007 Red Hat, Inc. All Rights Reserved.
5  * Written by David Howells (dhowells@redhat.com)
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
22 struct cachefiles_cache;
23 struct cachefiles_object;
24
25 extern 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  * node records
34  */
35 struct cachefiles_object {
36         struct fscache_object           fscache;        /* fscache handle */
37         struct cachefiles_lookup_data   *lookup_data;   /* cached lookup data */
38         struct dentry                   *dentry;        /* the file/dir representing this object */
39         struct dentry                   *backer;        /* backing file */
40         loff_t                          i_size;         /* object size */
41         unsigned long                   flags;
42 #define CACHEFILES_OBJECT_ACTIVE        0               /* T if marked active */
43         atomic_t                        usage;          /* object usage count */
44         uint8_t                         type;           /* object type */
45         uint8_t                         new;            /* T if object new */
46         spinlock_t                      work_lock;
47         struct rb_node                  active_node;    /* link in active tree (dentry is key) */
48 };
49
50 extern struct kmem_cache *cachefiles_object_jar;
51
52 /*
53  * Cache files cache definition
54  */
55 struct cachefiles_cache {
56         struct fscache_cache            cache;          /* FS-Cache record */
57         struct vfsmount                 *mnt;           /* mountpoint holding the cache */
58         struct dentry                   *graveyard;     /* directory into which dead objects go */
59         struct file                     *cachefilesd;   /* manager daemon handle */
60         const struct cred               *cache_cred;    /* security override for accessing cache */
61         struct mutex                    daemon_mutex;   /* command serialisation mutex */
62         wait_queue_head_t               daemon_pollwq;  /* poll waitqueue for daemon */
63         struct rb_root                  active_nodes;   /* active nodes (can't be culled) */
64         rwlock_t                        active_lock;    /* lock for active_nodes */
65         atomic_t                        gravecounter;   /* graveyard uniquifier */
66         atomic_t                        f_released;     /* number of objects released lately */
67         atomic_long_t                   b_released;     /* number of blocks released lately */
68         unsigned                        frun_percent;   /* when to stop culling (% files) */
69         unsigned                        fcull_percent;  /* when to start culling (% files) */
70         unsigned                        fstop_percent;  /* when to stop allocating (% files) */
71         unsigned                        brun_percent;   /* when to stop culling (% blocks) */
72         unsigned                        bcull_percent;  /* when to start culling (% blocks) */
73         unsigned                        bstop_percent;  /* when to stop allocating (% blocks) */
74         unsigned                        bsize;          /* cache's block size */
75         unsigned                        bshift;         /* min(ilog2(PAGE_SIZE / bsize), 0) */
76         uint64_t                        frun;           /* when to stop culling */
77         uint64_t                        fcull;          /* when to start culling */
78         uint64_t                        fstop;          /* when to stop allocating */
79         sector_t                        brun;           /* when to stop culling */
80         sector_t                        bcull;          /* when to start culling */
81         sector_t                        bstop;          /* when to stop allocating */
82         unsigned long                   flags;
83 #define CACHEFILES_READY                0       /* T if cache prepared */
84 #define CACHEFILES_DEAD                 1       /* T if cache dead */
85 #define CACHEFILES_CULLING              2       /* T if cull engaged */
86 #define CACHEFILES_STATE_CHANGED        3       /* T if state changed (poll trigger) */
87         char                            *rootdirname;   /* name of cache root directory */
88         char                            *secctx;        /* LSM security context */
89         char                            *tag;           /* cache binding tag */
90 };
91
92 /*
93  * backing file read tracking
94  */
95 struct cachefiles_one_read {
96         wait_queue_entry_t                      monitor;        /* link into monitored waitqueue */
97         struct page                     *back_page;     /* backing file page we're waiting for */
98         struct page                     *netfs_page;    /* netfs page we're going to fill */
99         struct fscache_retrieval        *op;            /* retrieval op covering this */
100         struct list_head                op_link;        /* link in op's todo list */
101 };
102
103 /*
104  * backing file write tracking
105  */
106 struct cachefiles_one_write {
107         struct page                     *netfs_page;    /* netfs page to copy */
108         struct cachefiles_object        *object;
109         struct list_head                obj_link;       /* link in object's lists */
110         fscache_rw_complete_t           end_io_func;
111         void                            *context;
112 };
113
114 /*
115  * auxiliary data xattr buffer
116  */
117 struct 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  * note change of state for daemon
127  */
128 static 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  * bind.c
136  */
137 extern int cachefiles_daemon_bind(struct cachefiles_cache *cache, char *args);
138 extern void cachefiles_daemon_unbind(struct cachefiles_cache *cache);
139
140 /*
141  * daemon.c
142  */
143 extern const struct file_operations cachefiles_daemon_fops;
144
145 extern int cachefiles_has_space(struct cachefiles_cache *cache,
146                                 unsigned fnr, unsigned bnr);
147
148 /*
149  * interface.c
150  */
151 extern const struct fscache_cache_ops cachefiles_cache_ops;
152
153 void cachefiles_put_object(struct fscache_object *_object,
154                            enum fscache_obj_ref_trace why);
155
156 /*
157  * key.c
158  */
159 extern char *cachefiles_cook_key(const u8 *raw, int keylen, uint8_t type);
160
161 /*
162  * namei.c
163  */
164 extern void cachefiles_mark_object_inactive(struct cachefiles_cache *cache,
165                                             struct cachefiles_object *object,
166                                             blkcnt_t i_blocks);
167 extern int cachefiles_delete_object(struct cachefiles_cache *cache,
168                                     struct cachefiles_object *object);
169 extern int cachefiles_walk_to_object(struct cachefiles_object *parent,
170                                      struct cachefiles_object *object,
171                                      const char *key,
172                                      struct cachefiles_xattr *auxdata);
173 extern struct dentry *cachefiles_get_directory(struct cachefiles_cache *cache,
174                                                struct dentry *dir,
175                                                const char *name);
176
177 extern int cachefiles_cull(struct cachefiles_cache *cache, struct dentry *dir,
178                            char *filename);
179
180 extern int cachefiles_check_in_use(struct cachefiles_cache *cache,
181                                    struct dentry *dir, char *filename);
182
183 /*
184  * proc.c
185  */
186 #ifdef CONFIG_CACHEFILES_HISTOGRAM
187 extern atomic_t cachefiles_lookup_histogram[HZ];
188 extern atomic_t cachefiles_mkdir_histogram[HZ];
189 extern atomic_t cachefiles_create_histogram[HZ];
190
191 extern int __init cachefiles_proc_init(void);
192 extern void cachefiles_proc_cleanup(void);
193 static inline
194 void cachefiles_hist(atomic_t histogram[], unsigned long start_jif)
195 {
196         unsigned long jif = jiffies - start_jif;
197         if (jif >= HZ)
198                 jif = HZ - 1;
199         atomic_inc(&histogram[jif]);
200 }
201
202 #else
203 #define cachefiles_proc_init()          (0)
204 #define cachefiles_proc_cleanup()       do {} while (0)
205 #define cachefiles_hist(hist, start_jif) do {} while (0)
206 #endif
207
208 /*
209  * rdwr.c
210  */
211 extern int cachefiles_read_or_alloc_page(struct fscache_retrieval *,
212                                          struct page *, gfp_t);
213 extern int cachefiles_read_or_alloc_pages(struct fscache_retrieval *,
214                                           struct list_head *, unsigned *,
215                                           gfp_t);
216 extern int cachefiles_allocate_page(struct fscache_retrieval *, struct page *,
217                                     gfp_t);
218 extern int cachefiles_allocate_pages(struct fscache_retrieval *,
219                                      struct list_head *, unsigned *, gfp_t);
220 extern int cachefiles_write_page(struct fscache_storage *, struct page *);
221 extern void cachefiles_uncache_page(struct fscache_object *, struct page *);
222
223 /*
224  * rdwr2.c
225  */
226 extern int cachefiles_begin_read_operation(struct netfs_read_request *,
227                                            struct fscache_retrieval *);
228
229 /*
230  * security.c
231  */
232 extern int cachefiles_get_security_ID(struct cachefiles_cache *cache);
233 extern int cachefiles_determine_cache_security(struct cachefiles_cache *cache,
234                                                struct dentry *root,
235                                                const struct cred **_saved_cred);
236
237 static inline void cachefiles_begin_secure(struct cachefiles_cache *cache,
238                                            const struct cred **_saved_cred)
239 {
240         *_saved_cred = override_creds(cache->cache_cred);
241 }
242
243 static inline void cachefiles_end_secure(struct cachefiles_cache *cache,
244                                          const struct cred *saved_cred)
245 {
246         revert_creds(saved_cred);
247 }
248
249 /*
250  * xattr.c
251  */
252 extern int cachefiles_check_object_type(struct cachefiles_object *object);
253 extern int cachefiles_set_object_xattr(struct cachefiles_object *object,
254                                        struct cachefiles_xattr *auxdata);
255 extern int cachefiles_update_object_xattr(struct cachefiles_object *object,
256                                           struct cachefiles_xattr *auxdata);
257 extern int cachefiles_check_auxdata(struct cachefiles_object *object);
258 extern int cachefiles_check_object_xattr(struct cachefiles_object *object,
259                                          struct cachefiles_xattr *auxdata);
260 extern int cachefiles_remove_object_xattr(struct cachefiles_cache *cache,
261                                           struct dentry *dentry);
262
263
264 /*
265  * error handling
266  */
267
268 #define cachefiles_io_error(___cache, FMT, ...)         \
269 do {                                                    \
270         pr_err("I/O Error: " FMT"\n", ##__VA_ARGS__);   \
271         fscache_io_error(&(___cache)->cache);           \
272         set_bit(CACHEFILES_DEAD, &(___cache)->flags);   \
273 } while (0)
274
275 #define cachefiles_io_error_obj(object, FMT, ...)                       \
276 do {                                                                    \
277         struct cachefiles_cache *___cache;                              \
278                                                                         \
279         ___cache = container_of((object)->fscache.cache,                \
280                                 struct cachefiles_cache, cache);        \
281         cachefiles_io_error(___cache, FMT, ##__VA_ARGS__);              \
282 } while (0)
283
284
285 /*
286  * debug tracing
287  */
288 #define dbgprintk(FMT, ...) \
289         printk(KERN_DEBUG "[%-6.6s] "FMT"\n", current->comm, ##__VA_ARGS__)
290
291 #define kenter(FMT, ...) dbgprintk("==> %s("FMT")", __func__, ##__VA_ARGS__)
292 #define kleave(FMT, ...) dbgprintk("<== %s()"FMT"", __func__, ##__VA_ARGS__)
293 #define kdebug(FMT, ...) dbgprintk(FMT, ##__VA_ARGS__)
294
295
296 #if defined(__KDEBUG)
297 #define _enter(FMT, ...) kenter(FMT, ##__VA_ARGS__)
298 #define _leave(FMT, ...) kleave(FMT, ##__VA_ARGS__)
299 #define _debug(FMT, ...) kdebug(FMT, ##__VA_ARGS__)
300
301 #elif defined(CONFIG_CACHEFILES_DEBUG)
302 #define _enter(FMT, ...)                                \
303 do {                                                    \
304         if (cachefiles_debug & CACHEFILES_DEBUG_KENTER) \
305                 kenter(FMT, ##__VA_ARGS__);             \
306 } while (0)
307
308 #define _leave(FMT, ...)                                \
309 do {                                                    \
310         if (cachefiles_debug & CACHEFILES_DEBUG_KLEAVE) \
311                 kleave(FMT, ##__VA_ARGS__);             \
312 } while (0)
313
314 #define _debug(FMT, ...)                                \
315 do {                                                    \
316         if (cachefiles_debug & CACHEFILES_DEBUG_KDEBUG) \
317                 kdebug(FMT, ##__VA_ARGS__);             \
318 } while (0)
319
320 #else
321 #define _enter(FMT, ...) no_printk("==> %s("FMT")", __func__, ##__VA_ARGS__)
322 #define _leave(FMT, ...) no_printk("<== %s()"FMT"", __func__, ##__VA_ARGS__)
323 #define _debug(FMT, ...) no_printk(FMT, ##__VA_ARGS__)
324 #endif
325
326 #if 1 /* defined(__KDEBUGALL) */
327
328 #define ASSERT(X)                                                       \
329 do {                                                                    \
330         if (unlikely(!(X))) {                                           \
331                 pr_err("\n");                                           \
332                 pr_err("Assertion failed\n");           \
333                 BUG();                                                  \
334         }                                                               \
335 } while (0)
336
337 #define ASSERTCMP(X, OP, Y)                                             \
338 do {                                                                    \
339         if (unlikely(!((X) OP (Y)))) {                                  \
340                 pr_err("\n");                                           \
341                 pr_err("Assertion failed\n");           \
342                 pr_err("%lx " #OP " %lx is false\n",                    \
343                        (unsigned long)(X), (unsigned long)(Y));         \
344                 BUG();                                                  \
345         }                                                               \
346 } while (0)
347
348 #define ASSERTIF(C, X)                                                  \
349 do {                                                                    \
350         if (unlikely((C) && !(X))) {                                    \
351                 pr_err("\n");                                           \
352                 pr_err("Assertion failed\n");           \
353                 BUG();                                                  \
354         }                                                               \
355 } while (0)
356
357 #define ASSERTIFCMP(C, X, OP, Y)                                        \
358 do {                                                                    \
359         if (unlikely((C) && !((X) OP (Y)))) {                           \
360                 pr_err("\n");                                           \
361                 pr_err("Assertion failed\n");           \
362                 pr_err("%lx " #OP " %lx is false\n",                    \
363                        (unsigned long)(X), (unsigned long)(Y));         \
364                 BUG();                                                  \
365         }                                                               \
366 } while (0)
367
368 #else
369
370 #define ASSERT(X)                       do {} while (0)
371 #define ASSERTCMP(X, OP, Y)             do {} while (0)
372 #define ASSERTIF(C, X)                  do {} while (0)
373 #define ASSERTIFCMP(C, X, OP, Y)        do {} while (0)
374
375 #endif