|
| 1 | +// SPDX-License-Identifier: GPL-2.0-only |
| 2 | +/* Network filesystem high-level write support. |
| 3 | + * |
| 4 | + * Copyright (C) 2023 Red Hat, Inc. All Rights Reserved. |
| 5 | + * Written by David Howells (dhowells@redhat.com) |
| 6 | + */ |
| 7 | + |
| 8 | +#include <linux/fs.h> |
| 9 | +#include <linux/mm.h> |
| 10 | +#include <linux/pagemap.h> |
| 11 | +#include <linux/slab.h> |
| 12 | +#include <linux/writeback.h> |
| 13 | +#include <linux/pagevec.h> |
| 14 | +#include "internal.h" |
| 15 | + |
| 16 | +/** |
| 17 | + * netfs_create_write_request - Create a write operation. |
| 18 | + * @wreq: The write request this is storing from. |
| 19 | + * @dest: The destination type |
| 20 | + * @start: Start of the region this write will modify |
| 21 | + * @len: Length of the modification |
| 22 | + * @worker: The worker function to handle the write(s) |
| 23 | + * |
| 24 | + * Allocate a write operation, set it up and add it to the list on a write |
| 25 | + * request. |
| 26 | + */ |
| 27 | +struct netfs_io_subrequest *netfs_create_write_request(struct netfs_io_request *wreq, |
| 28 | + enum netfs_io_source dest, |
| 29 | + loff_t start, size_t len, |
| 30 | + work_func_t worker) |
| 31 | +{ |
| 32 | + struct netfs_io_subrequest *subreq; |
| 33 | + |
| 34 | + subreq = netfs_alloc_subrequest(wreq); |
| 35 | + if (subreq) { |
| 36 | + INIT_WORK(&subreq->work, worker); |
| 37 | + subreq->source = dest; |
| 38 | + subreq->start = start; |
| 39 | + subreq->len = len; |
| 40 | + subreq->debug_index = wreq->subreq_counter++; |
| 41 | + |
| 42 | + switch (subreq->source) { |
| 43 | + case NETFS_UPLOAD_TO_SERVER: |
| 44 | + netfs_stat(&netfs_n_wh_upload); |
| 45 | + break; |
| 46 | + case NETFS_WRITE_TO_CACHE: |
| 47 | + netfs_stat(&netfs_n_wh_write); |
| 48 | + break; |
| 49 | + default: |
| 50 | + BUG(); |
| 51 | + } |
| 52 | + |
| 53 | + subreq->io_iter = wreq->io_iter; |
| 54 | + iov_iter_advance(&subreq->io_iter, subreq->start - wreq->start); |
| 55 | + iov_iter_truncate(&subreq->io_iter, subreq->len); |
| 56 | + |
| 57 | + trace_netfs_sreq_ref(wreq->debug_id, subreq->debug_index, |
| 58 | + refcount_read(&subreq->ref), |
| 59 | + netfs_sreq_trace_new); |
| 60 | + atomic_inc(&wreq->nr_outstanding); |
| 61 | + list_add_tail(&subreq->rreq_link, &wreq->subrequests); |
| 62 | + trace_netfs_sreq(subreq, netfs_sreq_trace_prepare); |
| 63 | + } |
| 64 | + |
| 65 | + return subreq; |
| 66 | +} |
| 67 | +EXPORT_SYMBOL(netfs_create_write_request); |
| 68 | + |
| 69 | +/* |
| 70 | + * Process a completed write request once all the component operations have |
| 71 | + * been completed. |
| 72 | + */ |
| 73 | +static void netfs_write_terminated(struct netfs_io_request *wreq, bool was_async) |
| 74 | +{ |
| 75 | + struct netfs_io_subrequest *subreq; |
| 76 | + struct netfs_inode *ctx = netfs_inode(wreq->inode); |
| 77 | + |
| 78 | + _enter("R=%x[]", wreq->debug_id); |
| 79 | + |
| 80 | + trace_netfs_rreq(wreq, netfs_rreq_trace_write_done); |
| 81 | + |
| 82 | + list_for_each_entry(subreq, &wreq->subrequests, rreq_link) { |
| 83 | + if (!subreq->error) |
| 84 | + continue; |
| 85 | + switch (subreq->source) { |
| 86 | + case NETFS_UPLOAD_TO_SERVER: |
| 87 | + /* Depending on the type of failure, this may prevent |
| 88 | + * writeback completion unless we're in disconnected |
| 89 | + * mode. |
| 90 | + */ |
| 91 | + if (!wreq->error) |
| 92 | + wreq->error = subreq->error; |
| 93 | + break; |
| 94 | + |
| 95 | + case NETFS_WRITE_TO_CACHE: |
| 96 | + /* Failure doesn't prevent writeback completion unless |
| 97 | + * we're in disconnected mode. |
| 98 | + */ |
| 99 | + if (subreq->error != -ENOBUFS) |
| 100 | + ctx->ops->invalidate_cache(wreq); |
| 101 | + break; |
| 102 | + |
| 103 | + default: |
| 104 | + WARN_ON_ONCE(1); |
| 105 | + if (!wreq->error) |
| 106 | + wreq->error = -EIO; |
| 107 | + return; |
| 108 | + } |
| 109 | + } |
| 110 | + |
| 111 | + wreq->cleanup(wreq); |
| 112 | + |
| 113 | + _debug("finished"); |
| 114 | + trace_netfs_rreq(wreq, netfs_rreq_trace_wake_ip); |
| 115 | + clear_bit_unlock(NETFS_RREQ_IN_PROGRESS, &wreq->flags); |
| 116 | + wake_up_bit(&wreq->flags, NETFS_RREQ_IN_PROGRESS); |
| 117 | + |
| 118 | + netfs_clear_subrequests(wreq, was_async); |
| 119 | + netfs_put_request(wreq, was_async, netfs_rreq_trace_put_complete); |
| 120 | +} |
| 121 | + |
| 122 | +/* |
| 123 | + * Deal with the completion of writing the data to the cache. |
| 124 | + */ |
| 125 | +void netfs_write_subrequest_terminated(void *_op, ssize_t transferred_or_error, |
| 126 | + bool was_async) |
| 127 | +{ |
| 128 | + struct netfs_io_subrequest *subreq = _op; |
| 129 | + struct netfs_io_request *wreq = subreq->rreq; |
| 130 | + unsigned int u; |
| 131 | + |
| 132 | + _enter("%x[%x] %zd", wreq->debug_id, subreq->debug_index, transferred_or_error); |
| 133 | + |
| 134 | + switch (subreq->source) { |
| 135 | + case NETFS_UPLOAD_TO_SERVER: |
| 136 | + netfs_stat(&netfs_n_wh_upload_done); |
| 137 | + break; |
| 138 | + case NETFS_WRITE_TO_CACHE: |
| 139 | + netfs_stat(&netfs_n_wh_write_done); |
| 140 | + break; |
| 141 | + case NETFS_INVALID_WRITE: |
| 142 | + break; |
| 143 | + default: |
| 144 | + BUG(); |
| 145 | + } |
| 146 | + |
| 147 | + if (IS_ERR_VALUE(transferred_or_error)) { |
| 148 | + subreq->error = transferred_or_error; |
| 149 | + trace_netfs_failure(wreq, subreq, transferred_or_error, |
| 150 | + netfs_fail_write); |
| 151 | + goto failed; |
| 152 | + } |
| 153 | + |
| 154 | + if (WARN(transferred_or_error > subreq->len - subreq->transferred, |
| 155 | + "Subreq excess write: R%x[%x] %zd > %zu - %zu", |
| 156 | + wreq->debug_id, subreq->debug_index, |
| 157 | + transferred_or_error, subreq->len, subreq->transferred)) |
| 158 | + transferred_or_error = subreq->len - subreq->transferred; |
| 159 | + |
| 160 | + subreq->error = 0; |
| 161 | + subreq->transferred += transferred_or_error; |
| 162 | + |
| 163 | + if (iov_iter_count(&subreq->io_iter) != subreq->len - subreq->transferred) |
| 164 | + pr_warn("R=%08x[%u] ITER POST-MISMATCH %zx != %zx-%zx %x\n", |
| 165 | + wreq->debug_id, subreq->debug_index, |
| 166 | + iov_iter_count(&subreq->io_iter), subreq->len, |
| 167 | + subreq->transferred, subreq->io_iter.iter_type); |
| 168 | + |
| 169 | + if (subreq->transferred < subreq->len) |
| 170 | + goto incomplete; |
| 171 | + |
| 172 | + __clear_bit(NETFS_SREQ_NO_PROGRESS, &subreq->flags); |
| 173 | +out: |
| 174 | + trace_netfs_sreq(subreq, netfs_sreq_trace_terminated); |
| 175 | + |
| 176 | + /* If we decrement nr_outstanding to 0, the ref belongs to us. */ |
| 177 | + u = atomic_dec_return(&wreq->nr_outstanding); |
| 178 | + if (u == 0) |
| 179 | + netfs_write_terminated(wreq, was_async); |
| 180 | + else if (u == 1) |
| 181 | + wake_up_var(&wreq->nr_outstanding); |
| 182 | + |
| 183 | + netfs_put_subrequest(subreq, was_async, netfs_sreq_trace_put_terminated); |
| 184 | + return; |
| 185 | + |
| 186 | +incomplete: |
| 187 | + if (transferred_or_error == 0) { |
| 188 | + if (__test_and_set_bit(NETFS_SREQ_NO_PROGRESS, &subreq->flags)) { |
| 189 | + subreq->error = -ENODATA; |
| 190 | + goto failed; |
| 191 | + } |
| 192 | + } else { |
| 193 | + __clear_bit(NETFS_SREQ_NO_PROGRESS, &subreq->flags); |
| 194 | + } |
| 195 | + |
| 196 | + __set_bit(NETFS_SREQ_SHORT_IO, &subreq->flags); |
| 197 | + set_bit(NETFS_RREQ_INCOMPLETE_IO, &wreq->flags); |
| 198 | + goto out; |
| 199 | + |
| 200 | +failed: |
| 201 | + switch (subreq->source) { |
| 202 | + case NETFS_WRITE_TO_CACHE: |
| 203 | + netfs_stat(&netfs_n_wh_write_failed); |
| 204 | + set_bit(NETFS_RREQ_INCOMPLETE_IO, &wreq->flags); |
| 205 | + break; |
| 206 | + case NETFS_UPLOAD_TO_SERVER: |
| 207 | + netfs_stat(&netfs_n_wh_upload_failed); |
| 208 | + set_bit(NETFS_RREQ_FAILED, &wreq->flags); |
| 209 | + wreq->error = subreq->error; |
| 210 | + break; |
| 211 | + default: |
| 212 | + break; |
| 213 | + } |
| 214 | + goto out; |
| 215 | +} |
| 216 | +EXPORT_SYMBOL(netfs_write_subrequest_terminated); |
| 217 | + |
| 218 | +static void netfs_write_to_cache_op(struct netfs_io_subrequest *subreq) |
| 219 | +{ |
| 220 | + struct netfs_io_request *wreq = subreq->rreq; |
| 221 | + struct netfs_cache_resources *cres = &wreq->cache_resources; |
| 222 | + |
| 223 | + trace_netfs_sreq(subreq, netfs_sreq_trace_submit); |
| 224 | + |
| 225 | + cres->ops->write(cres, subreq->start, &subreq->io_iter, |
| 226 | + netfs_write_subrequest_terminated, subreq); |
| 227 | +} |
| 228 | + |
| 229 | +static void netfs_write_to_cache_op_worker(struct work_struct *work) |
| 230 | +{ |
| 231 | + struct netfs_io_subrequest *subreq = |
| 232 | + container_of(work, struct netfs_io_subrequest, work); |
| 233 | + |
| 234 | + netfs_write_to_cache_op(subreq); |
| 235 | +} |
| 236 | + |
| 237 | +/** |
| 238 | + * netfs_queue_write_request - Queue a write request for attention |
| 239 | + * @subreq: The write request to be queued |
| 240 | + * |
| 241 | + * Queue the specified write request for processing by a worker thread. We |
| 242 | + * pass the caller's ref on the request to the worker thread. |
| 243 | + */ |
| 244 | +void netfs_queue_write_request(struct netfs_io_subrequest *subreq) |
| 245 | +{ |
| 246 | + if (!queue_work(system_unbound_wq, &subreq->work)) |
| 247 | + netfs_put_subrequest(subreq, false, netfs_sreq_trace_put_wip); |
| 248 | +} |
| 249 | +EXPORT_SYMBOL(netfs_queue_write_request); |
| 250 | + |
| 251 | +/* |
| 252 | + * Set up a op for writing to the cache. |
| 253 | + */ |
| 254 | +static void netfs_set_up_write_to_cache(struct netfs_io_request *wreq) |
| 255 | +{ |
| 256 | + struct netfs_cache_resources *cres; |
| 257 | + struct netfs_io_subrequest *subreq; |
| 258 | + struct netfs_inode *ctx = netfs_inode(wreq->inode); |
| 259 | + struct fscache_cookie *cookie = netfs_i_cookie(ctx); |
| 260 | + loff_t start = wreq->start; |
| 261 | + size_t len = wreq->len; |
| 262 | + int ret; |
| 263 | + |
| 264 | + if (!fscache_cookie_enabled(cookie)) { |
| 265 | + clear_bit(NETFS_RREQ_WRITE_TO_CACHE, &wreq->flags); |
| 266 | + return; |
| 267 | + } |
| 268 | + |
| 269 | + _debug("write to cache"); |
| 270 | + subreq = netfs_create_write_request(wreq, NETFS_WRITE_TO_CACHE, start, len, |
| 271 | + netfs_write_to_cache_op_worker); |
| 272 | + if (!subreq) |
| 273 | + return; |
| 274 | + |
| 275 | + cres = &wreq->cache_resources; |
| 276 | + ret = fscache_begin_read_operation(cres, cookie); |
| 277 | + if (ret < 0) { |
| 278 | + netfs_write_subrequest_terminated(subreq, ret, false); |
| 279 | + return; |
| 280 | + } |
| 281 | + |
| 282 | + ret = cres->ops->prepare_write(cres, &start, &len, i_size_read(wreq->inode), |
| 283 | + true); |
| 284 | + if (ret < 0) { |
| 285 | + netfs_write_subrequest_terminated(subreq, ret, false); |
| 286 | + return; |
| 287 | + } |
| 288 | + |
| 289 | + netfs_queue_write_request(subreq); |
| 290 | +} |
| 291 | + |
| 292 | +/* |
| 293 | + * Begin the process of writing out a chunk of data. |
| 294 | + * |
| 295 | + * We are given a write request that holds a series of dirty regions and |
| 296 | + * (partially) covers a sequence of folios, all of which are present. The |
| 297 | + * pages must have been marked as writeback as appropriate. |
| 298 | + * |
| 299 | + * We need to perform the following steps: |
| 300 | + * |
| 301 | + * (1) If encrypting, create an output buffer and encrypt each block of the |
| 302 | + * data into it, otherwise the output buffer will point to the original |
| 303 | + * folios. |
| 304 | + * |
| 305 | + * (2) If the data is to be cached, set up a write op for the entire output |
| 306 | + * buffer to the cache, if the cache wants to accept it. |
| 307 | + * |
| 308 | + * (3) If the data is to be uploaded (ie. not merely cached): |
| 309 | + * |
| 310 | + * (a) If the data is to be compressed, create a compression buffer and |
| 311 | + * compress the data into it. |
| 312 | + * |
| 313 | + * (b) For each destination we want to upload to, set up write ops to write |
| 314 | + * to that destination. We may need multiple writes if the data is not |
| 315 | + * contiguous or the span exceeds wsize for a server. |
| 316 | + */ |
| 317 | +int netfs_begin_write(struct netfs_io_request *wreq, bool may_wait, |
| 318 | + enum netfs_write_trace what) |
| 319 | +{ |
| 320 | + struct netfs_inode *ctx = netfs_inode(wreq->inode); |
| 321 | + |
| 322 | + _enter("R=%x %llx-%llx f=%lx", |
| 323 | + wreq->debug_id, wreq->start, wreq->start + wreq->len - 1, |
| 324 | + wreq->flags); |
| 325 | + |
| 326 | + trace_netfs_write(wreq, what); |
| 327 | + if (wreq->len == 0 || wreq->iter.count == 0) { |
| 328 | + pr_err("Zero-sized write [R=%x]\n", wreq->debug_id); |
| 329 | + return -EIO; |
| 330 | + } |
| 331 | + |
| 332 | + wreq->io_iter = wreq->iter; |
| 333 | + |
| 334 | + /* ->outstanding > 0 carries a ref */ |
| 335 | + netfs_get_request(wreq, netfs_rreq_trace_get_for_outstanding); |
| 336 | + atomic_set(&wreq->nr_outstanding, 1); |
| 337 | + |
| 338 | + /* Start the encryption/compression going. We can do that in the |
| 339 | + * background whilst we generate a list of write ops that we want to |
| 340 | + * perform. |
| 341 | + */ |
| 342 | + // TODO: Encrypt or compress the region as appropriate |
| 343 | + |
| 344 | + /* We need to write all of the region to the cache */ |
| 345 | + if (test_bit(NETFS_RREQ_WRITE_TO_CACHE, &wreq->flags)) |
| 346 | + netfs_set_up_write_to_cache(wreq); |
| 347 | + |
| 348 | + /* However, we don't necessarily write all of the region to the server. |
| 349 | + * Caching of reads is being managed this way also. |
| 350 | + */ |
| 351 | + if (test_bit(NETFS_RREQ_UPLOAD_TO_SERVER, &wreq->flags)) |
| 352 | + ctx->ops->create_write_requests(wreq, wreq->start, wreq->len); |
| 353 | + |
| 354 | + if (atomic_dec_and_test(&wreq->nr_outstanding)) |
| 355 | + netfs_write_terminated(wreq, false); |
| 356 | + |
| 357 | + if (!may_wait) |
| 358 | + return -EIOCBQUEUED; |
| 359 | + |
| 360 | + wait_on_bit(&wreq->flags, NETFS_RREQ_IN_PROGRESS, |
| 361 | + TASK_UNINTERRUPTIBLE); |
| 362 | + return wreq->error; |
| 363 | +} |
0 commit comments