Linux Audio

Check our new training course

Loading...
v6.8
  1/* SPDX-License-Identifier: GPL-2.0-or-later */
  2/* General netfs cache on cache files internal defs
  3 *
  4 * Copyright (C) 2021 Red Hat, Inc. All Rights Reserved.
  5 * Written by David Howells (dhowells@redhat.com)
 
 
 
 
 
  6 */
  7
  8#ifdef pr_fmt
  9#undef pr_fmt
 10#endif
 11
 12#define pr_fmt(fmt) "CacheFiles: " fmt
 13
 14
 15#include <linux/fscache-cache.h>
 16#include <linux/cred.h>
 
 
 17#include <linux/security.h>
 18#include <linux/xarray.h>
 19#include <linux/cachefiles.h>
 20
 21#define CACHEFILES_DIO_BLOCK_SIZE 4096
 22
 23struct cachefiles_cache;
 24struct cachefiles_object;
 25
 26enum cachefiles_content {
 27	/* These values are saved on disk */
 28	CACHEFILES_CONTENT_NO_DATA	= 0, /* No content stored */
 29	CACHEFILES_CONTENT_SINGLE	= 1, /* Content is monolithic, all is present */
 30	CACHEFILES_CONTENT_ALL		= 2, /* Content is all present, no map */
 31	CACHEFILES_CONTENT_BACKFS_MAP	= 3, /* Content is piecemeal, mapped through backing fs */
 32	CACHEFILES_CONTENT_DIRTY	= 4, /* Content is dirty (only seen on disk) */
 33	nr__cachefiles_content
 34};
 35
 36/*
 37 * Cached volume representation.
 38 */
 39struct cachefiles_volume {
 40	struct cachefiles_cache		*cache;
 41	struct list_head		cache_link;	/* Link in cache->volumes */
 42	struct fscache_volume		*vcookie;	/* The netfs's representation */
 43	struct dentry			*dentry;	/* The volume dentry */
 44	struct dentry			*fanout[256];	/* Fanout subdirs */
 45};
 46
 47enum cachefiles_object_state {
 48	CACHEFILES_ONDEMAND_OBJSTATE_CLOSE, /* Anonymous fd closed by daemon or initial state */
 49	CACHEFILES_ONDEMAND_OBJSTATE_OPEN, /* Anonymous fd associated with object is available */
 50	CACHEFILES_ONDEMAND_OBJSTATE_REOPENING, /* Object that was closed and is being reopened. */
 51};
 52
 53struct cachefiles_ondemand_info {
 54	struct work_struct		ondemand_work;
 55	int				ondemand_id;
 56	enum cachefiles_object_state	state;
 57	struct cachefiles_object	*object;
 58};
 59
 60/*
 61 * Backing file state.
 62 */
 63struct cachefiles_object {
 64	struct fscache_cookie		*cookie;	/* Netfs data storage object cookie */
 65	struct cachefiles_volume	*volume;	/* Cache volume that holds this object */
 66	struct list_head		cache_link;	/* Link in cache->*_list */
 67	struct file			*file;		/* The file representing this object */
 68	char				*d_name;	/* Backing file name */
 69	int				debug_id;
 70	spinlock_t			lock;
 71	refcount_t			ref;
 72	u8				d_name_len;	/* Length of filename */
 73	enum cachefiles_content		content_info:8;	/* Info about content presence */
 74	unsigned long			flags;
 75#define CACHEFILES_OBJECT_USING_TMPFILE	0		/* Have an unlinked tmpfile */
 76#ifdef CONFIG_CACHEFILES_ONDEMAND
 77	struct cachefiles_ondemand_info	*ondemand;
 78#endif
 
 
 
 79};
 80
 81#define CACHEFILES_ONDEMAND_ID_CLOSED	-1
 82
 83/*
 84 * Cache files cache definition
 85 */
 86struct cachefiles_cache {
 87	struct fscache_cache		*cache;		/* Cache cookie */
 88	struct vfsmount			*mnt;		/* mountpoint holding the cache */
 89	struct dentry			*store;		/* Directory into which live objects go */
 90	struct dentry			*graveyard;	/* directory into which dead objects go */
 91	struct file			*cachefilesd;	/* manager daemon handle */
 92	struct list_head		volumes;	/* List of volume objects */
 93	struct list_head		object_list;	/* List of active objects */
 94	spinlock_t			object_list_lock; /* Lock for volumes and object_list */
 95	const struct cred		*cache_cred;	/* security override for accessing cache */
 96	struct mutex			daemon_mutex;	/* command serialisation mutex */
 97	wait_queue_head_t		daemon_pollwq;	/* poll waitqueue for daemon */
 
 
 98	atomic_t			gravecounter;	/* graveyard uniquifier */
 99	atomic_t			f_released;	/* number of objects released lately */
100	atomic_long_t			b_released;	/* number of blocks released lately */
101	atomic_long_t			b_writing;	/* Number of blocks being written */
102	unsigned			frun_percent;	/* when to stop culling (% files) */
103	unsigned			fcull_percent;	/* when to start culling (% files) */
104	unsigned			fstop_percent;	/* when to stop allocating (% files) */
105	unsigned			brun_percent;	/* when to stop culling (% blocks) */
106	unsigned			bcull_percent;	/* when to start culling (% blocks) */
107	unsigned			bstop_percent;	/* when to stop allocating (% blocks) */
108	unsigned			bsize;		/* cache's block size */
109	unsigned			bshift;		/* ilog2(bsize) */
110	uint64_t			frun;		/* when to stop culling */
111	uint64_t			fcull;		/* when to start culling */
112	uint64_t			fstop;		/* when to stop allocating */
113	sector_t			brun;		/* when to stop culling */
114	sector_t			bcull;		/* when to start culling */
115	sector_t			bstop;		/* when to stop allocating */
116	unsigned long			flags;
117#define CACHEFILES_READY		0	/* T if cache prepared */
118#define CACHEFILES_DEAD			1	/* T if cache dead */
119#define CACHEFILES_CULLING		2	/* T if cull engaged */
120#define CACHEFILES_STATE_CHANGED	3	/* T if state changed (poll trigger) */
121#define CACHEFILES_ONDEMAND_MODE	4	/* T if in on-demand read mode */
122	char				*rootdirname;	/* name of cache root directory */
123	char				*secctx;	/* LSM security context */
124	char				*tag;		/* cache binding tag */
125	refcount_t			unbind_pincount;/* refcount to do daemon unbind */
126	struct xarray			reqs;		/* xarray of pending on-demand requests */
127	unsigned long			req_id_next;
128	struct xarray			ondemand_ids;	/* xarray for ondemand_id allocation */
129	u32				ondemand_id_next;
130};
131
132static inline bool cachefiles_in_ondemand_mode(struct cachefiles_cache *cache)
133{
134	return IS_ENABLED(CONFIG_CACHEFILES_ONDEMAND) &&
135		test_bit(CACHEFILES_ONDEMAND_MODE, &cache->flags);
136}
137
138struct cachefiles_req {
139	struct cachefiles_object *object;
140	struct completion done;
141	int error;
142	struct cachefiles_msg msg;
143};
144
145#define CACHEFILES_REQ_NEW	XA_MARK_1
146
147#include <trace/events/cachefiles.h>
148
149static inline
150struct file *cachefiles_cres_file(struct netfs_cache_resources *cres)
151{
152	return cres->cache_priv2;
153}
 
