daemon.c 16 KB

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