cache.c 11 KB

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