daemon.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /* Daemon interface
  3. *
  4. * Copyright (C) 2007, 2021 Red Hat, Inc. All Rights Reserved.
  5. * Written by David Howells (dhowells@redhat.com)
  6. */
  7. #include <linux/module.h>
  8. #include <linux/init.h>
  9. #include <linux/sched.h>
  10. #include <linux/completion.h>
  11. #include <linux/slab.h>
  12. #include <linux/fs.h>
  13. #include <linux/file.h>
  14. #include <linux/namei.h>
  15. #include <linux/poll.h>
  16. #include <linux/mount.h>
  17. #include <linux/security.h>
  18. #include <linux/statfs.h>
  19. #include <linux/ctype.h>
  20. #include <linux/string.h>
  21. #include <linux/fs_struct.h>
  22. #include "internal.h"
  23. static int cachefiles_daemon_open(struct inode *, struct file *);
  24. static int cachefiles_daemon_release(struct inode *, struct file *);
  25. static ssize_t cachefiles_daemon_read(struct file *, char __user *, size_t,
  26. loff_t *);
  27. static ssize_t cachefiles_daemon_write(struct file *, const char __user *,
  28. size_t, loff_t *);
  29. static __poll_t cachefiles_daemon_poll(struct file *,
  30. struct poll_table_struct *);
  31. static int cachefiles_daemon_frun(struct cachefiles_cache *, char *);
  32. static int cachefiles_daemon_fcull(struct cachefiles_cache *, char *);
  33. static int cachefiles_daemon_fstop(struct cachefiles_cache *, char *);
  34. static int cachefiles_daemon_brun(struct cachefiles_cache *, char *);
  35. static int cachefiles_daemon_bcull(struct cachefiles_cache *, char *);
  36. static int cachefiles_daemon_bstop(struct cachefiles_cache *, char *);
  37. static int cachefiles_daemon_cull(struct cachefiles_cache *, char *);
  38. static int cachefiles_daemon_debug(struct cachefiles_cache *, char *);
  39. static int cachefiles_daemon_dir(struct cachefiles_cache *, char *);
  40. static int cachefiles_daemon_inuse(struct cachefiles_cache *, char *);
  41. static int cachefiles_daemon_secctx(struct cachefiles_cache *, char *);
  42. static int cachefiles_daemon_tag(struct cachefiles_cache *, char *);
  43. static int cachefiles_daemon_bind(struct cachefiles_cache *, char *);
  44. static void cachefiles_daemon_unbind(struct cachefiles_cache *);
  45. static unsigned long cachefiles_open;
  46. const struct file_operations cachefiles_daemon_fops = {
  47. .owner = THIS_MODULE,
  48. .open = cachefiles_daemon_open,
  49. .release = cachefiles_daemon_release,
  50. .read = cachefiles_daemon_read,
  51. .write = cachefiles_daemon_write,
  52. .poll = cachefiles_daemon_poll,
  53. .llseek = noop_llseek,
  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. #ifdef CONFIG_CACHEFILES_ONDEMAND
  74. { "copen", cachefiles_ondemand_copen },
  75. { "restore", cachefiles_ondemand_restore },
  76. #endif
  77. { "", NULL }
  78. };
  79. /*
  80. * Prepare a cache for caching.
  81. */
  82. static int cachefiles_daemon_open(struct inode *inode, struct file *file)
  83. {
  84. struct cachefiles_cache *cache;
  85. _enter("");
  86. /* only the superuser may do this */
  87. if (!capable(CAP_SYS_ADMIN))
  88. return -EPERM;
  89. /* the cachefiles device may only be open once at a time */
  90. if (xchg(&cachefiles_open, 1) == 1)
  91. return -EBUSY;
  92. /* allocate a cache record */
  93. cache = kzalloc_obj(struct cachefiles_cache);
  94. if (!cache) {
  95. cachefiles_open = 0;
  96. return -ENOMEM;
  97. }
  98. mutex_init(&cache->daemon_mutex);
  99. init_waitqueue_head(&cache->daemon_pollwq);
  100. INIT_LIST_HEAD(&cache->volumes);
  101. INIT_LIST_HEAD(&cache->object_list);
  102. spin_lock_init(&cache->object_list_lock);
  103. refcount_set(&cache->unbind_pincount, 1);
  104. xa_init_flags(&cache->reqs, XA_FLAGS_ALLOC);
  105. xa_init_flags(&cache->ondemand_ids, XA_FLAGS_ALLOC1);
  106. /* set default caching limits
  107. * - limit at 1% free space and/or free files
  108. * - cull below 5% free space and/or free files
  109. * - cease culling above 7% free space and/or free files
  110. */
  111. cache->frun_percent = 7;
  112. cache->fcull_percent = 5;
  113. cache->fstop_percent = 1;
  114. cache->brun_percent = 7;
  115. cache->bcull_percent = 5;
  116. cache->bstop_percent = 1;
  117. file->private_data = cache;
  118. cache->cachefilesd = file;
  119. return 0;
  120. }
  121. void cachefiles_flush_reqs(struct cachefiles_cache *cache)
  122. {
  123. struct xarray *xa = &cache->reqs;
  124. struct cachefiles_req *req;
  125. unsigned long index;
  126. /*
  127. * Make sure the following two operations won't be reordered.
  128. * 1) set CACHEFILES_DEAD bit
  129. * 2) flush requests in the xarray
  130. * Otherwise the request may be enqueued after xarray has been
  131. * flushed, leaving the orphan request never being completed.
  132. *
  133. * CPU 1 CPU 2
  134. * ===== =====
  135. * flush requests in the xarray
  136. * test CACHEFILES_DEAD bit
  137. * enqueue the request
  138. * set CACHEFILES_DEAD bit
  139. */
  140. smp_mb();
  141. xa_lock(xa);
  142. xa_for_each(xa, index, req) {
  143. req->error = -EIO;
  144. complete(&req->done);
  145. __xa_erase(xa, index);
  146. }
  147. xa_unlock(xa);
  148. xa_destroy(&cache->reqs);
  149. xa_destroy(&cache->ondemand_ids);
  150. }
  151. void cachefiles_put_unbind_pincount(struct cachefiles_cache *cache)
  152. {
  153. if (refcount_dec_and_test(&cache->unbind_pincount)) {
  154. cachefiles_daemon_unbind(cache);
  155. cachefiles_open = 0;
  156. kfree(cache);
  157. }
  158. }
  159. void cachefiles_get_unbind_pincount(struct cachefiles_cache *cache)
  160. {
  161. refcount_inc(&cache->unbind_pincount);
  162. }
  163. /*
  164. * Release a cache.
  165. */
  166. static int cachefiles_daemon_release(struct inode *inode, struct file *file)
  167. {
  168. struct cachefiles_cache *cache = file->private_data;
  169. _enter("");
  170. ASSERT(cache);
  171. set_bit(CACHEFILES_DEAD, &cache->flags);
  172. if (cachefiles_in_ondemand_mode(cache))
  173. cachefiles_flush_reqs(cache);
  174. /* clean up the control file interface */
  175. cache->cachefilesd = NULL;
  176. file->private_data = NULL;
  177. cachefiles_put_unbind_pincount(cache);
  178. _leave("");
  179. return 0;
  180. }
  181. static ssize_t cachefiles_do_daemon_read(struct cachefiles_cache *cache,
  182. char __user *_buffer, size_t buflen)
  183. {
  184. unsigned long long b_released;
  185. unsigned f_released;
  186. char buffer[256];
  187. int n;
  188. /* check how much space the cache has */
  189. cachefiles_has_space(cache, 0, 0, cachefiles_has_space_check);
  190. /* summarise */
  191. f_released = atomic_xchg(&cache->f_released, 0);
  192. b_released = atomic_long_xchg(&cache->b_released, 0);
  193. clear_bit(CACHEFILES_STATE_CHANGED, &cache->flags);
  194. n = snprintf(buffer, sizeof(buffer),
  195. "cull=%c"
  196. " frun=%llx"
  197. " fcull=%llx"
  198. " fstop=%llx"
  199. " brun=%llx"
  200. " bcull=%llx"
  201. " bstop=%llx"
  202. " freleased=%x"
  203. " breleased=%llx",
  204. test_bit(CACHEFILES_CULLING, &cache->flags) ? '1' : '0',
  205. (unsigned long long) cache->frun,
  206. (unsigned long long) cache->fcull,
  207. (unsigned long long) cache->fstop,
  208. (unsigned long long) cache->brun,
  209. (unsigned long long) cache->bcull,
  210. (unsigned long long) cache->bstop,
  211. f_released,
  212. b_released);
  213. if (n > buflen)
  214. return -EMSGSIZE;
  215. if (copy_to_user(_buffer, buffer, n) != 0)
  216. return -EFAULT;
  217. return n;
  218. }
  219. /*
  220. * Read the cache state.
  221. */
  222. static ssize_t cachefiles_daemon_read(struct file *file, char __user *_buffer,
  223. size_t buflen, loff_t *pos)
  224. {
  225. struct cachefiles_cache *cache = file->private_data;
  226. //_enter(",,%zu,", buflen);
  227. if (!test_bit(CACHEFILES_READY, &cache->flags))
  228. return 0;
  229. if (cachefiles_in_ondemand_mode(cache))
  230. return cachefiles_ondemand_daemon_read(cache, _buffer, buflen);
  231. else
  232. return cachefiles_do_daemon_read(cache, _buffer, buflen);
  233. }
  234. /*
  235. * Take a command from cachefilesd, parse it and act on it.
  236. */
  237. static ssize_t cachefiles_daemon_write(struct file *file,
  238. const char __user *_data,
  239. size_t datalen,
  240. loff_t *pos)
  241. {
  242. const struct cachefiles_daemon_cmd *cmd;
  243. struct cachefiles_cache *cache = file->private_data;
  244. ssize_t ret;
  245. char *data, *args, *cp;
  246. //_enter(",,%zu,", datalen);
  247. ASSERT(cache);
  248. if (test_bit(CACHEFILES_DEAD, &cache->flags))
  249. return -EIO;
  250. if (datalen > PAGE_SIZE - 1)
  251. return -EOPNOTSUPP;
  252. /* drag the command string into the kernel so we can parse it */
  253. data = memdup_user_nul(_data, datalen);
  254. if (IS_ERR(data))
  255. return PTR_ERR(data);
  256. ret = -EINVAL;
  257. if (memchr(data, '\0', datalen))
  258. goto error;
  259. /* strip any newline */
  260. cp = memchr(data, '\n', datalen);
  261. if (cp) {
  262. if (cp == data)
  263. goto error;
  264. *cp = '\0';
  265. }
  266. /* parse the command */
  267. ret = -EOPNOTSUPP;
  268. for (args = data; *args; args++)
  269. if (isspace(*args))
  270. break;
  271. if (*args) {
  272. if (args == data)
  273. goto error;
  274. *args = '\0';
  275. args = skip_spaces(++args);
  276. }
  277. /* run the appropriate command handler */
  278. for (cmd = cachefiles_daemon_cmds; cmd->name[0]; cmd++)
  279. if (strcmp(cmd->name, data) == 0)
  280. goto found_command;
  281. error:
  282. kfree(data);
  283. //_leave(" = %zd", ret);
  284. return ret;
  285. found_command:
  286. mutex_lock(&cache->daemon_mutex);
  287. ret = -EIO;
  288. if (!test_bit(CACHEFILES_DEAD, &cache->flags))
  289. ret = cmd->handler(cache, args);
  290. mutex_unlock(&cache->daemon_mutex);
  291. if (ret == 0)
  292. ret = datalen;
  293. goto error;
  294. }
  295. /*
  296. * Poll for culling state
  297. * - use EPOLLOUT to indicate culling state
  298. */
  299. static __poll_t cachefiles_daemon_poll(struct file *file,
  300. struct poll_table_struct *poll)
  301. {
  302. struct cachefiles_cache *cache = file->private_data;
  303. XA_STATE(xas, &cache->reqs, 0);
  304. struct cachefiles_req *req;
  305. __poll_t mask;
  306. poll_wait(file, &cache->daemon_pollwq, poll);
  307. mask = 0;
  308. if (cachefiles_in_ondemand_mode(cache)) {
  309. if (!xa_empty(&cache->reqs)) {
  310. xas_lock(&xas);
  311. xas_for_each_marked(&xas, req, ULONG_MAX, CACHEFILES_REQ_NEW) {
  312. if (!cachefiles_ondemand_is_reopening_read(req)) {
  313. mask |= EPOLLIN;
  314. break;
  315. }
  316. }
  317. xas_unlock(&xas);
  318. }
  319. } else {
  320. if (test_bit(CACHEFILES_STATE_CHANGED, &cache->flags))
  321. mask |= EPOLLIN;
  322. }
  323. if (test_bit(CACHEFILES_CULLING, &cache->flags))
  324. mask |= EPOLLOUT;
  325. return mask;
  326. }
  327. /*
  328. * Give a range error for cache space constraints
  329. * - can be tail-called
  330. */
  331. static int cachefiles_daemon_range_error(struct cachefiles_cache *cache,
  332. char *args)
  333. {
  334. pr_err("Free space limits must be in range 0%%<=stop<cull<run<100%%\n");
  335. return -EINVAL;
  336. }
  337. /*
  338. * Set the percentage of files at which to stop culling
  339. * - command: "frun <N>%"
  340. */
  341. static int cachefiles_daemon_frun(struct cachefiles_cache *cache, char *args)
  342. {
  343. unsigned long frun;
  344. _enter(",%s", args);
  345. if (!*args)
  346. return -EINVAL;
  347. frun = simple_strtoul(args, &args, 10);
  348. if (args[0] != '%' || args[1] != '\0')
  349. return -EINVAL;
  350. if (frun <= cache->fcull_percent || frun >= 100)
  351. return cachefiles_daemon_range_error(cache, args);
  352. cache->frun_percent = frun;
  353. return 0;
  354. }
  355. /*
  356. * Set the percentage of files at which to start culling
  357. * - command: "fcull <N>%"
  358. */
  359. static int cachefiles_daemon_fcull(struct cachefiles_cache *cache, char *args)
  360. {
  361. unsigned long fcull;
  362. _enter(",%s", args);
  363. if (!*args)
  364. return -EINVAL;
  365. fcull = simple_strtoul(args, &args, 10);
  366. if (args[0] != '%' || args[1] != '\0')
  367. return -EINVAL;
  368. if (fcull <= cache->fstop_percent || fcull >= cache->frun_percent)
  369. return cachefiles_daemon_range_error(cache, args);
  370. cache->fcull_percent = fcull;
  371. return 0;
  372. }
  373. /*
  374. * Set the percentage of files at which to stop allocating
  375. * - command: "fstop <N>%"
  376. */
  377. static int cachefiles_daemon_fstop(struct cachefiles_cache *cache, char *args)
  378. {
  379. unsigned long fstop;
  380. _enter(",%s", args);
  381. if (!*args)
  382. return -EINVAL;
  383. fstop = simple_strtoul(args, &args, 10);
  384. if (args[0] != '%' || args[1] != '\0')
  385. return -EINVAL;
  386. if (fstop >= cache->fcull_percent)
  387. return cachefiles_daemon_range_error(cache, args);
  388. cache->fstop_percent = fstop;
  389. return 0;
  390. }
  391. /*
  392. * Set the percentage of blocks at which to stop culling
  393. * - command: "brun <N>%"
  394. */
  395. static int cachefiles_daemon_brun(struct cachefiles_cache *cache, char *args)
  396. {
  397. unsigned long brun;
  398. _enter(",%s", args);
  399. if (!*args)
  400. return -EINVAL;
  401. brun = simple_strtoul(args, &args, 10);
  402. if (args[0] != '%' || args[1] != '\0')
  403. return -EINVAL;
  404. if (brun <= cache->bcull_percent || brun >= 100)
  405. return cachefiles_daemon_range_error(cache, args);
  406. cache->brun_percent = brun;
  407. return 0;
  408. }
  409. /*
  410. * Set the percentage of blocks at which to start culling
  411. * - command: "bcull <N>%"
  412. */
  413. static int cachefiles_daemon_bcull(struct cachefiles_cache *cache, char *args)
  414. {
  415. unsigned long bcull;
  416. _enter(",%s", args);
  417. if (!*args)
  418. return -EINVAL;
  419. bcull = simple_strtoul(args, &args, 10);
  420. if (args[0] != '%' || args[1] != '\0')
  421. return -EINVAL;
  422. if (bcull <= cache->bstop_percent || bcull >= cache->brun_percent)
  423. return cachefiles_daemon_range_error(cache, args);
  424. cache->bcull_percent = bcull;
  425. return 0;
  426. }
  427. /*
  428. * Set the percentage of blocks at which to stop allocating
  429. * - command: "bstop <N>%"
  430. */
  431. static int cachefiles_daemon_bstop(struct cachefiles_cache *cache, char *args)
  432. {
  433. unsigned long bstop;
  434. _enter(",%s", args);
  435. if (!*args)
  436. return -EINVAL;
  437. bstop = simple_strtoul(args, &args, 10);
  438. if (args[0] != '%' || args[1] != '\0')
  439. return -EINVAL;
  440. if (bstop >= cache->bcull_percent)
  441. return cachefiles_daemon_range_error(cache, args);
  442. cache->bstop_percent = bstop;
  443. return 0;
  444. }
  445. /*
  446. * Set the cache directory
  447. * - command: "dir <name>"
  448. */
  449. static int cachefiles_daemon_dir(struct cachefiles_cache *cache, char *args)
  450. {
  451. char *dir;
  452. _enter(",%s", args);
  453. if (!*args) {
  454. pr_err("Empty directory specified\n");
  455. return -EINVAL;
  456. }
  457. if (cache->rootdirname) {
  458. pr_err("Second cache directory specified\n");
  459. return -EEXIST;
  460. }
  461. dir = kstrdup(args, GFP_KERNEL);
  462. if (!dir)
  463. return -ENOMEM;
  464. cache->rootdirname = dir;
  465. return 0;
  466. }
  467. /*
  468. * Set the cache security context
  469. * - command: "secctx <ctx>"
  470. */
  471. static int cachefiles_daemon_secctx(struct cachefiles_cache *cache, char *args)
  472. {
  473. int err;
  474. _enter(",%s", args);
  475. if (!*args) {
  476. pr_err("Empty security context specified\n");
  477. return -EINVAL;
  478. }
  479. if (cache->have_secid) {
  480. pr_err("Second security context specified\n");
  481. return -EINVAL;
  482. }
  483. err = security_secctx_to_secid(args, strlen(args), &cache->secid);
  484. if (err)
  485. return err;
  486. cache->have_secid = true;
  487. return 0;
  488. }
  489. /*
  490. * Set the cache tag
  491. * - command: "tag <name>"
  492. */
  493. static int cachefiles_daemon_tag(struct cachefiles_cache *cache, char *args)
  494. {
  495. char *tag;
  496. _enter(",%s", args);
  497. if (!*args) {
  498. pr_err("Empty tag specified\n");
  499. return -EINVAL;
  500. }
  501. if (cache->tag)
  502. return -EEXIST;
  503. tag = kstrdup(args, GFP_KERNEL);
  504. if (!tag)
  505. return -ENOMEM;
  506. cache->tag = tag;
  507. return 0;
  508. }
  509. /*
  510. * Request a node in the cache be culled from the current working directory
  511. * - command: "cull <name>"
  512. */
  513. static int cachefiles_daemon_cull(struct cachefiles_cache *cache, char *args)
  514. {
  515. struct path path;
  516. const struct cred *saved_cred;
  517. int ret;
  518. _enter(",%s", args);
  519. if (strchr(args, '/'))
  520. goto inval;
  521. if (!test_bit(CACHEFILES_READY, &cache->flags)) {
  522. pr_err("cull applied to unready cache\n");
  523. return -EIO;
  524. }
  525. if (test_bit(CACHEFILES_DEAD, &cache->flags)) {
  526. pr_err("cull applied to dead cache\n");
  527. return -EIO;
  528. }
  529. get_fs_pwd(current->fs, &path);
  530. if (!d_can_lookup(path.dentry))
  531. goto notdir;
  532. cachefiles_begin_secure(cache, &saved_cred);
  533. ret = cachefiles_cull(cache, path.dentry, args);
  534. cachefiles_end_secure(cache, saved_cred);
  535. path_put(&path);
  536. _leave(" = %d", ret);
  537. return ret;
  538. notdir:
  539. path_put(&path);
  540. pr_err("cull command requires dirfd to be a directory\n");
  541. return -ENOTDIR;
  542. inval:
  543. pr_err("cull command requires dirfd and filename\n");
  544. return -EINVAL;
  545. }
  546. /*
  547. * Set debugging mode
  548. * - command: "debug <mask>"
  549. */
  550. static int cachefiles_daemon_debug(struct cachefiles_cache *cache, char *args)
  551. {
  552. unsigned long mask;
  553. _enter(",%s", args);
  554. mask = simple_strtoul(args, &args, 0);
  555. if (args[0] != '\0')
  556. goto inval;
  557. cachefiles_debug = mask;
  558. _leave(" = 0");
  559. return 0;
  560. inval:
  561. pr_err("debug command requires mask\n");
  562. return -EINVAL;
  563. }
  564. /*
  565. * Find out whether an object in the current working directory is in use or not
  566. * - command: "inuse <name>"
  567. */
  568. static int cachefiles_daemon_inuse(struct cachefiles_cache *cache, char *args)
  569. {
  570. struct path path;
  571. const struct cred *saved_cred;
  572. int ret;
  573. //_enter(",%s", args);
  574. if (strchr(args, '/'))
  575. goto inval;
  576. if (!test_bit(CACHEFILES_READY, &cache->flags)) {
  577. pr_err("inuse applied to unready cache\n");
  578. return -EIO;
  579. }
  580. if (test_bit(CACHEFILES_DEAD, &cache->flags)) {
  581. pr_err("inuse applied to dead cache\n");
  582. return -EIO;
  583. }
  584. get_fs_pwd(current->fs, &path);
  585. if (!d_can_lookup(path.dentry))
  586. goto notdir;
  587. cachefiles_begin_secure(cache, &saved_cred);
  588. ret = cachefiles_check_in_use(cache, path.dentry, args);
  589. cachefiles_end_secure(cache, saved_cred);
  590. path_put(&path);
  591. //_leave(" = %d", ret);
  592. return ret;
  593. notdir:
  594. path_put(&path);
  595. pr_err("inuse command requires dirfd to be a directory\n");
  596. return -ENOTDIR;
  597. inval:
  598. pr_err("inuse command requires dirfd and filename\n");
  599. return -EINVAL;
  600. }
  601. /*
  602. * Bind a directory as a cache
  603. */
  604. static int cachefiles_daemon_bind(struct cachefiles_cache *cache, char *args)
  605. {
  606. _enter("{%u,%u,%u,%u,%u,%u},%s",
  607. cache->frun_percent,
  608. cache->fcull_percent,
  609. cache->fstop_percent,
  610. cache->brun_percent,
  611. cache->bcull_percent,
  612. cache->bstop_percent,
  613. args);
  614. if (cache->fstop_percent >= cache->fcull_percent ||
  615. cache->fcull_percent >= cache->frun_percent ||
  616. cache->frun_percent >= 100)
  617. return -ERANGE;
  618. if (cache->bstop_percent >= cache->bcull_percent ||
  619. cache->bcull_percent >= cache->brun_percent ||
  620. cache->brun_percent >= 100)
  621. return -ERANGE;
  622. if (!cache->rootdirname) {
  623. pr_err("No cache directory specified\n");
  624. return -EINVAL;
  625. }
  626. /* Don't permit already bound caches to be re-bound */
  627. if (test_bit(CACHEFILES_READY, &cache->flags)) {
  628. pr_err("Cache already bound\n");
  629. return -EBUSY;
  630. }
  631. if (IS_ENABLED(CONFIG_CACHEFILES_ONDEMAND)) {
  632. if (!strcmp(args, "ondemand")) {
  633. set_bit(CACHEFILES_ONDEMAND_MODE, &cache->flags);
  634. } else if (*args) {
  635. pr_err("Invalid argument to the 'bind' command\n");
  636. return -EINVAL;
  637. }
  638. } else if (*args) {
  639. pr_err("'bind' command doesn't take an argument\n");
  640. return -EINVAL;
  641. }
  642. /* Make sure we have copies of the tag string */
  643. if (!cache->tag) {
  644. /*
  645. * The tag string is released by the fops->release()
  646. * function, so we don't release it on error here
  647. */
  648. cache->tag = kstrdup("CacheFiles", GFP_KERNEL);
  649. if (!cache->tag)
  650. return -ENOMEM;
  651. }
  652. return cachefiles_add_cache(cache);
  653. }
  654. /*
  655. * Unbind a cache.
  656. */
  657. static void cachefiles_daemon_unbind(struct cachefiles_cache *cache)
  658. {
  659. _enter("");
  660. if (test_bit(CACHEFILES_READY, &cache->flags))
  661. cachefiles_withdraw_cache(cache);
  662. cachefiles_put_directory(cache->graveyard);
  663. cachefiles_put_directory(cache->store);
  664. mntput(cache->mnt);
  665. put_cred(cache->cache_cred);
  666. kfree(cache->rootdirname);
  667. kfree(cache->tag);
  668. _leave("");
  669. }