bind.c 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /* Bind and unbind a cache from the filesystem backing it
  3. *
  4. * Copyright (C) 2007 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/mount.h>
  16. #include <linux/statfs.h>
  17. #include <linux/ctype.h>
  18. #include <linux/xattr.h>
  19. #include "internal.h"
  20. static int cachefiles_daemon_add_cache(struct cachefiles_cache *caches);
  21. /*
  22. * bind a directory as a cache
  23. */
  24. int cachefiles_daemon_bind(struct cachefiles_cache *cache, char *args)
  25. {
  26. _enter("{%u,%u,%u,%u,%u,%u},%s",
  27. cache->frun_percent,
  28. cache->fcull_percent,
  29. cache->fstop_percent,
  30. cache->brun_percent,
  31. cache->bcull_percent,
  32. cache->bstop_percent,
  33. args);
  34. /* start by checking things over */
  35. ASSERT(cache->fstop_percent >= 0 &&
  36. cache->fstop_percent < cache->fcull_percent &&
  37. cache->fcull_percent < cache->frun_percent &&
  38. cache->frun_percent < 100);
  39. ASSERT(cache->bstop_percent >= 0 &&
  40. cache->bstop_percent < cache->bcull_percent &&
  41. cache->bcull_percent < cache->brun_percent &&
  42. cache->brun_percent < 100);
  43. if (*args) {
  44. pr_err("'bind' command doesn't take an argument\n");
  45. return -EINVAL;
  46. }
  47. if (!cache->rootdirname) {
  48. pr_err("No cache directory specified\n");
  49. return -EINVAL;
  50. }
  51. /* don't permit already bound caches to be re-bound */
  52. if (test_bit(CACHEFILES_READY, &cache->flags)) {
  53. pr_err("Cache already bound\n");
  54. return -EBUSY;
  55. }
  56. /* make sure we have copies of the tag and dirname strings */
  57. if (!cache->tag) {
  58. /* the tag string is released by the fops->release()
  59. * function, so we don't release it on error here */
  60. cache->tag = kstrdup("CacheFiles", GFP_KERNEL);
  61. if (!cache->tag)
  62. return -ENOMEM;
  63. }
  64. /* add the cache */
  65. return cachefiles_daemon_add_cache(cache);
  66. }
  67. /*
  68. * add a cache
  69. */
  70. static int cachefiles_daemon_add_cache(struct cachefiles_cache *cache)
  71. {
  72. struct cachefiles_object *fsdef;
  73. struct path path;
  74. struct kstatfs stats;
  75. struct dentry *graveyard, *cachedir, *root;
  76. const struct cred *saved_cred;
  77. int ret;
  78. _enter("");
  79. /* we want to work under the module's security ID */
  80. ret = cachefiles_get_security_ID(cache);
  81. if (ret < 0)
  82. return ret;
  83. cachefiles_begin_secure(cache, &saved_cred);
  84. /* allocate the root index object */
  85. ret = -ENOMEM;
  86. fsdef = kmem_cache_alloc(cachefiles_object_jar, GFP_KERNEL);
  87. if (!fsdef)
  88. goto error_root_object;
  89. ASSERTCMP(fsdef->backer, ==, NULL);
  90. atomic_set(&fsdef->usage, 1);
  91. fsdef->type = FSCACHE_COOKIE_TYPE_INDEX;
  92. _debug("- fsdef %p", fsdef);
  93. /* look up the directory at the root of the cache */
  94. ret = kern_path(cache->rootdirname, LOOKUP_DIRECTORY, &path);
  95. if (ret < 0)
  96. goto error_open_root;
  97. cache->mnt = path.mnt;
  98. root = path.dentry;
  99. /* check parameters */
  100. ret = -EOPNOTSUPP;
  101. if (d_is_negative(root) ||
  102. !d_backing_inode(root)->i_op->lookup ||
  103. !d_backing_inode(root)->i_op->mkdir ||
  104. !(d_backing_inode(root)->i_opflags & IOP_XATTR) ||
  105. !root->d_sb->s_op->statfs ||
  106. !root->d_sb->s_op->sync_fs)
  107. goto error_unsupported;
  108. ret = -EROFS;
  109. if (sb_rdonly(root->d_sb))
  110. goto error_unsupported;
  111. /* determine the security of the on-disk cache as this governs
  112. * security ID of files we create */
  113. ret = cachefiles_determine_cache_security(cache, root, &saved_cred);
  114. if (ret < 0)
  115. goto error_unsupported;
  116. /* get the cache size and blocksize */
  117. ret = vfs_statfs(&path, &stats);
  118. if (ret < 0)
  119. goto error_unsupported;
  120. ret = -ERANGE;
  121. if (stats.f_bsize <= 0)
  122. goto error_unsupported;
  123. ret = -EOPNOTSUPP;
  124. if (stats.f_bsize > PAGE_SIZE)
  125. goto error_unsupported;
  126. cache->bsize = stats.f_bsize;
  127. cache->bshift = 0;
  128. if (stats.f_bsize < PAGE_SIZE)
  129. cache->bshift = PAGE_SHIFT - ilog2(stats.f_bsize);
  130. _debug("blksize %u (shift %u)",
  131. cache->bsize, cache->bshift);
  132. _debug("size %llu, avail %llu",
  133. (unsigned long long) stats.f_blocks,
  134. (unsigned long long) stats.f_bavail);
  135. /* set up caching limits */
  136. do_div(stats.f_files, 100);
  137. cache->fstop = stats.f_files * cache->fstop_percent;
  138. cache->fcull = stats.f_files * cache->fcull_percent;
  139. cache->frun = stats.f_files * cache->frun_percent;
  140. _debug("limits {%llu,%llu,%llu} files",
  141. (unsigned long long) cache->frun,
  142. (unsigned long long) cache->fcull,
  143. (unsigned long long) cache->fstop);
  144. stats.f_blocks >>= cache->bshift;
  145. do_div(stats.f_blocks, 100);
  146. cache->bstop = stats.f_blocks * cache->bstop_percent;
  147. cache->bcull = stats.f_blocks * cache->bcull_percent;
  148. cache->brun = stats.f_blocks * cache->brun_percent;
  149. _debug("limits {%llu,%llu,%llu} blocks",
  150. (unsigned long long) cache->brun,
  151. (unsigned long long) cache->bcull,
  152. (unsigned long long) cache->bstop);
  153. /* get the cache directory and check its type */
  154. cachedir = cachefiles_get_directory(cache, root, "cache");
  155. if (IS_ERR(cachedir)) {
  156. ret = PTR_ERR(cachedir);
  157. goto error_unsupported;
  158. }
  159. fsdef->dentry = cachedir;
  160. fsdef->fscache.cookie = NULL;
  161. ret = cachefiles_check_object_type(fsdef);
  162. if (ret < 0)
  163. goto error_unsupported;
  164. /* get the graveyard directory */
  165. graveyard = cachefiles_get_directory(cache, root, "graveyard");
  166. if (IS_ERR(graveyard)) {
  167. ret = PTR_ERR(graveyard);
  168. goto error_unsupported;
  169. }
  170. cache->graveyard = graveyard;
  171. /* publish the cache */
  172. fscache_init_cache(&cache->cache,
  173. &cachefiles_cache_ops,
  174. "%s",
  175. fsdef->dentry->d_sb->s_id);
  176. fscache_object_init(&fsdef->fscache, &fscache_fsdef_index,
  177. &cache->cache);
  178. ret = fscache_add_cache(&cache->cache, &fsdef->fscache, cache->tag);
  179. if (ret < 0)
  180. goto error_add_cache;
  181. /* done */
  182. set_bit(CACHEFILES_READY, &cache->flags);
  183. dput(root);
  184. pr_info("File cache on %s registered\n", cache->cache.identifier);
  185. /* check how much space the cache has */
  186. cachefiles_has_space(cache, 0, 0);
  187. cachefiles_end_secure(cache, saved_cred);
  188. return 0;
  189. error_add_cache:
  190. dput(cache->graveyard);
  191. cache->graveyard = NULL;
  192. error_unsupported:
  193. mntput(cache->mnt);
  194. cache->mnt = NULL;
  195. dput(fsdef->dentry);
  196. fsdef->dentry = NULL;
  197. dput(root);
  198. error_open_root:
  199. kmem_cache_free(cachefiles_object_jar, fsdef);
  200. error_root_object:
  201. cachefiles_end_secure(cache, saved_cred);
  202. pr_err("Failed to register: %d\n", ret);
  203. return ret;
  204. }
  205. /*
  206. * unbind a cache on fd release
  207. */
  208. void cachefiles_daemon_unbind(struct cachefiles_cache *cache)
  209. {
  210. _enter("");
  211. if (test_bit(CACHEFILES_READY, &cache->flags)) {
  212. pr_info("File cache on %s unregistering\n",
  213. cache->cache.identifier);
  214. fscache_withdraw_cache(&cache->cache);
  215. }
  216. dput(cache->graveyard);
  217. mntput(cache->mnt);
  218. kfree(cache->rootdirname);
  219. kfree(cache->secctx);
  220. kfree(cache->tag);
  221. _leave("");
  222. }