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