blob: 8c52472d2efa4ec745821e52d9deaa14bd440515 [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
David Howells5f4f9f42012-12-20 21:52:33 +000033#define cachefiles_gfp (__GFP_WAIT | __GFP_NORETRY | __GFP_NOMEMALLOC)
34
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 */
David Howellsc61ea312010-05-11 16:51:39 +010046#define CACHEFILES_OBJECT_BURIED 1 /* T if preemptively buried */
David Howells9ae326a2009-04-03 16:42:41 +010047 atomic_t usage; /* object usage count */
48 uint8_t type; /* object type */
49 uint8_t new; /* T if object new */
50 spinlock_t work_lock;
51 struct rb_node active_node; /* link in active tree (dentry is key) */
52};
53
54extern struct kmem_cache *cachefiles_object_jar;
55
56/*
57 * Cache files cache definition
58 */
59struct cachefiles_cache {
60 struct fscache_cache cache; /* FS-Cache record */
61 struct vfsmount *mnt; /* mountpoint holding the cache */
62 struct dentry *graveyard; /* directory into which dead objects go */
63 struct file *cachefilesd; /* manager daemon handle */
64 const struct cred *cache_cred; /* security override for accessing cache */
65 struct mutex daemon_mutex; /* command serialisation mutex */
66 wait_queue_head_t daemon_pollwq; /* poll waitqueue for daemon */
67 struct rb_root active_nodes; /* active nodes (can't be culled) */
68 rwlock_t active_lock; /* lock for active_nodes */
69 atomic_t gravecounter; /* graveyard uniquifier */
70 unsigned frun_percent; /* when to stop culling (% files) */
71 unsigned fcull_percent; /* when to start culling (% files) */
72 unsigned fstop_percent; /* when to stop allocating (% files) */
73 unsigned brun_percent; /* when to stop culling (% blocks) */
74 unsigned bcull_percent; /* when to start culling (% blocks) */
75 unsigned bstop_percent; /* when to stop allocating (% blocks) */
76 unsigned bsize; /* cache's block size */
77 unsigned bshift; /* min(ilog2(PAGE_SIZE / bsize), 0) */
78 uint64_t frun; /* when to stop culling */
79 uint64_t fcull; /* when to start culling */
80 uint64_t fstop; /* when to stop allocating */
81 sector_t brun; /* when to stop culling */
82 sector_t bcull; /* when to start culling */
83 sector_t bstop; /* when to stop allocating */
84 unsigned long flags;
85#define CACHEFILES_READY 0 /* T if cache prepared */
86#define CACHEFILES_DEAD 1 /* T if cache dead */
87#define CACHEFILES_CULLING 2 /* T if cull engaged */
88#define CACHEFILES_STATE_CHANGED 3 /* T if state changed (poll trigger) */
89 char *rootdirname; /* name of cache root directory */
90 char *secctx; /* LSM security context */
91 char *tag; /* cache binding tag */
92};
93
94/*
95 * backing file read tracking
96 */
97struct cachefiles_one_read {
98 wait_queue_t monitor; /* link into monitored waitqueue */
99 struct page *back_page; /* backing file page we're waiting for */
100 struct page *netfs_page; /* netfs page we're going to fill */
101 struct fscache_retrieval *op; /* retrieval op covering this */
102 struct list_head op_link; /* link in op's todo list */
103};
104
105/*
106 * backing file write tracking
107 */
108struct cachefiles_one_write {
109 struct page *netfs_page; /* netfs page to copy */
110 struct cachefiles_object *object;
111 struct list_head obj_link; /* link in object's lists */
112 fscache_rw_complete_t end_io_func;
113 void *context;
114};
115
116/*
117 * auxiliary data xattr buffer
118 */
119struct cachefiles_xattr {
120 uint16_t len;
121 uint8_t type;
122 uint8_t data[];
123};
124
125/*
126 * note change of state for daemon
127 */
128static 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/*
David Howells911e6902009-05-27 15:46:55 +0100135 * bind.c
David Howells9ae326a2009-04-03 16:42:41 +0100136 */
137extern int cachefiles_daemon_bind(struct cachefiles_cache *cache, char *args);
138extern void cachefiles_daemon_unbind(struct cachefiles_cache *cache);
139
140/*
David Howells911e6902009-05-27 15:46:55 +0100141 * daemon.c
David Howells9ae326a2009-04-03 16:42:41 +0100142 */
143extern const struct file_operations cachefiles_daemon_fops;
144
145extern int cachefiles_has_space(struct cachefiles_cache *cache,
146 unsigned fnr, unsigned bnr);
147
148/*
David Howells911e6902009-05-27 15:46:55 +0100149 * interface.c
David Howells9ae326a2009-04-03 16:42:41 +0100150 */
151extern const struct fscache_cache_ops cachefiles_cache_ops;
152
153/*
David Howells911e6902009-05-27 15:46:55 +0100154 * key.c
David Howells9ae326a2009-04-03 16:42:41 +0100155 */
156extern char *cachefiles_cook_key(const u8 *raw, int keylen, uint8_t type);
157
158/*
David Howells911e6902009-05-27 15:46:55 +0100159 * namei.c
David Howells9ae326a2009-04-03 16:42:41 +0100160 */
161extern int cachefiles_delete_object(struct cachefiles_cache *cache,
162 struct cachefiles_object *object);
163extern int cachefiles_walk_to_object(struct cachefiles_object *parent,
164 struct cachefiles_object *object,
165 const char *key,
166 struct cachefiles_xattr *auxdata);
167extern struct dentry *cachefiles_get_directory(struct cachefiles_cache *cache,
168 struct dentry *dir,
169 const char *name);
170
171extern int cachefiles_cull(struct cachefiles_cache *cache, struct dentry *dir,
172 char *filename);
173
174extern int cachefiles_check_in_use(struct cachefiles_cache *cache,
175 struct dentry *dir, char *filename);
176
177/*
David Howells911e6902009-05-27 15:46:55 +0100178 * proc.c
David Howells9ae326a2009-04-03 16:42:41 +0100179 */
180#ifdef CONFIG_CACHEFILES_HISTOGRAM
181extern atomic_t cachefiles_lookup_histogram[HZ];
182extern atomic_t cachefiles_mkdir_histogram[HZ];
183extern atomic_t cachefiles_create_histogram[HZ];
184
185extern int __init cachefiles_proc_init(void);
186extern void cachefiles_proc_cleanup(void);
187static inline
188void cachefiles_hist(atomic_t histogram[], unsigned long start_jif)
189{
190 unsigned long jif = jiffies - start_jif;
191 if (jif >= HZ)
192 jif = HZ - 1;
193 atomic_inc(&histogram[jif]);
194}
195
196#else
197#define cachefiles_proc_init() (0)
198#define cachefiles_proc_cleanup() do {} while (0)
199#define cachefiles_hist(hist, start_jif) do {} while (0)
200#endif
201
202/*
David Howells911e6902009-05-27 15:46:55 +0100203 * rdwr.c
David Howells9ae326a2009-04-03 16:42:41 +0100204 */
205extern int cachefiles_read_or_alloc_page(struct fscache_retrieval *,
206 struct page *, gfp_t);
207extern int cachefiles_read_or_alloc_pages(struct fscache_retrieval *,
208 struct list_head *, unsigned *,
209 gfp_t);
210extern int cachefiles_allocate_page(struct fscache_retrieval *, struct page *,
211 gfp_t);
212extern int cachefiles_allocate_pages(struct fscache_retrieval *,
213 struct list_head *, unsigned *, gfp_t);
214extern int cachefiles_write_page(struct fscache_storage *, struct page *);
215extern void cachefiles_uncache_page(struct fscache_object *, struct page *);
216
217/*
David Howells911e6902009-05-27 15:46:55 +0100218 * security.c
David Howells9ae326a2009-04-03 16:42:41 +0100219 */
220extern int cachefiles_get_security_ID(struct cachefiles_cache *cache);
221extern int cachefiles_determine_cache_security(struct cachefiles_cache *cache,
222 struct dentry *root,
223 const struct cred **_saved_cred);
224
225static inline void cachefiles_begin_secure(struct cachefiles_cache *cache,
226 const struct cred **_saved_cred)
227{
228 *_saved_cred = override_creds(cache->cache_cred);
229}
230
231static inline void cachefiles_end_secure(struct cachefiles_cache *cache,
232 const struct cred *saved_cred)
233{
234 revert_creds(saved_cred);
235}
236
237/*
David Howells911e6902009-05-27 15:46:55 +0100238 * xattr.c
David Howells9ae326a2009-04-03 16:42:41 +0100239 */
240extern int cachefiles_check_object_type(struct cachefiles_object *object);
241extern int cachefiles_set_object_xattr(struct cachefiles_object *object,
242 struct cachefiles_xattr *auxdata);
243extern int cachefiles_update_object_xattr(struct cachefiles_object *object,
244 struct cachefiles_xattr *auxdata);
David Howells5002d7b2013-08-21 17:29:21 -0400245extern int cachefiles_check_auxdata(struct cachefiles_object *object);
David Howells9ae326a2009-04-03 16:42:41 +0100246extern int cachefiles_check_object_xattr(struct cachefiles_object *object,
247 struct cachefiles_xattr *auxdata);
248extern int cachefiles_remove_object_xattr(struct cachefiles_cache *cache,
249 struct dentry *dentry);
250
251
252/*
253 * error handling
254 */
David Howells9ae326a2009-04-03 16:42:41 +0100255
256#define cachefiles_io_error(___cache, FMT, ...) \
257do { \
Fabian Frederick6ff66ac2014-09-25 16:05:27 -0700258 pr_err("I/O Error: " FMT"\n", ##__VA_ARGS__); \
David Howells9ae326a2009-04-03 16:42:41 +0100259 fscache_io_error(&(___cache)->cache); \
260 set_bit(CACHEFILES_DEAD, &(___cache)->flags); \
261} while (0)
262
263#define cachefiles_io_error_obj(object, FMT, ...) \
264do { \
265 struct cachefiles_cache *___cache; \
266 \
267 ___cache = container_of((object)->fscache.cache, \
268 struct cachefiles_cache, cache); \
269 cachefiles_io_error(___cache, FMT, ##__VA_ARGS__); \
270} while (0)
271
272
273/*
274 * debug tracing
275 */
276#define dbgprintk(FMT, ...) \
277 printk(KERN_DEBUG "[%-6.6s] "FMT"\n", current->comm, ##__VA_ARGS__)
278
David Howells9ae326a2009-04-03 16:42:41 +0100279#define kenter(FMT, ...) dbgprintk("==> %s("FMT")", __func__, ##__VA_ARGS__)
280#define kleave(FMT, ...) dbgprintk("<== %s()"FMT"", __func__, ##__VA_ARGS__)
281#define kdebug(FMT, ...) dbgprintk(FMT, ##__VA_ARGS__)
282
283
284#if defined(__KDEBUG)
285#define _enter(FMT, ...) kenter(FMT, ##__VA_ARGS__)
286#define _leave(FMT, ...) kleave(FMT, ##__VA_ARGS__)
287#define _debug(FMT, ...) kdebug(FMT, ##__VA_ARGS__)
288
289#elif defined(CONFIG_CACHEFILES_DEBUG)
290#define _enter(FMT, ...) \
291do { \
292 if (cachefiles_debug & CACHEFILES_DEBUG_KENTER) \
293 kenter(FMT, ##__VA_ARGS__); \
294} while (0)
295
296#define _leave(FMT, ...) \
297do { \
298 if (cachefiles_debug & CACHEFILES_DEBUG_KLEAVE) \
299 kleave(FMT, ##__VA_ARGS__); \
300} while (0)
301
302#define _debug(FMT, ...) \
303do { \
304 if (cachefiles_debug & CACHEFILES_DEBUG_KDEBUG) \
305 kdebug(FMT, ##__VA_ARGS__); \
306} while (0)
307
308#else
David Howells12fdff32010-08-12 16:54:57 +0100309#define _enter(FMT, ...) no_printk("==> %s("FMT")", __func__, ##__VA_ARGS__)
310#define _leave(FMT, ...) no_printk("<== %s()"FMT"", __func__, ##__VA_ARGS__)
311#define _debug(FMT, ...) no_printk(FMT, ##__VA_ARGS__)
David Howells9ae326a2009-04-03 16:42:41 +0100312#endif
313
314#if 1 /* defined(__KDEBUGALL) */
315
316#define ASSERT(X) \
317do { \
318 if (unlikely(!(X))) { \
Fabian Frederick4e1eb882014-06-06 14:37:32 -0700319 pr_err("\n"); \
Fabian Frederick0227d6ab2014-06-06 14:37:33 -0700320 pr_err("Assertion failed\n"); \
David Howells9ae326a2009-04-03 16:42:41 +0100321 BUG(); \
322 } \
323} while (0)
324
325#define ASSERTCMP(X, OP, Y) \
326do { \
327 if (unlikely(!((X) OP (Y)))) { \
Fabian Frederick4e1eb882014-06-06 14:37:32 -0700328 pr_err("\n"); \
Fabian Frederick0227d6ab2014-06-06 14:37:33 -0700329 pr_err("Assertion failed\n"); \
Fabian Frederick4e1eb882014-06-06 14:37:32 -0700330 pr_err("%lx " #OP " %lx is false\n", \
David Howells9ae326a2009-04-03 16:42:41 +0100331 (unsigned long)(X), (unsigned long)(Y)); \
332 BUG(); \
333 } \
334} while (0)
335
336#define ASSERTIF(C, X) \
337do { \
338 if (unlikely((C) && !(X))) { \
Fabian Frederick4e1eb882014-06-06 14:37:32 -0700339 pr_err("\n"); \
Fabian Frederick0227d6ab2014-06-06 14:37:33 -0700340 pr_err("Assertion failed\n"); \
David Howells9ae326a2009-04-03 16:42:41 +0100341 BUG(); \
342 } \
343} while (0)
344
345#define ASSERTIFCMP(C, X, OP, Y) \
346do { \
347 if (unlikely((C) && !((X) OP (Y)))) { \
Fabian Frederick4e1eb882014-06-06 14:37:32 -0700348 pr_err("\n"); \
Fabian Frederick0227d6ab2014-06-06 14:37:33 -0700349 pr_err("Assertion failed\n"); \
Fabian Frederick4e1eb882014-06-06 14:37:32 -0700350 pr_err("%lx " #OP " %lx is false\n", \
David Howells9ae326a2009-04-03 16:42:41 +0100351 (unsigned long)(X), (unsigned long)(Y)); \
352 BUG(); \
353 } \
354} while (0)
355
356#else
357
358#define ASSERT(X) do {} while (0)
359#define ASSERTCMP(X, OP, Y) do {} while (0)
360#define ASSERTIF(C, X) do {} while (0)
361#define ASSERTIFCMP(C, X, OP, Y) do {} while (0)
362
363#endif