/kern_oII/fs/fscache/cache.c

http://omnia2droid.googlecode.com/ · C · 415 lines · 237 code · 82 blank · 96 comment · 26 complexity · 1a71601007a5044dad1532f610a113fe MD5 · raw file

  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. /* add the cache's netfs definition index object to the top level index
  216. * cookie as a known backing object */
  217. spin_lock(&fscache_fsdef_index.lock);
  218. hlist_add_head(&ifsdef->cookie_link,
  219. &fscache_fsdef_index.backing_objects);
  220. atomic_inc(&fscache_fsdef_index.usage);
  221. /* done */
  222. spin_unlock(&fscache_fsdef_index.lock);
  223. up_write(&fscache_addremove_sem);
  224. printk(KERN_NOTICE "FS-Cache: Cache \"%s\" added (type %s)\n",
  225. cache->tag->name, cache->ops->name);
  226. kobject_uevent(cache->kobj, KOBJ_ADD);
  227. _leave(" = 0 [%s]", cache->identifier);
  228. return 0;
  229. tag_in_use:
  230. printk(KERN_ERR "FS-Cache: Cache tag '%s' already in use\n", tagname);
  231. __fscache_release_cache_tag(tag);
  232. _leave(" = -EXIST");
  233. return -EEXIST;
  234. error:
  235. __fscache_release_cache_tag(tag);
  236. _leave(" = -EINVAL");
  237. return -EINVAL;
  238. nomem:
  239. _leave(" = -ENOMEM");
  240. return -ENOMEM;
  241. }
  242. EXPORT_SYMBOL(fscache_add_cache);
  243. /**
  244. * fscache_io_error - Note a cache I/O error
  245. * @cache: The record describing the cache
  246. *
  247. * Note that an I/O error occurred in a cache and that it should no longer be
  248. * used for anything. This also reports the error into the kernel log.
  249. *
  250. * See Documentation/filesystems/caching/backend-api.txt for a complete
  251. * description.
  252. */
  253. void fscache_io_error(struct fscache_cache *cache)
  254. {
  255. set_bit(FSCACHE_IOERROR, &cache->flags);
  256. printk(KERN_ERR "FS-Cache: Cache %s stopped due to I/O error\n",
  257. cache->ops->name);
  258. }
  259. EXPORT_SYMBOL(fscache_io_error);
  260. /*
  261. * request withdrawal of all the objects in a cache
  262. * - all the objects being withdrawn are moved onto the supplied list
  263. */
  264. static void fscache_withdraw_all_objects(struct fscache_cache *cache,
  265. struct list_head *dying_objects)
  266. {
  267. struct fscache_object *object;
  268. spin_lock(&cache->object_list_lock);
  269. while (!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. spin_lock(&object->lock);
  275. spin_unlock(&cache->object_list_lock);
  276. fscache_raise_event(object, FSCACHE_OBJECT_EV_WITHDRAW);
  277. spin_unlock(&object->lock);
  278. cond_resched();
  279. spin_lock(&cache->object_list_lock);
  280. }
  281. spin_unlock(&cache->object_list_lock);
  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.txt for a complete
  291. * description.
  292. */
  293. void fscache_withdraw_cache(struct fscache_cache *cache)
  294. {
  295. LIST_HEAD(dying_objects);
  296. _enter("");
  297. printk(KERN_NOTICE "FS-Cache: 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. cache->ops->sync_cache(cache);
  309. /* dissociate all the netfs pages backed by this cache from the block
  310. * mappings in the cache */
  311. cache->ops->dissociate_pages(cache);
  312. /* we now have to destroy all the active objects pertaining to this
  313. * cache - which we do by passing them off to thread pool to be
  314. * disposed of */
  315. _debug("destroy");
  316. fscache_withdraw_all_objects(cache, &dying_objects);
  317. /* wait for all extant objects to finish their outstanding operations
  318. * and go away */
  319. _debug("wait for finish");
  320. wait_event(fscache_cache_cleared_wq,
  321. atomic_read(&cache->object_count) == 0);
  322. _debug("wait for clearance");
  323. wait_event(fscache_cache_cleared_wq,
  324. list_empty(&cache->object_list));
  325. _debug("cleared");
  326. ASSERT(list_empty(&dying_objects));
  327. kobject_put(cache->kobj);
  328. clear_bit(FSCACHE_TAG_RESERVED, &cache->tag->flags);
  329. fscache_release_cache_tag(cache->tag);
  330. cache->tag = NULL;
  331. _leave("");
  332. }
  333. EXPORT_SYMBOL(fscache_withdraw_cache);