cache.c 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /* Manage high-level VFS aspects of a cache.
  3. *
  4. * Copyright (C) 2007, 2021 Red Hat, Inc. All Rights Reserved.
  5. * Written by David Howells ([email protected])
  6. */
  7. #include <linux/slab.h>
  8. #include <linux/statfs.h>
  9. #include <linux/namei.h>
  10. #include "internal.h"
  11. /*
  12. * Bring a cache online.
  13. */
  14. int cachefiles_add_cache(struct cachefiles_cache *cache)
  15. {
  16. struct fscache_cache *cache_cookie;
  17. struct path path;
  18. struct kstatfs stats;
  19. struct dentry *graveyard, *cachedir, *root;
  20. const struct cred *saved_cred;
  21. int ret;
  22. _enter("");
  23. cache_cookie = fscache_acquire_cache(cache->tag);
  24. if (IS_ERR(cache_cookie))
  25. return PTR_ERR(cache_cookie);
  26. /* we want to work under the module's security ID */
  27. ret = cachefiles_get_security_ID(cache);
  28. if (ret < 0)
  29. goto error_getsec;
  30. cachefiles_begin_secure(cache, &saved_cred);
  31. /* look up the directory at the root of the cache */
  32. ret = kern_path(cache->rootdirname, LOOKUP_DIRECTORY, &path);
  33. if (ret < 0)
  34. goto error_open_root;
  35. cache->mnt = path.mnt;
  36. root = path.dentry;
  37. ret = -EINVAL;
  38. if (is_idmapped_mnt(path.mnt)) {
  39. pr_warn("File cache on idmapped mounts not supported");
  40. goto error_unsupported;
  41. }
  42. /* Check features of the backing filesystem:
  43. * - Directories must support looking up and directory creation
  44. * - We create tmpfiles to handle invalidation
  45. * - We use xattrs to store metadata
  46. * - We need to be able to query the amount of space available
  47. * - We want to be able to sync the filesystem when stopping the cache
  48. * - We use DIO to/from pages, so the blocksize mustn't be too big.
  49. */
  50. ret = -EOPNOTSUPP;
  51. if (d_is_negative(root) ||
  52. !d_backing_inode(root)->i_op->lookup ||
  53. !d_backing_inode(root)->i_op->mkdir ||
  54. !d_backing_inode(root)->i_op->tmpfile ||
  55. !(d_backing_inode(root)->i_opflags & IOP_XATTR) ||
  56. !root->d_sb->s_op->statfs ||
  57. !root->d_sb->s_op->sync_fs ||
  58. root->d_sb->s_blocksize > PAGE_SIZE)
  59. goto error_unsupported;
  60. ret = -EROFS;
  61. if (sb_rdonly(root->d_sb))
  62. goto error_unsupported;
  63. /* determine the security of the on-disk cache as this governs
  64. * security ID of files we create */
  65. ret = cachefiles_determine_cache_security(cache, root, &saved_cred);
  66. if (ret < 0)
  67. goto error_unsupported;
  68. /* get the cache size and blocksize */
  69. ret = vfs_statfs(&path, &stats);
  70. if (ret < 0)
  71. goto error_unsupported;
  72. ret = -ERANGE;
  73. if (stats.f_bsize <= 0)
  74. goto error_unsupported;
  75. ret = -EOPNOTSUPP;
  76. if (stats.f_bsize > PAGE_SIZE)
  77. goto error_unsupported;
  78. cache->bsize = stats.f_bsize;
  79. cache->bshift = ilog2(stats.f_bsize);
  80. _debug("blksize %u (shift %u)",
  81. cache->bsize, cache->bshift);
  82. _debug("size %llu, avail %llu",
  83. (unsigned long long) stats.f_blocks,
  84. (unsigned long long) stats.f_bavail);
  85. /* set up caching limits */
  86. do_div(stats.f_files, 100);
  87. cache->fstop = stats.f_files * cache->fstop_percent;
  88. cache->fcull = stats.f_files * cache->fcull_percent;
  89. cache->frun = stats.f_files * cache->frun_percent;
  90. _debug("limits {%llu,%llu,%llu} files",
  91. (unsigned long long) cache->frun,
  92. (unsigned long long) cache->fcull,
  93. (unsigned long long) cache->fstop);
  94. do_div(stats.f_blocks, 100);
  95. cache->bstop = stats.f_blocks * cache->bstop_percent;
  96. cache->bcull = stats.f_blocks * cache->bcull_percent;
  97. cache->brun = stats.f_blocks * cache->brun_percent;
  98. _debug("limits {%llu,%llu,%llu} blocks",
  99. (unsigned long long) cache->brun,
  100. (unsigned long long) cache->bcull,
  101. (unsigned long long) cache->bstop);
  102. /* get the cache directory and check its type */
  103. cachedir = cachefiles_get_directory(cache, root, "cache", NULL);
  104. if (IS_ERR(cachedir)) {
  105. ret = PTR_ERR(cachedir);
  106. goto error_unsupported;
  107. }
  108. cache->store = cachedir;
  109. /* get the graveyard directory */
  110. graveyard = cachefiles_get_directory(cache, root, "graveyard", NULL);
  111. if (IS_ERR(graveyard)) {
  112. ret = PTR_ERR(graveyard);
  113. goto error_unsupported;
  114. }
  115. cache->graveyard = graveyard;
  116. cache->cache = cache_cookie;
  117. ret = fscache_add_cache(cache_cookie, &cachefiles_cache_ops, cache);
  118. if (ret < 0)
  119. goto error_add_cache;
  120. /* done */
  121. set_bit(CACHEFILES_READY, &cache->flags);
  122. dput(root);
  123. pr_info("File cache on %s registered\n", cache_cookie->name);
  124. /* check how much space the cache has */
  125. cachefiles_has_space(cache, 0, 0, cachefiles_has_space_check);
  126. cachefiles_end_secure(cache, saved_cred);
  127. _leave(" = 0 [%px]", cache->cache);
  128. return 0;
  129. error_add_cache:
  130. cachefiles_put_directory(cache->graveyard);
  131. cache->graveyard = NULL;
  132. error_unsupported:
  133. cachefiles_put_directory(cache->store);
  134. cache->store = NULL;
  135. mntput(cache->mnt);
  136. cache->mnt = NULL;
  137. dput(root);
  138. error_open_root:
  139. cachefiles_end_secure(cache, saved_cred);
  140. error_getsec:
  141. fscache_relinquish_cache(cache_cookie);
  142. cache->cache = NULL;
  143. pr_err("Failed to register: %d\n", ret);
  144. return ret;
  145. }
  146. /*
  147. * See if we have space for a number of pages and/or a number of files in the
  148. * cache
  149. */
  150. int cachefiles_has_space(struct cachefiles_cache *cache,
  151. unsigned fnr, unsigned bnr,
  152. enum cachefiles_has_space_for reason)
  153. {
  154. struct kstatfs stats;
  155. u64 b_avail, b_writing;
  156. int ret;
  157. struct path path = {
  158. .mnt = cache->mnt,
  159. .dentry = cache->mnt->mnt_root,
  160. };
  161. //_enter("{%llu,%llu,%llu,%llu,%llu,%llu},%u,%u",
  162. // (unsigned long long) cache->frun,
  163. // (unsigned long long) cache->fcull,
  164. // (unsigned long long) cache->fstop,
  165. // (unsigned long long) cache->brun,
  166. // (unsigned long long) cache->bcull,
  167. // (unsigned long long) cache->bstop,
  168. // fnr, bnr);
  169. /* find out how many pages of blockdev are available */
  170. memset(&stats, 0, sizeof(stats));
  171. ret = vfs_statfs(&path, &stats);
  172. if (ret < 0) {
  173. trace_cachefiles_vfs_error(NULL, d_inode(path.dentry), ret,
  174. cachefiles_trace_statfs_error);
  175. if (ret == -EIO)
  176. cachefiles_io_error(cache, "statfs failed");
  177. _leave(" = %d", ret);
  178. return ret;
  179. }
  180. b_avail = stats.f_bavail;
  181. b_writing = atomic_long_read(&cache->b_writing);
  182. if (b_avail > b_writing)
  183. b_avail -= b_writing;
  184. else
  185. b_avail = 0;
  186. //_debug("avail %llu,%llu",
  187. // (unsigned long long)stats.f_ffree,
  188. // (unsigned long long)b_avail);
  189. /* see if there is sufficient space */
  190. if (stats.f_ffree > fnr)
  191. stats.f_ffree -= fnr;
  192. else
  193. stats.f_ffree = 0;
  194. if (b_avail > bnr)
  195. b_avail -= bnr;
  196. else
  197. b_avail = 0;
  198. ret = -ENOBUFS;
  199. if (stats.f_ffree < cache->fstop ||
  200. b_avail < cache->bstop)
  201. goto stop_and_begin_cull;
  202. ret = 0;
  203. if (stats.f_ffree < cache->fcull ||
  204. b_avail < cache->bcull)
  205. goto begin_cull;
  206. if (test_bit(CACHEFILES_CULLING, &cache->flags) &&
  207. stats.f_ffree >= cache->frun &&
  208. b_avail >= cache->brun &&
  209. test_and_clear_bit(CACHEFILES_CULLING, &cache->flags)
  210. ) {
  211. _debug("cease culling");
  212. cachefiles_state_changed(cache);
  213. }
  214. //_leave(" = 0");
  215. return 0;
  216. stop_and_begin_cull:
  217. switch (reason) {
  218. case cachefiles_has_space_for_write:
  219. fscache_count_no_write_space();
  220. break;
  221. case cachefiles_has_space_for_create:
  222. fscache_count_no_create_space();
  223. break;
  224. default:
  225. break;
  226. }
  227. begin_cull:
  228. if (!test_and_set_bit(CACHEFILES_CULLING, &cache->flags)) {
  229. _debug("### CULL CACHE ###");
  230. cachefiles_state_changed(cache);
  231. }
  232. _leave(" = %d", ret);
  233. return ret;
  234. }
  235. /*
  236. * Mark all the objects as being out of service and queue them all for cleanup.
  237. */
  238. static void cachefiles_withdraw_objects(struct cachefiles_cache *cache)
  239. {
  240. struct cachefiles_object *object;
  241. unsigned int count = 0;
  242. _enter("");
  243. spin_lock(&cache->object_list_lock);
  244. while (!list_empty(&cache->object_list)) {
  245. object = list_first_entry(&cache->object_list,
  246. struct cachefiles_object, cache_link);
  247. cachefiles_see_object(object, cachefiles_obj_see_withdrawal);
  248. list_del_init(&object->cache_link);
  249. fscache_withdraw_cookie(object->cookie);
  250. count++;
  251. if ((count & 63) == 0) {
  252. spin_unlock(&cache->object_list_lock);
  253. cond_resched();
  254. spin_lock(&cache->object_list_lock);
  255. }
  256. }
  257. spin_unlock(&cache->object_list_lock);
  258. _leave(" [%u objs]", count);
  259. }
  260. /*
  261. * Withdraw volumes.
  262. */
  263. static void cachefiles_withdraw_volumes(struct cachefiles_cache *cache)
  264. {
  265. _enter("");
  266. for (;;) {
  267. struct cachefiles_volume *volume = NULL;
  268. spin_lock(&cache->object_list_lock);
  269. if (!list_empty(&cache->volumes)) {
  270. volume = list_first_entry(&cache->volumes,
  271. struct cachefiles_volume, cache_link);
  272. list_del_init(&volume->cache_link);
  273. }
  274. spin_unlock(&cache->object_list_lock);
  275. if (!volume)
  276. break;
  277. cachefiles_withdraw_volume(volume);
  278. }
  279. _leave("");
  280. }
  281. /*
  282. * Sync a cache to backing disk.
  283. */
  284. static void cachefiles_sync_cache(struct cachefiles_cache *cache)
  285. {
  286. const struct cred *saved_cred;
  287. int ret;
  288. _enter("%s", cache->cache->name);
  289. /* make sure all pages pinned by operations on behalf of the netfs are
  290. * written to disc */
  291. cachefiles_begin_secure(cache, &saved_cred);
  292. down_read(&cache->mnt->mnt_sb->s_umount);
  293. ret = sync_filesystem(cache->mnt->mnt_sb);
  294. up_read(&cache->mnt->mnt_sb->s_umount);
  295. cachefiles_end_secure(cache, saved_cred);
  296. if (ret == -EIO)
  297. cachefiles_io_error(cache,
  298. "Attempt to sync backing fs superblock returned error %d",
  299. ret);
  300. }
  301. /*
  302. * Withdraw cache objects.
  303. */
  304. void cachefiles_withdraw_cache(struct cachefiles_cache *cache)
  305. {
  306. struct fscache_cache *fscache = cache->cache;
  307. pr_info("File cache on %s unregistering\n", fscache->name);
  308. fscache_withdraw_cache(fscache);
  309. /* we now have to destroy all the active objects pertaining to this
  310. * cache - which we do by passing them off to thread pool to be
  311. * disposed of */
  312. cachefiles_withdraw_objects(cache);
  313. fscache_wait_for_objects(fscache);
  314. cachefiles_withdraw_volumes(cache);
  315. cachefiles_sync_cache(cache);
  316. cache->cache = NULL;
  317. fscache_relinquish_cache(fscache);
  318. }