fscache-cache.h 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  1. /* SPDX-License-Identifier: GPL-2.0-or-later */
  2. /* General filesystem caching backing cache interface
  3. *
  4. * Copyright (C) 2021 Red Hat, Inc. All Rights Reserved.
  5. * Written by David Howells ([email protected])
  6. *
  7. * NOTE!!! See:
  8. *
  9. * Documentation/filesystems/caching/backend-api.rst
  10. *
  11. * for a description of the cache backend interface declared here.
  12. */
  13. #ifndef _LINUX_FSCACHE_CACHE_H
  14. #define _LINUX_FSCACHE_CACHE_H
  15. #include <linux/fscache.h>
  16. enum fscache_cache_trace;
  17. enum fscache_cookie_trace;
  18. enum fscache_access_trace;
  19. enum fscache_cache_state {
  20. FSCACHE_CACHE_IS_NOT_PRESENT, /* No cache is present for this name */
  21. FSCACHE_CACHE_IS_PREPARING, /* A cache is preparing to come live */
  22. FSCACHE_CACHE_IS_ACTIVE, /* Attached cache is active and can be used */
  23. FSCACHE_CACHE_GOT_IOERROR, /* Attached cache stopped on I/O error */
  24. FSCACHE_CACHE_IS_WITHDRAWN, /* Attached cache is being withdrawn */
  25. #define NR__FSCACHE_CACHE_STATE (FSCACHE_CACHE_IS_WITHDRAWN + 1)
  26. };
  27. /*
  28. * Cache cookie.
  29. */
  30. struct fscache_cache {
  31. const struct fscache_cache_ops *ops;
  32. struct list_head cache_link; /* Link in cache list */
  33. void *cache_priv; /* Private cache data (or NULL) */
  34. refcount_t ref;
  35. atomic_t n_volumes; /* Number of active volumes; */
  36. atomic_t n_accesses; /* Number of in-progress accesses on the cache */
  37. atomic_t object_count; /* no. of live objects in this cache */
  38. unsigned int debug_id;
  39. enum fscache_cache_state state;
  40. char *name;
  41. };
  42. /*
  43. * cache operations
  44. */
  45. struct fscache_cache_ops {
  46. /* name of cache provider */
  47. const char *name;
  48. /* Acquire a volume */
  49. void (*acquire_volume)(struct fscache_volume *volume);
  50. /* Free the cache's data attached to a volume */
  51. void (*free_volume)(struct fscache_volume *volume);
  52. /* Look up a cookie in the cache */
  53. bool (*lookup_cookie)(struct fscache_cookie *cookie);
  54. /* Withdraw an object without any cookie access counts held */
  55. void (*withdraw_cookie)(struct fscache_cookie *cookie);
  56. /* Change the size of a data object */
  57. void (*resize_cookie)(struct netfs_cache_resources *cres,
  58. loff_t new_size);
  59. /* Invalidate an object */
  60. bool (*invalidate_cookie)(struct fscache_cookie *cookie);
  61. /* Begin an operation for the netfs lib */
  62. bool (*begin_operation)(struct netfs_cache_resources *cres,
  63. enum fscache_want_state want_state);
  64. /* Prepare to write to a live cache object */
  65. void (*prepare_to_write)(struct fscache_cookie *cookie);
  66. };
  67. extern struct workqueue_struct *fscache_wq;
  68. extern wait_queue_head_t fscache_clearance_waiters;
  69. /*
  70. * out-of-line cache backend functions
  71. */
  72. extern struct rw_semaphore fscache_addremove_sem;
  73. extern struct fscache_cache *fscache_acquire_cache(const char *name);
  74. extern void fscache_relinquish_cache(struct fscache_cache *cache);
  75. extern int fscache_add_cache(struct fscache_cache *cache,
  76. const struct fscache_cache_ops *ops,
  77. void *cache_priv);
  78. extern void fscache_withdraw_cache(struct fscache_cache *cache);
  79. extern void fscache_withdraw_volume(struct fscache_volume *volume);
  80. extern void fscache_withdraw_cookie(struct fscache_cookie *cookie);
  81. extern void fscache_io_error(struct fscache_cache *cache);
  82. extern void fscache_end_volume_access(struct fscache_volume *volume,
  83. struct fscache_cookie *cookie,
  84. enum fscache_access_trace why);
  85. extern struct fscache_cookie *fscache_get_cookie(struct fscache_cookie *cookie,
  86. enum fscache_cookie_trace where);
  87. extern void fscache_put_cookie(struct fscache_cookie *cookie,
  88. enum fscache_cookie_trace where);
  89. extern void fscache_end_cookie_access(struct fscache_cookie *cookie,
  90. enum fscache_access_trace why);
  91. extern void fscache_cookie_lookup_negative(struct fscache_cookie *cookie);
  92. extern void fscache_resume_after_invalidation(struct fscache_cookie *cookie);
  93. extern void fscache_caching_failed(struct fscache_cookie *cookie);
  94. extern bool fscache_wait_for_operation(struct netfs_cache_resources *cred,
  95. enum fscache_want_state state);
  96. /**
  97. * fscache_cookie_state - Read the state of a cookie
  98. * @cookie: The cookie to query
  99. *
  100. * Get the state of a cookie, imposing an ordering between the cookie contents
  101. * and the state value. Paired with fscache_set_cookie_state().
  102. */
  103. static inline
  104. enum fscache_cookie_state fscache_cookie_state(struct fscache_cookie *cookie)
  105. {
  106. return smp_load_acquire(&cookie->state);
  107. }
  108. /**
  109. * fscache_get_key - Get a pointer to the cookie key
  110. * @cookie: The cookie to query
  111. *
  112. * Return a pointer to the where a cookie's key is stored.
  113. */
  114. static inline void *fscache_get_key(struct fscache_cookie *cookie)
  115. {
  116. if (cookie->key_len <= sizeof(cookie->inline_key))
  117. return cookie->inline_key;
  118. else
  119. return cookie->key;
  120. }
  121. static inline struct fscache_cookie *fscache_cres_cookie(struct netfs_cache_resources *cres)
  122. {
  123. return cres->cache_priv;
  124. }
  125. /**
  126. * fscache_count_object - Tell fscache that an object has been added
  127. * @cache: The cache to account to
  128. *
  129. * Tell fscache that an object has been added to the cache. This prevents the
  130. * cache from tearing down the cache structure until the object is uncounted.
  131. */
  132. static inline void fscache_count_object(struct fscache_cache *cache)
  133. {
  134. atomic_inc(&cache->object_count);
  135. }
  136. /**
  137. * fscache_uncount_object - Tell fscache that an object has been removed
  138. * @cache: The cache to account to
  139. *
  140. * Tell fscache that an object has been removed from the cache and will no
  141. * longer be accessed. After this point, the cache cookie may be destroyed.
  142. */
  143. static inline void fscache_uncount_object(struct fscache_cache *cache)
  144. {
  145. if (atomic_dec_and_test(&cache->object_count))
  146. wake_up_all(&fscache_clearance_waiters);
  147. }
  148. /**
  149. * fscache_wait_for_objects - Wait for all objects to be withdrawn
  150. * @cache: The cache to query
  151. *
  152. * Wait for all extant objects in a cache to finish being withdrawn
  153. * and go away.
  154. */
  155. static inline void fscache_wait_for_objects(struct fscache_cache *cache)
  156. {
  157. wait_event(fscache_clearance_waiters,
  158. atomic_read(&cache->object_count) == 0);
  159. }
  160. #ifdef CONFIG_FSCACHE_STATS
  161. extern atomic_t fscache_n_read;
  162. extern atomic_t fscache_n_write;
  163. extern atomic_t fscache_n_no_write_space;
  164. extern atomic_t fscache_n_no_create_space;
  165. extern atomic_t fscache_n_culled;
  166. #define fscache_count_read() atomic_inc(&fscache_n_read)
  167. #define fscache_count_write() atomic_inc(&fscache_n_write)
  168. #define fscache_count_no_write_space() atomic_inc(&fscache_n_no_write_space)
  169. #define fscache_count_no_create_space() atomic_inc(&fscache_n_no_create_space)
  170. #define fscache_count_culled() atomic_inc(&fscache_n_culled)
  171. #else
  172. #define fscache_count_read() do {} while(0)
  173. #define fscache_count_write() do {} while(0)
  174. #define fscache_count_no_write_space() do {} while(0)
  175. #define fscache_count_no_create_space() do {} while(0)
  176. #define fscache_count_culled() do {} while(0)
  177. #endif
  178. #endif /* _LINUX_FSCACHE_CACHE_H */