154
155static inline
156struct cachefiles_object *cachefiles_cres_object(struct netfs_cache_resources *cres)
157{
158	return fscache_cres_cookie(cres)->cache_priv;
159}
 
 
 
160
161/*
162 * note change of state for daemon
163 */
164static inline void cachefiles_state_changed(struct cachefiles_cache *cache)
165{
166	set_bit(CACHEFILES_STATE_CHANGED, &cache->flags);
167	wake_up_all(&cache->daemon_pollwq);
168}
169
170/*
171 * cache.c
172 */
173extern int cachefiles_add_cache(struct cachefiles_cache *cache);
174extern void cachefiles_withdraw_cache(struct cachefiles_cache *cache);
175
176enum cachefiles_has_space_for {
177	cachefiles_has_space_check,
178	cachefiles_has_space_for_write,
179	cachefiles_has_space_for_create,
180};
181extern int cachefiles_has_space(struct cachefiles_cache *cache,
182				unsigned fnr, unsigned bnr,
183				enum cachefiles_has_space_for reason);
184
185/*
186 * daemon.c
187 */
188extern const struct file_operations cachefiles_daemon_fops;
189extern void cachefiles_get_unbind_pincount(struct cachefiles_cache *cache);
190extern void cachefiles_put_unbind_pincount(struct cachefiles_cache *cache);
191
192/*
193 * error_inject.c
194 */
195#ifdef CONFIG_CACHEFILES_ERROR_INJECTION
196extern unsigned int cachefiles_error_injection_state;
197extern int cachefiles_register_error_injection(void);
198extern void cachefiles_unregister_error_injection(void);
199
200#else
201#define cachefiles_error_injection_state 0
202
203static inline int cachefiles_register_error_injection(void)
204{
205	return 0;
206}
207
208static inline void cachefiles_unregister_error_injection(void)
209{
210}
211#endif
212
213
214static inline int cachefiles_inject_read_error(void)
215{
216	return cachefiles_error_injection_state & 2 ? -EIO : 0;
217}
218
219static inline int cachefiles_inject_write_error(void)
220{
221	return cachefiles_error_injection_state & 2 ? -EIO :
222		cachefiles_error_injection_state & 1 ? -ENOSPC :
223		0;
224}
225
226static inline int cachefiles_inject_remove_error(void)
227{
228	return cachefiles_error_injection_state & 2 ? -EIO : 0;
229}
230
231/*
232 * interface.c
233 */
234extern const struct fscache_cache_ops cachefiles_cache_ops;
235extern void cachefiles_see_object(struct cachefiles_object *object,
236				  enum cachefiles_obj_ref_trace why);
237extern struct cachefiles_object *cachefiles_grab_object(struct cachefiles_object *object,
238							enum cachefiles_obj_ref_trace why);
239extern void cachefiles_put_object(struct cachefiles_object *object,
240				  enum cachefiles_obj_ref_trace why);
241
242/*
243 * io.c
244 */
245extern bool cachefiles_begin_operation(struct netfs_cache_resources *cres,
246				       enum fscache_want_state want_state);
247extern int __cachefiles_prepare_write(struct cachefiles_object *object,
248				      struct file *file,
249				      loff_t *_start, size_t *_len, size_t upper_len,
250				      bool no_space_allocated_yet);
251extern int __cachefiles_write(struct cachefiles_object *object,
252			      struct file *file,
253			      loff_t start_pos,
254			      struct iov_iter *iter,
255			      netfs_io_terminated_t term_func,
256			      void *term_func_priv);
257
258/*
259 * key.c
260 */
261extern bool cachefiles_cook_key(struct cachefiles_object *object);
262
263/*
264 * main.c
265 */
266extern struct kmem_cache *cachefiles_object_jar;
267
268/*
269 * namei.c
270 */
271extern void cachefiles_unmark_inode_in_use(struct cachefiles_object *object,
272					   struct file *file);
273extern int cachefiles_bury_object(struct cachefiles_cache *cache,
274				  struct cachefiles_object *object,
275				  struct dentry *dir,
276				  struct dentry *rep,
277				  enum fscache_why_object_killed why);
278extern int cachefiles_delete_object(struct cachefiles_object *object,
279				    enum fscache_why_object_killed why);
280extern bool cachefiles_look_up_object(struct cachefiles_object *object);
281extern struct dentry *cachefiles_get_directory(struct cachefiles_cache *cache,
282					       struct dentry *dir,
283					       const char *name,
284					       bool *_is_new);
285extern void cachefiles_put_directory(struct dentry *dir);
286
287extern int cachefiles_cull(struct cachefiles_cache *cache, struct dentry *dir,
288			   char *filename);
289
290extern int cachefiles_check_in_use(struct cachefiles_cache *cache,
291				   struct dentry *dir, char *filename);
292extern struct file *cachefiles_create_tmpfile(struct cachefiles_object *object);
293extern bool cachefiles_commit_tmpfile(struct cachefiles_cache *cache,
294				      struct cachefiles_object *object);
295
296/*
297 * ondemand.c
298 */
299#ifdef CONFIG_CACHEFILES_ONDEMAND
300extern ssize_t cachefiles_ondemand_daemon_read(struct cachefiles_cache *cache,
301					char __user *_buffer, size_t buflen);
302
303extern int cachefiles_ondemand_copen(struct cachefiles_cache *cache,
304				     char *args);
305
306extern int cachefiles_ondemand_restore(struct cachefiles_cache *cache,
307					char *args);
308
309extern int cachefiles_ondemand_init_object(struct cachefiles_object *object);
310extern void cachefiles_ondemand_clean_object(struct cachefiles_object *object);
311
312extern int cachefiles_ondemand_read(struct cachefiles_object *object,
313				    loff_t pos, size_t len);
314
315extern int cachefiles_ondemand_init_obj_info(struct cachefiles_object *obj,
316					struct cachefiles_volume *volume);
317extern void cachefiles_ondemand_deinit_obj_info(struct cachefiles_object *obj);
318
319#define CACHEFILES_OBJECT_STATE_FUNCS(_state, _STATE)	\
320static inline bool								\
321cachefiles_ondemand_object_is_##_state(const struct cachefiles_object *object) \
322{												\
323	return object->ondemand->state == CACHEFILES_ONDEMAND_OBJSTATE_##_STATE; \
324}												\
325												\
326static inline void								\
327cachefiles_ondemand_set_object_##_state(struct cachefiles_object *object) \
328{												\
329	object->ondemand->state = CACHEFILES_ONDEMAND_OBJSTATE_##_STATE; \
330}
331
332CACHEFILES_OBJECT_STATE_FUNCS(open, OPEN);
333CACHEFILES_OBJECT_STATE_FUNCS(close, CLOSE);
334CACHEFILES_OBJECT_STATE_FUNCS(reopening, REOPENING);
 
 
 
 
335
336static inline bool cachefiles_ondemand_is_reopening_read(struct cachefiles_req *req)
 
 
 
