xref: /openbmc/linux/fs/cachefiles/internal.h (revision 3bf0b803)
177443f61SDavid Howells /* SPDX-License-Identifier: GPL-2.0-or-later */
277443f61SDavid Howells /* General netfs cache on cache files internal defs
377443f61SDavid Howells  *
477443f61SDavid Howells  * Copyright (C) 2021 Red Hat, Inc. All Rights Reserved.
577443f61SDavid Howells  * Written by David Howells (dhowells@redhat.com)
677443f61SDavid Howells  */
777443f61SDavid Howells 
877443f61SDavid Howells #ifdef pr_fmt
977443f61SDavid Howells #undef pr_fmt
1077443f61SDavid Howells #endif
1177443f61SDavid Howells 
1277443f61SDavid Howells #define pr_fmt(fmt) "CacheFiles: " fmt
1377443f61SDavid Howells 
1477443f61SDavid Howells 
1577443f61SDavid Howells #include <linux/fscache-cache.h>
1677443f61SDavid Howells #include <linux/cred.h>
1777443f61SDavid Howells #include <linux/security.h>
18c8383054SJeffle Xu #include <linux/xarray.h>
19c8383054SJeffle Xu #include <linux/cachefiles.h>
2077443f61SDavid Howells 
211f08c925SDavid Howells #define CACHEFILES_DIO_BLOCK_SIZE 4096
221f08c925SDavid Howells 
238390fbc4SDavid Howells struct cachefiles_cache;
248390fbc4SDavid Howells struct cachefiles_object;
258390fbc4SDavid Howells 
26df98e87fSDavid Howells enum cachefiles_content {
27df98e87fSDavid Howells 	/* These values are saved on disk */
28df98e87fSDavid Howells 	CACHEFILES_CONTENT_NO_DATA	= 0, /* No content stored */
29df98e87fSDavid Howells 	CACHEFILES_CONTENT_SINGLE	= 1, /* Content is monolithic, all is present */
30df98e87fSDavid Howells 	CACHEFILES_CONTENT_ALL		= 2, /* Content is all present, no map */
31df98e87fSDavid Howells 	CACHEFILES_CONTENT_BACKFS_MAP	= 3, /* Content is piecemeal, mapped through backing fs */
32df98e87fSDavid Howells 	CACHEFILES_CONTENT_DIRTY	= 4, /* Content is dirty (only seen on disk) */
33df98e87fSDavid Howells 	nr__cachefiles_content
34df98e87fSDavid Howells };
35df98e87fSDavid Howells 
368390fbc4SDavid Howells /*
37fe2140e2SDavid Howells  * Cached volume representation.
38fe2140e2SDavid Howells  */
39fe2140e2SDavid Howells struct cachefiles_volume {
40fe2140e2SDavid Howells 	struct cachefiles_cache		*cache;
41fe2140e2SDavid Howells 	struct list_head		cache_link;	/* Link in cache->volumes */
42fe2140e2SDavid Howells 	struct fscache_volume		*vcookie;	/* The netfs's representation */
43fe2140e2SDavid Howells 	struct dentry			*dentry;	/* The volume dentry */
44fe2140e2SDavid Howells 	struct dentry			*fanout[256];	/* Fanout subdirs */
45fe2140e2SDavid Howells };
46fe2140e2SDavid Howells 
47955190e1SJia Zhu enum cachefiles_object_state {
48955190e1SJia Zhu 	CACHEFILES_ONDEMAND_OBJSTATE_CLOSE, /* Anonymous fd closed by daemon or initial state */
49955190e1SJia Zhu 	CACHEFILES_ONDEMAND_OBJSTATE_OPEN, /* Anonymous fd associated with object is available */
50f740fd94SJia Zhu 	CACHEFILES_ONDEMAND_OBJSTATE_REOPENING, /* Object that was closed and is being reopened. */
51955190e1SJia Zhu };
52955190e1SJia Zhu 
5333d21f06SJia Zhu struct cachefiles_ondemand_info {
54f740fd94SJia Zhu 	struct work_struct		ondemand_work;
5533d21f06SJia Zhu 	int				ondemand_id;
5633d21f06SJia Zhu 	enum cachefiles_object_state	state;
5733d21f06SJia Zhu 	struct cachefiles_object	*object;
58e564e48cSBaokun Li 	spinlock_t			lock;
5933d21f06SJia Zhu };
6033d21f06SJia Zhu 
61fe2140e2SDavid Howells /*
62df98e87fSDavid Howells  * Backing file state.
638390fbc4SDavid Howells  */
648390fbc4SDavid Howells struct cachefiles_object {
65df98e87fSDavid Howells 	struct fscache_cookie		*cookie;	/* Netfs data storage object cookie */
66df98e87fSDavid Howells 	struct cachefiles_volume	*volume;	/* Cache volume that holds this object */
67df98e87fSDavid Howells 	struct list_head		cache_link;	/* Link in cache->*_list */
68df98e87fSDavid Howells 	struct file			*file;		/* The file representing this object */
69df98e87fSDavid Howells 	char				*d_name;	/* Backing file name */
70df98e87fSDavid Howells 	int				debug_id;
71df98e87fSDavid Howells 	spinlock_t			lock;
72df98e87fSDavid Howells 	refcount_t			ref;
73df98e87fSDavid Howells 	u8				d_name_len;	/* Length of filename */
74df98e87fSDavid Howells 	enum cachefiles_content		content_info:8;	/* Info about content presence */
75df98e87fSDavid Howells 	unsigned long			flags;
7672b95785SDavid Howells #define CACHEFILES_OBJECT_USING_TMPFILE	0		/* Have an unlinked tmpfile */
77c8383054SJeffle Xu #ifdef CONFIG_CACHEFILES_ONDEMAND
7833d21f06SJia Zhu 	struct cachefiles_ondemand_info	*ondemand;
79c8383054SJeffle Xu #endif
808390fbc4SDavid Howells };
818390fbc4SDavid Howells 
82c8383054SJeffle Xu #define CACHEFILES_ONDEMAND_ID_CLOSED	-1
83c8383054SJeffle Xu 
848390fbc4SDavid Howells /*
858390fbc4SDavid Howells  * Cache files cache definition
868390fbc4SDavid Howells  */
878390fbc4SDavid Howells struct cachefiles_cache {
881493bf74SDavid Howells 	struct fscache_cache		*cache;		/* Cache cookie */
898390fbc4SDavid Howells 	struct vfsmount			*mnt;		/* mountpoint holding the cache */
90d1065b0aSDavid Howells 	struct dentry			*store;		/* Directory into which live objects go */
91d1065b0aSDavid Howells 	struct dentry			*graveyard;	/* directory into which dead objects go */
928390fbc4SDavid Howells 	struct file			*cachefilesd;	/* manager daemon handle */
93fe2140e2SDavid Howells 	struct list_head		volumes;	/* List of volume objects */
941f08c925SDavid Howells 	struct list_head		object_list;	/* List of active objects */
95fe2140e2SDavid Howells 	spinlock_t			object_list_lock; /* Lock for volumes and object_list */
968390fbc4SDavid Howells 	const struct cred		*cache_cred;	/* security override for accessing cache */
978390fbc4SDavid Howells 	struct mutex			daemon_mutex;	/* command serialisation mutex */
988390fbc4SDavid Howells 	wait_queue_head_t		daemon_pollwq;	/* poll waitqueue for daemon */
998390fbc4SDavid Howells 	atomic_t			gravecounter;	/* graveyard uniquifier */
1008390fbc4SDavid Howells 	atomic_t			f_released;	/* number of objects released lately */
1018390fbc4SDavid Howells 	atomic_long_t			b_released;	/* number of blocks released lately */
10280f94f29SDavid Howells 	atomic_long_t			b_writing;	/* Number of blocks being written */
1038390fbc4SDavid Howells 	unsigned			frun_percent;	/* when to stop culling (% files) */
1048390fbc4SDavid Howells 	unsigned			fcull_percent;	/* when to start culling (% files) */
1058390fbc4SDavid Howells 	unsigned			fstop_percent;	/* when to stop allocating (% files) */
1068390fbc4SDavid Howells 	unsigned			brun_percent;	/* when to stop culling (% blocks) */
1078390fbc4SDavid Howells 	unsigned			bcull_percent;	/* when to start culling (% blocks) */
1088390fbc4SDavid Howells 	unsigned			bstop_percent;	/* when to stop allocating (% blocks) */
1098390fbc4SDavid Howells 	unsigned			bsize;		/* cache's block size */
1105638b067SDavid Howells 	unsigned			bshift;		/* ilog2(bsize) */
1118390fbc4SDavid Howells 	uint64_t			frun;		/* when to stop culling */
1128390fbc4SDavid Howells 	uint64_t			fcull;		/* when to start culling */
1138390fbc4SDavid Howells 	uint64_t			fstop;		/* when to stop allocating */
1148390fbc4SDavid Howells 	sector_t			brun;		/* when to stop culling */
1158390fbc4SDavid Howells 	sector_t			bcull;		/* when to start culling */
1168390fbc4SDavid Howells 	sector_t			bstop;		/* when to stop allocating */
1178390fbc4SDavid Howells 	unsigned long			flags;
1188390fbc4SDavid Howells #define CACHEFILES_READY		0	/* T if cache prepared */
1198390fbc4SDavid Howells #define CACHEFILES_DEAD			1	/* T if cache dead */
1208390fbc4SDavid Howells #define CACHEFILES_CULLING		2	/* T if cull engaged */
1218390fbc4SDavid Howells #define CACHEFILES_STATE_CHANGED	3	/* T if state changed (poll trigger) */
122c8383054SJeffle Xu #define CACHEFILES_ONDEMAND_MODE	4	/* T if in on-demand read mode */
1238390fbc4SDavid Howells 	char				*rootdirname;	/* name of cache root directory */
1248390fbc4SDavid Howells 	char				*secctx;	/* LSM security context */
1258390fbc4SDavid Howells 	char				*tag;		/* cache binding tag */
126d11b0b04SJeffle Xu 	refcount_t			unbind_pincount;/* refcount to do daemon unbind */
127c8383054SJeffle Xu 	struct xarray			reqs;		/* xarray of pending on-demand requests */
1281122f400SXin Yin 	unsigned long			req_id_next;
129c8383054SJeffle Xu 	struct xarray			ondemand_ids;	/* xarray for ondemand_id allocation */
130c8383054SJeffle Xu 	u32				ondemand_id_next;
1318390fbc4SDavid Howells };
1328390fbc4SDavid Howells 
cachefiles_in_ondemand_mode(struct cachefiles_cache * cache)133c8383054SJeffle Xu static inline bool cachefiles_in_ondemand_mode(struct cachefiles_cache *cache)
134c8383054SJeffle Xu {
135c8383054SJeffle Xu 	return IS_ENABLED(CONFIG_CACHEFILES_ONDEMAND) &&
136c8383054SJeffle Xu 		test_bit(CACHEFILES_ONDEMAND_MODE, &cache->flags);
137c8383054SJeffle Xu }
138c8383054SJeffle Xu 
139c8383054SJeffle Xu struct cachefiles_req {
140c8383054SJeffle Xu 	struct cachefiles_object *object;
141c8383054SJeffle Xu 	struct completion done;
142a6de8276SBaokun Li 	refcount_t ref;
143c8383054SJeffle Xu 	int error;
144c8383054SJeffle Xu 	struct cachefiles_msg msg;
145c8383054SJeffle Xu };
146c8383054SJeffle Xu 
147c8383054SJeffle Xu #define CACHEFILES_REQ_NEW	XA_MARK_1
148c8383054SJeffle Xu 
149ecf5a6ceSDavid Howells #include <trace/events/cachefiles.h>
15077443f61SDavid Howells 
151287fd611SDavid Howells static inline
cachefiles_cres_file(struct netfs_cache_resources * cres)152287fd611SDavid Howells struct file *cachefiles_cres_file(struct netfs_cache_resources *cres)
153287fd611SDavid Howells {
154287fd611SDavid Howells 	return cres->cache_priv2;
155287fd611SDavid Howells }
156287fd611SDavid Howells 
157287fd611SDavid Howells static inline
cachefiles_cres_object(struct netfs_cache_resources * cres)158287fd611SDavid Howells struct cachefiles_object *cachefiles_cres_object(struct netfs_cache_resources *cres)
159287fd611SDavid Howells {
160287fd611SDavid Howells 	return fscache_cres_cookie(cres)->cache_priv;
161287fd611SDavid Howells }
162287fd611SDavid Howells 
16377443f61SDavid Howells /*
1648667d434SDavid Howells  * note change of state for daemon
1658667d434SDavid Howells  */
cachefiles_state_changed(struct cachefiles_cache * cache)1668667d434SDavid Howells static inline void cachefiles_state_changed(struct cachefiles_cache *cache)
1678667d434SDavid Howells {
1688667d434SDavid Howells 	set_bit(CACHEFILES_STATE_CHANGED, &cache->flags);
1698667d434SDavid Howells 	wake_up_all(&cache->daemon_pollwq);
1708667d434SDavid Howells }
1718667d434SDavid Howells 
1728667d434SDavid Howells /*
17380f94f29SDavid Howells  * cache.c
17480f94f29SDavid Howells  */
175d1065b0aSDavid Howells extern int cachefiles_add_cache(struct cachefiles_cache *cache);
176d1065b0aSDavid Howells extern void cachefiles_withdraw_cache(struct cachefiles_cache *cache);
17780f94f29SDavid Howells 
1783929eca7SDavid Howells enum cachefiles_has_space_for {
1793929eca7SDavid Howells 	cachefiles_has_space_check,
1803929eca7SDavid Howells 	cachefiles_has_space_for_write,
1813929eca7SDavid Howells 	cachefiles_has_space_for_create,
1823929eca7SDavid Howells };
1833929eca7SDavid Howells extern int cachefiles_has_space(struct cachefiles_cache *cache,
1843929eca7SDavid Howells 				unsigned fnr, unsigned bnr,
1853929eca7SDavid Howells 				enum cachefiles_has_space_for reason);
1863929eca7SDavid Howells 
18780f94f29SDavid Howells /*
1888667d434SDavid Howells  * daemon.c
1898667d434SDavid Howells  */
1908667d434SDavid Howells extern const struct file_operations cachefiles_daemon_fops;
1913bf0b803SBaokun Li extern void cachefiles_flush_reqs(struct cachefiles_cache *cache);
192d11b0b04SJeffle Xu extern void cachefiles_get_unbind_pincount(struct cachefiles_cache *cache);
193d11b0b04SJeffle Xu extern void cachefiles_put_unbind_pincount(struct cachefiles_cache *cache);
1948667d434SDavid Howells 
1958667d434SDavid Howells /*
196a70f6526SDavid Howells  * error_inject.c
197a70f6526SDavid Howells  */
198a70f6526SDavid Howells #ifdef CONFIG_CACHEFILES_ERROR_INJECTION
199a70f6526SDavid Howells extern unsigned int cachefiles_error_injection_state;
200a70f6526SDavid Howells extern int cachefiles_register_error_injection(void);
201a70f6526SDavid Howells extern void cachefiles_unregister_error_injection(void);
202a70f6526SDavid Howells 
203a70f6526SDavid Howells #else
204a70f6526SDavid Howells #define cachefiles_error_injection_state 0
205a70f6526SDavid Howells 
cachefiles_register_error_injection(void)206a70f6526SDavid Howells static inline int cachefiles_register_error_injection(void)
207a70f6526SDavid Howells {
208a70f6526SDavid Howells 	return 0;
209a70f6526SDavid Howells }
210a70f6526SDavid Howells 
cachefiles_unregister_error_injection(void)211a70f6526SDavid Howells static inline void cachefiles_unregister_error_injection(void)
212a70f6526SDavid Howells {
213a70f6526SDavid Howells }
214a70f6526SDavid Howells #endif
215a70f6526SDavid Howells 
216a70f6526SDavid Howells 
cachefiles_inject_read_error(void)217a70f6526SDavid Howells static inline int cachefiles_inject_read_error(void)
218a70f6526SDavid Howells {
219a70f6526SDavid Howells 	return cachefiles_error_injection_state & 2 ? -EIO : 0;
220a70f6526SDavid Howells }
221a70f6526SDavid Howells 
cachefiles_inject_write_error(void)222a70f6526SDavid Howells static inline int cachefiles_inject_write_error(void)
223a70f6526SDavid Howells {
224a70f6526SDavid Howells 	return cachefiles_error_injection_state & 2 ? -EIO :
225a70f6526SDavid Howells 		cachefiles_error_injection_state & 1 ? -ENOSPC :
226a70f6526SDavid Howells 		0;
227a70f6526SDavid Howells }
228a70f6526SDavid Howells 
cachefiles_inject_remove_error(void)229a70f6526SDavid Howells static inline int cachefiles_inject_remove_error(void)
230a70f6526SDavid Howells {
231a70f6526SDavid Howells 	return cachefiles_error_injection_state & 2 ? -EIO : 0;
232a70f6526SDavid Howells }
233a70f6526SDavid Howells 
2341493bf74SDavid Howells /*
235d1065b0aSDavid Howells  * interface.c
236d1065b0aSDavid Howells  */
237d1065b0aSDavid Howells extern const struct fscache_cache_ops cachefiles_cache_ops;
238df98e87fSDavid Howells extern void cachefiles_see_object(struct cachefiles_object *object,
239df98e87fSDavid Howells 				  enum cachefiles_obj_ref_trace why);
240df98e87fSDavid Howells extern struct cachefiles_object *cachefiles_grab_object(struct cachefiles_object *object,
241df98e87fSDavid Howells 							enum cachefiles_obj_ref_trace why);
242df98e87fSDavid Howells extern void cachefiles_put_object(struct cachefiles_object *object,
243df98e87fSDavid Howells 				  enum cachefiles_obj_ref_trace why);
244df98e87fSDavid Howells 
245df98e87fSDavid Howells /*
246287fd611SDavid Howells  * io.c
247287fd611SDavid Howells  */
248287fd611SDavid Howells extern bool cachefiles_begin_operation(struct netfs_cache_resources *cres,
249287fd611SDavid Howells 				       enum fscache_want_state want_state);
250a06fac15SJeffle Xu extern int __cachefiles_prepare_write(struct cachefiles_object *object,
251a06fac15SJeffle Xu 				      struct file *file,
252a06fac15SJeffle Xu 				      loff_t *_start, size_t *_len,
253a06fac15SJeffle Xu 				      bool no_space_allocated_yet);
254a06fac15SJeffle Xu extern int __cachefiles_write(struct cachefiles_object *object,
255a06fac15SJeffle Xu 			      struct file *file,
256a06fac15SJeffle Xu 			      loff_t start_pos,
257a06fac15SJeffle Xu 			      struct iov_iter *iter,
258a06fac15SJeffle Xu 			      netfs_io_terminated_t term_func,
259a06fac15SJeffle Xu 			      void *term_func_priv);
260287fd611SDavid Howells 
261287fd611SDavid Howells /*
2625d439467SDavid Howells  * key.c
2635d439467SDavid Howells  */
2645d439467SDavid Howells extern bool cachefiles_cook_key(struct cachefiles_object *object);
2655d439467SDavid Howells 
2665d439467SDavid Howells /*
267df98e87fSDavid Howells  * main.c
268df98e87fSDavid Howells  */
269df98e87fSDavid Howells extern struct kmem_cache *cachefiles_object_jar;
270d1065b0aSDavid Howells 
271d1065b0aSDavid Howells /*
27232759f7dSDavid Howells  * namei.c
27332759f7dSDavid Howells  */
274169379eaSDavid Howells extern void cachefiles_unmark_inode_in_use(struct cachefiles_object *object,
275169379eaSDavid Howells 					   struct file *file);
27607a90e97SDavid Howells extern int cachefiles_bury_object(struct cachefiles_cache *cache,
27707a90e97SDavid Howells 				  struct cachefiles_object *object,
27807a90e97SDavid Howells 				  struct dentry *dir,
27907a90e97SDavid Howells 				  struct dentry *rep,
28007a90e97SDavid Howells 				  enum fscache_why_object_killed why);
2811f08c925SDavid Howells extern int cachefiles_delete_object(struct cachefiles_object *object,
2821f08c925SDavid Howells 				    enum fscache_why_object_killed why);
2831f08c925SDavid Howells extern bool cachefiles_look_up_object(struct cachefiles_object *object);
28432759f7dSDavid Howells extern struct dentry *cachefiles_get_directory(struct cachefiles_cache *cache,
28532759f7dSDavid Howells 					       struct dentry *dir,
28632759f7dSDavid Howells 					       const char *name,
28732759f7dSDavid Howells 					       bool *_is_new);
28832759f7dSDavid Howells extern void cachefiles_put_directory(struct dentry *dir);
28932759f7dSDavid Howells 
29007a90e97SDavid Howells extern int cachefiles_cull(struct cachefiles_cache *cache, struct dentry *dir,
29107a90e97SDavid Howells 			   char *filename);
29207a90e97SDavid Howells 
29307a90e97SDavid Howells extern int cachefiles_check_in_use(struct cachefiles_cache *cache,
29407a90e97SDavid Howells 				   struct dentry *dir, char *filename);
2951f08c925SDavid Howells extern struct file *cachefiles_create_tmpfile(struct cachefiles_object *object);
2961f08c925SDavid Howells extern bool cachefiles_commit_tmpfile(struct cachefiles_cache *cache,
2971f08c925SDavid Howells 				      struct cachefiles_object *object);
29807a90e97SDavid Howells 
29932759f7dSDavid Howells /*
300c8383054SJeffle Xu  * ondemand.c
301c8383054SJeffle Xu  */
302c8383054SJeffle Xu #ifdef CONFIG_CACHEFILES_ONDEMAND
303c8383054SJeffle Xu extern ssize_t cachefiles_ondemand_daemon_read(struct cachefiles_cache *cache,
304c8383054SJeffle Xu 					char __user *_buffer, size_t buflen);
305c8383054SJeffle Xu 
306c8383054SJeffle Xu extern int cachefiles_ondemand_copen(struct cachefiles_cache *cache,
307c8383054SJeffle Xu 				     char *args);
308c8383054SJeffle Xu 
3099f5fa40fSJia Zhu extern int cachefiles_ondemand_restore(struct cachefiles_cache *cache,
3109f5fa40fSJia Zhu 					char *args);
3119f5fa40fSJia Zhu 
312c8383054SJeffle Xu extern int cachefiles_ondemand_init_object(struct cachefiles_object *object);
313324b954aSJeffle Xu extern void cachefiles_ondemand_clean_object(struct cachefiles_object *object);
314c8383054SJeffle Xu 
3159032b6e8SJeffle Xu extern int cachefiles_ondemand_read(struct cachefiles_object *object,
3169032b6e8SJeffle Xu 				    loff_t pos, size_t len);
3179032b6e8SJeffle Xu 
31833d21f06SJia Zhu extern int cachefiles_ondemand_init_obj_info(struct cachefiles_object *obj,
31933d21f06SJia Zhu 					struct cachefiles_volume *volume);
32033d21f06SJia Zhu extern void cachefiles_ondemand_deinit_obj_info(struct cachefiles_object *obj);
32133d21f06SJia Zhu 
322955190e1SJia Zhu #define CACHEFILES_OBJECT_STATE_FUNCS(_state, _STATE)	\
323955190e1SJia Zhu static inline bool								\
324955190e1SJia Zhu cachefiles_ondemand_object_is_##_state(const struct cachefiles_object *object) \
325955190e1SJia Zhu {												\
32633d21f06SJia Zhu 	return object->ondemand->state == CACHEFILES_ONDEMAND_OBJSTATE_##_STATE; \
327955190e1SJia Zhu }												\
328955190e1SJia Zhu 												\
329955190e1SJia Zhu static inline void								\
330955190e1SJia Zhu cachefiles_ondemand_set_object_##_state(struct cachefiles_object *object) \
331955190e1SJia Zhu {												\
33233d21f06SJia Zhu 	object->ondemand->state = CACHEFILES_ONDEMAND_OBJSTATE_##_STATE; \
333955190e1SJia Zhu }
334955190e1SJia Zhu 
335955190e1SJia Zhu CACHEFILES_OBJECT_STATE_FUNCS(open, OPEN);
336955190e1SJia Zhu CACHEFILES_OBJECT_STATE_FUNCS(close, CLOSE);
337f740fd94SJia Zhu CACHEFILES_OBJECT_STATE_FUNCS(reopening, REOPENING);
338c8383054SJeffle Xu #else
cachefiles_ondemand_daemon_read(struct cachefiles_cache * cache,char __user * _buffer,size_t buflen)339c8383054SJeffle Xu static inline ssize_t cachefiles_ondemand_daemon_read(struct cachefiles_cache *cache,
340c8383054SJeffle Xu 					char __user *_buffer, size_t buflen)
341c8383054SJeffle Xu {
342c8383054SJeffle Xu 	return -EOPNOTSUPP;
343c8383054SJeffle Xu }
344c8383054SJeffle Xu 
cachefiles_ondemand_init_object(struct cachefiles_object * object)345c8383054SJeffle Xu static inline int cachefiles_ondemand_init_object(struct cachefiles_object *object)
346c8383054SJeffle Xu {
347c8383054SJeffle Xu 	return 0;
348c8383054SJeffle Xu }
349324b954aSJeffle Xu 
cachefiles_ondemand_clean_object(struct cachefiles_object * object)350324b954aSJeffle Xu static inline void cachefiles_ondemand_clean_object(struct cachefiles_object *object)
351324b954aSJeffle Xu {
352324b954aSJeffle Xu }
3539032b6e8SJeffle Xu 
cachefiles_ondemand_read(struct cachefiles_object * object,loff_t pos,size_t len)3549032b6e8SJeffle Xu static inline int cachefiles_ondemand_read(struct cachefiles_object *object,
3559032b6e8SJeffle Xu 					   loff_t pos, size_t len)
3569032b6e8SJeffle Xu {
3579032b6e8SJeffle Xu 	return -EOPNOTSUPP;
3589032b6e8SJeffle Xu }
35933d21f06SJia Zhu 
cachefiles_ondemand_init_obj_info(struct cachefiles_object * obj,struct cachefiles_volume * volume)36033d21f06SJia Zhu static inline int cachefiles_ondemand_init_obj_info(struct cachefiles_object *obj,
36133d21f06SJia Zhu 						struct cachefiles_volume *volume)
36233d21f06SJia Zhu {
36333d21f06SJia Zhu 	return 0;
36433d21f06SJia Zhu }
cachefiles_ondemand_deinit_obj_info(struct cachefiles_object * obj)36533d21f06SJia Zhu static inline void cachefiles_ondemand_deinit_obj_info(struct cachefiles_object *obj)
36633d21f06SJia Zhu {
36733d21f06SJia Zhu }
368c8383054SJeffle Xu #endif
369c8383054SJeffle Xu 
370c8383054SJeffle Xu /*
371254947d4SDavid Howells  * security.c
372254947d4SDavid Howells  */
373254947d4SDavid Howells extern int cachefiles_get_security_ID(struct cachefiles_cache *cache);
374254947d4SDavid Howells extern int cachefiles_determine_cache_security(struct cachefiles_cache *cache,
375254947d4SDavid Howells 					       struct dentry *root,
376254947d4SDavid Howells 					       const struct cred **_saved_cred);
377254947d4SDavid Howells 
cachefiles_begin_secure(struct cachefiles_cache * cache,const struct cred ** _saved_cred)378254947d4SDavid Howells static inline void cachefiles_begin_secure(struct cachefiles_cache *cache,
379254947d4SDavid Howells 					   const struct cred **_saved_cred)
380254947d4SDavid Howells {
381254947d4SDavid Howells 	*_saved_cred = override_creds(cache->cache_cred);
382254947d4SDavid Howells }
383254947d4SDavid Howells 
cachefiles_end_secure(struct cachefiles_cache * cache,const struct cred * saved_cred)384254947d4SDavid Howells static inline void cachefiles_end_secure(struct cachefiles_cache *cache,
385254947d4SDavid Howells 					 const struct cred *saved_cred)
386254947d4SDavid Howells {
387254947d4SDavid Howells 	revert_creds(saved_cred);
388254947d4SDavid Howells }
389254947d4SDavid Howells 
390254947d4SDavid Howells /*
391fe2140e2SDavid Howells  * volume.c
392fe2140e2SDavid Howells  */
393fe2140e2SDavid Howells void cachefiles_acquire_volume(struct fscache_volume *volume);
394fe2140e2SDavid Howells void cachefiles_free_volume(struct fscache_volume *volume);
395fe2140e2SDavid Howells void cachefiles_withdraw_volume(struct cachefiles_volume *volume);
396fe2140e2SDavid Howells 
397fe2140e2SDavid Howells /*
39872b95785SDavid Howells  * xattr.c
39972b95785SDavid Howells  */
40072b95785SDavid Howells extern int cachefiles_set_object_xattr(struct cachefiles_object *object);
40172b95785SDavid Howells extern int cachefiles_check_auxdata(struct cachefiles_object *object,
40272b95785SDavid Howells 				    struct file *file);
40372b95785SDavid Howells extern int cachefiles_remove_object_xattr(struct cachefiles_cache *cache,
40472b95785SDavid Howells 					  struct cachefiles_object *object,
40572b95785SDavid Howells 					  struct dentry *dentry);
40672b95785SDavid Howells extern void cachefiles_prepare_to_write(struct fscache_cookie *cookie);
40732e15003SDavid Howells extern bool cachefiles_set_volume_xattr(struct cachefiles_volume *volume);
40832e15003SDavid Howells extern int cachefiles_check_volume_xattr(struct cachefiles_volume *volume);
40972b95785SDavid Howells 
41072b95785SDavid Howells /*
4111493bf74SDavid Howells  * Error handling
4121493bf74SDavid Howells  */
4131493bf74SDavid Howells #define cachefiles_io_error(___cache, FMT, ...)		\
4141493bf74SDavid Howells do {							\
4151493bf74SDavid Howells 	pr_err("I/O Error: " FMT"\n", ##__VA_ARGS__);	\
4161493bf74SDavid Howells 	fscache_io_error((___cache)->cache);		\
4171493bf74SDavid Howells 	set_bit(CACHEFILES_DEAD, &(___cache)->flags);	\
4183bf0b803SBaokun Li 	if (cachefiles_in_ondemand_mode(___cache))	\
4193bf0b803SBaokun Li 		cachefiles_flush_reqs(___cache);	\
4201493bf74SDavid Howells } while (0)
4211493bf74SDavid Howells 
42272b95785SDavid Howells #define cachefiles_io_error_obj(object, FMT, ...)			\
42372b95785SDavid Howells do {									\
42472b95785SDavid Howells 	struct cachefiles_cache *___cache;				\
42572b95785SDavid Howells 									\
42672b95785SDavid Howells 	___cache = (object)->volume->cache;				\
42772b95785SDavid Howells 	cachefiles_io_error(___cache, FMT " [o=%08x]", ##__VA_ARGS__,	\
42872b95785SDavid Howells 			    (object)->debug_id);			\
42972b95785SDavid Howells } while (0)
43072b95785SDavid Howells 
431a70f6526SDavid Howells 
432a70f6526SDavid Howells /*
433a70f6526SDavid Howells  * Debug tracing
43477443f61SDavid Howells  */
43577443f61SDavid Howells extern unsigned cachefiles_debug;
43677443f61SDavid Howells #define CACHEFILES_DEBUG_KENTER	1
43777443f61SDavid Howells #define CACHEFILES_DEBUG_KLEAVE	2
43877443f61SDavid Howells #define CACHEFILES_DEBUG_KDEBUG	4
43977443f61SDavid Howells 
44077443f61SDavid Howells #define dbgprintk(FMT, ...) \
44177443f61SDavid Howells 	printk(KERN_DEBUG "[%-6.6s] "FMT"\n", current->comm, ##__VA_ARGS__)
44277443f61SDavid Howells 
44377443f61SDavid Howells #define kenter(FMT, ...) dbgprintk("==> %s("FMT")", __func__, ##__VA_ARGS__)
44477443f61SDavid Howells #define kleave(FMT, ...) dbgprintk("<== %s()"FMT"", __func__, ##__VA_ARGS__)
44577443f61SDavid Howells #define kdebug(FMT, ...) dbgprintk(FMT, ##__VA_ARGS__)
44677443f61SDavid Howells 
44777443f61SDavid Howells 
44877443f61SDavid Howells #if defined(__KDEBUG)
44977443f61SDavid Howells #define _enter(FMT, ...) kenter(FMT, ##__VA_ARGS__)
45077443f61SDavid Howells #define _leave(FMT, ...) kleave(FMT, ##__VA_ARGS__)
45177443f61SDavid Howells #define _debug(FMT, ...) kdebug(FMT, ##__VA_ARGS__)
45277443f61SDavid Howells 
45377443f61SDavid Howells #elif defined(CONFIG_CACHEFILES_DEBUG)
45477443f61SDavid Howells #define _enter(FMT, ...)				\
45577443f61SDavid Howells do {							\
45677443f61SDavid Howells 	if (cachefiles_debug & CACHEFILES_DEBUG_KENTER)	\
45777443f61SDavid Howells 		kenter(FMT, ##__VA_ARGS__);		\
45877443f61SDavid Howells } while (0)
45977443f61SDavid Howells 
46077443f61SDavid Howells #define _leave(FMT, ...)				\
46177443f61SDavid Howells do {							\
46277443f61SDavid Howells 	if (cachefiles_debug & CACHEFILES_DEBUG_KLEAVE)	\
46377443f61SDavid Howells 		kleave(FMT, ##__VA_ARGS__);		\
46477443f61SDavid Howells } while (0)
46577443f61SDavid Howells 
46677443f61SDavid Howells #define _debug(FMT, ...)				\
46777443f61SDavid Howells do {							\
46877443f61SDavid Howells 	if (cachefiles_debug & CACHEFILES_DEBUG_KDEBUG)	\
46977443f61SDavid Howells 		kdebug(FMT, ##__VA_ARGS__);		\
47077443f61SDavid Howells } while (0)
47177443f61SDavid Howells 
47277443f61SDavid Howells #else
47377443f61SDavid Howells #define _enter(FMT, ...) no_printk("==> %s("FMT")", __func__, ##__VA_ARGS__)
47477443f61SDavid Howells #define _leave(FMT, ...) no_printk("<== %s()"FMT"", __func__, ##__VA_ARGS__)
47577443f61SDavid Howells #define _debug(FMT, ...) no_printk(FMT, ##__VA_ARGS__)
47677443f61SDavid Howells #endif
47777443f61SDavid Howells 
47877443f61SDavid Howells #if 1 /* defined(__KDEBUGALL) */
47977443f61SDavid Howells 
48077443f61SDavid Howells #define ASSERT(X)							\
48177443f61SDavid Howells do {									\
48277443f61SDavid Howells 	if (unlikely(!(X))) {						\
48377443f61SDavid Howells 		pr_err("\n");						\
48477443f61SDavid Howells 		pr_err("Assertion failed\n");		\
48577443f61SDavid Howells 		BUG();							\
48677443f61SDavid Howells 	}								\
48777443f61SDavid Howells } while (0)
48877443f61SDavid Howells 
48977443f61SDavid Howells #define ASSERTCMP(X, OP, Y)						\
49077443f61SDavid Howells do {									\
49177443f61SDavid Howells 	if (unlikely(!((X) OP (Y)))) {					\
49277443f61SDavid Howells 		pr_err("\n");						\
49377443f61SDavid Howells 		pr_err("Assertion failed\n");		\
49477443f61SDavid Howells 		pr_err("%lx " #OP " %lx is false\n",			\
49577443f61SDavid Howells 		       (unsigned long)(X), (unsigned long)(Y));		\
49677443f61SDavid Howells 		BUG();							\
49777443f61SDavid Howells 	}								\
49877443f61SDavid Howells } while (0)
49977443f61SDavid Howells 
50077443f61SDavid Howells #define ASSERTIF(C, X)							\
50177443f61SDavid Howells do {									\
50277443f61SDavid Howells 	if (unlikely((C) && !(X))) {					\
50377443f61SDavid Howells 		pr_err("\n");						\
50477443f61SDavid Howells 		pr_err("Assertion failed\n");		\
50577443f61SDavid Howells 		BUG();							\
50677443f61SDavid Howells 	}								\
50777443f61SDavid Howells } while (0)
50877443f61SDavid Howells 
50977443f61SDavid Howells #define ASSERTIFCMP(C, X, OP, Y)					\
51077443f61SDavid Howells do {									\
51177443f61SDavid Howells 	if (unlikely((C) && !((X) OP (Y)))) {				\
51277443f61SDavid Howells 		pr_err("\n");						\
51377443f61SDavid Howells 		pr_err("Assertion failed\n");		\
51477443f61SDavid Howells 		pr_err("%lx " #OP " %lx is false\n",			\
51577443f61SDavid Howells 		       (unsigned long)(X), (unsigned long)(Y));		\
51677443f61SDavid Howells 		BUG();							\
51777443f61SDavid Howells 	}								\
51877443f61SDavid Howells } while (0)
51977443f61SDavid Howells 
52077443f61SDavid Howells #else
52177443f61SDavid Howells 
52277443f61SDavid Howells #define ASSERT(X)			do {} while (0)
52377443f61SDavid Howells #define ASSERTCMP(X, OP, Y)		do {} while (0)
52477443f61SDavid Howells #define ASSERTIF(C, X)			do {} while (0)
52577443f61SDavid Howells #define ASSERTIFCMP(C, X, OP, Y)	do {} while (0)
52677443f61SDavid Howells 
52777443f61SDavid Howells #endif
528