daemon.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751
  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 = kmalloc(datalen + 1, GFP_KERNEL);
  191. if (!data)
  192. return -ENOMEM;
  193. ret = -EFAULT;
  194. if (copy_from_user(data, _data, datalen) != 0)
  195. goto error;
  196. data[datalen] = '\0';
  197. ret = -EINVAL;
  198. if (memchr(data, '\0', datalen))
  199. goto error;
  200. /* strip any newline */
  201. cp = memchr(data, '\n', datalen);
  202. if (cp) {
  203. if (cp == data)
  204. goto error;
  205. *cp = '\0';
  206. }
  207. /* parse the command */
  208. ret = -EOPNOTSUPP;
  209. for (args = data; *args; args++)
  210. if (isspace(*args))
  211. break;
  212. if (*args) {
  213. if (args == data)
  214. goto error;
  215. *args = '\0';
  216. args = skip_spaces(++args);
  217. }
  218. /* run the appropriate command handler */
  219. for (cmd = cachefiles_daemon_cmds; cmd->name[0]; cmd++)
  220. if (strcmp(cmd->name, data) == 0)
  221. goto found_command;
  222. error:
  223. kfree(data);
  224. //_leave(" = %zd", ret);
  225. return ret;
  226. found_command:
  227. mutex_lock(&cache->daemon_mutex);
  228. ret = -EIO;
  229. if (!test_bit(CACHEFILES_DEAD, &cache->flags))
  230. ret = cmd->handler(cache, args);
  231. mutex_unlock(&cache->daemon_mutex);
  232. if (ret == 0)
  233. ret = datalen;
  234. goto error;
  235. }
  236. /*
  237. * poll for culling state
  238. * - use POLLOUT to indicate culling state
  239. */
  240. static unsigned int cachefiles_daemon_poll(struct file *file,
  241. struct poll_table_struct *poll)
  242. {
  243. struct cachefiles_cache *cache = file->private_data;
  244. unsigned int mask;
  245. poll_wait(file, &cache->daemon_pollwq, poll);
  246. mask = 0;
  247. if (test_bit(CACHEFILES_STATE_CHANGED, &cache->flags))
  248. mask |= POLLIN;
  249. if (test_bit(CACHEFILES_CULLING, &cache->flags))
  250. mask |= POLLOUT;
  251. return mask;
  252. }
  253. /*
  254. * give a range error for cache space constraints
  255. * - can be tail-called
  256. */
  257. static int cachefiles_daemon_range_error(struct cachefiles_cache *cache,
  258. char *args)
  259. {
  260. pr_err("Free space limits must be in range 0%%<=stop<cull<run<100%%\n");
  261. return -EINVAL;
  262. }
  263. /*
  264. * set the percentage of files at which to stop culling
  265. * - command: "frun <N>%"
  266. */
  267. static int cachefiles_daemon_frun(struct cachefiles_cache *cache, char *args)
  268. {
  269. unsigned long frun;
  270. _enter(",%s", args);
  271. if (!*args)
  272. return -EINVAL;
  273. frun = simple_strtoul(args, &args, 10);
  274. if (args[0] != '%' || args[1] != '\0')
  275. return -EINVAL;
  276. if (frun <= cache->fcull_percent || frun >= 100)
  277. return cachefiles_daemon_range_error(cache, args);
  278. cache->frun_percent = frun;
  279. return 0;
  280. }
  281. /*
  282. * set the percentage of files at which to start culling
  283. * - command: "fcull <N>%"
  284. */
  285. static int cachefiles_daemon_fcull(struct cachefiles_cache *cache, char *args)
  286. {
  287. unsigned long fcull;
  288. _enter(",%s", args);
  289. if (!*args)
  290. return -EINVAL;
  291. fcull = simple_strtoul(args, &args, 10);
  292. if (args[0] != '%' || args[1] != '\0')
  293. return -EINVAL;
  294. if (fcull <= cache->fstop_percent || fcull >= cache->frun_percent)
  295. return cachefiles_daemon_range_error(cache, args);
  296. cache->fcull_percent = fcull;
  297. return 0;
  298. }
  299. /*
  300. * set the percentage of files at which to stop allocating
  301. * - command: "fstop <N>%"
  302. */
  303. static int cachefiles_daemon_fstop(struct cachefiles_cache *cache, char *args)
  304. {
  305. unsigned long fstop;
  306. _enter(",%s", args);
  307. if (!*args)
  308. return -EINVAL;
  309. fstop = simple_strtoul(args, &args, 10);
  310. if (args[0] != '%' || args[1] != '\0')
  311. return -EINVAL;
  312. if (fstop < 0 || fstop >= cache->fcull_percent)
  313. return cachefiles_daemon_range_error(cache, args);
  314. cache->fstop_percent = fstop;
  315. return 0;
  316. }
  317. /*
  318. * set the percentage of blocks at which to stop culling
  319. * - command: "brun <N>%"
  320. */
  321. static int cachefiles_daemon_brun(struct cachefiles_cache *cache, char *args)
  322. {
  323. unsigned long brun;
  324. _enter(",%s", args);
  325. if (!*args)
  326. return -EINVAL;
  327. brun = simple_strtoul(args, &args, 10);
  328. if (args[0] != '%' || args[1] != '\0')
  329. return -EINVAL;
  330. if (brun <= cache->bcull_percent || brun >= 100)
  331. return cachefiles_daemon_range_error(cache, args);
  332. cache->brun_percent = brun;
  333. return 0;
  334. }
  335. /*
  336. * set the percentage of blocks at which to start culling
  337. * - command: "bcull <N>%"
  338. */
  339. static int cachefiles_daemon_bcull(struct cachefiles_cache *cache, char *args)
  340. {
  341. unsigned long bcull;
  342. _enter(",%s", args);
  343. if (!*args)
  344. return -EINVAL;
  345. bcull = simple_strtoul(args, &args, 10);
  346. if (args[0] != '%' || args[1] != '\0')
  347. return -EINVAL;
  348. if (bcull <= cache->bstop_percent || bcull >= cache->brun_percent)
  349. return cachefiles_daemon_range_error(cache, args);
  350. cache->bcull_percent = bcull;
  351. return 0;
  352. }
  353. /*
  354. * set the percentage of blocks at which to stop allocating
  355. * - command: "bstop <N>%"
  356. */
  357. static int cachefiles_daemon_bstop(struct cachefiles_cache *cache, char *args)
  358. {
  359. unsigned long bstop;
  360. _enter(",%s", args);
  361. if (!*args)
  362. return -EINVAL;
  363. bstop = simple_strtoul(args, &args, 10);
  364. if (args[0] != '%' || args[1] != '\0')
  365. return -EINVAL;
  366. if (bstop < 0 || bstop >= cache->bcull_percent)
  367. return cachefiles_daemon_range_error(cache, args);
  368. cache->bstop_percent = bstop;
  369. return 0;
  370. }
  371. /*
  372. * set the cache directory
  373. * - command: "dir <name>"
  374. */
  375. static int cachefiles_daemon_dir(struct cachefiles_cache *cache, char *args)
  376. {
  377. char *dir;
  378. _enter(",%s", args);
  379. if (!*args) {
  380. pr_err("Empty directory specified\n");
  381. return -EINVAL;
  382. }
  383. if (cache->rootdirname) {
  384. pr_err("Second cache directory specified\n");
  385. return -EEXIST;
  386. }
  387. dir = kstrdup(args, GFP_KERNEL);
  388. if (!dir)
  389. return -ENOMEM;
  390. cache->rootdirname = dir;
  391. return 0;
  392. }
  393. /*
  394. * set the cache security context
  395. * - command: "secctx <ctx>"
  396. */
  397. static int cachefiles_daemon_secctx(struct cachefiles_cache *cache, char *args)
  398. {
  399. char *secctx;
  400. _enter(",%s", args);
  401. if (!*args) {
  402. pr_err("Empty security context specified\n");
  403. return -EINVAL;
  404. }
  405. if (cache->secctx) {
  406. pr_err("Second security context specified\n");
  407. return -EINVAL;
  408. }
  409. secctx = kstrdup(args, GFP_KERNEL);
  410. if (!secctx)
  411. return -ENOMEM;
  412. cache->secctx = secctx;
  413. return 0;
  414. }
  415. /*
  416. * set the cache tag
  417. * - command: "tag <name>"
  418. */
  419. static int cachefiles_daemon_tag(struct cachefiles_cache *cache, char *args)
  420. {
  421. char *tag;
  422. _enter(",%s", args);
  423. if (!*args) {
  424. pr_err("Empty tag specified\n");
  425. return -EINVAL;
  426. }
  427. if (cache->tag)
  428. return -EEXIST;
  429. tag = kstrdup(args, GFP_KERNEL);
  430. if (!tag)
  431. return -ENOMEM;
  432. cache->tag = tag;
  433. return 0;
  434. }
  435. /*
  436. * request a node in the cache be culled from the current working directory
  437. * - command: "cull <name>"
  438. */
  439. static int cachefiles_daemon_cull(struct cachefiles_cache *cache, char *args)
  440. {
  441. struct path path;
  442. const struct cred *saved_cred;
  443. int ret;
  444. _enter(",%s", args);
  445. if (strchr(args, '/'))
  446. goto inval;
  447. if (!test_bit(CACHEFILES_READY, &cache->flags)) {
  448. pr_err("cull applied to unready cache\n");
  449. return -EIO;
  450. }
  451. if (test_bit(CACHEFILES_DEAD, &cache->flags)) {
  452. pr_err("cull applied to dead cache\n");
  453. return -EIO;
  454. }
  455. /* extract the directory dentry from the cwd */
  456. get_fs_pwd(current->fs, &path);
  457. if (!d_can_lookup(path.dentry))
  458. goto notdir;
  459. cachefiles_begin_secure(cache, &saved_cred);
  460. ret = cachefiles_cull(cache, path.dentry, args);
  461. cachefiles_end_secure(cache, saved_cred);
  462. path_put(&path);
  463. _leave(" = %d", ret);
  464. return ret;
  465. notdir:
  466. path_put(&path);
  467. pr_err("cull command requires dirfd to be a directory\n");
  468. return -ENOTDIR;
  469. inval:
  470. pr_err("cull command requires dirfd and filename\n");
  471. return -EINVAL;
  472. }
  473. /*
  474. * set debugging mode
  475. * - command: "debug <mask>"
  476. */
  477. static int cachefiles_daemon_debug(struct cachefiles_cache *cache, char *args)
  478. {
  479. unsigned long mask;
  480. _enter(",%s", args);
  481. mask = simple_strtoul(args, &args, 0);
  482. if (args[0] != '\0')
  483. goto inval;
  484. cachefiles_debug = mask;
  485. _leave(" = 0");
  486. return 0;
  487. inval:
  488. pr_err("debug command requires mask\n");
  489. return -EINVAL;
  490. }
  491. /*
  492. * find out whether an object in the current working directory is in use or not
  493. * - command: "inuse <name>"
  494. */
  495. static int cachefiles_daemon_inuse(struct cachefiles_cache *cache, char *args)
  496. {
  497. struct path path;
  498. const struct cred *saved_cred;
  499. int ret;
  500. //_enter(",%s", args);
  501. if (strchr(args, '/'))
  502. goto inval;
  503. if (!test_bit(CACHEFILES_READY, &cache->flags)) {
  504. pr_err("inuse applied to unready cache\n");
  505. return -EIO;
  506. }
  507. if (test_bit(CACHEFILES_DEAD, &cache->flags)) {
  508. pr_err("inuse applied to dead cache\n");
  509. return -EIO;
  510. }
  511. /* extract the directory dentry from the cwd */
  512. get_fs_pwd(current->fs, &path);
  513. if (!d_can_lookup(path.dentry))
  514. goto notdir;
  515. cachefiles_begin_secure(cache, &saved_cred);
  516. ret = cachefiles_check_in_use(cache, path.dentry, args);
  517. cachefiles_end_secure(cache, saved_cred);
  518. path_put(&path);
  519. //_leave(" = %d", ret);
  520. return ret;
  521. notdir:
  522. path_put(&path);
  523. pr_err("inuse command requires dirfd to be a directory\n");
  524. return -ENOTDIR;
  525. inval:
  526. pr_err("inuse command requires dirfd and filename\n");
  527. return -EINVAL;
  528. }
  529. /*
  530. * see if we have space for a number of pages and/or a number of files in the
  531. * cache
  532. */
  533. int cachefiles_has_space(struct cachefiles_cache *cache,
  534. unsigned fnr, unsigned bnr)
  535. {
  536. struct kstatfs stats;
  537. struct path path = {
  538. .mnt = cache->mnt,
  539. .dentry = cache->mnt->mnt_root,
  540. };
  541. int ret;
  542. //_enter("{%llu,%llu,%llu,%llu,%llu,%llu},%u,%u",
  543. // (unsigned long long) cache->frun,
  544. // (unsigned long long) cache->fcull,
  545. // (unsigned long long) cache->fstop,
  546. // (unsigned long long) cache->brun,
  547. // (unsigned long long) cache->bcull,
  548. // (unsigned long long) cache->bstop,
  549. // fnr, bnr);
  550. /* find out how many pages of blockdev are available */
  551. memset(&stats, 0, sizeof(stats));
  552. ret = vfs_statfs(&path, &stats);
  553. if (ret < 0) {
  554. if (ret == -EIO)
  555. cachefiles_io_error(cache, "statfs failed");
  556. _leave(" = %d", ret);
  557. return ret;
  558. }
  559. stats.f_bavail >>= cache->bshift;
  560. //_debug("avail %llu,%llu",
  561. // (unsigned long long) stats.f_ffree,
  562. // (unsigned long long) stats.f_bavail);
  563. /* see if there is sufficient space */
  564. if (stats.f_ffree > fnr)
  565. stats.f_ffree -= fnr;
  566. else
  567. stats.f_ffree = 0;
  568. if (stats.f_bavail > bnr)
  569. stats.f_bavail -= bnr;
  570. else
  571. stats.f_bavail = 0;
  572. ret = -ENOBUFS;
  573. if (stats.f_ffree < cache->fstop ||
  574. stats.f_bavail < cache->bstop)
  575. goto begin_cull;
  576. ret = 0;
  577. if (stats.f_ffree < cache->fcull ||
  578. stats.f_bavail < cache->bcull)
  579. goto begin_cull;
  580. if (test_bit(CACHEFILES_CULLING, &cache->flags) &&
  581. stats.f_ffree >= cache->frun &&
  582. stats.f_bavail >= cache->brun &&
  583. test_and_clear_bit(CACHEFILES_CULLING, &cache->flags)
  584. ) {
  585. _debug("cease culling");
  586. cachefiles_state_changed(cache);
  587. }
  588. //_leave(" = 0");
  589. return 0;
  590. begin_cull:
  591. if (!test_and_set_bit(CACHEFILES_CULLING, &cache->flags)) {
  592. _debug("### CULL CACHE ###");
  593. cachefiles_state_changed(cache);
  594. }
  595. _leave(" = %d", ret);
  596. return ret;
  597. }