xref: /openbmc/linux/fs/cachefiles/daemon.c (revision e6486939)
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /* Daemon interface
3  *
4  * Copyright (C) 2007, 2021 Red Hat, Inc. All Rights Reserved.
5  * Written by David Howells (dhowells@redhat.com)
6  */
7 
8 #include <linux/module.h>
9 #include <linux/init.h>
10 #include <linux/sched.h>
11 #include <linux/completion.h>
12 #include <linux/slab.h>
13 #include <linux/fs.h>
14 #include <linux/file.h>
15 #include <linux/namei.h>
16 #include <linux/poll.h>
17 #include <linux/mount.h>
18 #include <linux/statfs.h>
19 #include <linux/ctype.h>
20 #include <linux/string.h>
21 #include <linux/fs_struct.h>
22 #include "internal.h"
23 
24 static int cachefiles_daemon_open(struct inode *, struct file *);
25 static int cachefiles_daemon_release(struct inode *, struct file *);
26 static ssize_t cachefiles_daemon_read(struct file *, char __user *, size_t,
27 				      loff_t *);
28 static ssize_t cachefiles_daemon_write(struct file *, const char __user *,
29 				       size_t, loff_t *);
30 static __poll_t cachefiles_daemon_poll(struct file *,
31 					   struct poll_table_struct *);
32 static int cachefiles_daemon_frun(struct cachefiles_cache *, char *);
33 static int cachefiles_daemon_fcull(struct cachefiles_cache *, char *);
34 static int cachefiles_daemon_fstop(struct cachefiles_cache *, char *);
35 static int cachefiles_daemon_brun(struct cachefiles_cache *, char *);
36 static int cachefiles_daemon_bcull(struct cachefiles_cache *, char *);
37 static int cachefiles_daemon_bstop(struct cachefiles_cache *, char *);
38 static int cachefiles_daemon_cull(struct cachefiles_cache *, char *);
39 static int cachefiles_daemon_debug(struct cachefiles_cache *, char *);
40 static int cachefiles_daemon_dir(struct cachefiles_cache *, char *);
41 static int cachefiles_daemon_inuse(struct cachefiles_cache *, char *);
42 static int cachefiles_daemon_secctx(struct cachefiles_cache *, char *);
43 static int cachefiles_daemon_tag(struct cachefiles_cache *, char *);
44 static int cachefiles_daemon_bind(struct cachefiles_cache *, char *);
45 static void cachefiles_daemon_unbind(struct cachefiles_cache *);
46 
47 static unsigned long cachefiles_open;
48 
49 const struct file_operations cachefiles_daemon_fops = {
50 	.owner		= THIS_MODULE,
51 	.open		= cachefiles_daemon_open,
52 	.release	= cachefiles_daemon_release,
53 	.read		= cachefiles_daemon_read,
54 	.write		= cachefiles_daemon_write,
55 	.poll		= cachefiles_daemon_poll,
56 	.llseek		= noop_llseek,
57 };
58 
59 struct cachefiles_daemon_cmd {
60 	char name[8];
61 	int (*handler)(struct cachefiles_cache *cache, char *args);
62 };
63 
64 static const struct cachefiles_daemon_cmd cachefiles_daemon_cmds[] = {
65 	{ "bind",	cachefiles_daemon_bind		},
66 	{ "brun",	cachefiles_daemon_brun		},
67 	{ "bcull",	cachefiles_daemon_bcull		},
68 	{ "bstop",	cachefiles_daemon_bstop		},
69 	{ "cull",	cachefiles_daemon_cull		},
70 	{ "debug",	cachefiles_daemon_debug		},
71 	{ "dir",	cachefiles_daemon_dir		},
72 	{ "frun",	cachefiles_daemon_frun		},
73 	{ "fcull",	cachefiles_daemon_fcull		},
74 	{ "fstop",	cachefiles_daemon_fstop		},
75 	{ "inuse",	cachefiles_daemon_inuse		},
76 	{ "secctx",	cachefiles_daemon_secctx	},
77 	{ "tag",	cachefiles_daemon_tag		},
78 	{ "",		NULL				}
79 };
80 
81 
82 /*
83  * Prepare a cache for caching.
84  */
85 static int cachefiles_daemon_open(struct inode *inode, struct file *file)
86 {
87 	struct cachefiles_cache *cache;
88 
89 	_enter("");
90 
91 	/* only the superuser may do this */
92 	if (!capable(CAP_SYS_ADMIN))
93 		return -EPERM;
94 
95 	/* the cachefiles device may only be open once at a time */
96 	if (xchg(&cachefiles_open, 1) == 1)
97 		return -EBUSY;
98 
99 	/* allocate a cache record */
100 	cache = kzalloc(sizeof(struct cachefiles_cache), GFP_KERNEL);
101 	if (!cache) {
102 		cachefiles_open = 0;
103 		return -ENOMEM;
104 	}
105 
106 	mutex_init(&cache->daemon_mutex);
107 	init_waitqueue_head(&cache->daemon_pollwq);
108 	INIT_LIST_HEAD(&cache->volumes);
109 	INIT_LIST_HEAD(&cache->object_list);
110 	spin_lock_init(&cache->object_list_lock);
111 
112 	/* set default caching limits
113 	 * - limit at 1% free space and/or free files
114 	 * - cull below 5% free space and/or free files
115 	 * - cease culling above 7% free space and/or free files
116 	 */
117 	cache->frun_percent = 7;
118 	cache->fcull_percent = 5;
119 	cache->fstop_percent = 1;
120 	cache->brun_percent = 7;
121 	cache->bcull_percent = 5;
122 	cache->bstop_percent = 1;
123 
124 	file->private_data = cache;
125 	cache->cachefilesd = file;
126 	return 0;
127 }
128 
129 /*
130  * Release a cache.
131  */
132 static int cachefiles_daemon_release(struct inode *inode, struct file *file)
133 {
134 	struct cachefiles_cache *cache = file->private_data;
135 
136 	_enter("");
137 
138 	ASSERT(cache);
139 
140 	set_bit(CACHEFILES_DEAD, &cache->flags);
141 
142 	cachefiles_daemon_unbind(cache);
143 
144 	/* clean up the control file interface */
145 	cache->cachefilesd = NULL;
146 	file->private_data = NULL;
147 	cachefiles_open = 0;
148 
149 	kfree(cache);
150 
151 	_leave("");
152 	return 0;
153 }
154 
155 /*
156  * Read the cache state.
157  */
158 static ssize_t cachefiles_daemon_read(struct file *file, char __user *_buffer,
159 				      size_t buflen, loff_t *pos)
160 {
161 	struct cachefiles_cache *cache = file->private_data;
162 	unsigned long long b_released;
163 	unsigned f_released;
164 	char buffer[256];
165 	int n;
166 
167 	//_enter(",,%zu,", buflen);
168 
169 	if (!test_bit(CACHEFILES_READY, &cache->flags))
170 		return 0;
171 
172 	/* check how much space the cache has */
173 	cachefiles_has_space(cache, 0, 0, cachefiles_has_space_check);
174 
175 	/* summarise */
176 	f_released = atomic_xchg(&cache->f_released, 0);
177 	b_released = atomic_long_xchg(&cache->b_released, 0);
178 	clear_bit(CACHEFILES_STATE_CHANGED, &cache->flags);
179 
180 	n = snprintf(buffer, sizeof(buffer),
181 		     "cull=%c"
182 		     " frun=%llx"
183 		     " fcull=%llx"
184 		     " fstop=%llx"
185 		     " brun=%llx"
186 		     " bcull=%llx"
187 		     " bstop=%llx"
188 		     " freleased=%x"
189 		     " breleased=%llx",
190 		     test_bit(CACHEFILES_CULLING, &cache->flags) ? '1' : '0',
191 		     (unsigned long long) cache->frun,
192 		     (unsigned long long) cache->fcull,
193 		     (unsigned long long) cache->fstop,
194 		     (unsigned long long) cache->brun,
195 		     (unsigned long long) cache->bcull,
196 		     (unsigned long long) cache->bstop,
197 		     f_released,
198 		     b_released);
199 
200 	if (n > buflen)
201 		return -EMSGSIZE;
202 
203 	if (copy_to_user(_buffer, buffer, n) != 0)
204 		return -EFAULT;
205 
206 	return n;
207 }
208 
209 /*
210  * Take a command from cachefilesd, parse it and act on it.
211  */
212 static ssize_t cachefiles_daemon_write(struct file *file,
213 				       const char __user *_data,
214 				       size_t datalen,
215 				       loff_t *pos)
216 {
217 	const struct cachefiles_daemon_cmd *cmd;
218 	struct cachefiles_cache *cache = file->private_data;
219 	ssize_t ret;
220 	char *data, *args, *cp;
221 
222 	//_enter(",,%zu,", datalen);
223 
224 	ASSERT(cache);
225 
226 	if (test_bit(CACHEFILES_DEAD, &cache->flags))
227 		return -EIO;
228 
229 	if (datalen > PAGE_SIZE - 1)
230 		return -EOPNOTSUPP;
231 
232 	/* drag the command string into the kernel so we can parse it */
233 	data = memdup_user_nul(_data, datalen);
234 	if (IS_ERR(data))
235 		return PTR_ERR(data);
236 
237 	ret = -EINVAL;
238 	if (memchr(data, '\0', datalen))
239 		goto error;
240 
241 	/* strip any newline */
242 	cp = memchr(data, '\n', datalen);
243 	if (cp) {
244 		if (cp == data)
245 			goto error;
246 
247 		*cp = '\0';
248 	}
249 
250 	/* parse the command */
251 	ret = -EOPNOTSUPP;
252 
253 	for (args = data; *args; args++)
254 		if (isspace(*args))
255 			break;
256 	if (*args) {
257 		if (args == data)
258 			goto error;
259 		*args = '\0';
260 		args = skip_spaces(++args);
261 	}
262 
263 	/* run the appropriate command handler */
264 	for (cmd = cachefiles_daemon_cmds; cmd->name[0]; cmd++)
265 		if (strcmp(cmd->name, data) == 0)
266 			goto found_command;
267 
268 error:
269 	kfree(data);
270 	//_leave(" = %zd", ret);
271 	return ret;
272 
273 found_command:
274 	mutex_lock(&cache->daemon_mutex);
275 
276 	ret = -EIO;
277 	if (!test_bit(CACHEFILES_DEAD, &cache->flags))
278 		ret = cmd->handler(cache, args);
279 
280 	mutex_unlock(&cache->daemon_mutex);
281 
282 	if (ret == 0)
283 		ret = datalen;
284 	goto error;
285 }
286 
287 /*
288  * Poll for culling state
289  * - use EPOLLOUT to indicate culling state
290  */
291 static __poll_t cachefiles_daemon_poll(struct file *file,
292 					   struct poll_table_struct *poll)
293 {
294 	struct cachefiles_cache *cache = file->private_data;
295 	__poll_t mask;
296 
297 	poll_wait(file, &cache->daemon_pollwq, poll);
298 	mask = 0;
299 
300 	if (test_bit(CACHEFILES_STATE_CHANGED, &cache->flags))
301 		mask |= EPOLLIN;
302 
303 	if (test_bit(CACHEFILES_CULLING, &cache->flags))
304 		mask |= EPOLLOUT;
305 
306 	return mask;
307 }
308 
309 /*
310  * Give a range error for cache space constraints
311  * - can be tail-called
312  */
313 static int cachefiles_daemon_range_error(struct cachefiles_cache *cache,
314 					 char *args)
315 {
316 	pr_err("Free space limits must be in range 0%%<=stop<cull<run<100%%\n");
317 
318 	return -EINVAL;
319 }
320 
321 /*
322  * Set the percentage of files at which to stop culling
323  * - command: "frun <N>%"
324  */
325 static int cachefiles_daemon_frun(struct cachefiles_cache *cache, char *args)
326 {
327 	unsigned long frun;
328 
329 	_enter(",%s", args);
330 
331 	if (!*args)
332 		return -EINVAL;
333 
334 	frun = simple_strtoul(args, &args, 10);
335 	if (args[0] != '%' || args[1] != '\0')
336 		return -EINVAL;
337 
338 	if (frun <= cache->fcull_percent || frun >= 100)
339 		return cachefiles_daemon_range_error(cache, args);
340 
341 	cache->frun_percent = frun;
342 	return 0;
343 }
344 
345 /*
346  * Set the percentage of files at which to start culling
347  * - command: "fcull <N>%"
348  */
349 static int cachefiles_daemon_fcull(struct cachefiles_cache *cache, char *args)
350 {
351 	unsigned long fcull;
352 
353 	_enter(",%s", args);
354 
355 	if (!*args)
356 		return -EINVAL;
357 
358 	fcull = simple_strtoul(args, &args, 10);
359 	if (args[0] != '%' || args[1] != '\0')
360 		return -EINVAL;
361 
362 	if (fcull <= cache->fstop_percent || fcull >= cache->frun_percent)
363 		return cachefiles_daemon_range_error(cache, args);
364 
365 	cache->fcull_percent = fcull;
366 	return 0;
367 }
368 
369 /*
370  * Set the percentage of files at which to stop allocating
371  * - command: "fstop <N>%"
372  */
373 static int cachefiles_daemon_fstop(struct cachefiles_cache *cache, char *args)
374 {
375 	unsigned long fstop;
376 
377 	_enter(",%s", args);
378 
379 	if (!*args)
380 		return -EINVAL;
381 
382 	fstop = simple_strtoul(args, &args, 10);
383 	if (args[0] != '%' || args[1] != '\0')
384 		return -EINVAL;
385 
386 	if (fstop >= cache->fcull_percent)
387 		return cachefiles_daemon_range_error(cache, args);
388 
389 	cache->fstop_percent = fstop;
390 	return 0;
391 }
392 
393 /*
394  * Set the percentage of blocks at which to stop culling
395  * - command: "brun <N>%"
396  */
397 static int cachefiles_daemon_brun(struct cachefiles_cache *cache, char *args)
398 {
399 	unsigned long brun;
400 
401 	_enter(",%s", args);
402 
403 	if (!*args)
404 		return -EINVAL;
405 
406 	brun = simple_strtoul(args, &args, 10);
407 	if (args[0] != '%' || args[1] != '\0')
408 		return -EINVAL;
409 
410 	if (brun <= cache->bcull_percent || brun >= 100)
411 		return cachefiles_daemon_range_error(cache, args);
412 
413 	cache->brun_percent = brun;
414 	return 0;
415 }
416 
417 /*
418  * Set the percentage of blocks at which to start culling
419  * - command: "bcull <N>%"
420  */
421 static int cachefiles_daemon_bcull(struct cachefiles_cache *cache, char *args)
422 {
423 	unsigned long bcull;
424 
425 	_enter(",%s", args);
426 
427 	if (!*args)
428 		return -EINVAL;
429 
430 	bcull = simple_strtoul(args, &args, 10);
431 	if (args[0] != '%' || args[1] != '\0')
432 		return -EINVAL;
433 
434 	if (bcull <= cache->bstop_percent || bcull >= cache->brun_percent)
435 		return cachefiles_daemon_range_error(cache, args);
436 
437 	cache->bcull_percent = bcull;
438 	return 0;
439 }
440 
441 /*
442  * Set the percentage of blocks at which to stop allocating
443  * - command: "bstop <N>%"
444  */
445 static int cachefiles_daemon_bstop(struct cachefiles_cache *cache, char *args)
446 {
447 	unsigned long bstop;
448 
449 	_enter(",%s", args);
450 
451 	if (!*args)
452 		return -EINVAL;
453 
454 	bstop = simple_strtoul(args, &args, 10);
455 	if (args[0] != '%' || args[1] != '\0')
456 		return -EINVAL;
457 
458 	if (bstop >= cache->bcull_percent)
459 		return cachefiles_daemon_range_error(cache, args);
460 
461 	cache->bstop_percent = bstop;
462 	return 0;
463 }
464 
465 /*
466  * Set the cache directory
467  * - command: "dir <name>"
468  */
469 static int cachefiles_daemon_dir(struct cachefiles_cache *cache, char *args)
470 {
471 	char *dir;
472 
473 	_enter(",%s", args);
474 
475 	if (!*args) {
476 		pr_err("Empty directory specified\n");
477 		return -EINVAL;
478 	}
479 
480 	if (cache->rootdirname) {
481 		pr_err("Second cache directory specified\n");
482 		return -EEXIST;
483 	}
484 
485 	dir = kstrdup(args, GFP_KERNEL);
486 	if (!dir)
487 		return -ENOMEM;
488 
489 	cache->rootdirname = dir;
490 	return 0;
491 }
492 
493 /*
494  * Set the cache security context
495  * - command: "secctx <ctx>"
496  */
497 static int cachefiles_daemon_secctx(struct cachefiles_cache *cache, char *args)
498 {
499 	char *secctx;
500 
501 	_enter(",%s", args);
502 
503 	if (!*args) {
504 		pr_err("Empty security context specified\n");
505 		return -EINVAL;
506 	}
507 
508 	if (cache->secctx) {
509 		pr_err("Second security context specified\n");
510 		return -EINVAL;
511 	}
512 
513 	secctx = kstrdup(args, GFP_KERNEL);
514 	if (!secctx)
515 		return -ENOMEM;
516 
517 	cache->secctx = secctx;
518 	return 0;
519 }
520 
521 /*
522  * Set the cache tag
523  * - command: "tag <name>"
524  */
525 static int cachefiles_daemon_tag(struct cachefiles_cache *cache, char *args)
526 {
527 	char *tag;
528 
529 	_enter(",%s", args);
530 
531 	if (!*args) {
532 		pr_err("Empty tag specified\n");
533 		return -EINVAL;
534 	}
535 
536 	if (cache->tag)
537 		return -EEXIST;
538 
539 	tag = kstrdup(args, GFP_KERNEL);
540 	if (!tag)
541 		return -ENOMEM;
542 
543 	cache->tag = tag;
544 	return 0;
545 }
546 
547 /*
548  * Request a node in the cache be culled from the current working directory
549  * - command: "cull <name>"
550  */
551 static int cachefiles_daemon_cull(struct cachefiles_cache *cache, char *args)
552 {
553 	struct path path;
554 	const struct cred *saved_cred;
555 	int ret;
556 
557 	_enter(",%s", args);
558 
559 	if (strchr(args, '/'))
560 		goto inval;
561 
562 	if (!test_bit(CACHEFILES_READY, &cache->flags)) {
563 		pr_err("cull applied to unready cache\n");
564 		return -EIO;
565 	}
566 
567 	if (test_bit(CACHEFILES_DEAD, &cache->flags)) {
568 		pr_err("cull applied to dead cache\n");
569 		return -EIO;
570 	}
571 
572 	get_fs_pwd(current->fs, &path);
573 
574 	if (!d_can_lookup(path.dentry))
575 		goto notdir;
576 
577 	cachefiles_begin_secure(cache, &saved_cred);
578 	ret = cachefiles_cull(cache, path.dentry, args);
579 	cachefiles_end_secure(cache, saved_cred);
580 
581 	path_put(&path);
582 	_leave(" = %d", ret);
583 	return ret;
584 
585 notdir:
586 	path_put(&path);
587 	pr_err("cull command requires dirfd to be a directory\n");
588 	return -ENOTDIR;
589 
590 inval:
591 	pr_err("cull command requires dirfd and filename\n");
592 	return -EINVAL;
593 }
594 
595 /*
596  * Set debugging mode
597  * - command: "debug <mask>"
598  */
599 static int cachefiles_daemon_debug(struct cachefiles_cache *cache, char *args)
600 {
601 	unsigned long mask;
602 
603 	_enter(",%s", args);
604 
605 	mask = simple_strtoul(args, &args, 0);
606 	if (args[0] != '\0')
607 		goto inval;
608 
609 	cachefiles_debug = mask;
610 	_leave(" = 0");
611 	return 0;
612 
613 inval:
614 	pr_err("debug command requires mask\n");
615 	return -EINVAL;
616 }
617 
618 /*
619  * Find out whether an object in the current working directory is in use or not
620  * - command: "inuse <name>"
621  */
622 static int cachefiles_daemon_inuse(struct cachefiles_cache *cache, char *args)
623 {
624 	struct path path;
625 	const struct cred *saved_cred;
626 	int ret;
627 
628 	//_enter(",%s", args);
629 
630 	if (strchr(args, '/'))
631 		goto inval;
632 
633 	if (!test_bit(CACHEFILES_READY, &cache->flags)) {
634 		pr_err("inuse applied to unready cache\n");
635 		return -EIO;
636 	}
637 
638 	if (test_bit(CACHEFILES_DEAD, &cache->flags)) {
639 		pr_err("inuse applied to dead cache\n");
640 		return -EIO;
641 	}
642 
643 	get_fs_pwd(current->fs, &path);
644 
645 	if (!d_can_lookup(path.dentry))
646 		goto notdir;
647 
648 	cachefiles_begin_secure(cache, &saved_cred);
649 	ret = cachefiles_check_in_use(cache, path.dentry, args);
650 	cachefiles_end_secure(cache, saved_cred);
651 
652 	path_put(&path);
653 	//_leave(" = %d", ret);
654 	return ret;
655 
656 notdir:
657 	path_put(&path);
658 	pr_err("inuse command requires dirfd to be a directory\n");
659 	return -ENOTDIR;
660 
661 inval:
662 	pr_err("inuse command requires dirfd and filename\n");
663 	return -EINVAL;
664 }
665 
666 /*
667  * Bind a directory as a cache
668  */
669 static int cachefiles_daemon_bind(struct cachefiles_cache *cache, char *args)
670 {
671 	_enter("{%u,%u,%u,%u,%u,%u},%s",
672 	       cache->frun_percent,
673 	       cache->fcull_percent,
674 	       cache->fstop_percent,
675 	       cache->brun_percent,
676 	       cache->bcull_percent,
677 	       cache->bstop_percent,
678 	       args);
679 
680 	if (cache->fstop_percent >= cache->fcull_percent ||
681 	    cache->fcull_percent >= cache->frun_percent ||
682 	    cache->frun_percent  >= 100)
683 		return -ERANGE;
684 
685 	if (cache->bstop_percent >= cache->bcull_percent ||
686 	    cache->bcull_percent >= cache->brun_percent ||
687 	    cache->brun_percent  >= 100)
688 		return -ERANGE;
689 
690 	if (*args) {
691 		pr_err("'bind' command doesn't take an argument\n");
692 		return -EINVAL;
693 	}
694 
695 	if (!cache->rootdirname) {
696 		pr_err("No cache directory specified\n");
697 		return -EINVAL;
698 	}
699 
700 	/* Don't permit already bound caches to be re-bound */
701 	if (test_bit(CACHEFILES_READY, &cache->flags)) {
702 		pr_err("Cache already bound\n");
703 		return -EBUSY;
704 	}
705 
706 	/* Make sure we have copies of the tag string */
707 	if (!cache->tag) {
708 		/*
709 		 * The tag string is released by the fops->release()
710 		 * function, so we don't release it on error here
711 		 */
712 		cache->tag = kstrdup("CacheFiles", GFP_KERNEL);
713 		if (!cache->tag)
714 			return -ENOMEM;
715 	}
716 
717 	return cachefiles_add_cache(cache);
718 }
719 
720 /*
721  * Unbind a cache.
722  */
723 static void cachefiles_daemon_unbind(struct cachefiles_cache *cache)
724 {
725 	_enter("");
726 
727 	if (test_bit(CACHEFILES_READY, &cache->flags))
728 		cachefiles_withdraw_cache(cache);
729 
730 	cachefiles_put_directory(cache->graveyard);
731 	cachefiles_put_directory(cache->store);
732 	mntput(cache->mnt);
733 
734 	kfree(cache->rootdirname);
735 	kfree(cache->secctx);
736 	kfree(cache->tag);
737 
738 	_leave("");
739 }
740