cache.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /* FS-Cache cache handling
  3. *
  4. * Copyright (C) 2007 Red Hat, Inc. All Rights Reserved.
  5. * Written by David Howells (dhowells@redhat.com)
  6. */
  7. #define FSCACHE_DEBUG_LEVEL CACHE
  8. #include <linux/module.h>
  9. #include <linux/slab.h>
  10. #include "internal.h"
  11. LIST_HEAD(fscache_cache_list);
  12. DECLARE_RWSEM(fscache_addremove_sem);
  13. DECLARE_WAIT_QUEUE_HEAD(fscache_cache_cleared_wq);
  14. EXPORT_SYMBOL(fscache_cache_cleared_wq);
  15. static LIST_HEAD(fscache_cache_tag_list);
  16. /*
  17. * look up a cache tag
  18. */
  19. struct fscache_cache_tag *__fscache_lookup_cache_tag(const char *name)
  20. {
  21. struct fscache_cache_tag *tag, *xtag;
  22. /* firstly check for the existence of the tag under read lock */
  23. down_read(&fscache_addremove_sem);
  24. list_for_each_entry(tag, &fscache_cache_tag_list, link) {
  25. if (strcmp(tag->name, name) == 0) {
  26. atomic_inc(&tag->usage);
  27. up_read(&fscache_addremove_sem);
  28. return tag;
  29. }
  30. }
  31. up_read(&fscache_addremove_sem);
  32. /* the tag does not exist - create a candidate */
  33. xtag = kzalloc(sizeof(*xtag) + strlen(name) + 1, GFP_KERNEL);
  34. if (!xtag)
  35. /* return a dummy tag if out of memory */
  36. return ERR_PTR(-ENOMEM);
  37. atomic_set(&xtag->usage, 1);
  38. strcpy(xtag->name, name);
  39. /* write lock, search again and add if still not present */
  40. down_write(&fscache_addremove_sem);
  41. list_for_each_entry(tag, &fscache_cache_tag_list, link) {
  42. if (strcmp(tag->name, name) == 0) {
  43. atomic_inc(&tag->usage);
  44. up_write(&fscache_addremove_sem);
  45. kfree(xtag);
  46. return tag;
  47. }
  48. }
  49. list_add_tail(&xtag->link, &fscache_cache_tag_list);
  50. up_write(&fscache_addremove_sem);
  51. return xtag;
  52. }
  53. /*
  54. * release a reference to a cache tag
  55. */
  56. void __fscache_release_cache_tag(struct fscache_cache_tag *tag)
  57. {
  58. if (tag != ERR_PTR(-ENOMEM)) {
  59. down_write(&fscache_addremove_sem);
  60. if (atomic_dec_and_test(&tag->usage))
  61. list_del_init(&tag->link);
  62. else
  63. tag = NULL;
  64. up_write(&fscache_addremove_sem);
  65. kfree(tag);
  66. }
  67. }
  68. /*
  69. * select a cache in which to store an object
  70. * - the cache addremove semaphore must be at least read-locked by the caller
  71. * - the object will never be an index
  72. */
  73. struct fscache_cache *fscache_select_cache_for_object(
  74. struct fscache_cookie *cookie)
  75. {
  76. struct fscache_cache_tag *tag;
  77. struct fscache_object *object;
  78. struct fscache_cache *cache;
  79. _enter("");
  80. if (list_empty(&fscache_cache_list)) {
  81. _leave(" = NULL [no cache]");
  82. return NULL;
  83. }
  84. /* we check the parent to determine the cache to use */
  85. spin_lock(&cookie->lock);
  86. /* the first in the parent's backing list should be the preferred
  87. * cache */
  88. if (!hlist_empty(&cookie->backing_objects)) {
  89. object = hlist_entry(cookie->backing_objects.first,
  90. struct fscache_object, cookie_link);
  91. cache = object->cache;
  92. if (fscache_object_is_dying(object) ||
  93. test_bit(FSCACHE_IOERROR, &cache->flags))
  94. cache = NULL;
  95. spin_unlock(&cookie->lock);
  96. _leave(" = %p [parent]", cache);
  97. return cache;
  98. }
  99. /* the parent is unbacked */
  100. if (cookie->type != FSCACHE_COOKIE_TYPE_INDEX) {
  101. /* cookie not an index and is unbacked */
  102. spin_unlock(&cookie->lock);
  103. _leave(" = NULL [cookie ub,ni]");
  104. return NULL;
  105. }
  106. spin_unlock(&cookie->lock);
  107. if (!cookie->def->select_cache)
  108. goto no_preference;
  109. /* ask the netfs for its preference */
  110. tag = cookie->def->select_cache(cookie->parent->netfs_data,
  111. cookie->netfs_data);
  112. if (!tag)
  113. goto no_preference;
  114. if (tag == ERR_PTR(-ENOMEM)) {
  115. _leave(" = NULL [nomem tag]");
  116. return NULL;
  117. }
  118. if (!tag->cache) {
  119. _leave(" = NULL [unbacked tag]");
  120. return NULL;
  121. }
  122. if (test_bit(FSCACHE_IOERROR, &tag->cache->flags))
  123. return NULL;
  124. _leave(" = %p [specific]", tag->cache);
  125. return tag->cache;
  126. no_preference:
  127. /* netfs has no preference - just select first cache */
  128. cache = list_entry(fscache_cache_list.next,
  129. struct fscache_cache, link);
  130. _leave(" = %p [first]", cache);
  131. return cache;
  132. }
  133. /**
  134. * fscache_init_cache - Initialise a cache record
  135. * @cache: The cache record to be initialised
  136. * @ops: The cache operations to be installed in that record
  137. * @idfmt: Format string to define identifier
  138. * @...: sprintf-style arguments
  139. *
  140. * Initialise a record of a cache and fill in the name.
  141. *
  142. * See Documentation/filesystems/caching/backend-api.rst for a complete
  143. * description.
  144. */
  145. void fscache_init_cache(struct fscache_cache *cache,
  146. const struct fscache_cache_ops *ops,
  147. const char *idfmt,
  148. ...)
  149. {
  150. va_list va;
  151. memset(cache, 0, sizeof(*cache));
  152. cache->ops = ops;
  153. va_start(va, idfmt);
  154. vsnprintf(cache->identifier, sizeof(cache->identifier), idfmt, va);
  155. va_end(va);
  156. INIT_WORK(&cache->op_gc, fscache_operation_gc);
  157. INIT_LIST_HEAD(&cache->link);
  158. INIT_LIST_HEAD(&cache->object_list);
  159. INIT_LIST_HEAD(&cache->op_gc_list);
  160. spin_lock_init(&cache->object_list_lock);
  161. spin_lock_init(&cache->op_gc_list_lock);
  162. }
  163. EXPORT_SYMBOL(fscache_init_cache);
  164. /**
  165. * fscache_add_cache - Declare a cache as being open for business
  166. * @cache: The record describing the cache
  167. * @ifsdef: The record of the cache object describing the top-level index
  168. * @tagname: The tag describing this cache
  169. *
  170. * Add a cache to the system, making it available for netfs's to use.
  171. *
  172. * See Documentation/filesystems/caching/backend-api.rst for a complete
  173. * description.
  174. */
  175. int fscache_add_cache(struct fscache_cache *cache,
  176. struct fscache_object *ifsdef,
  177. const char *tagname)
  178. {
  179. struct fscache_cache_tag *tag;
  180. ASSERTCMP(ifsdef->cookie, ==, &fscache_fsdef_index);
  181. BUG_ON(!cache->ops);
  182. BUG_ON(!ifsdef);
  183. cache->flags = 0;
  184. ifsdef->event_mask =
  185. ((1 << NR_FSCACHE_OBJECT_EVENTS) - 1) &
  186. ~(1 << FSCACHE_OBJECT_EV_CLEARED);
  187. __set_bit(FSCACHE_OBJECT_IS_AVAILABLE, &ifsdef->flags);
  188. if (!tagname)
  189. tagname = cache->identifier;
  190. BUG_ON(!tagname[0]);
  191. _enter("{%s.%s},,%s", cache->ops->name, cache->identifier, tagname);
  192. /* we use the cache tag to uniquely identify caches */
  193. tag = __fscache_lookup_cache_tag(tagname);
  194. if (IS_ERR(tag))
  195. goto nomem;
  196. if (test_and_set_bit(FSCACHE_TAG_RESERVED, &tag->flags))
  197. goto tag_in_use;
  198. cache->kobj = kobject_create_and_add(tagname, fscache_root);
  199. if (!cache->kobj)
  200. goto error;
  201. ifsdef->cache = cache;
  202. cache->fsdef = ifsdef;
  203. down_write(&fscache_addremove_sem);
  204. tag->cache = cache;
  205. cache->tag = tag;
  206. /* add the cache to the list */
  207. list_add(&cache->link, &fscache_cache_list);
  208. /* add the cache's netfs definition index object to the cache's
  209. * list */
  210. spin_lock(&cache->object_list_lock);
  211. list_add_tail(&ifsdef->cache_link, &cache->object_list);
  212. spin_unlock(&cache->object_list_lock);
  213. fscache_objlist_add(ifsdef);
  214. /* add the cache's netfs definition index object to the top level index
  215. * cookie as a known backing object */
  216. spin_lock(&fscache_fsdef_index.lock);
  217. hlist_add_head(&ifsdef->cookie_link,
  218. &fscache_fsdef_index.backing_objects);
  219. atomic_inc(&fscache_fsdef_index.usage);
  220. /* done */
  221. spin_unlock(&fscache_fsdef_index.lock);
  222. up_write(&fscache_addremove_sem);
  223. pr_notice("Cache \"%s\" added (type %s)\n",
  224. cache->tag->name, cache->ops->name);
  225. kobject_uevent(cache->kobj, KOBJ_ADD);
  226. _leave(" = 0 [%s]", cache->identifier);
  227. return 0;
  228. tag_in_use:
  229. pr_err("Cache tag '%s' already in use\n", tagname);
  230. __fscache_release_cache_tag(tag);
  231. _leave(" = -EXIST");
  232. return -EEXIST;
  233. error:
  234. __fscache_release_cache_tag(tag);
  235. _leave(" = -EINVAL");
  236. return -EINVAL;
  237. nomem:
  238. _leave(" = -ENOMEM");
  239. return -ENOMEM;
  240. }
  241. EXPORT_SYMBOL(fscache_add_cache);
  242. /**
  243. * fscache_io_error - Note a cache I/O error
  244. * @cache: The record describing the cache
  245. *
  246. * Note that an I/O error occurred in a cache and that it should no longer be
  247. * used for anything. This also reports the error into the kernel log.
  248. *
  249. * See Documentation/filesystems/caching/backend-api.rst for a complete
  250. * description.
  251. */
  252. void fscache_io_error(struct fscache_cache *cache)
  253. {
  254. if (!test_and_set_bit(FSCACHE_IOERROR, &cache->flags))
  255. pr_err("Cache '%s' stopped due to I/O error\n",
  256. cache->ops->name);
  257. }
  258. EXPORT_SYMBOL(fscache_io_error);
  259. /*
  260. * request withdrawal of all the objects in a cache
  261. * - all the objects being withdrawn are moved onto the supplied list
  262. */
  263. static void fscache_withdraw_all_objects(struct fscache_cache *cache,
  264. struct list_head *dying_objects)
  265. {
  266. struct fscache_object *object;
  267. while (!list_empty(&cache->object_list)) {
  268. spin_lock(&cache->object_list_lock);
  269. if (!list_empty(&cache->object_list)) {
  270. object = list_entry(cache->object_list.next,
  271. struct fscache_object, cache_link);
  272. list_move_tail(&object->cache_link, dying_objects);
  273. _debug("withdraw %p", object->cookie);
  274. /* This must be done under object_list_lock to prevent
  275. * a race with fscache_drop_object().
  276. */
  277. fscache_raise_event(object, FSCACHE_OBJECT_EV_KILL);
  278. }
  279. spin_unlock(&cache->object_list_lock);
  280. cond_resched();
  281. }
  282. }
  283. /**
  284. * fscache_withdraw_cache - Withdraw a cache from the active service
  285. * @cache: The record describing the cache
  286. *
  287. * Withdraw a cache from service, unbinding all its cache objects from the
  288. * netfs cookies they're currently representing.
  289. *
  290. * See Documentation/filesystems/caching/backend-api.rst for a complete
  291. * description.
  292. */
  293. void fscache_withdraw_cache(struct fscache_cache *cache)
  294. {
  295. LIST_HEAD(dying_objects);
  296. _enter("");
  297. pr_notice("Withdrawing cache \"%s\"\n",
  298. cache->tag->name);
  299. /* make the cache unavailable for cookie acquisition */
  300. if (test_and_set_bit(FSCACHE_CACHE_WITHDRAWN, &cache->flags))
  301. BUG();
  302. down_write(&fscache_addremove_sem);
  303. list_del_init(&cache->link);
  304. cache->tag->cache = NULL;
  305. up_write(&fscache_addremove_sem);
  306. /* make sure all pages pinned by operations on behalf of the netfs are
  307. * written to disk */
  308. fscache_stat(&fscache_n_cop_sync_cache);
  309. cache->ops->sync_cache(cache);
  310. fscache_stat_d(&fscache_n_cop_sync_cache);
  311. /* dissociate all the netfs pages backed by this cache from the block
  312. * mappings in the cache */
  313. fscache_stat(&fscache_n_cop_dissociate_pages);
  314. cache->ops->dissociate_pages(cache);
  315. fscache_stat_d(&fscache_n_cop_dissociate_pages);
  316. /* we now have to destroy all the active objects pertaining to this
  317. * cache - which we do by passing them off to thread pool to be
  318. * disposed of */
  319. _debug("destroy");
  320. fscache_withdraw_all_objects(cache, &dying_objects);
  321. /* wait for all extant objects to finish their outstanding operations
  322. * and go away */
  323. _debug("wait for finish");
  324. wait_event(fscache_cache_cleared_wq,
  325. atomic_read(&cache->object_count) == 0);
  326. _debug("wait for clearance");
  327. wait_event(fscache_cache_cleared_wq,
  328. list_empty(&cache->object_list));
  329. _debug("cleared");
  330. ASSERT(list_empty(&dying_objects));
  331. kobject_put(cache->kobj);
  332. clear_bit(FSCACHE_TAG_RESERVED, &cache->tag->flags);
  333. fscache_release_cache_tag(cache->tag);
  334. cache->tag = NULL;
  335. _leave("");
  336. }
  337. EXPORT_SYMBOL(fscache_withdraw_cache);