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