1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /* Cache data I/O routines
4 * Copyright (C) 2021 Red Hat, Inc. All Rights Reserved.
7 #define FSCACHE_DEBUG_LEVEL OPERATION
8 #include <linux/fscache-cache.h>
10 #include <linux/bvec.h>
11 #include <linux/slab.h>
15 * fscache_wait_for_operation - Wait for an object become accessible
16 * @cres: The cache resources for the operation being performed
17 * @want_state: The minimum state the object must be at
19 * See if the target cache object is at the specified minimum state of
20 * accessibility yet, and if not, wait for it.
22 bool fscache_wait_for_operation(struct netfs_cache_resources *cres,
23 enum fscache_want_state want_state)
25 struct fscache_cookie *cookie = fscache_cres_cookie(cres);
26 enum fscache_cookie_state state;
29 if (!fscache_cache_is_live(cookie->volume->cache)) {
34 state = fscache_cookie_state(cookie);
35 _enter("c=%08x{%u},%x", cookie->debug_id, state, want_state);
38 case FSCACHE_COOKIE_STATE_CREATING:
39 case FSCACHE_COOKIE_STATE_INVALIDATING:
40 if (want_state == FSCACHE_WANT_PARAMS)
41 goto ready; /* There can be no content */
43 case FSCACHE_COOKIE_STATE_LOOKING_UP:
44 case FSCACHE_COOKIE_STATE_LRU_DISCARDING:
45 wait_var_event(&cookie->state,
46 fscache_cookie_state(cookie) != state);
49 case FSCACHE_COOKIE_STATE_ACTIVE:
51 case FSCACHE_COOKIE_STATE_DROPPED:
52 case FSCACHE_COOKIE_STATE_RELINQUISHING:
54 _leave(" [not live]");
59 if (!cres->cache_priv2)
60 return cookie->volume->cache->ops->begin_operation(cres, want_state);
63 EXPORT_SYMBOL(fscache_wait_for_operation);
66 * Begin an I/O operation on the cache, waiting till we reach the right state.
68 * Attaches the resources required to the operation resources record.
70 static int fscache_begin_operation(struct netfs_cache_resources *cres,
71 struct fscache_cookie *cookie,
72 enum fscache_want_state want_state,
73 enum fscache_access_trace why)
75 enum fscache_cookie_state state;
77 bool once_only = false;
80 cres->cache_priv = cookie;
81 cres->cache_priv2 = NULL;
82 cres->debug_id = cookie->debug_id;
83 cres->inval_counter = cookie->inval_counter;
85 if (!fscache_begin_cookie_access(cookie, why)) {
86 cres->cache_priv = NULL;
91 spin_lock(&cookie->lock);
93 state = fscache_cookie_state(cookie);
94 _enter("c=%08x{%u},%x", cookie->debug_id, state, want_state);
97 case FSCACHE_COOKIE_STATE_LOOKING_UP:
98 case FSCACHE_COOKIE_STATE_LRU_DISCARDING:
99 case FSCACHE_COOKIE_STATE_INVALIDATING:
100 goto wait_for_file_wrangling;
101 case FSCACHE_COOKIE_STATE_CREATING:
102 if (want_state == FSCACHE_WANT_PARAMS)
103 goto ready; /* There can be no content */
104 goto wait_for_file_wrangling;
105 case FSCACHE_COOKIE_STATE_ACTIVE:
107 case FSCACHE_COOKIE_STATE_DROPPED:
108 case FSCACHE_COOKIE_STATE_RELINQUISHING:
109 WARN(1, "Can't use cookie in state %u\n", cookie->state);
116 spin_unlock(&cookie->lock);
117 if (!cookie->volume->cache->ops->begin_operation(cres, want_state))
121 wait_for_file_wrangling:
122 spin_unlock(&cookie->lock);
123 trace_fscache_access(cookie->debug_id, refcount_read(&cookie->ref),
124 atomic_read(&cookie->n_accesses),
125 fscache_access_io_wait);
126 timeo = wait_var_event_timeout(&cookie->state,
127 fscache_cookie_state(cookie) != state, 20 * HZ);
128 if (timeo <= 1 && !once_only) {
129 pr_warn("%s: cookie state change wait timed out: cookie->state=%u state=%u",
130 __func__, fscache_cookie_state(cookie), state);
131 fscache_print_cookie(cookie, 'O');
137 spin_unlock(&cookie->lock);
139 cres->cache_priv = NULL;
141 fscache_end_cookie_access(cookie, fscache_access_io_not_live);
142 _leave(" = -ENOBUFS");
146 int __fscache_begin_read_operation(struct netfs_cache_resources *cres,
147 struct fscache_cookie *cookie)
149 return fscache_begin_operation(cres, cookie, FSCACHE_WANT_PARAMS,
150 fscache_access_io_read);
152 EXPORT_SYMBOL(__fscache_begin_read_operation);
154 int __fscache_begin_write_operation(struct netfs_cache_resources *cres,
155 struct fscache_cookie *cookie)
157 return fscache_begin_operation(cres, cookie, FSCACHE_WANT_PARAMS,
158 fscache_access_io_write);
160 EXPORT_SYMBOL(__fscache_begin_write_operation);
162 struct fscache_write_request {
163 struct netfs_cache_resources cache_resources;
164 struct address_space *mapping;
169 netfs_io_terminated_t term_func;
170 void *term_func_priv;
173 void __fscache_clear_page_bits(struct address_space *mapping,
174 loff_t start, size_t len)
176 pgoff_t first = start / PAGE_SIZE;
177 pgoff_t last = (start + len - 1) / PAGE_SIZE;
181 XA_STATE(xas, &mapping->i_pages, first);
184 xas_for_each(&xas, page, last) {
185 folio_end_private_2(page_folio(page));
190 EXPORT_SYMBOL(__fscache_clear_page_bits);
193 * Deal with the completion of writing the data to the cache.
195 static void fscache_wreq_done(void *priv, ssize_t transferred_or_error,
198 struct fscache_write_request *wreq = priv;
200 if (wreq->using_pgpriv2)
201 fscache_clear_page_bits(wreq->mapping, wreq->start, wreq->len,
205 wreq->term_func(wreq->term_func_priv, transferred_or_error,
207 fscache_end_operation(&wreq->cache_resources);
211 void __fscache_write_to_cache(struct fscache_cookie *cookie,
212 struct address_space *mapping,
213 loff_t start, size_t len, loff_t i_size,
214 netfs_io_terminated_t term_func,
215 void *term_func_priv,
216 bool using_pgpriv2, bool cond)
218 struct fscache_write_request *wreq;
219 struct netfs_cache_resources *cres;
220 struct iov_iter iter;
226 _enter("%llx,%zx", start, len);
228 wreq = kzalloc(sizeof(struct fscache_write_request), GFP_NOFS);
231 wreq->mapping = mapping;
234 wreq->using_pgpriv2 = using_pgpriv2;
235 wreq->set_bits = cond;
236 wreq->term_func = term_func;
237 wreq->term_func_priv = term_func_priv;
239 cres = &wreq->cache_resources;
240 if (fscache_begin_operation(cres, cookie, FSCACHE_WANT_WRITE,
241 fscache_access_io_write) < 0)
244 ret = cres->ops->prepare_write(cres, &start, &len, len, i_size, false);
248 /* TODO: Consider clearing page bits now for space the write isn't
249 * covering. This is more complicated than it appears when THPs are
250 * taken into account.
253 iov_iter_xarray(&iter, ITER_SOURCE, &mapping->i_pages, start, len);
254 fscache_write(cres, start, &iter, fscache_wreq_done, wreq);
258 return fscache_wreq_done(wreq, ret, false);
263 fscache_clear_page_bits(mapping, start, len, cond);
265 term_func(term_func_priv, ret, false);
267 EXPORT_SYMBOL(__fscache_write_to_cache);
270 * Change the size of a backing object.
272 void __fscache_resize_cookie(struct fscache_cookie *cookie, loff_t new_size)
274 struct netfs_cache_resources cres;
276 trace_fscache_resize(cookie, new_size);
277 if (fscache_begin_operation(&cres, cookie, FSCACHE_WANT_WRITE,
278 fscache_access_io_resize) == 0) {
279 fscache_stat(&fscache_n_resizes);
280 set_bit(FSCACHE_COOKIE_NEEDS_UPDATE, &cookie->flags);
282 /* We cannot defer a resize as we need to do it inside the
283 * netfs's inode lock so that we're serialised with respect to
286 cookie->volume->cache->ops->resize_cookie(&cres, new_size);
287 fscache_end_operation(&cres);
289 fscache_stat(&fscache_n_resizes_null);
292 EXPORT_SYMBOL(__fscache_resize_cookie);