bind.c 7.0 KB

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