blob: bb3a02ca9da4a3fc15984385323b3bb3c8e95b26 [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>
Ingo Molnar5dd43ce2017-06-20 12:19:09 +020021#include <linux/wait_bit.h>
Ingo Molnar5b825c32017-02-02 17:54:15 +010022#include <linux/cred.h>
David Howells9ae326a2009-04-03 16:42:41 +010023#include <linux/workqueue.h>
24#include <linux/security.h>
25
26struct cachefiles_cache;
27struct cachefiles_object;
28
29extern unsigned cachefiles_debug;
30#define CACHEFILES_DEBUG_KENTER 1
31#define CACHEFILES_DEBUG_KLEAVE 2
32#define CACHEFILES_DEBUG_KDEBUG 4
33
Mel Gorman71baba42015-11-06 16:28:28 -080034#define cachefiles_gfp (__GFP_RECLAIM | __GFP_NORETRY | __GFP_NOMEMALLOC)
David Howells5f4f9f42012-12-20 21:52:33 +000035
David Howells9ae326a2009-04-03 16:42:41 +010036/*
37 * node records
38 */
39struct cachefiles_object {
40 struct fscache_object fscache; /* fscache handle */
41 struct cachefiles_lookup_data *lookup_data; /* cached lookup data */
42 struct dentry *dentry; /* the file/dir representing this object */
43 struct dentry *backer; /* backing file */
44 loff_t i_size; /* object size */
45 unsigned long flags;
46#define CACHEFILES_OBJECT_ACTIVE 0 /* T if marked active */
47 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 */
David Howellsa5b3a802016-02-01 16:43:04 +000070 atomic_t f_released; /* number of objects released lately */
71 atomic_long_t b_released; /* number of blocks released lately */
David Howells9ae326a2009-04-03 16:42:41 +010072 unsigned frun_percent; /* when to stop culling (% files) */
73 unsigned fcull_percent; /* when to start culling (% files) */
74 unsigned fstop_percent; /* when to stop allocating (% files) */
75 unsigned brun_percent; /* when to stop culling (% blocks) */
76 unsigned bcull_percent; /* when to start culling (% blocks) */
77 unsigned bstop_percent; /* when to stop allocating (% blocks) */
78 unsigned bsize; /* cache's block size */
79 unsigned bshift; /* min(ilog2(PAGE_SIZE / bsize), 0) */
80 uint64_t frun; /* when to stop culling */
81 uint64_t fcull; /* when to start culling */
82 uint64_t fstop; /* when to stop allocating */
83 sector_t brun; /* when to stop culling */
84 sector_t bcull; /* when to start culling */
85 sector_t bstop; /* when to stop allocating */
86 unsigned long flags;
87#define CACHEFILES_READY 0 /* T if cache prepared */
88#define CACHEFILES_DEAD 1 /* T if cache dead */
89#define CACHEFILES_CULLING 2 /* T if cull engaged */
90#define CACHEFILES_STATE_CHANGED 3 /* T if state changed (poll trigger) */
91 char *rootdirname; /* name of cache root directory */
92 char *secctx; /* LSM security context */
93 char *tag; /* cache binding tag */
94};
95
96/*
97 * backing file read tracking
98 */
99struct cachefiles_one_read {
Ingo Molnarac6424b2017-06-20 12:06:13 +0200100 wait_queue_entry_t monitor; /* link into monitored waitqueue */
David Howells9ae326a2009-04-03 16:42:41 +0100101 struct page *back_page; /* backing file page we're waiting for */
102 struct page *netfs_page; /* netfs page we're going to fill */
103 struct fscache_retrieval *op; /* retrieval op covering this */
104 struct list_head op_link; /* link in op's todo list */
105};
106
107/*
108 * backing file write tracking
109 */
110struct cachefiles_one_write {
111 struct page *netfs_page; /* netfs page to copy */
112 struct cachefiles_object *object;
113 struct list_head obj_link; /* link in object's lists */
114 fscache_rw_complete_t end_io_func;
115 void *context;
116};
117
118/*
119 * auxiliary data xattr buffer
120 */
121struct cachefiles_xattr {
122 uint16_t len;
123 uint8_t type;
124 uint8_t data[];
125};
126
127/*
128 * note change of state for daemon
129 */
130static inline void cachefiles_state_changed(struct cachefiles_cache *cache)
131{
132 set_bit(CACHEFILES_STATE_CHANGED, &cache->flags);
133 wake_up_all(&cache->daemon_pollwq);
134}
135
136/*
David Howells911e6902009-05-27 15:46:55 +0100137 * bind.c
David Howells9ae326a2009-04-03 16:42:41 +0100138 */
139extern int cachefiles_daemon_bind(struct cachefiles_cache *cache, char *args);
140extern void cachefiles_daemon_unbind(struct cachefiles_cache *cache);
141
142/*
David Howells911e6902009-05-27 15:46:55 +0100143 * daemon.c
David Howells9ae326a2009-04-03 16:42:41 +0100144 */
145extern const struct file_operations cachefiles_daemon_fops;
146
147extern int cachefiles_has_space(struct cachefiles_cache *cache,
148 unsigned fnr, unsigned bnr);
149
150/*
David Howells911e6902009-05-27 15:46:55 +0100151 * interface.c
David Howells9ae326a2009-04-03 16:42:41 +0100152 */
153extern const struct fscache_cache_ops cachefiles_cache_ops;
154
155/*
David Howells911e6902009-05-27 15:46:55 +0100156 * key.c
David Howells9ae326a2009-04-03 16:42:41 +0100157 */
158extern char *cachefiles_cook_key(const u8 *raw, int keylen, uint8_t type);
159
160/*
David Howells911e6902009-05-27 15:46:55 +0100161 * namei.c
David Howells9ae326a2009-04-03 16:42:41 +0100162 */
David Howellsa5b3a802016-02-01 16:43:04 +0000163extern void cachefiles_mark_object_inactive(struct cachefiles_cache *cache,
David Howellsa8181012016-08-09 17:41:16 +0100164 struct cachefiles_object *object,
165 blkcnt_t i_blocks);
David Howells9ae326a2009-04-03 16:42:41 +0100166extern int cachefiles_delete_object(struct cachefiles_cache *cache,
167 struct cachefiles_object *object);
168extern int cachefiles_walk_to_object(struct cachefiles_object *parent,
169 struct cachefiles_object *object,
170 const char *key,
171 struct cachefiles_xattr *auxdata);
172extern struct dentry *cachefiles_get_directory(struct cachefiles_cache *cache,
173 struct dentry *dir,
174 const char *name);
175
176extern int cachefiles_cull(struct cachefiles_cache *cache, struct dentry *dir,
177 char *filename);
178
179extern int cachefiles_check_in_use(struct cachefiles_cache *cache,
180 struct dentry *dir, char *filename);
181
182/*
David Howells911e6902009-05-27 15:46:55 +0100183 * proc.c
David Howells9ae326a2009-04-03 16:42:41 +0100184 */
185#ifdef CONFIG_CACHEFILES_HISTOGRAM
186extern atomic_t cachefiles_lookup_histogram[HZ];
187extern atomic_t cachefiles_mkdir_histogram[HZ];
188extern atomic_t cachefiles_create_histogram[HZ];
189
190extern int __init cachefiles_proc_init(void);
191extern void cachefiles_proc_cleanup(void);
192static inline
193void cachefiles_hist(atomic_t histogram[], unsigned long start_jif)
194{
195 unsigned long jif = jiffies - start_jif;
196 if (jif >= HZ)
197 jif = HZ - 1;
198 atomic_inc(&histogram[jif]);
199}
200
201#else
202#define cachefiles_proc_init() (0)
203#define cachefiles_proc_cleanup() do {} while (0)
204#define cachefiles_hist(hist, start_jif) do {} while (0)
205#endif
206
207/*
David Howells911e6902009-05-27 15:46:55 +0100208 * rdwr.c
David Howells9ae326a2009-04-03 16:42:41 +0100209 */
210extern int cachefiles_read_or_alloc_page(struct fscache_retrieval *,
211 struct page *, gfp_t);
212extern int cachefiles_read_or_alloc_pages(struct fscache_retrieval *,
213 struct list_head *, unsigned *,
214 gfp_t);
215extern int cachefiles_allocate_page(struct fscache_retrieval *, struct page *,
216 gfp_t);
217extern int cachefiles_allocate_pages(struct fscache_retrieval *,
218 struct list_head *, unsigned *, gfp_t);
219extern int cachefiles_write_page(struct fscache_storage *, struct page *);
220extern void cachefiles_uncache_page(struct fscache_object *, struct page *);
221
222/*
David Howells911e6902009-05-27 15:46:55 +0100223 * security.c
David Howells9ae326a2009-04-03 16:42:41 +0100224 */
225extern int cachefiles_get_security_ID(struct cachefiles_cache *cache);
226extern int cachefiles_determine_cache_security(struct cachefiles_cache *cache,
227 struct dentry *root,
228 const struct cred **_saved_cred);
229
230static inline void cachefiles_begin_secure(struct cachefiles_cache *cache,
231 const struct cred **_saved_cred)
232{
233 *_saved_cred = override_creds(cache->cache_cred);
234}
235
236static inline void cachefiles_end_secure(struct cachefiles_cache *cache,
237 const struct cred *saved_cred)
238{
239 revert_creds(saved_cred);
240}
241
242/*
David Howells911e6902009-05-27 15:46:55 +0100243 * xattr.c
David Howells9ae326a2009-04-03 16:42:41 +0100244 */
245extern int cachefiles_check_object_type(struct cachefiles_object *object);
246extern int cachefiles_set_object_xattr(struct cachefiles_object *object,
247 struct cachefiles_xattr *auxdata);
248extern int cachefiles_update_object_xattr(struct cachefiles_object *object,
249 struct cachefiles_xattr *auxdata);
David Howells5002d7b2013-08-21 17:29:21 -0400250extern int cachefiles_check_auxdata(struct cachefiles_object *object);
David Howells9ae326a2009-04-03 16:42:41 +0100251extern int cachefiles_check_object_xattr(struct cachefiles_object *object,
252 struct cachefiles_xattr *auxdata);
253extern int cachefiles_remove_object_xattr(struct cachefiles_cache *cache,
254 struct dentry *dentry);
255
256
257/*
258 * error handling
259 */
David Howells9ae326a2009-04-03 16:42:41 +0100260
261#define cachefiles_io_error(___cache, FMT, ...) \
262do { \
Fabian Frederick6ff66ac2014-09-25 16:05:27 -0700263 pr_err("I/O Error: " FMT"\n", ##__VA_ARGS__); \
David Howells9ae326a2009-04-03 16:42:41 +0100264 fscache_io_error(&(___cache)->cache); \
265 set_bit(CACHEFILES_DEAD, &(___cache)->flags); \
266} while (0)
267
268#define cachefiles_io_error_obj(object, FMT, ...) \
269do { \
270 struct cachefiles_cache *___cache; \
271 \
272 ___cache = container_of((object)->fscache.cache, \
273 struct cachefiles_cache, cache); \
274 cachefiles_io_error(___cache, FMT, ##__VA_ARGS__); \
275} while (0)
276
277
278/*
279 * debug tracing
280 */
281#define dbgprintk(FMT, ...) \
282 printk(KERN_DEBUG "[%-6.6s] "FMT"\n", current->comm, ##__VA_ARGS__)
283
David Howells9ae326a2009-04-03 16:42:41 +0100284#define kenter(FMT, ...) dbgprintk("==> %s("FMT")", __func__, ##__VA_ARGS__)
285#define kleave(FMT, ...) dbgprintk("<== %s()"FMT"", __func__, ##__VA_ARGS__)
286#define kdebug(FMT, ...) dbgprintk(FMT, ##__VA_ARGS__)
287
288
289#if defined(__KDEBUG)
290#define _enter(FMT, ...) kenter(FMT, ##__VA_ARGS__)
291#define _leave(FMT, ...) kleave(FMT, ##__VA_ARGS__)
292#define _debug(FMT, ...) kdebug(FMT, ##__VA_ARGS__)
293
294#elif defined(CONFIG_CACHEFILES_DEBUG)
295#define _enter(FMT, ...) \
296do { \
297 if (cachefiles_debug & CACHEFILES_DEBUG_KENTER) \
298 kenter(FMT, ##__VA_ARGS__); \
299} while (0)
300
301#define _leave(FMT, ...) \
302do { \
303 if (cachefiles_debug & CACHEFILES_DEBUG_KLEAVE) \
304 kleave(FMT, ##__VA_ARGS__); \
305} while (0)
306
307#define _debug(FMT, ...) \
308do { \
309 if (cachefiles_debug & CACHEFILES_DEBUG_KDEBUG) \
310 kdebug(FMT, ##__VA_ARGS__); \
311} while (0)
312
313#else
David Howells12fdff32010-08-12 16:54:57 +0100314#define _enter(FMT, ...) no_printk("==> %s("FMT")", __func__, ##__VA_ARGS__)
315#define _leave(FMT, ...) no_printk("<== %s()"FMT"", __func__, ##__VA_ARGS__)
316#define _debug(FMT, ...) no_printk(FMT, ##__VA_ARGS__)
David Howells9ae326a2009-04-03 16:42:41 +0100317#endif
318
319#if 1 /* defined(__KDEBUGALL) */
320
321#define ASSERT(X) \
322do { \
323 if (unlikely(!(X))) { \
Fabian Frederick4e1eb882014-06-06 14:37:32 -0700324 pr_err("\n"); \
Fabian Frederick0227d6ab2014-06-06 14:37:33 -0700325 pr_err("Assertion failed\n"); \
David Howells9ae326a2009-04-03 16:42:41 +0100326 BUG(); \
327 } \
328} while (0)
329
330#define ASSERTCMP(X, OP, Y) \
331do { \
332 if (unlikely(!((X) OP (Y)))) { \
Fabian Frederick4e1eb882014-06-06 14:37:32 -0700333 pr_err("\n"); \
Fabian Frederick0227d6ab2014-06-06 14:37:33 -0700334 pr_err("Assertion failed\n"); \
Fabian Frederick4e1eb882014-06-06 14:37:32 -0700335 pr_err("%lx " #OP " %lx is false\n", \
David Howells9ae326a2009-04-03 16:42:41 +0100336 (unsigned long)(X), (unsigned long)(Y)); \
337 BUG(); \
338 } \
339} while (0)
340
341#define ASSERTIF(C, X) \
342do { \
343 if (unlikely((C) && !(X))) { \
Fabian Frederick4e1eb882014-06-06 14:37:32 -0700344 pr_err("\n"); \
Fabian Frederick0227d6ab2014-06-06 14:37:33 -0700345 pr_err("Assertion failed\n"); \
David Howells9ae326a2009-04-03 16:42:41 +0100346 BUG(); \
347 } \
348} while (0)
349
350#define ASSERTIFCMP(C, X, OP, Y) \
351do { \
352 if (unlikely((C) && !((X) OP (Y)))) { \
Fabian Frederick4e1eb882014-06-06 14:37:32 -0700353 pr_err("\n"); \
Fabian Frederick0227d6ab2014-06-06 14:37:33 -0700354 pr_err("Assertion failed\n"); \
Fabian Frederick4e1eb882014-06-06 14:37:32 -0700355 pr_err("%lx " #OP " %lx is false\n", \
David Howells9ae326a2009-04-03 16:42:41 +0100356 (unsigned long)(X), (unsigned long)(Y)); \
357 BUG(); \
358 } \
359} while (0)
360
361#else
362
363#define ASSERT(X) do {} while (0)
364#define ASSERTCMP(X, OP, Y) do {} while (0)
365#define ASSERTIF(C, X) do {} while (0)
366#define ASSERTIFCMP(C, X, OP, Y) do {} while (0)
367
368#endif