xref: /openbmc/linux/fs/cachefiles/internal.h (revision e2ad626f)
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 	unsigned long			req_id_next;
115 	struct xarray			ondemand_ids;	/* xarray for ondemand_id allocation */
116 	u32				ondemand_id_next;
117 };
118 
119 static inline bool cachefiles_in_ondemand_mode(struct cachefiles_cache *cache)
120 {
121 	return IS_ENABLED(CONFIG_CACHEFILES_ONDEMAND) &&
122 		test_bit(CACHEFILES_ONDEMAND_MODE, &cache->flags);
123 }
124 
125 struct cachefiles_req {
126 	struct cachefiles_object *object;
127 	struct completion done;
128 	int error;
129 	struct cachefiles_msg msg;
130 };
131 
132 #define CACHEFILES_REQ_NEW	XA_MARK_1
133 
134 #include <trace/events/cachefiles.h>
135 
136 static inline
137 struct file *cachefiles_cres_file(struct netfs_cache_resources *cres)
138 {
139 	return cres->cache_priv2;
140 }
141 
142 static inline
143 struct cachefiles_object *cachefiles_cres_object(struct netfs_cache_resources *cres)
144 {
145 	return fscache_cres_cookie(cres)->cache_priv;
146 }
147 
148 /*
149  * note change of state for daemon
150  */
151 static inline void cachefiles_state_changed(struct cachefiles_cache *cache)
152 {
153 	set_bit(CACHEFILES_STATE_CHANGED, &cache->flags);
154 	wake_up_all(&cache->daemon_pollwq);
155 }
156 
157 /*
158  * cache.c
159  */
160 extern int cachefiles_add_cache(struct cachefiles_cache *cache);
161 extern void cachefiles_withdraw_cache(struct cachefiles_cache *cache);
162 
163 enum cachefiles_has_space_for {
164 	cachefiles_has_space_check,
165 	cachefiles_has_space_for_write,
166 	cachefiles_has_space_for_create,
167 };
168 extern int cachefiles_has_space(struct cachefiles_cache *cache,
169 				unsigned fnr, unsigned bnr,
170 				enum cachefiles_has_space_for reason);
171 
172 /*
173  * daemon.c
174  */
175 extern const struct file_operations cachefiles_daemon_fops;
176 extern void cachefiles_get_unbind_pincount(struct cachefiles_cache *cache);
177 extern void cachefiles_put_unbind_pincount(struct cachefiles_cache *cache);
178 
179 /*
180  * error_inject.c
181  */
182 #ifdef CONFIG_CACHEFILES_ERROR_INJECTION
183 extern unsigned int cachefiles_error_injection_state;
184 extern int cachefiles_register_error_injection(void);
185 extern void cachefiles_unregister_error_injection(void);
186 
187 #else
188 #define cachefiles_error_injection_state 0
189 
190 static inline int cachefiles_register_error_injection(void)
191 {
192 	return 0;
193 }
194 
195 static inline void cachefiles_unregister_error_injection(void)
196 {
197 }
198 #endif
199 
200 
201 static inline int cachefiles_inject_read_error(void)
202 {
203 	return cachefiles_error_injection_state & 2 ? -EIO : 0;
204 }
205 
206 static inline int cachefiles_inject_write_error(void)
207 {
208 	return cachefiles_error_injection_state & 2 ? -EIO :
209 		cachefiles_error_injection_state & 1 ? -ENOSPC :
210 		0;
211 }
212 
213 static inline int cachefiles_inject_remove_error(void)
214 {
215 	return cachefiles_error_injection_state & 2 ? -EIO : 0;
216 }
217 
218 /*
219  * interface.c
220  */
221 extern const struct fscache_cache_ops cachefiles_cache_ops;
222 extern void cachefiles_see_object(struct cachefiles_object *object,
223 				  enum cachefiles_obj_ref_trace why);
224 extern struct cachefiles_object *cachefiles_grab_object(struct cachefiles_object *object,
225 							enum cachefiles_obj_ref_trace why);
226 extern void cachefiles_put_object(struct cachefiles_object *object,
227 				  enum cachefiles_obj_ref_trace why);
228 
229 /*
230  * io.c
231  */
232 extern bool cachefiles_begin_operation(struct netfs_cache_resources *cres,
233 				       enum fscache_want_state want_state);
234 extern int __cachefiles_prepare_write(struct cachefiles_object *object,
235 				      struct file *file,
236 				      loff_t *_start, size_t *_len,
237 				      bool no_space_allocated_yet);
238 extern int __cachefiles_write(struct cachefiles_object *object,
239 			      struct file *file,
240 			      loff_t start_pos,
241 			      struct iov_iter *iter,
242 			      netfs_io_terminated_t term_func,
243 			      void *term_func_priv);
244 
245 /*
246  * key.c
247  */
248 extern bool cachefiles_cook_key(struct cachefiles_object *object);
249 
250 /*
251  * main.c
252  */
253 extern struct kmem_cache *cachefiles_object_jar;
254 
255 /*
256  * namei.c
257  */
258 extern void cachefiles_unmark_inode_in_use(struct cachefiles_object *object,
259 					   struct file *file);
260 extern int cachefiles_bury_object(struct cachefiles_cache *cache,
261 				  struct cachefiles_object *object,
262 				  struct dentry *dir,
263 				  struct dentry *rep,
264 				  enum fscache_why_object_killed why);
265 extern int cachefiles_delete_object(struct cachefiles_object *object,
266 				    enum fscache_why_object_killed why);
267 extern bool cachefiles_look_up_object(struct cachefiles_object *object);
268 extern struct dentry *cachefiles_get_directory(struct cachefiles_cache *cache,
269 					       struct dentry *dir,
270 					       const char *name,
271 					       bool *_is_new);
272 extern void cachefiles_put_directory(struct dentry *dir);
273 
274 extern int cachefiles_cull(struct cachefiles_cache *cache, struct dentry *dir,
275 			   char *filename);
276 
277 extern int cachefiles_check_in_use(struct cachefiles_cache *cache,
278 				   struct dentry *dir, char *filename);
279 extern struct file *cachefiles_create_tmpfile(struct cachefiles_object *object);
280 extern bool cachefiles_commit_tmpfile(struct cachefiles_cache *cache,
281 				      struct cachefiles_object *object);
282 
283 /*
284  * ondemand.c
285  */
286 #ifdef CONFIG_CACHEFILES_ONDEMAND
287 extern ssize_t cachefiles_ondemand_daemon_read(struct cachefiles_cache *cache,
288 					char __user *_buffer, size_t buflen);
289 
290 extern int cachefiles_ondemand_copen(struct cachefiles_cache *cache,
291 				     char *args);
292 
293 extern int cachefiles_ondemand_init_object(struct cachefiles_object *object);
294 extern void cachefiles_ondemand_clean_object(struct cachefiles_object *object);
295 
296 extern int cachefiles_ondemand_read(struct cachefiles_object *object,
297 				    loff_t pos, size_t len);
298 
299 #else
300 static inline ssize_t cachefiles_ondemand_daemon_read(struct cachefiles_cache *cache,
301 					char __user *_buffer, size_t buflen)
302 {
303 	return -EOPNOTSUPP;
304 }
305 
306 static inline int cachefiles_ondemand_init_object(struct cachefiles_object *object)
307 {
308 	return 0;
309 }
310 
311 static inline void cachefiles_ondemand_clean_object(struct cachefiles_object *object)
312 {
313 }
314 
315 static inline int cachefiles_ondemand_read(struct cachefiles_object *object,
316 					   loff_t pos, size_t len)
317 {
318 	return -EOPNOTSUPP;
319 }
320 #endif
321 
322 /*
323  * security.c
324  */
325 extern int cachefiles_get_security_ID(struct cachefiles_cache *cache);
326 extern int cachefiles_determine_cache_security(struct cachefiles_cache *cache,
327 					       struct dentry *root,
328 					       const struct cred **_saved_cred);
329 
330 static inline void cachefiles_begin_secure(struct cachefiles_cache *cache,
331 					   const struct cred **_saved_cred)
332 {
333 	*_saved_cred = override_creds(cache->cache_cred);
334 }
335 
336 static inline void cachefiles_end_secure(struct cachefiles_cache *cache,
337 					 const struct cred *saved_cred)
338 {
339 	revert_creds(saved_cred);
340 }
341 
342 /*
343  * volume.c
344  */
345 void cachefiles_acquire_volume(struct fscache_volume *volume);
346 void cachefiles_free_volume(struct fscache_volume *volume);
347 void cachefiles_withdraw_volume(struct cachefiles_volume *volume);
348 
349 /*
350  * xattr.c
351  */
352 extern int cachefiles_set_object_xattr(struct cachefiles_object *object);
353 extern int cachefiles_check_auxdata(struct cachefiles_object *object,
354 				    struct file *file);
355 extern int cachefiles_remove_object_xattr(struct cachefiles_cache *cache,
356 					  struct cachefiles_object *object,
357 					  struct dentry *dentry);
358 extern void cachefiles_prepare_to_write(struct fscache_cookie *cookie);
359 extern bool cachefiles_set_volume_xattr(struct cachefiles_volume *volume);
360 extern int cachefiles_check_volume_xattr(struct cachefiles_volume *volume);
361 
362 /*
363  * Error handling
364  */
365 #define cachefiles_io_error(___cache, FMT, ...)		\
366 do {							\
367 	pr_err("I/O Error: " FMT"\n", ##__VA_ARGS__);	\
368 	fscache_io_error((___cache)->cache);		\
369 	set_bit(CACHEFILES_DEAD, &(___cache)->flags);	\
370 } while (0)
371 
372 #define cachefiles_io_error_obj(object, FMT, ...)			\
373 do {									\
374 	struct cachefiles_cache *___cache;				\
375 									\
376 	___cache = (object)->volume->cache;				\
377 	cachefiles_io_error(___cache, FMT " [o=%08x]", ##__VA_ARGS__,	\
378 			    (object)->debug_id);			\
379 } while (0)
380 
381 
382 /*
383  * Debug tracing
384  */
385 extern unsigned cachefiles_debug;
386 #define CACHEFILES_DEBUG_KENTER	1
387 #define CACHEFILES_DEBUG_KLEAVE	2
388 #define CACHEFILES_DEBUG_KDEBUG	4
389 
390 #define dbgprintk(FMT, ...) \
391 	printk(KERN_DEBUG "[%-6.6s] "FMT"\n", current->comm, ##__VA_ARGS__)
392 
393 #define kenter(FMT, ...) dbgprintk("==> %s("FMT")", __func__, ##__VA_ARGS__)
394 #define kleave(FMT, ...) dbgprintk("<== %s()"FMT"", __func__, ##__VA_ARGS__)
395 #define kdebug(FMT, ...) dbgprintk(FMT, ##__VA_ARGS__)
396 
397 
398 #if defined(__KDEBUG)
399 #define _enter(FMT, ...) kenter(FMT, ##__VA_ARGS__)
400 #define _leave(FMT, ...) kleave(FMT, ##__VA_ARGS__)
401 #define _debug(FMT, ...) kdebug(FMT, ##__VA_ARGS__)
402 
403 #elif defined(CONFIG_CACHEFILES_DEBUG)
404 #define _enter(FMT, ...)				\
405 do {							\
406 	if (cachefiles_debug & CACHEFILES_DEBUG_KENTER)	\
407 		kenter(FMT, ##__VA_ARGS__);		\
408 } while (0)
409 
410 #define _leave(FMT, ...)				\
411 do {							\
412 	if (cachefiles_debug & CACHEFILES_DEBUG_KLEAVE)	\
413 		kleave(FMT, ##__VA_ARGS__);		\
414 } while (0)
415 
416 #define _debug(FMT, ...)				\
417 do {							\
418 	if (cachefiles_debug & CACHEFILES_DEBUG_KDEBUG)	\
419 		kdebug(FMT, ##__VA_ARGS__);		\
420 } while (0)
421 
422 #else
423 #define _enter(FMT, ...) no_printk("==> %s("FMT")", __func__, ##__VA_ARGS__)
424 #define _leave(FMT, ...) no_printk("<== %s()"FMT"", __func__, ##__VA_ARGS__)
425 #define _debug(FMT, ...) no_printk(FMT, ##__VA_ARGS__)
426 #endif
427 
428 #if 1 /* defined(__KDEBUGALL) */
429 
430 #define ASSERT(X)							\
431 do {									\
432 	if (unlikely(!(X))) {						\
433 		pr_err("\n");						\
434 		pr_err("Assertion failed\n");		\
435 		BUG();							\
436 	}								\
437 } while (0)
438 
439 #define ASSERTCMP(X, OP, Y)						\
440 do {									\
441 	if (unlikely(!((X) OP (Y)))) {					\
442 		pr_err("\n");						\
443 		pr_err("Assertion failed\n");		\
444 		pr_err("%lx " #OP " %lx is false\n",			\
445 		       (unsigned long)(X), (unsigned long)(Y));		\
446 		BUG();							\
447 	}								\
448 } while (0)
449 
450 #define ASSERTIF(C, X)							\
451 do {									\
452 	if (unlikely((C) && !(X))) {					\
453 		pr_err("\n");						\
454 		pr_err("Assertion failed\n");		\
455 		BUG();							\
456 	}								\
457 } while (0)
458 
459 #define ASSERTIFCMP(C, X, OP, Y)					\
460 do {									\
461 	if (unlikely((C) && !((X) OP (Y)))) {				\
462 		pr_err("\n");						\
463 		pr_err("Assertion failed\n");		\
464 		pr_err("%lx " #OP " %lx is false\n",			\
465 		       (unsigned long)(X), (unsigned long)(Y));		\
466 		BUG();							\
467 	}								\
468 } while (0)
469 
470 #else
471 
472 #define ASSERT(X)			do {} while (0)
473 #define ASSERTCMP(X, OP, Y)		do {} while (0)
474 #define ASSERTIF(C, X)			do {} while (0)
475 #define ASSERTIFCMP(C, X, OP, Y)	do {} while (0)
476 
477 #endif
478