cache.c 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  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. static LIST_HEAD(fscache_cache_tag_list);
  18. /*
  19. * look up a cache tag
  20. */
  21. struct fscache_cache_tag *__fscache_lookup_cache_tag(const char *name)
  22. {
  23. struct fscache_cache_tag *tag, *xtag;
  24. /* firstly check for the existence of the tag under read lock */
  25. down_read(&fscache_addremove_sem);
  26. list_for_each_entry(tag, &fscache_cache_tag_list, link) {
  27. if (strcmp(tag->name, name) == 0) {
  28. atomic_inc(&tag->usage);
  29. up_read(&fscache_addremove_sem);
  30. return tag;
  31. }
  32. }
  33. up_read(&fscache_addremove_sem);
  34. /* the tag does not exist - create a candidate */
  35. xtag = kzalloc(sizeof(*xtag) + strlen(name) + 1, GFP_KERNEL);
  36. if (!xtag)
  37. /* return a dummy tag if out of memory */
  38. return ERR_PTR(-ENOMEM);
  39. atomic_set(&xtag->usage, 1);
  40. strcpy(xtag->name, name);
  41. /* write lock, search again and add if still not present */
  42. down_write(&fscache_addremove_sem);
  43. list_for_each_entry(tag, &fscache_cache_tag_list, link) {
  44. if (strcmp(tag->name, name) == 0) {
  45. atomic_inc(&tag->usage);
  46. up_write(&fscache_addremove_sem);
  47. kfree(xtag);
  48. return tag;
  49. }
  50. }
  51. list_add_tail(&xtag->link, &fscache_cache_tag_list);
  52. up_write(&fscache_addremove_sem);
  53. return xtag;
  54. }
  55. /*
  56. * release a reference to a cache tag
  57. */
  58. void __fscache_release_cache_tag(struct fscache_cache_tag *tag)
  59. {
  60. if (tag != ERR_PTR(-ENOMEM)) {
  61. down_write(&fscache_addremove_sem);
  62. if (atomic_dec_and_test(&tag->usage))
  63. list_del_init(&tag->link);
  64. else
  65. tag = NULL;
  66. up_write(&fscache_addremove_sem);
  67. kfree(tag);
  68. }
  69. }
  70. /*
  71. * select a cache in which to store an object
  72. * - the cache addremove semaphore must be at least read-locked by the caller
  73. * - the object will never be an index
  74. */
  75. struct fscache_cache *fscache_select_cache_for_object(
  76. struct fscache_cookie *cookie)
  77. {
  78. struct fscache_cache_tag *tag;
  79. struct fscache_object *object;
  80. struct fscache_cache *cache;
  81. _enter("");
  82. if (list_empty(&fscache_cache_list)) {
  83. _leave(" = NULL [no cache]");
  84. return NULL;
  85. }
  86. /* we check the parent to determine the cache to use */
  87. spin_lock(&cookie->lock);
  88. /* the first in the parent's backing list should be the preferred
  89. * cache */
  90. if (!hlist_empty(&cookie->backing_objects)) {
  91. object = hlist_entry(cookie->backing_objects.first,
  92. struct fscache_object, cookie_link);
  93. cache = object->cache;
  94. if (object->state >= FSCACHE_OBJECT_DYING ||
  95. test_bit(FSCACHE_IOERROR, &cache->flags))
  96. cache = NULL;
  97. spin_unlock(&cookie->lock);
  98. _leave(" = %p [parent]", cache);
  99. return cache;
  100. }
  101. /* the parent is unbacked */
  102. if (cookie->def->type != FSCACHE_COOKIE_TYPE_INDEX) {
  103. /* cookie not an index and is unbacked */
  104. spin_unlock(&cookie->lock);
  105. _leave(" = NULL [cookie ub,ni]");
  106. return NULL;
  107. }
  108. spin_unlock(&cookie->lock);
  109. if (!cookie->def->select_cache)
  110. goto no_preference;
  111. /* ask the netfs for its preference */
  112. tag = cookie->def->select_cache(cookie->parent->netfs_data,
  113. cookie->netfs_data);
  114. if (!tag)
  115. goto no_preference;
  116. if (tag == ERR_PTR(-ENOMEM)) {
  117. _leave(" = NULL [nomem tag]");
  118. return NULL;
  119. }
  120. if (!tag->cache) {
  121. _leave(" = NULL [unbacked tag]");
  122. return NULL;
  123. }
  124. if (test_bit(FSCACHE_IOERROR, &tag->cache->flags))
  125. return NULL;
  126. _leave(" = %p [specific]", tag->cache);
  127. return tag->cache;
  128. no_preference:
  129. /* netfs has no preference - just select first cache */
  130. cache = list_entry(fscache_cache_list.next,
  131. struct fscache_cache, link);
  132. _leave(" = %p [first]", cache);
  133. return cache;
  134. }