Blame view

fs/cachefiles/daemon.c 16.4 KB
b4d0d230c   Thomas Gleixner   treewide: Replace...
1
  // SPDX-License-Identifier: GPL-2.0-or-later
9ae326a69   David Howells   CacheFiles: A cac...
2
3
4
5
  /* Daemon interface
   *
   * Copyright (C) 2007 Red Hat, Inc. All Rights Reserved.
   * Written by David Howells (dhowells@redhat.com)
9ae326a69   David Howells   CacheFiles: A cac...
6
7
8
9
10
11
12
13
14
15
16
17
18
19
   */
  
  #include <linux/module.h>
  #include <linux/init.h>
  #include <linux/sched.h>
  #include <linux/completion.h>
  #include <linux/slab.h>
  #include <linux/fs.h>
  #include <linux/file.h>
  #include <linux/namei.h>
  #include <linux/poll.h>
  #include <linux/mount.h>
  #include <linux/statfs.h>
  #include <linux/ctype.h>
e7d2860b6   AndrĂ© Goddard Rosa   tree-wide: conver...
20
  #include <linux/string.h>
9ae326a69   David Howells   CacheFiles: A cac...
21
22
23
24
25
26
27
28
29
  #include <linux/fs_struct.h>
  #include "internal.h"
  
  static int cachefiles_daemon_open(struct inode *, struct file *);
  static int cachefiles_daemon_release(struct inode *, struct file *);
  static ssize_t cachefiles_daemon_read(struct file *, char __user *, size_t,
  				      loff_t *);
  static ssize_t cachefiles_daemon_write(struct file *, const char __user *,
  				       size_t, loff_t *);