337{
338	return cachefiles_ondemand_object_is_reopening(req->object) &&
339			req->msg.opcode == CACHEFILES_OP_READ;
 
 
340}
341
342#else
343static inline ssize_t cachefiles_ondemand_daemon_read(struct cachefiles_cache *cache,
344					char __user *_buffer, size_t buflen)
345{
346	return -EOPNOTSUPP;
347}
348
349static inline int cachefiles_ondemand_init_object(struct cachefiles_object *object)
350{
351	return 0;
352}
353
354static inline void cachefiles_ondemand_clean_object(struct cachefiles_object *object)
355{
356}
357
358static inline int cachefiles_ondemand_read(struct cachefiles_object *object,
359					   loff_t pos, size_t len)
360{
361	return -EOPNOTSUPP;
362}
363
364static inline int cachefiles_ondemand_init_obj_info(struct cachefiles_object *obj,
365						struct cachefiles_volume *volume)
366{
367	return 0;
368}
369static inline void cachefiles_ondemand_deinit_obj_info(struct cachefiles_object *obj)
370{
371}
372
373static inline bool cachefiles_ondemand_is_reopening_read(struct cachefiles_req *req)
374{
375	return false;
376}
377#endif
378
379/*
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
380 * security.c
381 */
382extern int cachefiles_get_security_ID(struct cachefiles_cache *cache);
383extern int cachefiles_determine_cache_security(struct cachefiles_cache *cache,
384					       struct dentry *root,
385					       const struct cred **_saved_cred);
386
387static inline void cachefiles_begin_secure(struct cachefiles_cache *cache,
388					   const struct cred **_saved_cred)
389{
390	*_saved_cred = override_creds(cache->cache_cred);
391}
392
393static inline void cachefiles_end_secure(struct cachefiles_cache *cache,
394					 const struct cred *saved_cred)
395{
396	revert_creds(saved_cred);
397}
398
399/*
400 * volume.c
401 */
402void cachefiles_acquire_volume(struct fscache_volume *volume);
403void cachefiles_free_volume(struct fscache_volume *volume);
404void cachefiles_withdraw_volume(struct cachefiles_volume *volume);
405
406/*
407 * xattr.c
408 */
409extern int cachefiles_set_object_xattr(struct cachefiles_object *object);
410extern int cachefiles_check_auxdata(struct cachefiles_object *object,
411				    struct file *file);
 
 
 
 
 
