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. };
  55. struct cachefiles_daemon_cmd {
  56. char name[8];
  57. int (*handler)(struct cachefiles_cache *cache, char *args);
  58. };
  59. static const struct cachefiles_daemon_cmd cachefiles_daemon_cmds[] = {
  60. { "bind", cachefiles_daemon_bind },
  61. { "brun", cachefiles_daemon_brun },
  62. { "bcull", cachefiles_daemon_bcull },
  63. { "bstop", cachefiles_daemon_bstop },
  64. { "cull", cachefiles_daemon_cull },
  65. { "debug", cachefiles_daemon_debug },
  66. { "dir", cachefiles_daemon_dir },
  67. { "frun", cachefiles_daemon_frun },
  68. { "fcull", cachefiles_daemon_fcull },
  69. { "fstop", cachefiles_daemon_fstop },
  70. { "inuse", cachefiles_daemon_inuse },
  71. { "secctx", cachefiles_daemon_secctx },
  72. { "tag", cachefiles_daemon_tag },
  73. { "", NULL }
  74. };
  75. /*
  76. * do various checks
  77. */
  78. static int cachefiles_daemon_open(struct inode *inode, struct file *file)
  79. {
  80. struct cachefiles_cache *cache;
  81. _enter("");
  82. /* only the superuser may do this */
  83. if (!capable(CAP_SYS_ADMIN))
  84. return -EPERM;
  85. /* the cachefiles device may only be open once at a time */
  86. if (xchg(&cachefiles_open, 1) == 1)
  87. return -EBUSY;
  88. /* allocate a cache record */
  89. cache = kzalloc(sizeof(struct cachefiles_cache), GFP_KERNEL);
  90. if (!cache) {
  91. cachefiles_open = 0;
  92. return -ENOMEM;
  93. }
  94. mutex_init(&cache->daemon_mutex);
  95. cache->active_nodes = RB_ROOT;
  96. rwlock_init(&cache->active_lock);
  97. init_waitqueue_head(&cache->daemon_pollwq);
  98. /* set default caching limits
  99. * - limit at 1% free space and/or free files
  100. * - cull below 5% free space and/or free files
  101. * - cease culling above 7% free space and/or free files
  102. */
  103. cache->frun_percent = 7;
  104. cache->fcull_percent = 5;
  105. cache->fstop_percent = 1;
  106. cache->brun_percent = 7;
  107. cache->bcull_percent = 5;
  108. cache->bstop_percent = 1;
  109. file->private_data = cache;
  110. cache->cachefilesd = file;
  111. return 0;
  112. }
  113. /*
  114. * release a cache
  115. */
  116. static int cachefiles_daemon_release(struct inode *inode, struct file *file)
  117. {
  118. struct cachefiles_cache *cache = file->private_data;
  119. _enter("");
  120. ASSERT(cache);
  121. set_bit(CACHEFILES_DEAD, &cache->flags);
  122. cachefiles_daemon_unbind(cache);
  123. ASSERT(!cache->active_nodes.rb_node);
  124. /* clean up the control file interface */
  125. cache->cachefilesd = NULL;
  126. file->private_data = NULL;
  127. cachefiles_open = 0;
  128. kfree(cache);
  129. _leave("");
  130. return 0;
  131. }
  132. /*
  133. * read the cache state
  134. */
  135. static ssize_t cachefiles_daemon_read(struct file *file, char __user *_buffer,
  136. size_t buflen, loff_t *pos)
  137. {
  138. struct cachefiles_cache *cache = file->private_data;
  139. char buffer[256];
  140. int n;
  141. //_enter(",,%zu,", buflen);
  142. if (!test_bit(CACHEFILES_READY, &cache->flags))
  143. return 0;
  144. /* check how much space the cache has */
  145. cachefiles_has_space(cache, 0, 0);
  146. /* summarise */
  147. clear_bit(CACHEFILES_STATE_CHANGED, &cache->flags);
  148. n = snprintf(buffer, sizeof(buffer),
  149. "cull=%c"
  150. " frun=%llx"
  151. " fcull=%llx"
  152. " fstop=%llx"
  153. " brun=%llx"
  154. " bcull=%llx"
  155. " bstop=%llx",
  156. test_bit(CACHEFILES_CULLING, &cache->flags) ? '1' : '0',
  157. (unsigned long long) cache->frun,
  158. (unsigned long long) cache->fcull,
  159. (unsigned long long) cache->fstop,
  160. (unsigned long long) cache->brun,
  161. (unsigned long long) cache->bcull,
  162. (unsigned long long) cache->bstop
  163. );
  164. if (n > buflen)
  165. return -EMSGSIZE;
  166. if (copy_to_user(_buffer, buffer, n) != 0)
  167. return -EFAULT;
  168. return n;
  169. }
  170. /*
  171. * command the cache
  172. */
  173. static ssize_t cachefiles_daemon_write(struct file *file,
  174. const char __user *_data,
  175. size_t datalen,
  176. loff_t *pos)
  177. {
  178. const struct cachefiles_daemon_cmd *cmd;
  179. struct cachefiles_cache *cache = file->private_data;
  180. ssize_t ret;
  181. char *data, *args, *cp;
  182. //_enter(",,%zu,", datalen);
  183. ASSERT(cache);
  184. if (test_bit(CACHEFILES_DEAD, &cache->flags))
  185. return -EIO;
  186. if (datalen < 0 || datalen > PAGE_SIZE - 1)
  187. return -EOPNOTSUPP;
  188. /* drag the command string into the kernel so we can parse it */
  189. data = kmalloc(datalen + 1, GFP_KERNEL);
  190. if (!data)
  191. return -ENOMEM;
  192. ret = -EFAULT;
  193. if (copy_from_user(data, _data, datalen) != 0)
  194. goto error;
  195. data[datalen] = '\0';
  196. ret = -EINVAL;
  197. if (memchr(data, '\0', datalen))
  198. goto error;
  199. /* strip any newline */
  200. cp = memchr(data, '\n', datalen);
  201. if (cp) {
  202. if (cp == data)
  203. goto error;
  204. *cp = '\0';
  205. }
  206. /* parse the command */
  207. ret = -EOPNOTSUPP;
  208. for (args = data; *args; args++)
  209. if (isspace(*args))
  210. break;
  211. if (*args) {
  212. if (args == data)
  213. goto error;
  214. *args = '\0';
  215. args = skip_spaces(++args);
  216. }
  217. /* run the appropriate command handler */
  218. for (cmd = cachefiles_daemon_cmds; cmd->name[0]; cmd++)
  219. if (strcmp(cmd->name, data) == 0)
  220. goto found_command;
  221. error:
  222. kfree(data);
  223. //_leave(" = %zd", ret);
  224. return ret;
  225. found_command:
  226. mutex_lock(&cache->daemon_mutex);
  227. ret = -EIO;
  228. if (!test_bit(CACHEFILES_DEAD, &cache->flags))
  229. ret = cmd->handler(cache, args);
  230. mutex_unlock(&cache->daemon_mutex);
  231. if (ret == 0)
  232. ret = datalen;
  233. goto error;
  234. }
  235. /*
  236. * poll for culling state
  237. * - use POLLOUT to indicate culling state
  238. */
  239. static unsigned int cachefiles_daemon_poll(struct file *file,
  240. struct poll_table_struct *poll)
  241. {
  242. struct cachefiles_cache *cache = file->private_data;
  243. unsigned int mask;
  244. poll_wait(file, &cache->daemon_pollwq, poll);
  245. mask = 0;
  246. if (test_bit(CACHEFILES_STATE_CHANGED, &cache->flags))
  247. mask |= POLLIN;
  248. if (test_bit(CACHEFILES_CULLING, &cache->flags))
  249. mask |= POLLOUT;
  250. return mask;
  251. }
  252. /*
  253. * give a range error for cache space constraints
  254. * - can be tail-called
  255. */
  256. static int cachefiles_daemon_range_error(struct cachefiles_cache *cache,
  257. char *args)
  258. {
  259. kerror("Free space limits must be in range"
  260. " 0%%<=stop<cull<run<100%%");
  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. kerror("Empty directory specified");
  381. return -EINVAL;
  382. }
  383. if (cache->rootdirname) {
  384. kerror("Second cache directory specified");
  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. kerror("Empty security context specified");
  403. return -EINVAL;
  404. }
  405. if (cache->secctx) {
  406. kerror("Second security context specified");
  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. kerror("Empty tag specified");
  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. kerror("cull applied to unready cache");
  449. return -EIO;
  450. }
  451. if (test_bit(CACHEFILES_DEAD, &cache->flags)) {
  452. kerror("cull applied to dead cache");
  453. return -EIO;
  454. }
  455. /* extract the directory dentry from the cwd */
  456. get_fs_pwd(current->fs, &path);
  457. if (!S_ISDIR(path.dentry->d_inode->i_mode))
  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. kerror("cull command requires dirfd to be a directory");
  468. return -ENOTDIR;
  469. inval:
  470. kerror("cull command requires dirfd and filename");
  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. kerror("debug command requires mask");
  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. kerror("inuse applied to unready cache");
  505. return -EIO;
  506. }
  507. if (test_bit(CACHEFILES_DEAD, &cache->flags)) {
  508. kerror("inuse applied to dead cache");
  509. return -EIO;
  510. }
  511. /* extract the directory dentry from the cwd */
  512. get_fs_pwd(current->fs, &path);
  513. if (!S_ISDIR(path.dentry->d_inode->i_mode))
  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. kerror("inuse command requires dirfd to be a directory");
  524. return -ENOTDIR;
  525. inval:
  526. kerror("inuse command requires dirfd and filename");
  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. }