xref: /linux/fs/cachefiles/internal.h (revision dec1c62e91ba268ab2a6e339d4d7a59287d5eba1)
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 extern void cachefiles_ondemand_clean_object(struct cachefiles_object *object);
294 
295 extern int cachefiles_ondemand_read(struct cachefiles_object *object,
296 				    loff_t pos, size_t len);
297 
298 #else
299 static inline ssize_t cachefiles_ondemand_daemon_read(struct cachefiles_cache *cache,
300 					char __user *_buffer, size_t buflen)
301 {
302 	return -EOPNOTSUPP;
303 }
304 
305 static inline int cachefiles_ondemand_init_object(struct cachefiles_object *object)
306 {
307 	return 0;
308 }
309 
310 static inline void cachefiles_ondemand_clean_object(struct cachefiles_object *object)
311 {
312 }
313 
314 static inline int cachefiles_ondemand_read(struct cachefiles_object *object,
315 					   loff_t pos, size_t len)
316 {
317 	return -EOPNOTSUPP;
318 }
319 #endif
320 
321 /*
322  * security.c
323  */
324 extern int cachefiles_get_security_ID(struct cachefiles_cache *cache);
325 extern int cachefiles_determine_cache_security(struct cachefiles_cache *cache,
326 					       struct dentry *root,
327 					       const struct cred **_saved_cred);
328 
329 static inline void cachefiles_begin_secure(struct cachefiles_cache *cache,
330 					   const struct cred **_saved_cred)
331 {
332 	*_saved_cred = override_creds(cache->cache_cred);
333 }
334 
335 static inline void cachefiles_end_secure(struct cachefiles_cache *cache,
336 					 const struct cred *saved_cred)
337 {
338 	revert_creds(saved_cred);
339 }
340 
341 /*
342  * volume.c
343  */
344 void cachefiles_acquire_volume(struct fscache_volume *volume);
345 void cachefiles_free_volume(struct fscache_volume *volume);
346 void cachefiles_withdraw_volume(struct cachefiles_volume *volume);
347 
348 /*
349  * xattr.c
350  */
351 extern int cachefiles_set_object_xattr(struct cachefiles_object *object);
352 extern int cachefiles_check_auxdata(struct cachefiles_object *object,
353 				    struct file *file);
354 extern int cachefiles_remove_object_xattr(struct cachefiles_cache *cache,
355 					  struct cachefiles_object *object,
356 					  struct dentry *dentry);
357 extern void cachefiles_prepare_to_write(struct fscache_cookie *cookie);
358 extern bool cachefiles_set_volume_xattr(struct cachefiles_volume *volume);
359 extern int cachefiles_check_volume_xattr(struct cachefiles_volume *volume);
360 
361 /*
362  * Error handling
363  */
364 #define cachefiles_io_error(___cache, FMT, ...)		\
365 do {							\
366 	pr_err("I/O Error: " FMT"\n", ##__VA_ARGS__);	\
367 	fscache_io_error((___cache)->cache);		\
368 	set_bit(CACHEFILES_DEAD, &(___cache)->flags);	\
369 } while (0)
370 
371 #define cachefiles_io_error_obj(object, FMT, ...)			\
372 do {									\
373 	struct cachefiles_cache *___cache;				\
374 									\
375 	___cache = (object)->volume->cache;				\
376 	cachefiles_io_error(___cache, FMT " [o=%08x]", ##__VA_ARGS__,	\
377 			    (object)->debug_id);			\
378 } while (0)
379 
380 
381 /*
382  * Debug tracing
383  */
384 extern unsigned cachefiles_debug;
385 #define CACHEFILES_DEBUG_KENTER	1
386 #define CACHEFILES_DEBUG_KLEAVE	2
387 #define CACHEFILES_DEBUG_KDEBUG	4
388 
389 #define dbgprintk(FMT, ...) \
390 	printk(KERN_DEBUG "[%-6.6s] "FMT"\n", current->comm, ##__VA_ARGS__)
391 
392 #define kenter(FMT, ...) dbgprintk("==> %s("FMT")", __func__, ##__VA_ARGS__)
393 #define kleave(FMT, ...) dbgprintk("<== %s()"FMT"", __func__, ##__VA_ARGS__)
394 #define kdebug(FMT, ...) dbgprintk(FMT, ##__VA_ARGS__)
395 
396 
397 #if defined(__KDEBUG)
398 #define _enter(FMT, ...) kenter(FMT, ##__VA_ARGS__)
399 #define _leave(FMT, ...) kleave(FMT, ##__VA_ARGS__)
400 #define _debug(FMT, ...) kdebug(FMT, ##__VA_ARGS__)
401 
402 #elif defined(CONFIG_CACHEFILES_DEBUG)
403 #define _enter(FMT, ...)				\
404 do {							\
405 	if (cachefiles_debug & CACHEFILES_DEBUG_KENTER)	\
406 		kenter(FMT, ##__VA_ARGS__);		\
407 } while (0)
408 
409 #define _leave(FMT, ...)				\
410 do {							\
411 	if (cachefiles_debug & CACHEFILES_DEBUG_KLEAVE)	\
412 		kleave(FMT, ##__VA_ARGS__);		\
413 } while (0)
414 
415 #define _debug(FMT, ...)				\
416 do {							\
417 	if (cachefiles_debug & CACHEFILES_DEBUG_KDEBUG)	\
418 		kdebug(FMT, ##__VA_ARGS__);		\
419 } while (0)
420 
421 #else
422 #define _enter(FMT, ...) no_printk("==> %s("FMT")", __func__, ##__VA_ARGS__)
423 #define _leave(FMT, ...) no_printk("<== %s()"FMT"", __func__, ##__VA_ARGS__)
424 #define _debug(FMT, ...) no_printk(FMT, ##__VA_ARGS__)
425 #endif
426 
427 #if 1 /* defined(__KDEBUGALL) */
428 
429 #define ASSERT(X)							\
430 do {									\
431 	if (unlikely(!(X))) {						\
432 		pr_err("\n");						\
433 		pr_err("Assertion failed\n");		\
434 		BUG();							\
435 	}								\
436 } while (0)
437 
438 #define ASSERTCMP(X, OP, Y)						\
439 do {									\
440 	if (unlikely(!((X) OP (Y)))) {					\
441 		pr_err("\n");						\
442 		pr_err("Assertion failed\n");		\
443 		pr_err("%lx " #OP " %lx is false\n",			\
444 		       (unsigned long)(X), (unsigned long)(Y));		\
445 		BUG();							\
446 	}								\
447 } while (0)
448 
449 #define ASSERTIF(C, X)							\
450 do {									\
451 	if (unlikely((C) && !(X))) {					\
452 		pr_err("\n");						\
453 		pr_err("Assertion failed\n");		\
454 		BUG();							\
455 	}								\
456 } while (0)
457 
458 #define ASSERTIFCMP(C, X, OP, Y)					\
459 do {									\
460 	if (unlikely((C) && !((X) OP (Y)))) {				\
461 		pr_err("\n");						\
462 		pr_err("Assertion failed\n");		\
463 		pr_err("%lx " #OP " %lx is false\n",			\
464 		       (unsigned long)(X), (unsigned long)(Y));		\
465 		BUG();							\
466 	}								\
467 } while (0)
468 
469 #else
470 
471 #define ASSERT(X)			do {} while (0)
472 #define ASSERTCMP(X, OP, Y)		do {} while (0)
473 #define ASSERTIF(C, X)			do {} while (0)
474 #define ASSERTIFCMP(C, X, OP, Y)	do {} while (0)
475 
476 #endif
477