412extern int cachefiles_remove_object_xattr(struct cachefiles_cache *cache,
413					  struct cachefiles_object *object,
414					  struct dentry *dentry);
415extern void cachefiles_prepare_to_write(struct fscache_cookie *cookie);
416extern bool cachefiles_set_volume_xattr(struct cachefiles_volume *volume);
417extern int cachefiles_check_volume_xattr(struct cachefiles_volume *volume);
418
419/*
420 * Error handling
421 */
 
 
422#define cachefiles_io_error(___cache, FMT, ...)		\
423do {							\
424	pr_err("I/O Error: " FMT"\n", ##__VA_ARGS__);	\
425	fscache_io_error((___cache)->cache);		\
426	set_bit(CACHEFILES_DEAD, &(___cache)->flags);	\
427} while (0)
428
429#define cachefiles_io_error_obj(object, FMT, ...)			\
430do {									\
431	struct cachefiles_cache *___cache;				\
432									\
433	___cache = (object)->volume->cache;				\
434	cachefiles_io_error(___cache, FMT " [o=%08x]", ##__VA_ARGS__,	\
435			    (object)->debug_id);			\
436} while (0)
437
438
439/*
440 * Debug tracing
441 */
442extern unsigned cachefiles_debug;
443#define CACHEFILES_DEBUG_KENTER	1
444#define CACHEFILES_DEBUG_KLEAVE	2
445#define CACHEFILES_DEBUG_KDEBUG	4
446
447#define dbgprintk(FMT, ...) \
448	printk(KERN_DEBUG "[%-6.6s] "FMT"\n", current->comm, ##__VA_ARGS__)
449
450#define kenter(FMT, ...) dbgprintk("==> %s("FMT")", __func__, ##__VA_ARGS__)
451#define kleave(FMT, ...) dbgprintk("<== %s()"FMT"", __func__, ##__VA_ARGS__)
452#define kdebug(FMT, ...) dbgprintk(FMT, ##__VA_ARGS__)
453
454
455#if defined(__KDEBUG)
456#define _enter(FMT, ...) kenter(FMT, ##__VA_ARGS__)
457#define _leave(FMT, ...) kleave(FMT, ##__VA_ARGS__)
458#define _debug(FMT, ...) kdebug(FMT, ##__VA_ARGS__)
459
460#elif defined(CONFIG_CACHEFILES_DEBUG)
461#define _enter(FMT, ...)				\
462do {							\
463	if (cachefiles_debug & CACHEFILES_DEBUG_KENTER)	\
464		kenter(FMT, ##__VA_ARGS__);		\
465} while (0)
466
467#define _leave(FMT, ...)				\
468do {							\
469	if (cachefiles_debug & CACHEFILES_DEBUG_KLEAVE)	\
470		kleave(FMT, ##__VA_ARGS__);		\
471} while (0)
472
473#define _debug(FMT, ...)				\
474do {							\
475	if (cachefiles_debug & CACHEFILES_DEBUG_KDEBUG)	\
476		kdebug(FMT, ##__VA_ARGS__);		\
477} while (0)
478
479#else
480#define _enter(FMT, ...) no_printk("==> %s("FMT")", __func__, ##__VA_ARGS__)
481#define _leave(FMT, ...) no_printk("<== %s()"FMT"", __func__, ##__VA_ARGS__)
482#define _debug(FMT, ...) no_printk(FMT, ##__VA_ARGS__)
483#endif
484
485#if 1 /* defined(__KDEBUGALL) */
486
487#define ASSERT(X)							\
488do {									\
489	if (unlikely(!(X))) {						\
490		pr_err("\n");						\
491		pr_err("Assertion failed\n");		\
492		BUG();							\
493	}								\
494} while (0)
495
496#define ASSERTCMP(X, OP, Y)						\
497do {									\
498	if (unlikely(!((X) OP (Y)))) {					\
499		pr_err("\n");						\
500		pr_err("Assertion failed\n");		\
501		pr_err("%lx " #OP " %lx is false\n",			\
502		       (unsigned long)(X), (unsigned long)(Y));		\
503		BUG();							\
504	}								\
505} while (0)
506
507#define ASSERTIF(C, X)							\
508do {									\
509	if (unlikely((C) && !(X))) {					\
510		pr_err("\n");						\
511		pr_err("Assertion failed\n");		\
512		BUG();							\
513	}								\
514} while (0)
515
516#define ASSERTIFCMP(C, X, OP, Y)					\
517do {									\
518	if (unlikely((C) && !((X) OP (Y)))) {				\
519		pr_err("\n");						\
520		pr_err("Assertion failed\n");		\
521		pr_err("%lx " #OP " %lx is false\n",			\
522		       (unsigned long)(X), (unsigned long)(Y));		\
523		BUG();							\
524	}								\
525} while (0)
526
527#else
528
529#define ASSERT(X)			do {} while (0)
530#define ASSERTCMP(X, OP, Y)		do {} while (0)
531#define ASSERTIF(C, X)			do {} while (0)
532#define ASSERTIFCMP(C, X, OP, Y)	do {} while (0)
533
534#endif
v3.15
 
  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
 18struct cachefiles_cache;
 19struct cachefiles_object;
 20
 21extern unsigned cachefiles_debug;
 22#define CACHEFILES_DEBUG_KENTER	1
 23#define CACHEFILES_DEBUG_KLEAVE	2
 24#define CACHEFILES_DEBUG_KDEBUG	4
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 25
 26#define cachefiles_gfp (__GFP_WAIT | __GFP_NORETRY | __GFP_NOMEMALLOC)
 
 
 
 
 
 27
 28/*
 29 * node records
 30 */
 31struct cachefiles_object {
 32	struct fscache_object		fscache;	/* fscache handle */
 33	struct cachefiles_lookup_data	*lookup_data;	/* cached lookup data */
 34	struct dentry			*dentry;	/* the file/dir representing this object */
 35	struct dentry			*backer;	/* backing file */
 36	loff_t				i_size;		/* object size */
 
 
 
 
 
 37	unsigned long			flags;
 38#define CACHEFILES_OBJECT_ACTIVE	0		/* T if marked active */
 39#define CACHEFILES_OBJECT_BURIED	1		/* T if preemptively buried */
 40	atomic_t			usage;		/* object usage count */
 41	uint8_t				type;		/* object type */
 42	uint8_t				new;		/* T if object new */
 43	spinlock_t			work_lock;
 44	struct rb_node			active_node;	/* link in active tree (dentry is key) */
 45};
 46
 47extern struct kmem_cache *cachefiles_object_jar;
 48
 49/*
 50 * Cache files cache definition
 51 */
 52struct cachefiles_cache {
 53	struct fscache_cache		cache;		/* FS-Cache record */
 54	struct vfsmount			*mnt;		/* mountpoint holding the cache */
 
 55	struct dentry			*graveyard;	/* directory into which dead objects go */
 56	struct file			*cachefilesd;	/* manager daemon handle */
 
 
 
 57	const struct cred		*cache_cred;	/* security override for accessing cache */
 58	struct mutex			daemon_mutex;	/* command serialisation mutex */
 59	wait_queue_head_t		daemon_pollwq;	/* poll waitqueue for daemon */
 60	struct rb_root			active_nodes;	/* active nodes (can't be culled) */
 61	rwlock_t			active_lock;	/* lock for active_nodes */
 62	atomic_t			gravecounter;	/* graveyard uniquifier */
 
 
 
 63	unsigned			frun_percent;	/* when to stop culling (% files) */
 64	unsigned			fcull_percent;	/* when to start culling (% files) */
 65	unsigned			fstop_percent;	/* when to stop allocating (% files) */
 66	unsigned			brun_percent;	/* when to stop culling (% blocks) */
 67	unsigned			bcull_percent;	/* when to start culling (% blocks) */
 68	unsigned			bstop_percent;	/* when to stop allocating (% blocks) */
 69	unsigned			bsize;		/* cache's block size */
 70	unsigned			bshift;		/* min(ilog2(PAGE_SIZE / bsize), 0) */
 71	uint64_t			frun;		/* when to stop culling */
 72	uint64_t			fcull;		/* when to start culling */
 73	uint64_t			fstop;		/* when to stop allocating */
 74	sector_t			brun;		/* when to stop culling */
 75	sector_t			bcull;		/* when to start culling */
 76	sector_t			bstop;		/* when to stop allocating */
 77	unsigned long			flags;
 78#define CACHEFILES_READY		0	/* T if cache prepared */
 79#define CACHEFILES_DEAD			1	/* T if cache dead */
 80#define CACHEFILES_CULLING		2	/* T if cull engaged */
 81#define CACHEFILES_STATE_CHANGED	3	/* T if state changed (poll trigger) */
 
 82	char				*rootdirname;	/* name of cache root directory */
 83	char				*secctx;	/* LSM security context */
 84	char				*tag;		/* cache binding tag */
 
 
 
 
 
 85};
 86
 87/*
 88 * backing file read tracking
 89 */
 90struct cachefiles_one_read {
 91	wait_queue_t			monitor;	/* link into monitored waitqueue */
 92	struct page			*back_page;	/* backing file page we're waiting for */
 93	struct page			*netfs_page;	/* netfs page we're going to fill */
 94	struct fscache_retrieval	*op;		/* retrieval op covering this */
 95	struct list_head		op_link;	/* link in op's todo list */
 
 
 96};
 97
 98/*
 99 * backing file write tracking
100 */
101struct cachefiles_one_write {
102	struct page			*netfs_page;	/* netfs page to copy */
103	struct cachefiles_object	*object;
104	struct list_head		obj_link;	/* link in object's lists */
105	fscache_rw_complete_t		end_io_func;
106	void				*context;
107};
108
109/*
110 * auxiliary data xattr buffer
111 */
112struct cachefiles_xattr {
113	uint16_t			len;
114	uint8_t				type;
115	uint8_t				data[];
116};
117
118/*
119 * note change of state for daemon
120 */
121static inline void cachefiles_state_changed(struct cachefiles_cache *cache)
122{
123	set_bit(CACHEFILES_STATE_CHANGED, &cache->flags);
124	wake_up_all(&cache->daemon_pollwq);
125}
126
127/*
128 * bind.c
129 */
130extern int cachefiles_daemon_bind(struct cachefiles_cache *cache, char *args);
131extern void cachefiles_daemon_unbind(struct cachefiles_cache *cache);
 
 
 
 
 
 
 
 
 
132
133/*
134 * daemon.c
135 */
136extern const struct file_operations cachefiles_daemon_fops;
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
137
138extern int cachefiles_has_space(struct cachefiles_cache *cache,
139				unsigned fnr, unsigned bnr);
 
 
 
 
 
 
 
 
 
140
141/*
142 * interface.c
143 */
144extern const struct fscache_cache_ops cachefiles_cache_ops;
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
145
146/*
147 * key.c
148 */
149extern char *cachefiles_cook_key(const u8 *raw, int keylen, uint8_t type);
 
 
 
 
 
150
151/*
152 * namei.c
153 */
154extern int cachefiles_delete_object(struct cachefiles_cache *cache,
155				    struct cachefiles_object *object);
156extern int cachefiles_walk_to_object(struct cachefiles_object *parent,
157				     struct cachefiles_object *object,
158				     const char *key,
159				     struct cachefiles_xattr *auxdata);
 
 
 
 
160extern struct dentry *cachefiles_get_directory(struct cachefiles_cache *cache,
161					       struct dentry *dir,
162					       const char *name);
 
 
163
164extern int cachefiles_cull(struct cachefiles_cache *cache, struct dentry *dir,
165			   char *filename);
166
167extern int cachefiles_check_in_use(struct cachefiles_cache *cache,
168				   struct dentry *dir, char *filename);
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
169
170/*
171 * proc.c
172 */
173#ifdef CONFIG_CACHEFILES_HISTOGRAM
174extern atomic_t cachefiles_lookup_histogram[HZ];
175extern atomic_t cachefiles_mkdir_histogram[HZ];
176extern atomic_t cachefiles_create_histogram[HZ];
177
178extern int __init cachefiles_proc_init(void);
179extern void cachefiles_proc_cleanup(void);
180static inline
181void cachefiles_hist(atomic_t histogram[], unsigned long start_jif)
182{
183	unsigned long jif = jiffies - start_jif;
184	if (jif >= HZ)
185		jif = HZ - 1;
186	atomic_inc(&histogram[jif]);
187}
188
189#else
190#define cachefiles_proc_init()		(0)
191#define cachefiles_proc_cleanup()	do {} while (0)
192#define cachefiles_hist(hist, start_jif) do {} while (0)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
193#endif
194
195/*
196 * rdwr.c
197 */
198extern int cachefiles_read_or_alloc_page(struct fscache_retrieval *,
199					 struct page *, gfp_t);
200extern int cachefiles_read_or_alloc_pages(struct fscache_retrieval *,
201					  struct list_head *, unsigned *,
202					  gfp_t);
203extern int cachefiles_allocate_page(struct fscache_retrieval *, struct page *,
204				    gfp_t);
205extern int cachefiles_allocate_pages(struct fscache_retrieval *,
206				     struct list_head *, unsigned *, gfp_t);
207extern int cachefiles_write_page(struct fscache_storage *, struct page *);
208extern void cachefiles_uncache_page(struct fscache_object *, struct page *);
209
210/*
211 * security.c
212 */
213extern int cachefiles_get_security_ID(struct cachefiles_cache *cache);
214extern int cachefiles_determine_cache_security(struct cachefiles_cache *cache,
215					       struct dentry *root,
216					       const struct cred **_saved_cred);
217
218static inline void cachefiles_begin_secure(struct cachefiles_cache *cache,
219					   const struct cred **_saved_cred)
220{
221	*_saved_cred = override_creds(cache->cache_cred);
222}
223
224static inline void cachefiles_end_secure(struct cachefiles_cache *cache,
225					 const struct cred *saved_cred)
226{
227	revert_creds(saved_cred);
228}
229
230/*
 
 
 
 
 
 
 
231 * xattr.c
232 */
233extern int cachefiles_check_object_type(struct cachefiles_object *object);
234extern int cachefiles_set_object_xattr(struct cachefiles_object *object,
235				       struct cachefiles_xattr *auxdata);
236extern int cachefiles_update_object_xattr(struct cachefiles_object *object,
237					  struct cachefiles_xattr *auxdata);
238extern int cachefiles_check_auxdata(struct cachefiles_object *object);
239extern int cachefiles_check_object_xattr(struct cachefiles_object *object,
240					 struct cachefiles_xattr *auxdata);
241extern int cachefiles_remove_object_xattr(struct cachefiles_cache *cache,
 
242					  struct dentry *dentry);
243
 
 
244
245/*
246 * error handling
247 */
248#define kerror(FMT, ...) printk(KERN_ERR "CacheFiles: "FMT"\n", ##__VA_ARGS__)
249
250#define cachefiles_io_error(___cache, FMT, ...)		\
251do {							\
252	kerror("I/O Error: " FMT, ##__VA_ARGS__);	\
253	fscache_io_error(&(___cache)->cache);		\
254	set_bit(CACHEFILES_DEAD, &(___cache)->flags);	\
255} while (0)
256
257#define cachefiles_io_error_obj(object, FMT, ...)			\
258do {									\
259	struct cachefiles_cache *___cache;				\
260									\
261	___cache = container_of((object)->fscache.cache,		\
262				struct cachefiles_cache, cache);	\
263	cachefiles_io_error(___cache, FMT, ##__VA_ARGS__);		\
264} while (0)
265
266
267/*
268 * debug tracing
269 */
 
 
 
 
 
270#define dbgprintk(FMT, ...) \
271	printk(KERN_DEBUG "[%-6.6s] "FMT"\n", current->comm, ##__VA_ARGS__)
272
273#define kenter(FMT, ...) dbgprintk("==> %s("FMT")", __func__, ##__VA_ARGS__)
274#define kleave(FMT, ...) dbgprintk("<== %s()"FMT"", __func__, ##__VA_ARGS__)
275#define kdebug(FMT, ...) dbgprintk(FMT, ##__VA_ARGS__)
276
277
278#if defined(__KDEBUG)
279#define _enter(FMT, ...) kenter(FMT, ##__VA_ARGS__)
280#define _leave(FMT, ...) kleave(FMT, ##__VA_ARGS__)
281#define _debug(FMT, ...) kdebug(FMT, ##__VA_ARGS__)
282
283#elif defined(CONFIG_CACHEFILES_DEBUG)
284#define _enter(FMT, ...)				\
285do {							\
286	if (cachefiles_debug & CACHEFILES_DEBUG_KENTER)	\
287		kenter(FMT, ##__VA_ARGS__);		\
288} while (0)
289
290#define _leave(FMT, ...)				\
291do {							\
292	if (cachefiles_debug & CACHEFILES_DEBUG_KLEAVE)	\
293		kleave(FMT, ##__VA_ARGS__);		\
294} while (0)
295
296#define _debug(FMT, ...)				\
297do {							\
298	if (cachefiles_debug & CACHEFILES_DEBUG_KDEBUG)	\
299		kdebug(FMT, ##__VA_ARGS__);		\
300} while (0)
301
302#else
303#define _enter(FMT, ...) no_printk("==> %s("FMT")", __func__, ##__VA_ARGS__)
304#define _leave(FMT, ...) no_printk("<== %s()"FMT"", __func__, ##__VA_ARGS__)
305#define _debug(FMT, ...) no_printk(FMT, ##__VA_ARGS__)
306#endif
307
308#if 1 /* defined(__KDEBUGALL) */
309
310#define ASSERT(X)							\
311do {									\
312	if (unlikely(!(X))) {						\
313		printk(KERN_ERR "\n");					\
314		printk(KERN_ERR "CacheFiles: Assertion failed\n");	\
315		BUG();							\
316	}								\
317} while (0)
318
319#define ASSERTCMP(X, OP, Y)						\
320do {									\
321	if (unlikely(!((X) OP (Y)))) {					\
322		printk(KERN_ERR "\n");					\
323		printk(KERN_ERR "CacheFiles: Assertion failed\n");	\
324		printk(KERN_ERR "%lx " #OP " %lx is false\n",		\
325		       (unsigned long)(X), (unsigned long)(Y));		\
326		BUG();							\
327	}								\
328} while (0)
329
330#define ASSERTIF(C, X)							\
331do {									\
332	if (unlikely((C) && !(X))) {					\
333		printk(KERN_ERR "\n");					\
334		printk(KERN_ERR "CacheFiles: Assertion failed\n");	\
335		BUG();							\
336	}								\
337} while (0)
338
339#define ASSERTIFCMP(C, X, OP, Y)					\
340do {									\
341	if (unlikely((C) && !((X) OP (Y)))) {				\
342		printk(KERN_ERR "\n");					\
343		printk(KERN_ERR "CacheFiles: Assertion failed\n");	\
344		printk(KERN_ERR "%lx " #OP " %lx is false\n",		\
345		       (unsigned long)(X), (unsigned long)(Y));		\
346		BUG();							\
347	}								\
348} while (0)
349
350#else
351
352#define ASSERT(X)			do {} while (0)
353#define ASSERTCMP(X, OP, Y)		do {} while (0)
354#define ASSERTIF(C, X)			do {} while (0)
355#define ASSERTIFCMP(C, X, OP, Y)	do {} while (0)
356
357#endif