| 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 | /** |
| 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 |
| 18 | * |
| 19 | * See if the target cache object is at the specified minimum state of |
| 20 | * accessibility yet, and if not, wait for it. |
| 21 | */ |
| 22 | bool fscache_wait_for_operation(struct netfs_cache_resources *cres, |
| 23 | enum fscache_want_state want_state) |
| 24 | { |
| 25 | struct fscache_cookie *cookie = fscache_cres_cookie(cres); |
| 26 | enum fscache_cookie_state state; |
| 27 | |
| 28 | again: |
| 29 | if (!fscache_cache_is_live(cache: cookie->volume->cache)) { |
| 30 | _leave(" [broken]" ); |
| 31 | return false; |
| 32 | } |
| 33 | |
| 34 | state = fscache_cookie_state(cookie); |
| 35 | _enter("c=%08x{%u},%x" , cookie->debug_id, state, want_state); |
| 36 | |
| 37 | switch (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 */ |
| 42 | fallthrough; |
| 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); |
| 47 | goto again; |
| 48 | |
| 49 | case FSCACHE_COOKIE_STATE_ACTIVE: |
| 50 | goto ready; |
| 51 | case FSCACHE_COOKIE_STATE_DROPPED: |
| 52 | case FSCACHE_COOKIE_STATE_RELINQUISHING: |
| 53 | default: |
| 54 | _leave(" [not live]" ); |
| 55 | return false; |
| 56 | } |
| 57 | |
| 58 | ready: |
| 59 | if (!cres->cache_priv2) |
| 60 | return cookie->volume->cache->ops->begin_operation(cres, want_state); |
| 61 | return true; |
| 62 | } |
| 63 | EXPORT_SYMBOL(fscache_wait_for_operation); |
| 64 | |
| 65 | /* |
| 66 | * Begin an I/O operation on the cache, waiting till we reach the right state. |
| 67 | * |
| 68 | * Attaches the resources required to the operation resources record. |
| 69 | */ |
| 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) |
| 74 | { |
| 75 | enum fscache_cookie_state state; |
| 76 | long timeo; |
| 77 | bool once_only = false; |
| 78 | |
| 79 | cres->ops = NULL; |
| 80 | cres->cache_priv = cookie; |
| 81 | cres->cache_priv2 = NULL; |
| 82 | cres->debug_id = cookie->debug_id; |
| 83 | cres->inval_counter = cookie->inval_counter; |
| 84 | |
| 85 | if (!fscache_begin_cookie_access(cookie, why)) { |
| 86 | cres->cache_priv = NULL; |
| 87 | return -ENOBUFS; |
| 88 | } |
| 89 | |
| 90 | again: |
| 91 | spin_lock(lock: &cookie->lock); |
| 92 | |
| 93 | state = fscache_cookie_state(cookie); |
| 94 | _enter("c=%08x{%u},%x" , cookie->debug_id, state, want_state); |
| 95 | |
| 96 | switch (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: |
| 106 | goto ready; |
| 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); |
| 110 | goto not_live; |
| 111 | default: |
| 112 | goto not_live; |
| 113 | } |
| 114 | |
| 115 | ready: |
| 116 | spin_unlock(lock: &cookie->lock); |
| 117 | if (!cookie->volume->cache->ops->begin_operation(cres, want_state)) |
| 118 | goto failed; |
| 119 | return 0; |
| 120 | |
| 121 | wait_for_file_wrangling: |
| 122 | spin_unlock(lock: &cookie->lock); |
| 123 | trace_fscache_access(cookie_debug_id: cookie->debug_id, ref: refcount_read(r: &cookie->ref), |
| 124 | n_accesses: atomic_read(v: &cookie->n_accesses), |
| 125 | why: 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, prefix: 'O'); |
| 132 | once_only = true; |
| 133 | } |
| 134 | goto again; |
| 135 | |
| 136 | not_live: |
| 137 | spin_unlock(lock: &cookie->lock); |
| 138 | failed: |
| 139 | cres->cache_priv = NULL; |
| 140 | cres->ops = NULL; |
| 141 | fscache_end_cookie_access(cookie, why: fscache_access_io_not_live); |
| 142 | _leave(" = -ENOBUFS" ); |
| 143 | return -ENOBUFS; |
| 144 | } |
| 145 | |
| 146 | int __fscache_begin_read_operation(struct netfs_cache_resources *cres, |
| 147 | struct fscache_cookie *cookie) |
| 148 | { |
| 149 | return fscache_begin_operation(cres, cookie, want_state: FSCACHE_WANT_PARAMS, |
| 150 | why: fscache_access_io_read); |
| 151 | } |
| 152 | EXPORT_SYMBOL(__fscache_begin_read_operation); |
| 153 | |
| 154 | int __fscache_begin_write_operation(struct netfs_cache_resources *cres, |
| 155 | struct fscache_cookie *cookie) |
| 156 | { |
| 157 | return fscache_begin_operation(cres, cookie, want_state: FSCACHE_WANT_PARAMS, |
| 158 | why: fscache_access_io_write); |
| 159 | } |
| 160 | EXPORT_SYMBOL(__fscache_begin_write_operation); |
| 161 | |
| 162 | struct fscache_write_request { |
| 163 | struct netfs_cache_resources cache_resources; |
| 164 | struct address_space *mapping; |
| 165 | loff_t start; |
| 166 | size_t len; |
| 167 | bool set_bits; |
| 168 | bool using_pgpriv2; |
| 169 | netfs_io_terminated_t term_func; |
| 170 | void *term_func_priv; |
| 171 | }; |
| 172 | |
| 173 | void __fscache_clear_page_bits(struct address_space *mapping, |
| 174 | loff_t start, size_t len) |
| 175 | { |
| 176 | pgoff_t first = start / PAGE_SIZE; |
| 177 | pgoff_t last = (start + len - 1) / PAGE_SIZE; |
| 178 | struct page *page; |
| 179 | |
| 180 | if (len) { |
| 181 | XA_STATE(xas, &mapping->i_pages, first); |
| 182 | |
| 183 | rcu_read_lock(); |
| 184 | xas_for_each(&xas, page, last) { |
| 185 | folio_end_private_2(page_folio(page)); |
| 186 | } |
| 187 | rcu_read_unlock(); |
| 188 | } |
| 189 | } |
| 190 | EXPORT_SYMBOL(__fscache_clear_page_bits); |
| 191 | |
| 192 | /* |
| 193 | * Deal with the completion of writing the data to the cache. |
| 194 | */ |
| 195 | static void fscache_wreq_done(void *priv, ssize_t transferred_or_error) |
| 196 | { |
| 197 | struct fscache_write_request *wreq = priv; |
| 198 | |
| 199 | if (wreq->using_pgpriv2) |
| 200 | fscache_clear_page_bits(mapping: wreq->mapping, start: wreq->start, len: wreq->len, |
| 201 | caching: wreq->set_bits); |
| 202 | |
| 203 | if (wreq->term_func) |
| 204 | wreq->term_func(wreq->term_func_priv, transferred_or_error); |
| 205 | fscache_end_operation(cres: &wreq->cache_resources); |
| 206 | kfree(objp: wreq); |
| 207 | } |
| 208 | |
| 209 | void __fscache_write_to_cache(struct fscache_cookie *cookie, |
| 210 | struct address_space *mapping, |
| 211 | loff_t start, size_t len, loff_t i_size, |
| 212 | netfs_io_terminated_t term_func, |
| 213 | void *term_func_priv, |
| 214 | bool using_pgpriv2, bool cond) |
| 215 | { |
| 216 | struct fscache_write_request *wreq; |
| 217 | struct netfs_cache_resources *cres; |
| 218 | struct iov_iter iter; |
| 219 | int ret = -ENOBUFS; |
| 220 | |
| 221 | if (len == 0) |
| 222 | goto abandon; |
| 223 | |
| 224 | _enter("%llx,%zx" , start, len); |
| 225 | |
| 226 | wreq = kzalloc(sizeof(struct fscache_write_request), GFP_NOFS); |
| 227 | if (!wreq) |
| 228 | goto abandon; |
| 229 | wreq->mapping = mapping; |
| 230 | wreq->start = start; |
| 231 | wreq->len = len; |
| 232 | wreq->using_pgpriv2 = using_pgpriv2; |
| 233 | wreq->set_bits = cond; |
| 234 | wreq->term_func = term_func; |
| 235 | wreq->term_func_priv = term_func_priv; |
| 236 | |
| 237 | cres = &wreq->cache_resources; |
| 238 | if (fscache_begin_operation(cres, cookie, want_state: FSCACHE_WANT_WRITE, |
| 239 | why: fscache_access_io_write) < 0) |
| 240 | goto abandon_free; |
| 241 | |
| 242 | ret = cres->ops->prepare_write(cres, &start, &len, len, i_size, false); |
| 243 | if (ret < 0) |
| 244 | goto abandon_end; |
| 245 | |
| 246 | /* TODO: Consider clearing page bits now for space the write isn't |
| 247 | * covering. This is more complicated than it appears when THPs are |
| 248 | * taken into account. |
| 249 | */ |
| 250 | |
| 251 | iov_iter_xarray(i: &iter, ITER_SOURCE, xarray: &mapping->i_pages, start, count: len); |
| 252 | fscache_write(cres, start_pos: start, iter: &iter, term_func: fscache_wreq_done, term_func_priv: wreq); |
| 253 | return; |
| 254 | |
| 255 | abandon_end: |
| 256 | return fscache_wreq_done(priv: wreq, transferred_or_error: ret); |
| 257 | abandon_free: |
| 258 | kfree(objp: wreq); |
| 259 | abandon: |
| 260 | if (using_pgpriv2) |
| 261 | fscache_clear_page_bits(mapping, start, len, caching: cond); |
| 262 | if (term_func) |
| 263 | term_func(term_func_priv, ret); |
| 264 | } |
| 265 | EXPORT_SYMBOL(__fscache_write_to_cache); |
| 266 | |
| 267 | /* |
| 268 | * Change the size of a backing object. |
| 269 | */ |
| 270 | void __fscache_resize_cookie(struct fscache_cookie *cookie, loff_t new_size) |
| 271 | { |
| 272 | struct netfs_cache_resources cres; |
| 273 | |
| 274 | trace_fscache_resize(cookie, new_size); |
| 275 | if (fscache_begin_operation(cres: &cres, cookie, want_state: FSCACHE_WANT_WRITE, |
| 276 | why: fscache_access_io_resize) == 0) { |
| 277 | fscache_stat(stat: &fscache_n_resizes); |
| 278 | set_bit(FSCACHE_COOKIE_NEEDS_UPDATE, addr: &cookie->flags); |
| 279 | |
| 280 | /* We cannot defer a resize as we need to do it inside the |
| 281 | * netfs's inode lock so that we're serialised with respect to |
| 282 | * writes. |
| 283 | */ |
| 284 | cookie->volume->cache->ops->resize_cookie(&cres, new_size); |
| 285 | fscache_end_operation(cres: &cres); |
| 286 | } else { |
| 287 | fscache_stat(stat: &fscache_n_resizes_null); |
| 288 | } |
| 289 | } |
| 290 | EXPORT_SYMBOL(__fscache_resize_cookie); |
| 291 | |