bind.c 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284
  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. kerror("'bind' command doesn't take an argument");
  48. return -EINVAL;
  49. }
  50. if (!cache->rootdirname) {
  51. kerror("No cache directory specified");
  52. return -EINVAL;
  53. }
  54. /* don't permit already bound caches to be re-bound */
  55. if (test_bit(CACHEFILES_READY, &cache->flags)) {
  56. kerror("Cache already bound");
  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 (!root->d_inode ||
  105. !root->d_inode->i_op ||
  106. !root->d_inode->i_op->lookup ||
  107. !root->d_inode->i_op->mkdir ||
  108. !root->d_inode->i_op->setxattr ||
  109. !root->d_inode->i_op->getxattr ||
  110. !root->d_sb ||
  111. !root->d_sb->s_op ||
  112. !root->d_sb->s_op->statfs ||
  113. !root->d_sb->s_op->sync_fs)
  114. goto error_unsupported;
  115. ret = -EROFS;
  116. if (root->d_sb->s_flags & MS_RDONLY)
  117. goto error_unsupported;
  118. /* determine the security of the on-disk cache as this governs
  119. * security ID of files we create */
  120. ret = cachefiles_determine_cache_security(cache, root, &saved_cred);
  121. if (ret < 0)
  122. goto error_unsupported;
  123. /* get the cache size and blocksize */
  124. ret = vfs_statfs(&path, &stats);
  125. if (ret < 0)
  126. goto error_unsupported;
  127. ret = -ERANGE;
  128. if (stats.f_bsize <= 0)
  129. goto error_unsupported;
  130. ret = -EOPNOTSUPP;
  131. if (stats.f_bsize > PAGE_SIZE)
  132. goto error_unsupported;
  133. cache->bsize = stats.f_bsize;
  134. cache->bshift = 0;
  135. if (stats.f_bsize < PAGE_SIZE)
  136. cache->bshift = PAGE_SHIFT - ilog2(stats.f_bsize);
  137. _debug("blksize %u (shift %u)",
  138. cache->bsize, cache->bshift);
  139. _debug("size %llu, avail %llu",
  140. (unsigned long long) stats.f_blocks,
  141. (unsigned long long) stats.f_bavail);
  142. /* set up caching limits */
  143. do_div(stats.f_files, 100);
  144. cache->fstop = stats.f_files * cache->fstop_percent;
  145. cache->fcull = stats.f_files * cache->fcull_percent;
  146. cache->frun = stats.f_files * cache->frun_percent;
  147. _debug("limits {%llu,%llu,%llu} files",
  148. (unsigned long long) cache->frun,
  149. (unsigned long long) cache->fcull,
  150. (unsigned long long) cache->fstop);
  151. stats.f_blocks >>= cache->bshift;
  152. do_div(stats.f_blocks, 100);
  153. cache->bstop = stats.f_blocks * cache->bstop_percent;
  154. cache->bcull = stats.f_blocks * cache->bcull_percent;
  155. cache->brun = stats.f_blocks * cache->brun_percent;
  156. _debug("limits {%llu,%llu,%llu} blocks",
  157. (unsigned long long) cache->brun,
  158. (unsigned long long) cache->bcull,
  159. (unsigned long long) cache->bstop);
  160. /* get the cache directory and check its type */
  161. cachedir = cachefiles_get_directory(cache, root, "cache");
  162. if (IS_ERR(cachedir)) {
  163. ret = PTR_ERR(cachedir);
  164. goto error_unsupported;
  165. }
  166. fsdef->dentry = cachedir;
  167. fsdef->fscache.cookie = NULL;
  168. ret = cachefiles_check_object_type(fsdef);
  169. if (ret < 0)
  170. goto error_unsupported;
  171. /* get the graveyard directory */
  172. graveyard = cachefiles_get_directory(cache, root, "graveyard");
  173. if (IS_ERR(graveyard)) {
  174. ret = PTR_ERR(graveyard);
  175. goto error_unsupported;
  176. }
  177. cache->graveyard = graveyard;
  178. /* publish the cache */
  179. fscache_init_cache(&cache->cache,
  180. &cachefiles_cache_ops,
  181. "%s",
  182. fsdef->dentry->d_sb->s_id);
  183. fscache_object_init(&fsdef->fscache, NULL, &cache->cache);
  184. ret = fscache_add_cache(&cache->cache, &fsdef->fscache, cache->tag);
  185. if (ret < 0)
  186. goto error_add_cache;
  187. /* done */
  188. set_bit(CACHEFILES_READY, &cache->flags);
  189. dput(root);
  190. printk(KERN_INFO "CacheFiles:"
  191. " File cache on %s registered\n",
  192. cache->cache.identifier);
  193. /* check how much space the cache has */
  194. cachefiles_has_space(cache, 0, 0);
  195. cachefiles_end_secure(cache, saved_cred);
  196. return 0;
  197. error_add_cache:
  198. dput(cache->graveyard);
  199. cache->graveyard = NULL;
  200. error_unsupported:
  201. mntput(cache->mnt);
  202. cache->mnt = NULL;
  203. dput(fsdef->dentry);
  204. fsdef->dentry = NULL;
  205. dput(root);
  206. error_open_root:
  207. kmem_cache_free(cachefiles_object_jar, fsdef);
  208. error_root_object:
  209. cachefiles_end_secure(cache, saved_cred);
  210. kerror("Failed to register: %d", ret);
  211. return ret;
  212. }
  213. /*
  214. * unbind a cache on fd release
  215. */
  216. void cachefiles_daemon_unbind(struct cachefiles_cache *cache)
  217. {
  218. _enter("");
  219. if (test_bit(CACHEFILES_READY, &cache->flags)) {
  220. printk(KERN_INFO "CacheFiles:"
  221. " File cache on %s unregistering\n",
  222. cache->cache.identifier);
  223. fscache_withdraw_cache(&cache->cache);
  224. }
  225. dput(cache->graveyard);
  226. mntput(cache->mnt);
  227. kfree(cache->rootdirname);
  228. kfree(cache->secctx);
  229. kfree(cache->tag);
  230. _leave("");
  231. }