xref: /openbmc/linux/fs/cachefiles/internal.h (revision d11b0b04)
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 
23 struct cachefiles_cache;
24 struct cachefiles_object;
25 
26 enum 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  */
39 struct 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 
47 /*
48  * Backing file state.
49  */
50 struct cachefiles_object {
51 	struct fscache_cookie		*cookie;	/* Netfs data storage object cookie */
52 	struct cachefiles_volume	*volume;	/* Cache volume that holds this object */
53 	struct list_head		cache_link;	/* Link in cache->*_list */
54 	struct file			*file;		/* The file representing this object */
55 	char				*d_name;	/* Backing file name */
56 	int				debug_id;
57 	spinlock_t			lock;
58 	refcount_t			ref;
59 	u8				d_name_len;	/* Length of filename */
60 	enum cachefiles_content		content_info:8;	/* Info about content presence */
61 	unsigned long			flags;
62 #define CACHEFILES_OBJECT_USING_TMPFILE	0		/* Have an unlinked tmpfile */
63 #ifdef CONFIG_CACHEFILES_ONDEMAND
64 	int				ondemand_id;
65 #endif
66 };
67 
68 #define CACHEFILES_ONDEMAND_ID_CLOSED	-1
69 
70 /*
71  * Cache files cache definition
72  */
73 struct cachefiles_cache {
74 	struct fscache_cache		*cache;		/* Cache cookie */
75 	struct vfsmount			*mnt;		/* mountpoint holding the cache */
76 	struct dentry			*store;		/* Directory into which live objects go */
77 	struct dentry			*graveyard;	/* directory into which dead objects go */
78 	struct file			*cachefilesd;	/* manager daemon handle */
79 	struct list_head		volumes;	/* List of volume objects */
80 	struct list_head		object_list;	/* List of active objects */
81 	spinlock_t			object_list_lock; /* Lock for volumes and object_list */
82 	const struct cred		*cache_cred;	/* security override for accessing cache */
83 	struct mutex			daemon_mutex;	/* command serialisation mutex */
84 	wait_queue_head_t		daemon_pollwq;	/* poll waitqueue for daemon */
85 	atomic_t			gravecounter;	/* graveyard uniquifier */
86 	atomic_t			f_released;	/* number of objects released lately */
87 	atomic_long_t			b_released;	/* number of blocks released lately */
88 	atomic_long_t			b_writing;	/* Number of blocks being written */
89 	unsigned			frun_percent;	/* when to stop culling (% files) */
90 	unsigned			fcull_percent;	/* when to start culling (% files) */
91 	unsigned			fstop_percent;	/* when to stop allocating (% files) */
92 	unsigned			brun_percent;	/* when to stop culling (% blocks) */
93 	unsigned			bcull_percent;	/* when to start culling (% blocks) */
94 	unsigned			bstop_percent;	/* when to stop allocating (% blocks) */
95 	unsigned			bsize;		/* cache's block size */
96 	unsigned			bshift;		/* ilog2(bsize) */
97 	uint64_t			frun;		/* when to stop culling */
98 	uint64_t			fcull;		/* when to start culling */
99 	uint64_t			fstop;		/* when to stop allocating */
100 	sector_t			brun;		/* when to stop culling */
101 	sector_t			bcull;		/* when to start culling */
102 	sector_t			bstop;		/* when to stop allocating */
103 	unsigned long			flags;
104 #define CACHEFILES_READY		0	/* T if cache prepared */
105 #define CACHEFILES_DEAD			1	/* T if cache dead */
106 #define CACHEFILES_CULLING		2	/* T if cull engaged */
107 #define CACHEFILES_STATE_CHANGED	3	/* T if state changed (poll trigger) */
108 #define CACHEFILES_ONDEMAND_MODE	4	/* T if in on-demand read mode */
109 	char				*rootdirname;	/* name of cache root directory */
110 	char				*secctx;	/* LSM security context */
111 	char				*tag;		/* cache binding tag */
112 	refcount_t			unbind_pincount;/* refcount to do daemon unbind */
113 	struct xarray			reqs;		/* xarray of pending on-demand requests */
114 	struct xarray			ondemand_ids;	/* xarray for ondemand_id allocation */
115 	u32				ondemand_id_next;
116 };
117 
118 static inline bool cachefiles_in_ondemand_mode(struct cachefiles_cache *cache)
119 {
120 	return IS_ENABLED(CONFIG_CACHEFILES_ONDEMAND) &&
121 		test_bit(CACHEFILES_ONDEMAND_MODE, &cache->flags);
122 }
123 
124 struct cachefiles_req {
125 	struct cachefiles_object *object;
126 	struct completion done;
127 	int error;
128 	struct cachefiles_msg msg;
129 };
130 
131 #define CACHEFILES_REQ_NEW	XA_MARK_1
132 
133 #include <trace/events/cachefiles.h>
134 
135 static inline
136 struct file *cachefiles_cres_file(struct netfs_cache_resources *cres)
137 {
138 	return cres->cache_priv2;
139 }
140 
141 static inline
142 struct cachefiles_object *cachefiles_cres_object(struct netfs_cache_resources *cres)
143 {
144 	return fscache_cres_cookie(cres)->cache_priv;
145 }
146 
147 /*
148  * note change of state for daemon
149  */
150 static inline void cachefiles_state_changed(struct cachefiles_cache *cache)
151 {
152 	set_bit(CACHEFILES_STATE_CHANGED, &cache->flags);
153 	wake_up_all(&cache->daemon_pollwq);
154 }
155 
156 /*
157  * cache.c
158  */
159 extern int cachefiles_add_cache(struct cachefiles_cache *cache);
160 extern void cachefiles_withdraw_cache(struct cachefiles_cache *cache);
161 
162 enum cachefiles_has_space_for {
163 	cachefiles_has_space_check,
164 	cachefiles_has_space_for_write,
165 	cachefiles_has_space_for_create,
166 };
167 extern int cachefiles_has_space(struct cachefiles_cache *cache,
168 				unsigned fnr, unsigned bnr,
169 				enum cachefiles_has_space_for reason);
170 
171 /*
172  * daemon.c
173  */
174 extern const struct file_operations cachefiles_daemon_fops;
175 extern void cachefiles_get_unbind_pincount(struct cachefiles_cache *cache);
176 extern void cachefiles_put_unbind_pincount(struct cachefiles_cache *cache);
177 
178 /*
179  * error_inject.c
180  */
181 #ifdef CONFIG_CACHEFILES_ERROR_INJECTION
182 extern unsigned int cachefiles_error_injection_state;
183 extern int cachefiles_register_error_injection(void);
184 extern void cachefiles_unregister_error_injection(void);
185 
186 #else
187 #define cachefiles_error_injection_state 0
188 
189 static inline int cachefiles_register_error_injection(void)
190 {
191 	return 0;
192 }
193 
194 static inline void cachefiles_unregister_error_injection(void)
195 {
196 }
197 #endif
198 
199 
200 static inline int cachefiles_inject_read_error(void)
201 {
202 	return cachefiles_error_injection_state & 2 ? -EIO : 0;
203 }
204 
205 static inline int cachefiles_inject_write_error(void)
206 {
207 	return cachefiles_error_injection_state & 2 ? -EIO :
208 		cachefiles_error_injection_state & 1 ? -ENOSPC :
209 		0;
210 }
211 
212 static inline int cachefiles_inject_remove_error(void)
213 {
214 	return cachefiles_error_injection_state & 2 ? -EIO : 0;
215 }
216 
217 /*
218  * interface.c
219  */
220 extern const struct fscache_cache_ops cachefiles_cache_ops;
221 extern void cachefiles_see_object(struct cachefiles_object *object,
222 				  enum cachefiles_obj_ref_trace why);
223 extern struct cachefiles_object *cachefiles_grab_object(struct cachefiles_object *object,
224 							enum cachefiles_obj_ref_trace why);
225 extern void cachefiles_put_object(struct cachefiles_object *object,
226 				  enum cachefiles_obj_ref_trace why);
227 
228 /*
229  * io.c
230  */
231 extern bool cachefiles_begin_operation(struct netfs_cache_resources *cres,
232 				       enum fscache_want_state want_state);
233 extern int __cachefiles_prepare_write(struct cachefiles_object *object,
234 				      struct file *file,
235 				      loff_t *_start, size_t *_len,
236 				      bool no_space_allocated_yet);
237 extern int __cachefiles_write(struct cachefiles_object *object,
238 			      struct file *file,
239 			      loff_t start_pos,
240 			      struct iov_iter *iter,
241 			      netfs_io_terminated_t term_func,
242 			      void *term_func_priv);
243 
244 /*
245  * key.c
246  */
247 extern bool cachefiles_cook_key(struct cachefiles_object *object);
248 
249 /*
250  * main.c
251  */
252 extern struct kmem_cache *cachefiles_object_jar;
253 
254 /*
255  * namei.c
256  */
257 extern void cachefiles_unmark_inode_in_use(struct cachefiles_object *object,
258 					   struct file *file);
259 extern int cachefiles_bury_object(struct cachefiles_cache *cache,
260 				  struct cachefiles_object *object,
261 				  struct dentry *dir,
262 				  struct dentry *rep,
263 				  enum fscache_why_object_killed why);
264 extern int cachefiles_delete_object(struct cachefiles_object *object,
265 				    enum fscache_why_object_killed why);
266 extern bool cachefiles_look_up_object(struct cachefiles_object *object);
267 extern struct dentry *cachefiles_get_directory(struct cachefiles_cache *cache,
268 					       struct dentry *dir,
269 					       const char *name,
270 					       bool *_is_new);
271 extern void cachefiles_put_directory(struct dentry *dir);
272 
273 extern int cachefiles_cull(struct cachefiles_cache *cache, struct dentry *dir,
274 			   char *filename);
275 
276 extern int cachefiles_check_in_use(struct cachefiles_cache *cache,
277 				   struct dentry *dir, char *filename);
278 extern struct file *cachefiles_create_tmpfile(struct cachefiles_object *object);
279 extern bool cachefiles_commit_tmpfile(struct cachefiles_cache *cache,
280 				      struct cachefiles_object *object);
281 
282 /*
283  * ondemand.c
284  */
285 #ifdef CONFIG_CACHEFILES_ONDEMAND
286 extern ssize_t cachefiles_ondemand_daemon_read(struct cachefiles_cache *cache,
287 					char __user *_buffer, size_t buflen);
288 
289 extern int cachefiles_ondemand_copen(struct cachefiles_cache *cache,
290 				     char *args);
291 
292 extern int cachefiles_ondemand_init_object(struct cachefiles_object *object);
293 
294 #else
295 static inline ssize_t cachefiles_ondemand_daemon_read(struct cachefiles_cache *cache,
296 					char __user *_buffer, size_t buflen)
297 {
298 	return -EOPNOTSUPP;
299 }
300 
301 static inline int cachefiles_ondemand_init_object(struct cachefiles_object *object)
302 {
303 	return 0;
304 }
305 #endif
306 
307 /*
308  * security.c
309  */
310 extern int cachefiles_get_security_ID(struct cachefiles_cache *cache);
311 extern int cachefiles_determine_cache_security(struct cachefiles_cache *cache,
312 					       struct dentry *root,
313 					       const struct cred **_saved_cred);
314 
315 static inline void cachefiles_begin_secure(struct cachefiles_cache *cache,
316 					   const struct cred **_saved_cred)
317 {
318 	*_saved_cred = override_creds(cache->cache_cred);
319 }
320 
321 static inline void cachefiles_end_secure(struct cachefiles_cache *cache,
322 					 const struct cred *saved_cred)
323 {
324 	revert_creds(saved_cred);
325 }
326 
327 /*
328  * volume.c
329  */
330 void cachefiles_acquire_volume(struct fscache_volume *volume);
331 void cachefiles_free_volume(struct fscache_volume *volume);
332 void cachefiles_withdraw_volume(struct cachefiles_volume *volume);
333 
334 /*
335  * xattr.c
336  */
337 extern int cachefiles_set_object_xattr(struct cachefiles_object *object);
338 extern int cachefiles_check_auxdata(struct cachefiles_object *object,
339 				    struct file *file);
340 extern int cachefiles_remove_object_xattr(struct cachefiles_cache *cache,
341 					  struct cachefiles_object *object,
342 					  struct dentry *dentry);
343 extern void cachefiles_prepare_to_write(struct fscache_cookie *cookie);
344 extern bool cachefiles_set_volume_xattr(struct cachefiles_volume *volume);
345 extern int cachefiles_check_volume_xattr(struct cachefiles_volume *volume);
346 
347 /*
348  * Error handling
349  */
350 #define cachefiles_io_error(___cache, FMT, ...)		\
351 do {							\
352 	pr_err("I/O Error: " FMT"\n", ##__VA_ARGS__);	\
353 	fscache_io_error((___cache)->cache);		\
354 	set_bit(CACHEFILES_DEAD, &(___cache)->flags);	\
355 } while (0)
356 
357 #define cachefiles_io_error_obj(object, FMT, ...)			\
358 do {									\
359 	struct cachefiles_cache *___cache;				\
360 									\
361 	___cache = (object)->volume->cache;				\
362 	cachefiles_io_error(___cache, FMT " [o=%08x]", ##__VA_ARGS__,	\
363 			    (object)->debug_id);			\
364 } while (0)
365 
366 
367 /*
368  * Debug tracing
369  */
370 extern unsigned cachefiles_debug;
371 #define CACHEFILES_DEBUG_KENTER	1
372 #define CACHEFILES_DEBUG_KLEAVE	2
373 #define CACHEFILES_DEBUG_KDEBUG	4
374 
375 #define dbgprintk(FMT, ...) \
376 	printk(KERN_DEBUG "[%-6.6s] "FMT"\n", current->comm, ##__VA_ARGS__)
377 
378 #define kenter(FMT, ...) dbgprintk("==> %s("FMT")", __func__, ##__VA_ARGS__)
379 #define kleave(FMT, ...) dbgprintk("<== %s()"FMT"", __func__, ##__VA_ARGS__)
380 #define kdebug(FMT, ...) dbgprintk(FMT, ##__VA_ARGS__)
381 
382 
383 #if defined(__KDEBUG)
384 #define _enter(FMT, ...) kenter(FMT, ##__VA_ARGS__)
385 #define _leave(FMT, ...) kleave(FMT, ##__VA_ARGS__)
386 #define _debug(FMT, ...) kdebug(FMT, ##__VA_ARGS__)
387 
388 #elif defined(CONFIG_CACHEFILES_DEBUG)
389 #define _enter(FMT, ...)				\
390 do {							\
391 	if (cachefiles_debug & CACHEFILES_DEBUG_KENTER)	\
392 		kenter(FMT, ##__VA_ARGS__);		\
393 } while (0)
394 
395 #define _leave(FMT, ...)				\
396 do {							\
397 	if (cachefiles_debug & CACHEFILES_DEBUG_KLEAVE)	\
398 		kleave(FMT, ##__VA_ARGS__);		\
399 } while (0)
400 
401 #define _debug(FMT, ...)				\
402 do {							\
403 	if (cachefiles_debug & CACHEFILES_DEBUG_KDEBUG)	\
404 		kdebug(FMT, ##__VA_ARGS__);		\
405 } while (0)
406 
407 #else
408 #define _enter(FMT, ...) no_printk("==> %s("FMT")", __func__, ##__VA_ARGS__)
409 #define _leave(FMT, ...) no_printk("<== %s()"FMT"", __func__, ##__VA_ARGS__)
410 #define _debug(FMT, ...) no_printk(FMT, ##__VA_ARGS__)
411 #endif
412 
413 #if 1 /* defined(__KDEBUGALL) */
414 
415 #define ASSERT(X)							\
416 do {									\
417 	if (unlikely(!(X))) {						\
418 		pr_err("\n");						\
419 		pr_err("Assertion failed\n");		\
420 		BUG();							\
421 	}								\
422 } while (0)
423 
424 #define ASSERTCMP(X, OP, Y)						\
425 do {									\
426 	if (unlikely(!((X) OP (Y)))) {					\
427 		pr_err("\n");						\
428 		pr_err("Assertion failed\n");		\
429 		pr_err("%lx " #OP " %lx is false\n",			\
430 		       (unsigned long)(X), (unsigned long)(Y));		\
431 		BUG();							\
432 	}								\
433 } while (0)
434 
435 #define ASSERTIF(C, X)							\
436 do {									\
437 	if (unlikely((C) && !(X))) {					\
438 		pr_err("\n");						\
439 		pr_err("Assertion failed\n");		\
440 		BUG();							\
441 	}								\
442 } while (0)
443 
444 #define ASSERTIFCMP(C, X, OP, Y)					\
445 do {									\
446 	if (unlikely((C) && !((X) OP (Y)))) {				\
447 		pr_err("\n");						\
448 		pr_err("Assertion failed\n");		\
449 		pr_err("%lx " #OP " %lx is false\n",			\
450 		       (unsigned long)(X), (unsigned long)(Y));		\
451 		BUG();							\
452 	}								\
453 } while (0)
454 
455 #else
456 
457 #define ASSERT(X)			do {} while (0)
458 #define ASSERTCMP(X, OP, Y)		do {} while (0)
459 #define ASSERTIF(C, X)			do {} while (0)
460 #define ASSERTIFCMP(C, X, OP, Y)	do {} while (0)
461 
462 #endif
463