076ccb76e   Al Viro   fs: annotate ->po...
30
  static __poll_t cachefiles_daemon_poll(struct file *,
9ae326a69   David Howells   CacheFiles: A cac...
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
  					   struct poll_table_struct *);
  static int cachefiles_daemon_frun(struct cachefiles_cache *, char *);
  static int cachefiles_daemon_fcull(struct cachefiles_cache *, char *);
  static int cachefiles_daemon_fstop(struct cachefiles_cache *, char *);
  static int cachefiles_daemon_brun(struct cachefiles_cache *, char *);
  static int cachefiles_daemon_bcull(struct cachefiles_cache *, char *);
  static int cachefiles_daemon_bstop(struct cachefiles_cache *, char *);
  static int cachefiles_daemon_cull(struct cachefiles_cache *, char *);
  static int cachefiles_daemon_debug(struct cachefiles_cache *, char *);
  static int cachefiles_daemon_dir(struct cachefiles_cache *, char *);
  static int cachefiles_daemon_inuse(struct cachefiles_cache *, char *);
  static int cachefiles_daemon_secctx(struct cachefiles_cache *, char *);
  static int cachefiles_daemon_tag(struct cachefiles_cache *, char *);
  
  static unsigned long cachefiles_open;
  
  const struct file_operations cachefiles_daemon_fops = {
  	.owner		= THIS_MODULE,
  	.open		= cachefiles_daemon_open,
  	.release	= cachefiles_daemon_release,
  	.read		= cachefiles_daemon_read,
  	.write		= cachefiles_daemon_write,
  	.poll		= cachefiles_daemon_poll,
6038f373a   Arnd Bergmann   llseek: automatic...
54
  	.llseek		= noop_llseek,
9ae326a69   David Howells   CacheFiles: A cac...
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
  };
  
  struct cachefiles_daemon_cmd {
  	char name[8];
  	int (*handler)(struct cachefiles_cache *cache, char *args);
  };
  
  static const struct cachefiles_daemon_cmd cachefiles_daemon_cmds[] = {
  	{ "bind",	cachefiles_daemon_bind		},
  	{ "brun",	cachefiles_daemon_brun		},
  	{ "bcull",	cachefiles_daemon_bcull		},
  	{ "bstop",	cachefiles_daemon_bstop		},
  	{ "cull",	cachefiles_daemon_cull		},
  	{ "debug",	cachefiles_daemon_debug		},
  	{ "dir",	cachefiles_daemon_dir		},
  	{ "frun",	cachefiles_daemon_frun		},
  	{ "fcull",	cachefiles_daemon_fcull		},
  	{ "fstop",	cachefiles_daemon_fstop		},
  	{ "inuse",	cachefiles_daemon_inuse		},
  	{ "secctx",	cachefiles_daemon_secctx	},
  	{ "tag",	cachefiles_daemon_tag		},
  	{ "",		NULL				}
  };
  
  
  /*
   * do various checks
   */
  static int cachefiles_daemon_open(struct inode *inode, struct file *file)
  {
  	struct cachefiles_cache *cache;
  
  	_enter("");
  
  	/* only the superuser may do this */
  	if (!capable(CAP_SYS_ADMIN))
  		return -EPERM;
  
  	/* the cachefiles device may only be open once at a time */
  	if (xchg(&cachefiles_open, 1) == 1)
  		return -EBUSY;
  
  	/* allocate a cache record */
  	cache = kzalloc(sizeof(struct cachefiles_cache), GFP_KERNEL);
  	if (!cache) {
  		cachefiles_open = 0;
  		return -ENOMEM;
  	}
  
  	mutex_init(&cache->daemon_mutex);
  	cache->active_nodes = RB_ROOT;
  	rwlock_init(&cache->active_lock);
  	init_waitqueue_head(&cache->daemon_pollwq);
  
  	/* set default caching limits
  	 * - limit at 1% free space and/or free files
  	 * - cull below 5% free space and/or free files
  	 * - cease culling above 7% free space and/or free files
  	 */
  	cache->frun_percent = 7;
  	cache->fcull_percent = 5;
  	cache->fstop_percent = 1;
  	cache->brun_percent = 7;
  	cache->bcull_percent = 5;
  	cache->bstop_percent = 1;
  
  	file->private_data = cache;
  	cache->cachefilesd = file;
  	return 0;
  }
  
  /*
   * release a cache
   */
  static int cachefiles_daemon_release(struct inode *inode, struct file *file)
  {
  	struct cachefiles_cache *cache = file->private_data;
  
  	_enter("");
  
  	ASSERT(cache);
  
  	set_bit(CACHEFILES_DEAD, &cache->flags);
  
  	cachefiles_daemon_unbind(cache);
  
  	ASSERT(!cache->active_nodes.rb_node);
  
  	/* clean up the control file interface */
  	cache->cachefilesd = NULL;
  	file->private_data = NULL;
  	cachefiles_open = 0;
  
  	kfree(cache);
  
  	_leave("");
  	return 0;
  }
  
  /*
   * read the cache state
   */
  static ssize_t cachefiles_daemon_read(struct file *file, char __user *_buffer,
  				      size_t buflen, loff_t *pos)
  {
  	struct cachefiles_cache *cache = file->private_data;
a5b3a80b8   David Howells   CacheFiles: Provi...
161
162
  	unsigned long long b_released;
  	unsigned f_released;
9ae326a69   David Howells   CacheFiles: A cac...
163
164
165
166
167
168
169
170
171
172
173
174
  	char buffer[256];
  	int n;
  
  	//_enter(",,%zu,", buflen);
  
  	if (!test_bit(CACHEFILES_READY, &cache->flags))
  		return 0;
  
  	/* check how much space the cache has */
  	cachefiles_has_space(cache, 0, 0);
  
  	/* summarise */
a5b3a80b8   David Howells   CacheFiles: Provi...
175
176
  	f_released = atomic_xchg(&cache->f_released, 0);
  	b_released = atomic_long_xchg(&cache->b_released, 0);
9ae326a69   David Howells   CacheFiles: A cac...
177
178
179
180
181
182
183
184
185
  	clear_bit(CACHEFILES_STATE_CHANGED, &cache->flags);
  
  	n = snprintf(buffer, sizeof(buffer),
  		     "cull=%c"
  		     " frun=%llx"
  		     " fcull=%llx"
  		     " fstop=%llx"
  		     " brun=%llx"
  		     " bcull=%llx"
a5b3a80b8   David Howells   CacheFiles: Provi...
186
187
188
  		     " bstop=%llx"
  		     " freleased=%x"
  		     " breleased=%llx",
9ae326a69   David Howells   CacheFiles: A cac...
189
190
191
192
193
194
  		     test_bit(CACHEFILES_CULLING, &cache->flags) ? '1' : '0',
  		     (unsigned long long) cache->frun,
  		     (unsigned long long) cache->fcull,
  		     (unsigned long long) cache->fstop,
  		     (unsigned long long) cache->brun,
  		     (unsigned long long) cache->bcull,
a5b3a80b8   David Howells   CacheFiles: Provi...
195
196
197
  		     (unsigned long long) cache->bstop,
  		     f_released,
  		     b_released);
9ae326a69   David Howells   CacheFiles: A cac...
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
  
  	if (n > buflen)
  		return -EMSGSIZE;
  
  	if (copy_to_user(_buffer, buffer, n) != 0)
  		return -EFAULT;
  
  	return n;
  }
  
  /*
   * command the cache
   */
  static ssize_t cachefiles_daemon_write(struct file *file,
  				       const char __user *_data,
  				       size_t datalen,
  				       loff_t *pos)
  {
  	const struct cachefiles_daemon_cmd *cmd;
  	struct cachefiles_cache *cache = file->private_data;
  	ssize_t ret;
  	char *data, *args, *cp;
  
  	//_enter(",,%zu,", datalen);
  
  	ASSERT(cache);
  
  	if (test_bit(CACHEFILES_DEAD, &cache->flags))
  		return -EIO;
  
  	if (datalen < 0 || datalen > PAGE_SIZE - 1)
  		return -EOPNOTSUPP;
  
  	/* drag the command string into the kernel so we can parse it */
16e5c1fc3   Al Viro   convert a bunch o...
232
233
234
  	data = memdup_user_nul(_data, datalen);
  	if (IS_ERR(data))
  		return PTR_ERR(data);
9ae326a69   David Howells   CacheFiles: A cac...
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
  
  	ret = -EINVAL;
  	if (memchr(data, '\0', datalen))
  		goto error;
  
  	/* strip any newline */
  	cp = memchr(data, '
  ', datalen);
  	if (cp) {
  		if (cp == data)
  			goto error;
  
  		*cp = '\0';
  	}
  
  	/* parse the command */
  	ret = -EOPNOTSUPP;
  
  	for (args = data; *args; args++)
  		if (isspace(*args))
  			break;
  	if (*args) {
  		if (args == data)
  			goto error;
  		*args = '\0';
e7d2860b6   AndrĂ© Goddard Rosa   tree-wide: conver...
260
  		args = skip_spaces(++args);
9ae326a69   David Howells   CacheFiles: A cac...
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
  	}
  
  	/* run the appropriate command handler */
  	for (cmd = cachefiles_daemon_cmds; cmd->name[0]; cmd++)
  		if (strcmp(cmd->name, data) == 0)
  			goto found_command;
  
  error:
  	kfree(data);
  	//_leave(" = %zd", ret);
  	return ret;
  
  found_command:
  	mutex_lock(&cache->daemon_mutex);
  
  	ret = -EIO;
  	if (!test_bit(CACHEFILES_DEAD, &cache->flags))
  		ret = cmd->handler(cache, args);
  
  	mutex_unlock(&cache->daemon_mutex);
  
  	if (ret == 0)
  		ret = datalen;
  	goto error;
  }
  
  /*
   * poll for culling state
a9a08845e   Linus Torvalds   vfs: do bulk POLL...
289
   * - use EPOLLOUT to indicate culling state
9ae326a69   David Howells   CacheFiles: A cac...
290
   */
076ccb76e   Al Viro   fs: annotate ->po...
291
  static __poll_t cachefiles_daemon_poll(struct file *file,
9ae326a69   David Howells   CacheFiles: A cac...
292
293
294
  					   struct poll_table_struct *poll)
  {
  	struct cachefiles_cache *cache = file->private_data;
076ccb76e   Al Viro   fs: annotate ->po...
295
  	__poll_t mask;
9ae326a69   David Howells   CacheFiles: A cac...
296
297
298
299
300
  
  	poll_wait(file, &cache->daemon_pollwq, poll);
  	mask = 0;
  
  	if (test_bit(CACHEFILES_STATE_CHANGED, &cache->flags))
a9a08845e   Linus Torvalds   vfs: do bulk POLL...
301
  		mask |= EPOLLIN;
9ae326a69   David Howells   CacheFiles: A cac...
302
303
  
  	if (test_bit(CACHEFILES_CULLING, &cache->flags))
a9a08845e   Linus Torvalds   vfs: do bulk POLL...
304
  		mask |= EPOLLOUT;
9ae326a69   David Howells   CacheFiles: A cac...
305
306
307
308
309
310
311
312
313
314
315
  
  	return mask;
  }
  
  /*
   * give a range error for cache space constraints
   * - can be tail-called
   */
  static int cachefiles_daemon_range_error(struct cachefiles_cache *cache,
  					 char *args)
  {
6ff66ac77   Fabian Frederick   fs/cachefiles: ad...
316
317
  	pr_err("Free space limits must be in range 0%%<=stop<cull<run<100%%
  ");
9ae326a69   David Howells   CacheFiles: A cac...
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
  
  	return -EINVAL;
  }
  
  /*
   * set the percentage of files at which to stop culling
   * - command: "frun <N>%"
   */
  static int cachefiles_daemon_frun(struct cachefiles_cache *cache, char *args)
  {
  	unsigned long frun;
  
  	_enter(",%s", args);
  
  	if (!*args)
  		return -EINVAL;
  
  	frun = simple_strtoul(args, &args, 10);
  	if (args[0] != '%' || args[1] != '\0')
  		return -EINVAL;
  
  	if (frun <= cache->fcull_percent || frun >= 100)
  		return cachefiles_daemon_range_error(cache, args);
  
  	cache->frun_percent = frun;
  	return 0;
  }
  
  /*
   * set the percentage of files at which to start culling
   * - command: "fcull <N>%"
   */
  static int cachefiles_daemon_fcull(struct cachefiles_cache *cache, char *args)
  {
  	unsigned long fcull;
  
  	_enter(",%s", args);
  
  	if (!*args)
  		return -EINVAL;
  
  	fcull = simple_strtoul(args, &args, 10);
  	if (args[0] != '%' || args[1] != '\0')
  		return -EINVAL;
  
  	if (fcull <= cache->fstop_percent || fcull >= cache->frun_percent)
  		return cachefiles_daemon_range_error(cache, args);
  
  	cache->fcull_percent = fcull;
  	return 0;
  }
  
  /*
   * set the percentage of files at which to stop allocating
   * - command: "fstop <N>%"
   */
  static int cachefiles_daemon_fstop(struct cachefiles_cache *cache, char *args)
  {
  	unsigned long fstop;
  
  	_enter(",%s", args);
  
  	if (!*args)
  		return -EINVAL;
  
  	fstop = simple_strtoul(args, &args, 10);
  	if (args[0] != '%' || args[1] != '\0')
  		return -EINVAL;
  
  	if (fstop < 0 || fstop >= cache->fcull_percent)
  		return cachefiles_daemon_range_error(cache, args);
  
  	cache->fstop_percent = fstop;
  	return 0;
  }
  
  /*
   * set the percentage of blocks at which to stop culling
   * - command: "brun <N>%"
   */
  static int cachefiles_daemon_brun(struct cachefiles_cache *cache, char *args)
  {
  	unsigned long brun;
  
  	_enter(",%s", args);
  
  	if (!*args)
  		return -EINVAL;
  
  	brun = simple_strtoul(args, &args, 10);
  	if (args[0] != '%' || args[1] != '\0')
  		return -EINVAL;
  
  	if (brun <= cache->bcull_percent || brun >= 100)
  		return cachefiles_daemon_range_error(cache, args);
  
  	cache->brun_percent = brun;
  	return 0;
  }
  
  /*
   * set the percentage of blocks at which to start culling
   * - command: "bcull <N>%"
   */
  static int cachefiles_daemon_bcull(struct cachefiles_cache *cache, char *args)
  {
  	unsigned long bcull;
  
  	_enter(",%s", args);
  
  	if (!*args)
  		return -EINVAL;
  
  	bcull = simple_strtoul(args, &args, 10);
  	if (args[0] != '%' || args[1] != '\0')
  		return -EINVAL;
  
  	if (bcull <= cache->bstop_percent || bcull >= cache->brun_percent)
  		return cachefiles_daemon_range_error(cache, args);
  
  	cache->bcull_percent = bcull;
  	return 0;
  }
  
  /*
   * set the percentage of blocks at which to stop allocating
   * - command: "bstop <N>%"
   */
  static int cachefiles_daemon_bstop(struct cachefiles_cache *cache, char *args)
  {
  	unsigned long bstop;
  
  	_enter(",%s", args);
  
  	if (!*args)
  		return -EINVAL;
  
  	bstop = simple_strtoul(args, &args, 10);
  	if (args[0] != '%' || args[1] != '\0')
  		return -EINVAL;
  
  	if (bstop < 0 || bstop >= cache->bcull_percent)
  		return cachefiles_daemon_range_error(cache, args);
  
  	cache->bstop_percent = bstop;
  	return 0;
  }
  
  /*
   * set the cache directory
   * - command: "dir <name>"
   */
  static int cachefiles_daemon_dir(struct cachefiles_cache *cache, char *args)
  {
  	char *dir;
  
  	_enter(",%s", args);
  
  	if (!*args) {
6ff66ac77   Fabian Frederick   fs/cachefiles: ad...
477
478
  		pr_err("Empty directory specified
  ");
9ae326a69   David Howells   CacheFiles: A cac...
479
480
481
482
  		return -EINVAL;
  	}
  
  	if (cache->rootdirname) {
6ff66ac77   Fabian Frederick   fs/cachefiles: ad...
483
484
  		pr_err("Second cache directory specified
  ");
9ae326a69   David Howells   CacheFiles: A cac...
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
  		return -EEXIST;
  	}
  
  	dir = kstrdup(args, GFP_KERNEL);
  	if (!dir)
  		return -ENOMEM;
  
  	cache->rootdirname = dir;
  	return 0;
  }
  
  /*
   * set the cache security context
   * - command: "secctx <ctx>"
   */
  static int cachefiles_daemon_secctx(struct cachefiles_cache *cache, char *args)
  {
  	char *secctx;
  
  	_enter(",%s", args);
  
  	if (!*args) {
6ff66ac77   Fabian Frederick   fs/cachefiles: ad...
507
508
  		pr_err("Empty security context specified
  ");
9ae326a69   David Howells   CacheFiles: A cac...
509
510
511
512
  		return -EINVAL;
  	}
  
  	if (cache->secctx) {
6ff66ac77   Fabian Frederick   fs/cachefiles: ad...
513
514
  		pr_err("Second security context specified
  ");
9ae326a69   David Howells   CacheFiles: A cac...
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
  		return -EINVAL;
  	}
  
  	secctx = kstrdup(args, GFP_KERNEL);
  	if (!secctx)
  		return -ENOMEM;
  
  	cache->secctx = secctx;
  	return 0;
  }
  
  /*
   * set the cache tag
   * - command: "tag <name>"
   */
  static int cachefiles_daemon_tag(struct cachefiles_cache *cache, char *args)
  {
  	char *tag;
  
  	_enter(",%s", args);
  
  	if (!*args) {
6ff66ac77   Fabian Frederick   fs/cachefiles: ad...
537
538
  		pr_err("Empty tag specified
  ");
9ae326a69   David Howells   CacheFiles: A cac...
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
  		return -EINVAL;
  	}
  
  	if (cache->tag)
  		return -EEXIST;
  
  	tag = kstrdup(args, GFP_KERNEL);
  	if (!tag)
  		return -ENOMEM;
  
  	cache->tag = tag;
  	return 0;
  }
  
  /*
   * request a node in the cache be culled from the current working directory
   * - command: "cull <name>"
   */
  static int cachefiles_daemon_cull(struct cachefiles_cache *cache, char *args)
  {
542ce7a9b   Miklos Szeredi   cachefiles: use p...
559
  	struct path path;
9ae326a69   David Howells   CacheFiles: A cac...
560
561
562
563
564
565
566
567
568
  	const struct cred *saved_cred;
  	int ret;
  
  	_enter(",%s", args);
  
  	if (strchr(args, '/'))
  		goto inval;
  
  	if (!test_bit(CACHEFILES_READY, &cache->flags)) {
6ff66ac77   Fabian Frederick   fs/cachefiles: ad...
569
570
  		pr_err("cull applied to unready cache
  ");
9ae326a69   David Howells   CacheFiles: A cac...
571
572
573
574
  		return -EIO;
  	}
  
  	if (test_bit(CACHEFILES_DEAD, &cache->flags)) {
6ff66ac77   Fabian Frederick   fs/cachefiles: ad...
575
576
  		pr_err("cull applied to dead cache
  ");
9ae326a69   David Howells   CacheFiles: A cac...
577
578
579
580
  		return -EIO;
  	}
  
  	/* extract the directory dentry from the cwd */
f7ad3c6be   Miklos Szeredi   vfs: add helpers ...
581
  	get_fs_pwd(current->fs, &path);
9ae326a69   David Howells   CacheFiles: A cac...
582

ce40fa78e   David Howells   Cachefiles: Fix u...
583
  	if (!d_can_lookup(path.dentry))
9ae326a69   David Howells   CacheFiles: A cac...
584
585
586
  		goto notdir;
  
  	cachefiles_begin_secure(cache, &saved_cred);
542ce7a9b   Miklos Szeredi   cachefiles: use p...
587
  	ret = cachefiles_cull(cache, path.dentry, args);
9ae326a69   David Howells   CacheFiles: A cac...
588
  	cachefiles_end_secure(cache, saved_cred);
542ce7a9b   Miklos Szeredi   cachefiles: use p...
589
  	path_put(&path);
9ae326a69   David Howells   CacheFiles: A cac...
590
591
592
593
  	_leave(" = %d", ret);
  	return ret;
  
  notdir:
542ce7a9b   Miklos Szeredi   cachefiles: use p...
594
  	path_put(&path);
6ff66ac77   Fabian Frederick   fs/cachefiles: ad...
595
596
  	pr_err("cull command requires dirfd to be a directory
  ");
9ae326a69   David Howells   CacheFiles: A cac...
597
598
599
  	return -ENOTDIR;
  
  inval:
6ff66ac77   Fabian Frederick   fs/cachefiles: ad...
600
601
  	pr_err("cull command requires dirfd and filename
  ");
9ae326a69   David Howells   CacheFiles: A cac...
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
  	return -EINVAL;
  }
  
  /*
   * set debugging mode
   * - command: "debug <mask>"
   */
  static int cachefiles_daemon_debug(struct cachefiles_cache *cache, char *args)
  {
  	unsigned long mask;
  
  	_enter(",%s", args);
  
  	mask = simple_strtoul(args, &args, 0);
  	if (args[0] != '\0')
  		goto inval;
  
  	cachefiles_debug = mask;
  	_leave(" = 0");
  	return 0;
  
  inval:
6ff66ac77   Fabian Frederick   fs/cachefiles: ad...
624
625
  	pr_err("debug command requires mask
  ");
9ae326a69   David Howells   CacheFiles: A cac...
626
627
628
629
630
631
632
633
634
  	return -EINVAL;
  }
  
  /*
   * find out whether an object in the current working directory is in use or not
   * - command: "inuse <name>"
   */
  static int cachefiles_daemon_inuse(struct cachefiles_cache *cache, char *args)
  {
542ce7a9b   Miklos Szeredi   cachefiles: use p...
635
  	struct path path;
9ae326a69   David Howells   CacheFiles: A cac...
636
637
638
639
640
641
642
643
644
  	const struct cred *saved_cred;
  	int ret;
  
  	//_enter(",%s", args);
  
  	if (strchr(args, '/'))
  		goto inval;
  
  	if (!test_bit(CACHEFILES_READY, &cache->flags)) {
6ff66ac77   Fabian Frederick   fs/cachefiles: ad...
645
646
  		pr_err("inuse applied to unready cache
  ");
9ae326a69   David Howells   CacheFiles: A cac...
647
648
649
650
  		return -EIO;
  	}
  
  	if (test_bit(CACHEFILES_DEAD, &cache->flags)) {
6ff66ac77   Fabian Frederick   fs/cachefiles: ad...
651
652
  		pr_err("inuse applied to dead cache
  ");
9ae326a69   David Howells   CacheFiles: A cac...
653
654
655
656
  		return -EIO;
  	}
  
  	/* extract the directory dentry from the cwd */
f7ad3c6be   Miklos Szeredi   vfs: add helpers ...
657
  	get_fs_pwd(current->fs, &path);
9ae326a69   David Howells   CacheFiles: A cac...
658

ce40fa78e   David Howells   Cachefiles: Fix u...
659
  	if (!d_can_lookup(path.dentry))
9ae326a69   David Howells   CacheFiles: A cac...
660
661
662
  		goto notdir;
  
  	cachefiles_begin_secure(cache, &saved_cred);
542ce7a9b   Miklos Szeredi   cachefiles: use p...
663
  	ret = cachefiles_check_in_use(cache, path.dentry, args);
9ae326a69   David Howells   CacheFiles: A cac...
664
  	cachefiles_end_secure(cache, saved_cred);
542ce7a9b   Miklos Szeredi   cachefiles: use p...
665
  	path_put(&path);
9ae326a69   David Howells   CacheFiles: A cac...
666
667
668
669
  	//_leave(" = %d", ret);
  	return ret;
  
  notdir:
542ce7a9b   Miklos Szeredi   cachefiles: use p...
670
  	path_put(&path);
6ff66ac77   Fabian Frederick   fs/cachefiles: ad...
671
672
  	pr_err("inuse command requires dirfd to be a directory
  ");
9ae326a69   David Howells   CacheFiles: A cac...
673
674
675
  	return -ENOTDIR;
  
  inval:
6ff66ac77   Fabian Frederick   fs/cachefiles: ad...
676
677
  	pr_err("inuse command requires dirfd and filename
  ");
9ae326a69   David Howells   CacheFiles: A cac...
678
679
680
681
682
683
684
685
686
687
688
  	return -EINVAL;
  }
  
  /*
   * see if we have space for a number of pages and/or a number of files in the
   * cache
   */
  int cachefiles_has_space(struct cachefiles_cache *cache,
  			 unsigned fnr, unsigned bnr)
  {
  	struct kstatfs stats;
ebabe9a90   Christoph Hellwig   pass a struct pat...
689
690
691
692
  	struct path path = {
  		.mnt	= cache->mnt,
  		.dentry	= cache->mnt->mnt_root,
  	};
9ae326a69   David Howells   CacheFiles: A cac...
693
694
695
696
697
698
699
700
701
702
703
704
705
  	int ret;
  
  	//_enter("{%llu,%llu,%llu,%llu,%llu,%llu},%u,%u",
  	//       (unsigned long long) cache->frun,
  	//       (unsigned long long) cache->fcull,
  	//       (unsigned long long) cache->fstop,
  	//       (unsigned long long) cache->brun,
  	//       (unsigned long long) cache->bcull,
  	//       (unsigned long long) cache->bstop,
  	//       fnr, bnr);
  
  	/* find out how many pages of blockdev are available */
  	memset(&stats, 0, sizeof(stats));
ebabe9a90   Christoph Hellwig   pass a struct pat...
706
  	ret = vfs_statfs(&path, &stats);
9ae326a69   David Howells   CacheFiles: A cac...
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
  	if (ret < 0) {
  		if (ret == -EIO)
  			cachefiles_io_error(cache, "statfs failed");
  		_leave(" = %d", ret);
  		return ret;
  	}
  
  	stats.f_bavail >>= cache->bshift;
  
  	//_debug("avail %llu,%llu",
  	//       (unsigned long long) stats.f_ffree,
  	//       (unsigned long long) stats.f_bavail);
  
  	/* see if there is sufficient space */
  	if (stats.f_ffree > fnr)
  		stats.f_ffree -= fnr;
  	else
  		stats.f_ffree = 0;
  
  	if (stats.f_bavail > bnr)
  		stats.f_bavail -= bnr;
  	else
  		stats.f_bavail = 0;
  
  	ret = -ENOBUFS;
  	if (stats.f_ffree < cache->fstop ||
  	    stats.f_bavail < cache->bstop)
  		goto begin_cull;
  
  	ret = 0;
  	if (stats.f_ffree < cache->fcull ||
  	    stats.f_bavail < cache->bcull)
  		goto begin_cull;
  
  	if (test_bit(CACHEFILES_CULLING, &cache->flags) &&
  	    stats.f_ffree >= cache->frun &&
  	    stats.f_bavail >= cache->brun &&
  	    test_and_clear_bit(CACHEFILES_CULLING, &cache->flags)
  	    ) {
  		_debug("cease culling");
  		cachefiles_state_changed(cache);
  	}
  
  	//_leave(" = 0");
  	return 0;
  
  begin_cull:
  	if (!test_and_set_bit(CACHEFILES_CULLING, &cache->flags)) {
  		_debug("### CULL CACHE ###");
  		cachefiles_state_changed(cache);
  	}
  
  	_leave(" = %d", ret);
  	return ret;
  }