cache.c 11 KB

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