fscache_io.c 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /* Cache data I/O routines
  3. *
  4. * Copyright (C) 2021 Red Hat, Inc. All Rights Reserved.
  5. * Written by David Howells (dhowells@redhat.com)
  6. */
  7. #define FSCACHE_DEBUG_LEVEL OPERATION
  8. #include <linux/fscache-cache.h>
  9. #include <linux/uio.h>
  10. #include <linux/bvec.h>
  11. #include <linux/slab.h>
  12. #include "internal.h"
  13. /**
  14. * fscache_wait_for_operation - Wait for an object become accessible
  15. * @cres: The cache resources for the operation being performed
  16. * @want_state: The minimum state the object must be at
  17. *
  18. * See if the target cache object is at the specified minimum state of
  19. * accessibility yet, and if not, wait for it.
  20. */
  21. bool fscache_wait_for_operation(struct netfs_cache_resources *cres,
  22. enum fscache_want_state want_state)
  23. {
  24. struct fscache_cookie *cookie = fscache_cres_cookie(cres);
  25. enum fscache_cookie_state state;
  26. again:
  27. if (!fscache_cache_is_live(cookie->volume->cache)) {
  28. _leave(" [broken]");
  29. return false;
  30. }
  31. state = fscache_cookie_state(cookie);
  32. _enter("c=%08x{%u},%x", cookie->debug_id, state, want_state);
  33. switch (state) {
  34. case FSCACHE_COOKIE_STATE_CREATING:
  35. case FSCACHE_COOKIE_STATE_INVALIDATING:
  36. if (want_state == FSCACHE_WANT_PARAMS)
  37. goto ready; /* There can be no content */
  38. fallthrough;
  39. case FSCACHE_COOKIE_STATE_LOOKING_UP:
  40. case FSCACHE_COOKIE_STATE_LRU_DISCARDING:
  41. wait_var_event(&cookie->state,
  42. fscache_cookie_state(cookie) != state);
  43. goto again;
  44. case FSCACHE_COOKIE_STATE_ACTIVE:
  45. goto ready;
  46. case FSCACHE_COOKIE_STATE_DROPPED:
  47. case FSCACHE_COOKIE_STATE_RELINQUISHING:
  48. default:
  49. _leave(" [not live]");
  50. return false;
  51. }
  52. ready:
  53. if (!cres->cache_priv2)
  54. return cookie->volume->cache->ops->begin_operation(cres, want_state);
  55. return true;
  56. }
  57. EXPORT_SYMBOL(fscache_wait_for_operation);
  58. /*
  59. * Begin an I/O operation on the cache, waiting till we reach the right state.
  60. *
  61. * Attaches the resources required to the operation resources record.
  62. */
  63. static int fscache_begin_operation(struct netfs_cache_resources *cres,
  64. struct fscache_cookie *cookie,
  65. enum fscache_want_state want_state,
  66. enum fscache_access_trace why)
  67. {
  68. enum fscache_cookie_state state;
  69. long timeo;
  70. bool once_only = false;
  71. cres->ops = NULL;
  72. cres->cache_priv = cookie;
  73. cres->cache_priv2 = NULL;
  74. cres->debug_id = cookie->debug_id;
  75. cres->inval_counter = cookie->inval_counter;
  76. if (!fscache_begin_cookie_access(cookie, why)) {
  77. cres->cache_priv = NULL;
  78. return -ENOBUFS;
  79. }
  80. again:
  81. spin_lock(&cookie->lock);
  82. state = fscache_cookie_state(cookie);
  83. _enter("c=%08x{%u},%x", cookie->debug_id, state, want_state);
  84. switch (state) {
  85. case FSCACHE_COOKIE_STATE_LOOKING_UP:
  86. case FSCACHE_COOKIE_STATE_LRU_DISCARDING:
  87. case FSCACHE_COOKIE_STATE_INVALIDATING:
  88. goto wait_for_file_wrangling;
  89. case FSCACHE_COOKIE_STATE_CREATING:
  90. if (want_state == FSCACHE_WANT_PARAMS)
  91. goto ready; /* There can be no content */
  92. goto wait_for_file_wrangling;
  93. case FSCACHE_COOKIE_STATE_ACTIVE:
  94. goto ready;
  95. case FSCACHE_COOKIE_STATE_DROPPED:
  96. case FSCACHE_COOKIE_STATE_RELINQUISHING:
  97. WARN(1, "Can't use cookie in state %u\n", cookie->state);
  98. goto not_live;
  99. default:
  100. goto not_live;
  101. }
  102. ready:
  103. spin_unlock(&cookie->lock);
  104. if (!cookie->volume->cache->ops->begin_operation(cres, want_state))
  105. goto failed;
  106. return 0;
  107. wait_for_file_wrangling:
  108. spin_unlock(&cookie->lock);
  109. trace_fscache_access(cookie->debug_id, refcount_read(&cookie->ref),
  110. atomic_read(&cookie->n_accesses),
  111. fscache_access_io_wait);
  112. timeo = wait_var_event_timeout(&cookie->state,
  113. fscache_cookie_state(cookie) != state, 20 * HZ);
  114. if (timeo <= 1 && !once_only) {
  115. pr_warn("%s: cookie state change wait timed out: cookie->state=%u state=%u",
  116. __func__, fscache_cookie_state(cookie), state);
  117. fscache_print_cookie(cookie, 'O');
  118. once_only = true;
  119. }
  120. goto again;
  121. not_live:
  122. spin_unlock(&cookie->lock);
  123. failed:
  124. cres->cache_priv = NULL;
  125. cres->ops = NULL;
  126. fscache_end_cookie_access(cookie, fscache_access_io_not_live);
  127. _leave(" = -ENOBUFS");
  128. return -ENOBUFS;
  129. }
  130. int __fscache_begin_read_operation(struct netfs_cache_resources *cres,
  131. struct fscache_cookie *cookie)
  132. {
  133. return fscache_begin_operation(cres, cookie, FSCACHE_WANT_PARAMS,
  134. fscache_access_io_read);
  135. }
  136. EXPORT_SYMBOL(__fscache_begin_read_operation);
  137. int __fscache_begin_write_operation(struct netfs_cache_resources *cres,
  138. struct fscache_cookie *cookie)
  139. {
  140. return fscache_begin_operation(cres, cookie, FSCACHE_WANT_PARAMS,
  141. fscache_access_io_write);
  142. }
  143. EXPORT_SYMBOL(__fscache_begin_write_operation);
  144. struct fscache_write_request {
  145. struct netfs_cache_resources cache_resources;
  146. struct address_space *mapping;
  147. loff_t start;
  148. size_t len;
  149. bool set_bits;
  150. bool using_pgpriv2;
  151. netfs_io_terminated_t term_func;
  152. void *term_func_priv;
  153. };
  154. void __fscache_clear_page_bits(struct address_space *mapping,
  155. loff_t start, size_t len)
  156. {
  157. pgoff_t first = start / PAGE_SIZE;
  158. pgoff_t last = (start + len - 1) / PAGE_SIZE;
  159. struct page *page;
  160. if (len) {
  161. XA_STATE(xas, &mapping->i_pages, first);
  162. rcu_read_lock();
  163. xas_for_each(&xas, page, last) {
  164. folio_end_private_2(page_folio(page));
  165. }
  166. rcu_read_unlock();
  167. }
  168. }
  169. EXPORT_SYMBOL(__fscache_clear_page_bits);
  170. /*
  171. * Deal with the completion of writing the data to the cache.
  172. */
  173. static void fscache_wreq_done(void *priv, ssize_t transferred_or_error)
  174. {
  175. struct fscache_write_request *wreq = priv;
  176. if (wreq->using_pgpriv2)
  177. fscache_clear_page_bits(wreq->mapping, wreq->start, wreq->len,
  178. wreq->set_bits);
  179. if (wreq->term_func)
  180. wreq->term_func(wreq->term_func_priv, transferred_or_error);
  181. fscache_end_operation(&wreq->cache_resources);
  182. kfree(wreq);
  183. }
  184. void __fscache_write_to_cache(struct fscache_cookie *cookie,
  185. struct address_space *mapping,
  186. loff_t start, size_t len, loff_t i_size,
  187. netfs_io_terminated_t term_func,
  188. void *term_func_priv,
  189. bool using_pgpriv2, bool cond)
  190. {
  191. struct fscache_write_request *wreq;
  192. struct netfs_cache_resources *cres;
  193. struct iov_iter iter;
  194. int ret = -ENOBUFS;
  195. if (len == 0)
  196. goto abandon;
  197. _enter("%llx,%zx", start, len);
  198. wreq = kzalloc_obj(struct fscache_write_request, GFP_NOFS);
  199. if (!wreq)
  200. goto abandon;
  201. wreq->mapping = mapping;
  202. wreq->start = start;
  203. wreq->len = len;
  204. wreq->using_pgpriv2 = using_pgpriv2;
  205. wreq->set_bits = cond;
  206. wreq->term_func = term_func;
  207. wreq->term_func_priv = term_func_priv;
  208. cres = &wreq->cache_resources;
  209. if (fscache_begin_operation(cres, cookie, FSCACHE_WANT_WRITE,
  210. fscache_access_io_write) < 0)
  211. goto abandon_free;
  212. ret = cres->ops->prepare_write(cres, &start, &len, len, i_size, false);
  213. if (ret < 0)
  214. goto abandon_end;
  215. /* TODO: Consider clearing page bits now for space the write isn't
  216. * covering. This is more complicated than it appears when THPs are
  217. * taken into account.
  218. */
  219. iov_iter_xarray(&iter, ITER_SOURCE, &mapping->i_pages, start, len);
  220. fscache_write(cres, start, &iter, fscache_wreq_done, wreq);
  221. return;
  222. abandon_end:
  223. return fscache_wreq_done(wreq, ret);
  224. abandon_free:
  225. kfree(wreq);
  226. abandon:
  227. if (using_pgpriv2)
  228. fscache_clear_page_bits(mapping, start, len, cond);
  229. if (term_func)
  230. term_func(term_func_priv, ret);
  231. }
  232. EXPORT_SYMBOL(__fscache_write_to_cache);
  233. /*
  234. * Change the size of a backing object.
  235. */
  236. void __fscache_resize_cookie(struct fscache_cookie *cookie, loff_t new_size)
  237. {
  238. struct netfs_cache_resources cres;
  239. trace_fscache_resize(cookie, new_size);
  240. if (fscache_begin_operation(&cres, cookie, FSCACHE_WANT_WRITE,
  241. fscache_access_io_resize) == 0) {
  242. fscache_stat(&fscache_n_resizes);
  243. set_bit(FSCACHE_COOKIE_NEEDS_UPDATE, &cookie->flags);
  244. /* We cannot defer a resize as we need to do it inside the
  245. * netfs's inode lock so that we're serialised with respect to
  246. * writes.
  247. */
  248. cookie->volume->cache->ops->resize_cookie(&cres, new_size);
  249. fscache_end_operation(&cres);
  250. } else {
  251. fscache_stat(&fscache_n_resizes_null);
  252. }
  253. }
  254. EXPORT_SYMBOL(__fscache_resize_cookie);