blob: 2fcde1a34b7ce8357c82c2202ccb2f1b8fcfde21 [file] [log] [blame]
David Howells9ae326a2009-04-03 16:42:41 +01001/* 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
Fabian Frederick0227d6ab2014-06-06 14:37:33 -070012#ifdef pr_fmt
13#undef pr_fmt
14#endif
15
16#define pr_fmt(fmt) "CacheFiles: " fmt
17
18
David Howells9ae326a2009-04-03 16:42:41 +010019#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
Mel Gorman71baba42015-11-06 16:28:28 -080033#define cachefiles_gfp (__GFP_RECLAIM | __GFP_NORETRY | __GFP_NOMEMALLOC)
David Howells5f4f9f42012-12-20 21:52:33 +000034
David Howells9ae326a2009-04-03 16:42:41 +010035/*
36 * node records
37 */
38struct cachefiles_object {
39 struct fscache_object fscache; /* fscache handle */
40 struct cachefiles_lookup_data *lookup_data; /* cached lookup data */
41 struct dentry *dentry; /* the file/dir representing this object */
42 struct dentry *backer; /* backing file */
43 loff_t i_size; /* object size */
44 unsigned long flags;
45#define CACHEFILES_OBJECT_ACTIVE 0 /* T if marked active */
46 atomic_t usage; /* object usage count */
47 uint8_t type; /* object type */
48 uint8_t new; /* T if object new */
49 spinlock_t work_lock;
50 struct rb_node active_node; /* link in active tree (dentry is key) */
51};
52
53extern struct kmem_cache *cachefiles_object_jar;
54
55/*
56 * Cache files cache definition
57 */
58struct cachefiles_cache {
59 struct fscache_cache cache; /* FS-Cache record */
60 struct vfsmount *mnt; /* mountpoint holding the cache */
61 struct dentry *graveyard; /* directory into which dead objects go */
62 struct file *cachefilesd; /* manager daemon handle */
63 const struct cred *cache_cred; /* security override for accessing cache */
64 struct mutex daemon_mutex; /* command serialisation mutex */
65 wait_queue_head_t daemon_pollwq; /* poll waitqueue for daemon */
66 struct rb_root active_nodes; /* active nodes (can't be culled) */
67 rwlock_t active_lock; /* lock for active_nodes */
68 atomic_t gravecounter; /* graveyard uniquifier */
David Howellsa5b3a802016-02-01 16:43:04 +000069 atomic_t f_released; /* number of objects released lately */
70 atomic_long_t b_released; /* number of blocks released lately */
David Howells9ae326a2009-04-03 16:42:41 +010071 unsigned frun_percent; /* when to stop culling (% files) */
72 unsigned fcull_percent; /* when to start culling (% files) */
73 unsigned fstop_percent; /* when to stop allocating (% files) */
74 unsigned brun_percent; /* when to stop culling (% blocks) */
75 unsigned bcull_percent; /* when to start culling (% blocks) */
76 unsigned bstop_percent; /* when to stop allocating (% blocks) */
77 unsigned bsize; /* cache's block size */
78 unsigned bshift; /* min(ilog2(PAGE_SIZE / bsize), 0) */
79 uint64_t frun; /* when to stop culling */
80 uint64_t fcull; /* when to start culling */
81 uint64_t fstop; /* when to stop allocating */
82 sector_t brun; /* when to stop culling */
83 sector_t bcull; /* when to start culling */
84 sector_t bstop; /* when to stop allocating */
85 unsigned long flags;
86#define CACHEFILES_READY 0 /* T if cache prepared */
87#define CACHEFILES_DEAD 1 /* T if cache dead */
88#define CACHEFILES_CULLING 2 /* T if cull engaged */
89#define CACHEFILES_STATE_CHANGED 3 /* T if state changed (poll trigger) */
90 char *rootdirname; /* name of cache root directory */
91 char *secctx; /* LSM security context */
92 char *tag; /* cache binding tag */
93};
94
95/*
96 * backing file read tracking
97 */
98struct cachefiles_one_read {
99 wait_queue_t monitor; /* link into monitored waitqueue */
100 struct page *back_page; /* backing file page we're waiting for */
101 struct page *netfs_page; /* netfs page we're going to fill */
102 struct fscache_retrieval *op; /* retrieval op covering this */
103 struct list_head op_link; /* link in op's todo list */
104};
105
106/*
107 * backing file write tracking
108 */
109struct cachefiles_one_write {
110 struct page *netfs_page; /* netfs page to copy */
111 struct cachefiles_object *object;
112 struct list_head obj_link; /* link in object's lists */
113 fscache_rw_complete_t end_io_func;
114 void *context;
115};
116
117/*
118 * auxiliary data xattr buffer
119 */
120struct cachefiles_xattr {
121 uint16_t len;
122 uint8_t type;
123 uint8_t data[];
124};
125
126/*
127 * note change of state for daemon
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/*
David Howells911e6902009-05-27 15:46:55 +0100136 * bind.c
David Howells9ae326a2009-04-03 16:42:41 +0100137 */
138extern int cachefiles_daemon_bind(struct cachefiles_cache *cache, char *args);
139extern void cachefiles_daemon_unbind(struct cachefiles_cache *cache);
140
141/*
David Howells911e6902009-05-27 15:46:55 +0100142 * daemon.c
David Howells9ae326a2009-04-03 16:42:41 +0100143 */
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/*
David Howells911e6902009-05-27 15:46:55 +0100150 * interface.c
David Howells9ae326a2009-04-03 16:42:41 +0100151 */
152extern const struct fscache_cache_ops cachefiles_cache_ops;
153
154/*
David Howells911e6902009-05-27 15:46:55 +0100155 * key.c
David Howells9ae326a2009-04-03 16:42:41 +0100156 */
157extern char *cachefiles_cook_key(const u8 *raw, int keylen, uint8_t type);
158
159/*
David Howells911e6902009-05-27 15:46:55 +0100160 * namei.c
David Howells9ae326a2009-04-03 16:42:41 +0100161 */
David Howellsa5b3a802016-02-01 16:43:04 +0000162extern void cachefiles_mark_object_inactive(struct cachefiles_cache *cache,
163 struct cachefiles_object *object);
David Howells9ae326a2009-04-03 16:42:41 +0100164extern int cachefiles_delete_object(struct cachefiles_cache *cache,
165 struct cachefiles_object *object);
166extern int cachefiles_walk_to_object(struct cachefiles_object *parent,
167 struct cachefiles_object *object,
168 const char *key,
169 struct cachefiles_xattr *auxdata);
170extern struct dentry *cachefiles_get_directory(struct cachefiles_cache *cache,
171 struct dentry *dir,
172 const char *name);
173
174extern int cachefiles_cull(struct cachefiles_cache *cache, struct dentry *dir,
175 char *filename);
176
177extern int cachefiles_check_in_use(struct cachefiles_cache *cache,
178 struct dentry *dir, char *filename);
179
180/*
David Howells911e6902009-05-27 15:46:55 +0100181 * proc.c
David Howells9ae326a2009-04-03 16:42:41 +0100182 */
183#ifdef CONFIG_CACHEFILES_HISTOGRAM
184extern atomic_t cachefiles_lookup_histogram[HZ];
185extern atomic_t cachefiles_mkdir_histogram[HZ];
186extern atomic_t cachefiles_create_histogram[HZ];
187
188extern int __init cachefiles_proc_init(void);
189extern void cachefiles_proc_cleanup(void);
190static inline
191void cachefiles_hist(atomic_t histogram[], unsigned long start_jif)
192{
193 unsigned long jif = jiffies - start_jif;
194 if (jif >= HZ)
195 jif = HZ - 1;
196 atomic_inc(&histogram[jif]);
197}
198
199#else
200#define cachefiles_proc_init() (0)
201#define cachefiles_proc_cleanup() do {} while (0)
202#define cachefiles_hist(hist, start_jif) do {} while (0)
203#endif
204
205/*
David Howells911e6902009-05-27 15:46:55 +0100206 * rdwr.c
David Howells9ae326a2009-04-03 16:42:41 +0100207 */
208extern int cachefiles_read_or_alloc_page(struct fscache_retrieval *,
209 struct page *, gfp_t);
210extern int cachefiles_read_or_alloc_pages(struct fscache_retrieval *,
211 struct list_head *, unsigned *,
212 gfp_t);
213extern int cachefiles_allocate_page(struct fscache_retrieval *, struct page *,
214 gfp_t);
215extern int cachefiles_allocate_pages(struct fscache_retrieval *,
216 struct list_head *, unsigned *, gfp_t);
217extern int cachefiles_write_page(struct fscache_storage *, struct page *);
218extern void cachefiles_uncache_page(struct fscache_object *, struct page *);
219
220/*
David Howells911e6902009-05-27 15:46:55 +0100221 * security.c
David Howells9ae326a2009-04-03 16:42:41 +0100222 */
223extern int cachefiles_get_security_ID(struct cachefiles_cache *cache);
224extern int cachefiles_determine_cache_security(struct cachefiles_cache *cache,
225 struct dentry *root,
226 const struct cred **_saved_cred);
227
228static inline void cachefiles_begin_secure(struct cachefiles_cache *cache,
229 const struct cred **_saved_cred)
230{
231 *_saved_cred = override_creds(cache->cache_cred);
232}
233
234static inline void cachefiles_end_secure(struct cachefiles_cache *cache,
235 const struct cred *saved_cred)
236{
237 revert_creds(saved_cred);
238}
239
240/*
David Howells911e6902009-05-27 15:46:55 +0100241 * xattr.c
David Howells9ae326a2009-04-03 16:42:41 +0100242 */
243extern int cachefiles_check_object_type(struct cachefiles_object *object);
244extern int cachefiles_set_object_xattr(struct cachefiles_object *object,
245 struct cachefiles_xattr *auxdata);
246extern int cachefiles_update_object_xattr(struct cachefiles_object *object,
247 struct cachefiles_xattr *auxdata);
David Howells5002d7b2013-08-21 17:29:21 -0400248extern int cachefiles_check_auxdata(struct cachefiles_object *object);
David Howells9ae326a2009-04-03 16:42:41 +0100249extern int cachefiles_check_object_xattr(struct cachefiles_object *object,
250 struct cachefiles_xattr *auxdata);
251extern int cachefiles_remove_object_xattr(struct cachefiles_cache *cache,
252 struct dentry *dentry);
253
254
255/*
256 * error handling
257 */
David Howells9ae326a2009-04-03 16:42:41 +0100258
259#define cachefiles_io_error(___cache, FMT, ...) \
260do { \
Fabian Frederick6ff66ac2014-09-25 16:05:27 -0700261 pr_err("I/O Error: " FMT"\n", ##__VA_ARGS__); \
David Howells9ae326a2009-04-03 16:42:41 +0100262 fscache_io_error(&(___cache)->cache); \
263 set_bit(CACHEFILES_DEAD, &(___cache)->flags); \
264} while (0)
265
266#define cachefiles_io_error_obj(object, FMT, ...) \
267do { \
268 struct cachefiles_cache *___cache; \
269 \
270 ___cache = container_of((object)->fscache.cache, \
271 struct cachefiles_cache, cache); \
272 cachefiles_io_error(___cache, FMT, ##__VA_ARGS__); \
273} while (0)
274
275
276/*
277 * debug tracing
278 */
279#define dbgprintk(FMT, ...) \
280 printk(KERN_DEBUG "[%-6.6s] "FMT"\n", current->comm, ##__VA_ARGS__)
281
David Howells9ae326a2009-04-03 16:42:41 +0100282#define kenter(FMT, ...) dbgprintk("==> %s("FMT")", __func__, ##__VA_ARGS__)
283#define kleave(FMT, ...) dbgprintk("<== %s()"FMT"", __func__, ##__VA_ARGS__)
284#define kdebug(FMT, ...) dbgprintk(FMT, ##__VA_ARGS__)
285
286
287#if defined(__KDEBUG)
288#define _enter(FMT, ...) kenter(FMT, ##__VA_ARGS__)
289#define _leave(FMT, ...) kleave(FMT, ##__VA_ARGS__)
290#define _debug(FMT, ...) kdebug(FMT, ##__VA_ARGS__)
291
292#elif defined(CONFIG_CACHEFILES_DEBUG)
293#define _enter(FMT, ...) \
294do { \
295 if (cachefiles_debug & CACHEFILES_DEBUG_KENTER) \
296 kenter(FMT, ##__VA_ARGS__); \
297} while (0)
298
299#define _leave(FMT, ...) \
300do { \
301 if (cachefiles_debug & CACHEFILES_DEBUG_KLEAVE) \
302 kleave(FMT, ##__VA_ARGS__); \
303} while (0)
304
305#define _debug(FMT, ...) \
306do { \
307 if (cachefiles_debug & CACHEFILES_DEBUG_KDEBUG) \
308 kdebug(FMT, ##__VA_ARGS__); \
309} while (0)
310
311#else
David Howells12fdff32010-08-12 16:54:57 +0100312#define _enter(FMT, ...) no_printk("==> %s("FMT")", __func__, ##__VA_ARGS__)
313#define _leave(FMT, ...) no_printk("<== %s()"FMT"", __func__, ##__VA_ARGS__)
314#define _debug(FMT, ...) no_printk(FMT, ##__VA_ARGS__)
David Howells9ae326a2009-04-03 16:42:41 +0100315#endif
316
317#if 1 /* defined(__KDEBUGALL) */
318
319#define ASSERT(X) \
320do { \
321 if (unlikely(!(X))) { \
Fabian Frederick4e1eb882014-06-06 14:37:32 -0700322 pr_err("\n"); \
Fabian Frederick0227d6ab2014-06-06 14:37:33 -0700323 pr_err("Assertion failed\n"); \
David Howells9ae326a2009-04-03 16:42:41 +0100324 BUG(); \
325 } \
326} while (0)
327
328#define ASSERTCMP(X, OP, Y) \
329do { \
330 if (unlikely(!((X) OP (Y)))) { \
Fabian Frederick4e1eb882014-06-06 14:37:32 -0700331 pr_err("\n"); \
Fabian Frederick0227d6ab2014-06-06 14:37:33 -0700332 pr_err("Assertion failed\n"); \
Fabian Frederick4e1eb882014-06-06 14:37:32 -0700333 pr_err("%lx " #OP " %lx is false\n", \
David Howells9ae326a2009-04-03 16:42:41 +0100334 (unsigned long)(X), (unsigned long)(Y)); \
335 BUG(); \
336 } \
337} while (0)
338
339#define ASSERTIF(C, X) \
340do { \
341 if (unlikely((C) && !(X))) { \
Fabian Frederick4e1eb882014-06-06 14:37:32 -0700342 pr_err("\n"); \
Fabian Frederick0227d6ab2014-06-06 14:37:33 -0700343 pr_err("Assertion failed\n"); \
David Howells9ae326a2009-04-03 16:42:41 +0100344 BUG(); \
345 } \
346} while (0)
347
348#define ASSERTIFCMP(C, X, OP, Y) \
349do { \
350 if (unlikely((C) && !((X) OP (Y)))) { \
Fabian Frederick4e1eb882014-06-06 14:37:32 -0700351 pr_err("\n"); \
Fabian Frederick0227d6ab2014-06-06 14:37:33 -0700352 pr_err("Assertion failed\n"); \
Fabian Frederick4e1eb882014-06-06 14:37:32 -0700353 pr_err("%lx " #OP " %lx is false\n", \
David Howells9ae326a2009-04-03 16:42:41 +0100354 (unsigned long)(X), (unsigned long)(Y)); \
355 BUG(); \
356 } \
357} while (0)
358
359#else
360
361#define ASSERT(X) do {} while (0)
362#define ASSERTCMP(X, OP, Y) do {} while (0)
363#define ASSERTIF(C, X) do {} while (0)
364#define ASSERTIFCMP(C, X, OP, Y) do {} while (0)
365
366#endif