1 /* General netfs cache on cache files internal defs
3 * Copyright (C) 2007 Red Hat, Inc. All Rights Reserved.
4 * Written by David Howells (dhowells@redhat.com)
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.
16 #define pr_fmt(fmt) "CacheFiles: " fmt
19 #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>
25 struct cachefiles_cache
;
26 struct cachefiles_object
;
28 extern unsigned cachefiles_debug
;
29 #define CACHEFILES_DEBUG_KENTER 1
30 #define CACHEFILES_DEBUG_KLEAVE 2
31 #define CACHEFILES_DEBUG_KDEBUG 4
33 #define cachefiles_gfp (__GFP_RECLAIM | __GFP_NORETRY | __GFP_NOMEMALLOC)
38 struct 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 */
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 */
50 struct rb_node active_node
; /* link in active tree (dentry is key) */
53 extern struct kmem_cache
*cachefiles_object_jar
;
56 * Cache files cache definition
58 struct 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 */
69 atomic_t f_released
; /* number of objects released lately */
70 atomic_long_t b_released
; /* number of blocks released lately */
71 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 */
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 */
96 * backing file read tracking
98 struct 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 */
107 * backing file write tracking
109 struct 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
;
118 * auxiliary data xattr buffer
120 struct cachefiles_xattr
{
127 * note change of state for daemon
129 static inline void cachefiles_state_changed(struct cachefiles_cache
*cache
)
131 set_bit(CACHEFILES_STATE_CHANGED
, &cache
->flags
);
132 wake_up_all(&cache
->daemon_pollwq
);
138 extern int cachefiles_daemon_bind(struct cachefiles_cache
*cache
, char *args
);
139 extern void cachefiles_daemon_unbind(struct cachefiles_cache
*cache
);
144 extern const struct file_operations cachefiles_daemon_fops
;
146 extern int cachefiles_has_space(struct cachefiles_cache
*cache
,
147 unsigned fnr
, unsigned bnr
);
152 extern const struct fscache_cache_ops cachefiles_cache_ops
;
157 extern char *cachefiles_cook_key(const u8
*raw
, int keylen
, uint8_t type
);
162 extern void cachefiles_mark_object_inactive(struct cachefiles_cache
*cache
,
163 struct cachefiles_object
*object
,
165 extern int cachefiles_delete_object(struct cachefiles_cache
*cache
,
166 struct cachefiles_object
*object
);
167 extern int cachefiles_walk_to_object(struct cachefiles_object
*parent
,
168 struct cachefiles_object
*object
,
170 struct cachefiles_xattr
*auxdata
);
171 extern struct dentry
*cachefiles_get_directory(struct cachefiles_cache
*cache
,
175 extern int cachefiles_cull(struct cachefiles_cache
*cache
, struct dentry
*dir
,
178 extern int cachefiles_check_in_use(struct cachefiles_cache
*cache
,
179 struct dentry
*dir
, char *filename
);
184 #ifdef CONFIG_CACHEFILES_HISTOGRAM
185 extern atomic_t cachefiles_lookup_histogram
[HZ
];
186 extern atomic_t cachefiles_mkdir_histogram
[HZ
];
187 extern atomic_t cachefiles_create_histogram
[HZ
];
189 extern int __init
cachefiles_proc_init(void);
190 extern void cachefiles_proc_cleanup(void);
192 void cachefiles_hist(atomic_t histogram
[], unsigned long start_jif
)
194 unsigned long jif
= jiffies
- start_jif
;
197 atomic_inc(&histogram
[jif
]);
201 #define cachefiles_proc_init() (0)
202 #define cachefiles_proc_cleanup() do {} while (0)
203 #define cachefiles_hist(hist, start_jif) do {} while (0)
209 extern int cachefiles_read_or_alloc_page(struct fscache_retrieval
*,
210 struct page
*, gfp_t
);
211 extern int cachefiles_read_or_alloc_pages(struct fscache_retrieval
*,
212 struct list_head
*, unsigned *,
214 extern int cachefiles_allocate_page(struct fscache_retrieval
*, struct page
*,
216 extern int cachefiles_allocate_pages(struct fscache_retrieval
*,
217 struct list_head
*, unsigned *, gfp_t
);
218 extern int cachefiles_write_page(struct fscache_storage
*, struct page
*);
219 extern void cachefiles_uncache_page(struct fscache_object
*, struct page
*);
224 extern int cachefiles_get_security_ID(struct cachefiles_cache
*cache
);
225 extern int cachefiles_determine_cache_security(struct cachefiles_cache
*cache
,
227 const struct cred
**_saved_cred
);
229 static inline void cachefiles_begin_secure(struct cachefiles_cache
*cache
,
230 const struct cred
**_saved_cred
)
232 *_saved_cred
= override_creds(cache
->cache_cred
);
235 static inline void cachefiles_end_secure(struct cachefiles_cache
*cache
,
236 const struct cred
*saved_cred
)
238 revert_creds(saved_cred
);
244 extern int cachefiles_check_object_type(struct cachefiles_object
*object
);
245 extern int cachefiles_set_object_xattr(struct cachefiles_object
*object
,
246 struct cachefiles_xattr
*auxdata
);
247 extern int cachefiles_update_object_xattr(struct cachefiles_object
*object
,
248 struct cachefiles_xattr
*auxdata
);
249 extern int cachefiles_check_auxdata(struct cachefiles_object
*object
);
250 extern int cachefiles_check_object_xattr(struct cachefiles_object
*object
,
251 struct cachefiles_xattr
*auxdata
);
252 extern int cachefiles_remove_object_xattr(struct cachefiles_cache
*cache
,
253 struct dentry
*dentry
);
260 #define cachefiles_io_error(___cache, FMT, ...) \
262 pr_err("I/O Error: " FMT"\n", ##__VA_ARGS__); \
263 fscache_io_error(&(___cache)->cache); \
264 set_bit(CACHEFILES_DEAD, &(___cache)->flags); \
267 #define cachefiles_io_error_obj(object, FMT, ...) \
269 struct cachefiles_cache *___cache; \
271 ___cache = container_of((object)->fscache.cache, \
272 struct cachefiles_cache, cache); \
273 cachefiles_io_error(___cache, FMT, ##__VA_ARGS__); \
280 #define dbgprintk(FMT, ...) \
281 printk(KERN_DEBUG "[%-6.6s] "FMT"\n", current->comm, ##__VA_ARGS__)
283 #define kenter(FMT, ...) dbgprintk("==> %s("FMT")", __func__, ##__VA_ARGS__)
284 #define kleave(FMT, ...) dbgprintk("<== %s()"FMT"", __func__, ##__VA_ARGS__)
285 #define kdebug(FMT, ...) dbgprintk(FMT, ##__VA_ARGS__)
288 #if defined(__KDEBUG)
289 #define _enter(FMT, ...) kenter(FMT, ##__VA_ARGS__)
290 #define _leave(FMT, ...) kleave(FMT, ##__VA_ARGS__)
291 #define _debug(FMT, ...) kdebug(FMT, ##__VA_ARGS__)
293 #elif defined(CONFIG_CACHEFILES_DEBUG)
294 #define _enter(FMT, ...) \
296 if (cachefiles_debug & CACHEFILES_DEBUG_KENTER) \
297 kenter(FMT, ##__VA_ARGS__); \
300 #define _leave(FMT, ...) \
302 if (cachefiles_debug & CACHEFILES_DEBUG_KLEAVE) \
303 kleave(FMT, ##__VA_ARGS__); \
306 #define _debug(FMT, ...) \
308 if (cachefiles_debug & CACHEFILES_DEBUG_KDEBUG) \
309 kdebug(FMT, ##__VA_ARGS__); \
313 #define _enter(FMT, ...) no_printk("==> %s("FMT")", __func__, ##__VA_ARGS__)
314 #define _leave(FMT, ...) no_printk("<== %s()"FMT"", __func__, ##__VA_ARGS__)
315 #define _debug(FMT, ...) no_printk(FMT, ##__VA_ARGS__)
318 #if 1 /* defined(__KDEBUGALL) */
322 if (unlikely(!(X))) { \
324 pr_err("Assertion failed\n"); \
329 #define ASSERTCMP(X, OP, Y) \
331 if (unlikely(!((X) OP (Y)))) { \
333 pr_err("Assertion failed\n"); \
334 pr_err("%lx " #OP " %lx is false\n", \
335 (unsigned long)(X), (unsigned long)(Y)); \
340 #define ASSERTIF(C, X) \
342 if (unlikely((C) && !(X))) { \
344 pr_err("Assertion failed\n"); \
349 #define ASSERTIFCMP(C, X, OP, Y) \
351 if (unlikely((C) && !((X) OP (Y)))) { \
353 pr_err("Assertion failed\n"); \
354 pr_err("%lx " #OP " %lx is false\n", \
355 (unsigned long)(X), (unsigned long)(Y)); \
362 #define ASSERT(X) do {} while (0)
363 #define ASSERTCMP(X, OP, Y) do {} while (0)
364 #define ASSERTIF(C, X) do {} while (0)
365 #define ASSERTIFCMP(C, X, OP, Y) do {} while (0)