daemon.c 16 KB

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