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 (object->state >= FSCACHE_OBJECT_DYING ||
  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 = ULONG_MAX & ~(1 << FSCACHE_OBJECT_EV_CLEARED);
  188. ifsdef->state = FSCACHE_OBJECT_ACTIVE;
  189. if (!tagname)
  190. tagname = cache->identifier;
  191. BUG_ON(!tagname[0]);
  192. _enter("{%s.%s},,%s", cache->ops->name, cache->identifier, tagname);
  193. /* we use the cache tag to uniquely identify caches */
  194. tag = __fscache_lookup_cache_tag(tagname);
  195. if (IS_ERR(tag))
  196. goto nomem;
  197. if (test_and_set_bit(FSCACHE_TAG_RESERVED, &tag->flags))
  198. goto tag_in_use;
  199. cache->kobj = kobject_create_and_add(tagname, fscache_root);
  200. if (!cache->kobj)
  201. goto error;
  202. ifsdef->cookie = &fscache_fsdef_index;
  203. ifsdef->cache = cache;
  204. cache->fsdef = ifsdef;
  205. down_write(&fscache_addremove_sem);
  206. tag->cache = cache;
  207. cache->tag = tag;
  208. /* add the cache to the list */
  209. list_add(&cache->link, &fscache_cache_list);
  210. /* add the cache's netfs definition index object to the cache's
  211. * list */
  212. spin_lock(&cache->object_list_lock);
  213. list_add_tail(&ifsdef->cache_link, &cache->object_list);
  214. spin_unlock(&cache->object_list_lock);
  215. fscache_objlist_add(ifsdef);
  216. /* add the cache's netfs definition index object to the top level index
  217. * cookie as a known backing object */
  218. spin_lock(&fscache_fsdef_index.lock);
  219. hlist_add_head(&ifsdef->cookie_link,
  220. &fscache_fsdef_index.backing_objects);
  221. atomic_inc(&fscache_fsdef_index.usage);
  222. /* done */
  223. spin_unlock(&fscache_fsdef_index.lock);
  224. up_write(&fscache_addremove_sem);
  225. printk(KERN_NOTICE "FS-Cache: Cache \"%s\" added (type %s)\n",
  226. cache->tag->name, cache->ops->name);
  227. kobject_uevent(cache->kobj, KOBJ_ADD);
  228. _leave(" = 0 [%s]", cache->identifier);
  229. return 0;
  230. tag_in_use:
  231. printk(KERN_ERR "FS-Cache: Cache tag '%s' already in use\n", tagname);
  232. __fscache_release_cache_tag(tag);
  233. _leave(" = -EXIST");
  234. return -EEXIST;
  235. error:
  236. __fscache_release_cache_tag(tag);
  237. _leave(" = -EINVAL");
  238. return -EINVAL;
  239. nomem:
  240. _leave(" = -ENOMEM");
  241. return -ENOMEM;
  242. }
  243. EXPORT_SYMBOL(fscache_add_cache);
  244. /**
  245. * fscache_io_error - Note a cache I/O error
  246. * @cache: The record describing the cache
  247. *
  248. * Note that an I/O error occurred in a cache and that it should no longer be
  249. * used for anything. This also reports the error into the kernel log.
  250. *
  251. * See Documentation/filesystems/caching/backend-api.txt for a complete
  252. * description.
  253. */
  254. void fscache_io_error(struct fscache_cache *cache)
  255. {
  256. set_bit(FSCACHE_IOERROR, &cache->flags);
  257. printk(KERN_ERR "FS-Cache: Cache %s stopped due to I/O error\n",
  258. cache->ops->name);
  259. }
  260. EXPORT_SYMBOL(fscache_io_error);
  261. /*
  262. * request withdrawal of all the objects in a cache
  263. * - all the objects being withdrawn are moved onto the supplied list
  264. */
  265. static void fscache_withdraw_all_objects(struct fscache_cache *cache,
  266. struct list_head *dying_objects)
  267. {
  268. struct fscache_object *object;
  269. spin_lock(&cache->object_list_lock);
  270. while (!list_empty(&cache->object_list)) {
  271. object = list_entry(cache->object_list.next,
  272. struct fscache_object, cache_link);
  273. list_move_tail(&object->cache_link, dying_objects);
  274. _debug("withdraw %p", object->cookie);
  275. spin_lock(&object->lock);
  276. spin_unlock(&cache->object_list_lock);
  277. fscache_raise_event(object, FSCACHE_OBJECT_EV_WITHDRAW);
  278. spin_unlock(&object->lock);
  279. cond_resched();
  280. spin_lock(&cache->object_list_lock);
  281. }
  282. spin_unlock(&cache->object_list_lock);
  283. }
  284. /**
  285. * fscache_withdraw_cache - Withdraw a cache from the active service
  286. * @cache: The record describing the cache
  287. *
  288. * Withdraw a cache from service, unbinding all its cache objects from the
  289. * netfs cookies they're currently representing.
  290. *
  291. * See Documentation/filesystems/caching/backend-api.txt for a complete
  292. * description.
  293. */
  294. void fscache_withdraw_cache(struct fscache_cache *cache)
  295. {
  296. LIST_HEAD(dying_objects);
  297. _enter("");
  298. printk(KERN_NOTICE "FS-Cache: Withdrawing cache \"%s\"\n",
  299. cache->tag->name);
  300. /* make the cache unavailable for cookie acquisition */
  301. if (test_and_set_bit(FSCACHE_CACHE_WITHDRAWN, &cache->flags))
  302. BUG();
  303. down_write(&fscache_addremove_sem);
  304. list_del_init(&cache->link);
  305. cache->tag->cache = NULL;
  306. up_write(&fscache_addremove_sem);
  307. /* make sure all pages pinned by operations on behalf of the netfs are
  308. * written to disk */
  309. fscache_stat(&fscache_n_cop_sync_cache);
  310. cache->ops->sync_cache(cache);
  311. fscache_stat_d(&fscache_n_cop_sync_cache);
  312. /* dissociate all the netfs pages backed by this cache from the block
  313. * mappings in the cache */
  314. fscache_stat(&fscache_n_cop_dissociate_pages);
  315. cache->ops->dissociate_pages(cache);
  316. fscache_stat_d(&fscache_n_cop_dissociate_pages);
  317. /* we now have to destroy all the active objects pertaining to this
  318. * cache - which we do by passing them off to thread pool to be
  319. * disposed of */
  320. _debug("destroy");
  321. fscache_withdraw_all_objects(cache, &dying_objects);
  322. /* wait for all extant objects to finish their outstanding operations
  323. * and go away */
  324. _debug("wait for finish");
  325. wait_event(fscache_cache_cleared_wq,
  326. atomic_read(&cache->object_count) == 0);
  327. _debug("wait for clearance");
  328. wait_event(fscache_cache_cleared_wq,
  329. list_empty(&cache->object_list));
  330. _debug("cleared");
  331. ASSERT(list_empty(&dying_objects));
  332. kobject_put(cache->kobj);
  333. clear_bit(FSCACHE_TAG_RESERVED, &cache->tag->flags);
  334. fscache_release_cache_tag(cache->tag);
  335. cache->tag = NULL;
  336. _leave("");
  337. }
  338. EXPORT_SYMBOL(fscache_withdraw_cache);