cache.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /* FS-Cache cache handling
  3. *
  4. * Copyright (C) 2021 Red Hat, Inc. All Rights Reserved.
  5. * Written by David Howells ([email protected])
  6. */
  7. #define FSCACHE_DEBUG_LEVEL CACHE
  8. #include <linux/export.h>
  9. #include <linux/slab.h>
  10. #include "internal.h"
  11. static LIST_HEAD(fscache_caches);
  12. DECLARE_RWSEM(fscache_addremove_sem);
  13. EXPORT_SYMBOL(fscache_addremove_sem);
  14. DECLARE_WAIT_QUEUE_HEAD(fscache_clearance_waiters);
  15. EXPORT_SYMBOL(fscache_clearance_waiters);
  16. static atomic_t fscache_cache_debug_id;
  17. /*
  18. * Allocate a cache cookie.
  19. */
  20. static struct fscache_cache *fscache_alloc_cache(const char *name)
  21. {
  22. struct fscache_cache *cache;
  23. cache = kzalloc(sizeof(*cache), GFP_KERNEL);
  24. if (cache) {
  25. if (name) {
  26. cache->name = kstrdup(name, GFP_KERNEL);
  27. if (!cache->name) {
  28. kfree(cache);
  29. return NULL;
  30. }
  31. }
  32. refcount_set(&cache->ref, 1);
  33. INIT_LIST_HEAD(&cache->cache_link);
  34. cache->debug_id = atomic_inc_return(&fscache_cache_debug_id);
  35. }
  36. return cache;
  37. }
  38. static bool fscache_get_cache_maybe(struct fscache_cache *cache,
  39. enum fscache_cache_trace where)
  40. {
  41. bool success;
  42. int ref;
  43. success = __refcount_inc_not_zero(&cache->ref, &ref);
  44. if (success)
  45. trace_fscache_cache(cache->debug_id, ref + 1, where);
  46. return success;
  47. }
  48. /*
  49. * Look up a cache cookie.
  50. */
  51. struct fscache_cache *fscache_lookup_cache(const char *name, bool is_cache)
  52. {
  53. struct fscache_cache *candidate, *cache, *unnamed = NULL;
  54. /* firstly check for the existence of the cache under read lock */
  55. down_read(&fscache_addremove_sem);
  56. list_for_each_entry(cache, &fscache_caches, cache_link) {
  57. if (cache->name && name && strcmp(cache->name, name) == 0 &&
  58. fscache_get_cache_maybe(cache, fscache_cache_get_acquire))
  59. goto got_cache_r;
  60. if (!cache->name && !name &&
  61. fscache_get_cache_maybe(cache, fscache_cache_get_acquire))
  62. goto got_cache_r;
  63. }
  64. if (!name) {
  65. list_for_each_entry(cache, &fscache_caches, cache_link) {
  66. if (cache->name &&
  67. fscache_get_cache_maybe(cache, fscache_cache_get_acquire))
  68. goto got_cache_r;
  69. }
  70. }
  71. up_read(&fscache_addremove_sem);
  72. /* the cache does not exist - create a candidate */
  73. candidate = fscache_alloc_cache(name);
  74. if (!candidate)
  75. return ERR_PTR(-ENOMEM);
  76. /* write lock, search again and add if still not present */
  77. down_write(&fscache_addremove_sem);
  78. list_for_each_entry(cache, &fscache_caches, cache_link) {
  79. if (cache->name && name && strcmp(cache->name, name) == 0 &&
  80. fscache_get_cache_maybe(cache, fscache_cache_get_acquire))
  81. goto got_cache_w;
  82. if (!cache->name) {
  83. unnamed = cache;
  84. if (!name &&
  85. fscache_get_cache_maybe(cache, fscache_cache_get_acquire))
  86. goto got_cache_w;
  87. }
  88. }
  89. if (unnamed && is_cache &&
  90. fscache_get_cache_maybe(unnamed, fscache_cache_get_acquire))
  91. goto use_unnamed_cache;
  92. if (!name) {
  93. list_for_each_entry(cache, &fscache_caches, cache_link) {
  94. if (cache->name &&
  95. fscache_get_cache_maybe(cache, fscache_cache_get_acquire))
  96. goto got_cache_w;
  97. }
  98. }
  99. list_add_tail(&candidate->cache_link, &fscache_caches);
  100. trace_fscache_cache(candidate->debug_id,
  101. refcount_read(&candidate->ref),
  102. fscache_cache_new_acquire);
  103. up_write(&fscache_addremove_sem);
  104. return candidate;
  105. got_cache_r:
  106. up_read(&fscache_addremove_sem);
  107. return cache;
  108. use_unnamed_cache:
  109. cache = unnamed;
  110. cache->name = candidate->name;
  111. candidate->name = NULL;
  112. got_cache_w:
  113. up_write(&fscache_addremove_sem);
  114. kfree(candidate->name);
  115. kfree(candidate);
  116. return cache;
  117. }
  118. /**
  119. * fscache_acquire_cache - Acquire a cache-level cookie.
  120. * @name: The name of the cache.
  121. *
  122. * Get a cookie to represent an actual cache. If a name is given and there is
  123. * a nameless cache record available, this will acquire that and set its name,
  124. * directing all the volumes using it to this cache.
  125. *
  126. * The cache will be switched over to the preparing state if not currently in
  127. * use, otherwise -EBUSY will be returned.
  128. */
  129. struct fscache_cache *fscache_acquire_cache(const char *name)
  130. {
  131. struct fscache_cache *cache;
  132. ASSERT(name);
  133. cache = fscache_lookup_cache(name, true);
  134. if (IS_ERR(cache))
  135. return cache;
  136. if (!fscache_set_cache_state_maybe(cache,
  137. FSCACHE_CACHE_IS_NOT_PRESENT,
  138. FSCACHE_CACHE_IS_PREPARING)) {
  139. pr_warn("Cache tag %s in use\n", name);
  140. fscache_put_cache(cache, fscache_cache_put_cache);
  141. return ERR_PTR(-EBUSY);
  142. }
  143. return cache;
  144. }
  145. EXPORT_SYMBOL(fscache_acquire_cache);
  146. /**
  147. * fscache_put_cache - Release a cache-level cookie.
  148. * @cache: The cache cookie to be released
  149. * @where: An indication of where the release happened
  150. *
  151. * Release the caller's reference on a cache-level cookie. The @where
  152. * indication should give information about the circumstances in which the call
  153. * occurs and will be logged through a tracepoint.
  154. */
  155. void fscache_put_cache(struct fscache_cache *cache,
  156. enum fscache_cache_trace where)
  157. {
  158. unsigned int debug_id = cache->debug_id;
  159. bool zero;
  160. int ref;
  161. if (IS_ERR_OR_NULL(cache))
  162. return;
  163. zero = __refcount_dec_and_test(&cache->ref, &ref);
  164. trace_fscache_cache(debug_id, ref - 1, where);
  165. if (zero) {
  166. down_write(&fscache_addremove_sem);
  167. list_del_init(&cache->cache_link);
  168. up_write(&fscache_addremove_sem);
  169. kfree(cache->name);
  170. kfree(cache);
  171. }
  172. }
  173. /**
  174. * fscache_relinquish_cache - Reset cache state and release cookie
  175. * @cache: The cache cookie to be released
  176. *
  177. * Reset the state of a cache and release the caller's reference on a cache
  178. * cookie.
  179. */
  180. void fscache_relinquish_cache(struct fscache_cache *cache)
  181. {
  182. enum fscache_cache_trace where =
  183. (cache->state == FSCACHE_CACHE_IS_PREPARING) ?
  184. fscache_cache_put_prep_failed :
  185. fscache_cache_put_relinquish;
  186. cache->ops = NULL;
  187. cache->cache_priv = NULL;
  188. fscache_set_cache_state(cache, FSCACHE_CACHE_IS_NOT_PRESENT);
  189. fscache_put_cache(cache, where);
  190. }
  191. EXPORT_SYMBOL(fscache_relinquish_cache);
  192. /**
  193. * fscache_add_cache - Declare a cache as being open for business
  194. * @cache: The cache-level cookie representing the cache
  195. * @ops: Table of cache operations to use
  196. * @cache_priv: Private data for the cache record
  197. *
  198. * Add a cache to the system, making it available for netfs's to use.
  199. *
  200. * See Documentation/filesystems/caching/backend-api.rst for a complete
  201. * description.
  202. */
  203. int fscache_add_cache(struct fscache_cache *cache,
  204. const struct fscache_cache_ops *ops,
  205. void *cache_priv)
  206. {
  207. int n_accesses;
  208. _enter("{%s,%s}", ops->name, cache->name);
  209. BUG_ON(fscache_cache_state(cache) != FSCACHE_CACHE_IS_PREPARING);
  210. /* Get a ref on the cache cookie and keep its n_accesses counter raised
  211. * by 1 to prevent wakeups from transitioning it to 0 until we're
  212. * withdrawing caching services from it.
  213. */
  214. n_accesses = atomic_inc_return(&cache->n_accesses);
  215. trace_fscache_access_cache(cache->debug_id, refcount_read(&cache->ref),
  216. n_accesses, fscache_access_cache_pin);
  217. down_write(&fscache_addremove_sem);
  218. cache->ops = ops;
  219. cache->cache_priv = cache_priv;
  220. fscache_set_cache_state(cache, FSCACHE_CACHE_IS_ACTIVE);
  221. up_write(&fscache_addremove_sem);
  222. pr_notice("Cache \"%s\" added (type %s)\n", cache->name, ops->name);
  223. _leave(" = 0 [%s]", cache->name);
  224. return 0;
  225. }
  226. EXPORT_SYMBOL(fscache_add_cache);
  227. /**
  228. * fscache_begin_cache_access - Pin a cache so it can be accessed
  229. * @cache: The cache-level cookie
  230. * @why: An indication of the circumstances of the access for tracing
  231. *
  232. * Attempt to pin the cache to prevent it from going away whilst we're
  233. * accessing it and returns true if successful. This works as follows:
  234. *
  235. * (1) If the cache tests as not live (state is not FSCACHE_CACHE_IS_ACTIVE),
  236. * then we return false to indicate access was not permitted.
  237. *
  238. * (2) If the cache tests as live, then we increment the n_accesses count and
  239. * then recheck the liveness, ending the access if it ceased to be live.
  240. *
  241. * (3) When we end the access, we decrement n_accesses and wake up the any
  242. * waiters if it reaches 0.
  243. *
  244. * (4) Whilst the cache is caching, n_accesses is kept artificially
  245. * incremented to prevent wakeups from happening.
  246. *
  247. * (5) When the cache is taken offline, the state is changed to prevent new
  248. * accesses, n_accesses is decremented and we wait for n_accesses to
  249. * become 0.
  250. */
  251. bool fscache_begin_cache_access(struct fscache_cache *cache, enum fscache_access_trace why)
  252. {
  253. int n_accesses;
  254. if (!fscache_cache_is_live(cache))
  255. return false;
  256. n_accesses = atomic_inc_return(&cache->n_accesses);
  257. smp_mb__after_atomic(); /* Reread live flag after n_accesses */
  258. trace_fscache_access_cache(cache->debug_id, refcount_read(&cache->ref),
  259. n_accesses, why);
  260. if (!fscache_cache_is_live(cache)) {
  261. fscache_end_cache_access(cache, fscache_access_unlive);
  262. return false;
  263. }
  264. return true;
  265. }
  266. /**
  267. * fscache_end_cache_access - Unpin a cache at the end of an access.
  268. * @cache: The cache-level cookie
  269. * @why: An indication of the circumstances of the access for tracing
  270. *
  271. * Unpin a cache after we've accessed it. The @why indicator is merely
  272. * provided for tracing purposes.
  273. */
  274. void fscache_end_cache_access(struct fscache_cache *cache, enum fscache_access_trace why)
  275. {
  276. int n_accesses;
  277. smp_mb__before_atomic();
  278. n_accesses = atomic_dec_return(&cache->n_accesses);
  279. trace_fscache_access_cache(cache->debug_id, refcount_read(&cache->ref),
  280. n_accesses, why);
  281. if (n_accesses == 0)
  282. wake_up_var(&cache->n_accesses);
  283. }
  284. /**
  285. * fscache_io_error - Note a cache I/O error
  286. * @cache: The record describing the cache
  287. *
  288. * Note that an I/O error occurred in a cache and that it should no longer be
  289. * used for anything. This also reports the error into the kernel log.
  290. *
  291. * See Documentation/filesystems/caching/backend-api.rst for a complete
  292. * description.
  293. */
  294. void fscache_io_error(struct fscache_cache *cache)
  295. {
  296. if (fscache_set_cache_state_maybe(cache,
  297. FSCACHE_CACHE_IS_ACTIVE,
  298. FSCACHE_CACHE_GOT_IOERROR))
  299. pr_err("Cache '%s' stopped due to I/O error\n",
  300. cache->name);
  301. }
  302. EXPORT_SYMBOL(fscache_io_error);
  303. /**
  304. * fscache_withdraw_cache - Withdraw a cache from the active service
  305. * @cache: The cache cookie
  306. *
  307. * Begin the process of withdrawing a cache from service. This stops new
  308. * cache-level and volume-level accesses from taking place and waits for
  309. * currently ongoing cache-level accesses to end.
  310. */
  311. void fscache_withdraw_cache(struct fscache_cache *cache)
  312. {
  313. int n_accesses;
  314. pr_notice("Withdrawing cache \"%s\" (%u objs)\n",
  315. cache->name, atomic_read(&cache->object_count));
  316. fscache_set_cache_state(cache, FSCACHE_CACHE_IS_WITHDRAWN);
  317. /* Allow wakeups on dec-to-0 */
  318. n_accesses = atomic_dec_return(&cache->n_accesses);
  319. trace_fscache_access_cache(cache->debug_id, refcount_read(&cache->ref),
  320. n_accesses, fscache_access_cache_unpin);
  321. wait_var_event(&cache->n_accesses,
  322. atomic_read(&cache->n_accesses) == 0);
  323. }
  324. EXPORT_SYMBOL(fscache_withdraw_cache);
  325. #ifdef CONFIG_PROC_FS
  326. static const char fscache_cache_states[NR__FSCACHE_CACHE_STATE] = "-PAEW";
  327. /*
  328. * Generate a list of caches in /proc/fs/fscache/caches
  329. */
  330. static int fscache_caches_seq_show(struct seq_file *m, void *v)
  331. {
  332. struct fscache_cache *cache;
  333. if (v == &fscache_caches) {
  334. seq_puts(m,
  335. "CACHE REF VOLS OBJS ACCES S NAME\n"
  336. "======== ===== ===== ===== ===== = ===============\n"
  337. );
  338. return 0;
  339. }
  340. cache = list_entry(v, struct fscache_cache, cache_link);
  341. seq_printf(m,
  342. "%08x %5d %5d %5d %5d %c %s\n",
  343. cache->debug_id,
  344. refcount_read(&cache->ref),
  345. atomic_read(&cache->n_volumes),
  346. atomic_read(&cache->object_count),
  347. atomic_read(&cache->n_accesses),
  348. fscache_cache_states[cache->state],
  349. cache->name ?: "-");
  350. return 0;
  351. }
  352. static void *fscache_caches_seq_start(struct seq_file *m, loff_t *_pos)
  353. __acquires(fscache_addremove_sem)
  354. {
  355. down_read(&fscache_addremove_sem);
  356. return seq_list_start_head(&fscache_caches, *_pos);
  357. }
  358. static void *fscache_caches_seq_next(struct seq_file *m, void *v, loff_t *_pos)
  359. {
  360. return seq_list_next(v, &fscache_caches, _pos);
  361. }
  362. static void fscache_caches_seq_stop(struct seq_file *m, void *v)
  363. __releases(fscache_addremove_sem)
  364. {
  365. up_read(&fscache_addremove_sem);
  366. }
  367. const struct seq_operations fscache_caches_seq_ops = {
  368. .start = fscache_caches_seq_start,
  369. .next = fscache_caches_seq_next,
  370. .stop = fscache_caches_seq_stop,
  371. .show = fscache_caches_seq_show,
  372. };
  373. #endif /* CONFIG_PROC_FS */