at commit c03c1a14
| 1 | // SPDX-License-Identifier: GPL-2.0-or-later |
| 2 | /* Network filesystem high-level buffered read support. |
| 3 | * |
| 4 | * Copyright (C) 2021 Red Hat, Inc. All Rights Reserved. |
| 5 | * Written by David Howells (dhowells@redhat.com) |
| 6 | */ |
| 7 | |
| 8 | #include <linux/export.h> |
| 9 | #include <linux/task_io_accounting_ops.h> |
| 10 | #include "internal.h" |
| 11 | |
| 12 | static void netfs_cache_expand_readahead(struct netfs_io_request *rreq, |
| 13 | unsigned long long *_start, |
| 14 | unsigned long long *_len, |
| 15 | unsigned long long i_size) |
| 16 | { |
| 17 | struct netfs_cache_resources *cres = &rreq->cache_resources; |
| 18 | |
| 19 | if (cres->ops && cres->ops->expand_readahead) |
| 20 | cres->ops->expand_readahead(cres, _start, _len, i_size); |
| 21 | } |
| 22 | |
| 23 | static void netfs_rreq_expand(struct netfs_io_request *rreq, |
| 24 | struct readahead_control *ractl) |
| 25 | { |
| 26 | /* Give the cache a chance to change the request parameters. The |
| 27 | * resultant request must contain the original region. |
| 28 | */ |
| 29 | netfs_cache_expand_readahead(rreq, &rreq->start, &rreq->len, rreq->i_size); |
| 30 | |
| 31 | /* Give the netfs a chance to change the request parameters. The |
| 32 | * resultant request must contain the original region. |
| 33 | */ |
| 34 | if (rreq->netfs_ops->expand_readahead) |
| 35 | rreq->netfs_ops->expand_readahead(rreq); |
| 36 | |
| 37 | /* Expand the request if the cache wants it to start earlier. Note |
| 38 | * that the expansion may get further extended if the VM wishes to |
| 39 | * insert THPs and the preferred start and/or end wind up in the middle |
| 40 | * of THPs. |
| 41 | * |
| 42 | * If this is the case, however, the THP size should be an integer |
| 43 | * multiple of the cache granule size, so we get a whole number of |
| 44 | * granules to deal with. |
| 45 | */ |
| 46 | if (rreq->start != readahead_pos(ractl) || |
| 47 | rreq->len != readahead_length(ractl)) { |
| 48 | readahead_expand(ractl, rreq->start, rreq->len); |
| 49 | rreq->start = readahead_pos(ractl); |
| 50 | rreq->len = readahead_length(ractl); |
| 51 | |
| 52 | trace_netfs_read(rreq, readahead_pos(ractl), readahead_length(ractl), |
| 53 | netfs_read_trace_expanded); |
| 54 | } |
| 55 | } |
| 56 | |
| 57 | /* |
| 58 | * Drop the folio refs acquired from the readahead API. |
| 59 | */ |
| 60 | static void netfs_bulk_drop_ra_refs(struct netfs_io_request *rreq) |
| 61 | { |
| 62 | struct folio_batch fbatch; |
| 63 | struct folio *folio; |
| 64 | pgoff_t nr_pages = DIV_ROUND_UP(rreq->len, PAGE_SIZE); |
| 65 | pgoff_t first = rreq->start / PAGE_SIZE; |
| 66 | XA_STATE(xas, &rreq->mapping->i_pages, first); |
| 67 | |
| 68 | folio_batch_init(&fbatch); |
| 69 | |
| 70 | rcu_read_lock(); |
| 71 | |
| 72 | xas_for_each(&xas, folio, first + nr_pages - 1) { |
| 73 | if (xas_retry(&xas, folio)) |
| 74 | continue; |
| 75 | |
| 76 | if (!folio_batch_add(&fbatch, folio)) |
| 77 | folio_batch_release(&fbatch); |
| 78 | } |
| 79 | |
| 80 | rcu_read_unlock(); |
| 81 | folio_batch_release(&fbatch); |
| 82 | trace_netfs_rreq(rreq, netfs_rreq_trace_ra_put_ref); |
| 83 | clear_bit_unlock(NETFS_RREQ_NEED_PUT_RA_REFS, &rreq->flags); |
| 84 | wake_up(&rreq->waitq); |
| 85 | } |
| 86 | |
| 87 | static void netfs_maybe_bulk_drop_ra_refs(struct netfs_io_request *rreq) |
| 88 | { |
| 89 | if (test_bit(NETFS_RREQ_NEED_PUT_RA_REFS, &rreq->flags)) |
| 90 | netfs_bulk_drop_ra_refs(rreq); |
| 91 | } |
| 92 | |
| 93 | /* |
| 94 | * Begin an operation, and fetch the stored zero point value from the cookie if |
| 95 | * available. |
| 96 | */ |
| 97 | static int netfs_begin_cache_read(struct netfs_io_request *rreq, struct netfs_inode *ctx) |
| 98 | { |
| 99 | return fscache_begin_read_operation(&rreq->cache_resources, netfs_i_cookie(ctx)); |
| 100 | } |
| 101 | |
| 102 | /* |
| 103 | * netfs_prepare_read_iterator - Prepare the subreq iterator for I/O |
| 104 | * @subreq: The subrequest to be set up |
| 105 | * |
| 106 | * Prepare the I/O iterator representing the read buffer on a subrequest for |
| 107 | * the filesystem to use for I/O (it can be passed directly to a socket). This |
| 108 | * is intended to be called from the ->issue_read() method once the filesystem |
| 109 | * has trimmed the request to the size it wants. |
| 110 | * |
| 111 | * Returns the limited size if successful and -ENOMEM if insufficient memory |
| 112 | * available. |
| 113 | */ |
| 114 | static ssize_t netfs_prepare_read_iterator(struct netfs_io_subrequest *subreq) |
| 115 | { |
| 116 | struct netfs_io_request *rreq = subreq->rreq; |
| 117 | size_t rsize = subreq->len; |
| 118 | |
| 119 | if (subreq->source == NETFS_DOWNLOAD_FROM_SERVER) |
| 120 | rsize = umin(rsize, rreq->io_streams[0].sreq_max_len); |
| 121 | |
| 122 | subreq->len = rsize; |
| 123 | if (unlikely(rreq->io_streams[0].sreq_max_segs)) { |
| 124 | size_t limit = netfs_limit_iter(&rreq->buffer.iter, 0, rsize, |
| 125 | rreq->io_streams[0].sreq_max_segs); |
| 126 | |
| 127 | if (limit < rsize) { |
| 128 | subreq->len = limit; |
| 129 | trace_netfs_sreq(subreq, netfs_sreq_trace_limited); |
| 130 | } |
| 131 | } |
| 132 | |
| 133 | subreq->io_iter = rreq->buffer.iter; |
| 134 | |
| 135 | iov_iter_truncate(&subreq->io_iter, subreq->len); |
| 136 | rolling_buffer_advance(&rreq->buffer, subreq->len); |
| 137 | return subreq->len; |
| 138 | } |
| 139 | |
| 140 | static enum netfs_io_source netfs_cache_prepare_read(struct netfs_io_request *rreq, |
| 141 | struct netfs_io_subrequest *subreq, |
| 142 | loff_t i_size) |
| 143 | { |
| 144 | struct netfs_cache_resources *cres = &rreq->cache_resources; |
| 145 | enum netfs_io_source source; |
| 146 | |
| 147 | if (!cres->ops) |
| 148 | return NETFS_DOWNLOAD_FROM_SERVER; |
| 149 | source = cres->ops->prepare_read(subreq, i_size); |
| 150 | trace_netfs_sreq(subreq, netfs_sreq_trace_prepare); |
| 151 | return source; |
| 152 | |
| 153 | } |
| 154 | |
| 155 | /* |
| 156 | * Issue a read against the cache. |
| 157 | * - Eats the caller's ref on subreq. |
| 158 | */ |
| 159 | static void netfs_read_cache_to_pagecache(struct netfs_io_request *rreq, |
| 160 | struct netfs_io_subrequest *subreq) |
| 161 | { |
| 162 | struct netfs_cache_resources *cres = &rreq->cache_resources; |
| 163 | |
| 164 | netfs_stat(&netfs_n_rh_read); |
| 165 | cres->ops->read(cres, subreq->start, &subreq->io_iter, NETFS_READ_HOLE_IGNORE, |
| 166 | netfs_cache_read_terminated, subreq); |
| 167 | } |
| 168 | |
| 169 | void netfs_queue_read(struct netfs_io_request *rreq, |
| 170 | struct netfs_io_subrequest *subreq) |
| 171 | { |
| 172 | struct netfs_io_stream *stream = &rreq->io_streams[0]; |
| 173 | |
| 174 | __set_bit(NETFS_SREQ_IN_PROGRESS, &subreq->flags); |
| 175 | |
| 176 | /* We add to the end of the list whilst the collector may be walking |
| 177 | * the list. The collector only goes nextwards and uses the lock to |
| 178 | * remove entries off of the front. |
| 179 | */ |
| 180 | spin_lock(&rreq->lock); |
| 181 | /* Write IN_PROGRESS before pointer to new subreq */ |
| 182 | list_add_tail_release(&subreq->rreq_link, &stream->subrequests); |
| 183 | if (list_is_first(&subreq->rreq_link, &stream->subrequests)) { |
| 184 | if (!stream->active) { |
| 185 | stream->collected_to = subreq->start; |
| 186 | /* Store list pointers before active flag */ |
| 187 | smp_store_release(&stream->active, true); |
| 188 | } |
| 189 | } |
| 190 | |
| 191 | spin_unlock(&rreq->lock); |
| 192 | } |
| 193 | |
| 194 | static void netfs_issue_read(struct netfs_io_request *rreq, |
| 195 | struct netfs_io_subrequest *subreq) |
| 196 | { |
| 197 | switch (subreq->source) { |
| 198 | case NETFS_DOWNLOAD_FROM_SERVER: |
| 199 | rreq->netfs_ops->issue_read(subreq); |
| 200 | break; |
| 201 | case NETFS_READ_FROM_CACHE: |
| 202 | netfs_read_cache_to_pagecache(rreq, subreq); |
| 203 | break; |
| 204 | default: |
| 205 | __set_bit(NETFS_SREQ_CLEAR_TAIL, &subreq->flags); |
| 206 | subreq->error = 0; |
| 207 | iov_iter_zero(subreq->len, &subreq->io_iter); |
| 208 | subreq->transferred = subreq->len; |
| 209 | netfs_read_subreq_terminated(subreq); |
| 210 | break; |
| 211 | } |
| 212 | } |
| 213 | |
| 214 | /* |
| 215 | * Mark folios that we want to copy to the cache. For filesystems that use |
| 216 | * netfslib fully, we set folio->private to NETFS_FOLIO_COPY_TO_CACHE; |
| 217 | * otherwise we set the deprecated PG_private_2. |
| 218 | */ |
| 219 | static void netfs_mark_copy_to_cache(struct netfs_io_request *rreq, |
| 220 | struct folio_queue **fq, |
| 221 | unsigned int *offset, |
| 222 | int *slot, |
| 223 | size_t len, |
| 224 | bool copy) |
| 225 | { |
| 226 | while (len > 0) { |
| 227 | struct folio *folio; |
| 228 | size_t fsize, overlap; |
| 229 | |
| 230 | if (!*fq) |
| 231 | break; |
| 232 | if (*slot >= folioq_count(*fq)) { |
| 233 | *fq = (*fq)->next; |
| 234 | *slot = 0; |
| 235 | *offset = 0; |
| 236 | continue; |
| 237 | } |
| 238 | |
| 239 | /* Determine how much the subreq overlaps the folio, if at all. */ |
| 240 | fsize = folioq_folio_size(*fq, *slot); |
| 241 | overlap = min(len, fsize - *offset); |
| 242 | |
| 243 | if (overlap > 0 && copy) { |
| 244 | folio = folioq_folio(*fq, *slot); |
| 245 | if (unlikely(test_bit(NETFS_RREQ_USE_PGPRIV2, &rreq->flags))) { |
| 246 | if (!folio_test_private_2(folio)) |
| 247 | folio_start_private_2(folio); |
| 248 | } else { |
| 249 | if (!folio_get_private(folio)) |
| 250 | folio_attach_private(folio, NETFS_FOLIO_COPY_TO_CACHE); |
| 251 | } |
| 252 | trace_netfs_folio(folio, netfs_folio_trace_mark_copy); |
| 253 | } |
| 254 | |
| 255 | len -= overlap; |
| 256 | *offset += overlap; |
| 257 | if (*offset >= fsize) { |
| 258 | *slot += 1; |
| 259 | *offset = 0; |
| 260 | } |
| 261 | } |
| 262 | } |
| 263 | |
| 264 | /* |
| 265 | * Perform a read to the pagecache from a series of sources of different types, |
| 266 | * slicing up the region to be read according to available cache blocks and |
| 267 | * network rsize. |
| 268 | */ |
| 269 | static void netfs_read_to_pagecache(struct netfs_io_request *rreq) |
| 270 | { |
| 271 | struct folio_queue *fq = rreq->buffer.tail; |
| 272 | unsigned long long start = rreq->start; |
| 273 | unsigned int offset = 0; |
| 274 | ssize_t size = rreq->len; |
| 275 | int ret = 0, slot = 0; |
| 276 | |
| 277 | do { |
| 278 | struct netfs_io_subrequest *subreq; |
| 279 | enum netfs_io_source source = NETFS_SOURCE_UNKNOWN; |
| 280 | ssize_t slice; |
| 281 | |
| 282 | subreq = netfs_alloc_subrequest(rreq); |
| 283 | if (!subreq) { |
| 284 | ret = -ENOMEM; |
| 285 | break; |
| 286 | } |
| 287 | |
| 288 | subreq->start = start; |
| 289 | subreq->len = size; |
| 290 | |
| 291 | netfs_queue_read(rreq, subreq); |
| 292 | |
| 293 | source = netfs_cache_prepare_read(rreq, subreq, rreq->i_size); |
| 294 | subreq->source = source; |
| 295 | if (source == NETFS_DOWNLOAD_FROM_SERVER) { |
| 296 | unsigned long long zero_point = netfs_read_zero_point(rreq->inode); |
| 297 | unsigned long long zp = umin(zero_point, rreq->i_size); |
| 298 | size_t len = subreq->len; |
| 299 | |
| 300 | if (unlikely(rreq->origin == NETFS_READ_SINGLE)) |
| 301 | zp = rreq->i_size; |
| 302 | if (subreq->start >= zp) { |
| 303 | subreq->source = source = NETFS_FILL_WITH_ZEROES; |
| 304 | goto fill_with_zeroes; |
| 305 | } |
| 306 | |
| 307 | if (len > zp - subreq->start) |
| 308 | len = zp - subreq->start; |
| 309 | if (len == 0) { |
| 310 | pr_err("ZERO-LEN READ: R=%08x[%x] l=%zx/%zx s=%llx z=%llx i=%llx", |
| 311 | rreq->debug_id, subreq->debug_index, |
| 312 | subreq->len, size, |
| 313 | subreq->start, zero_point, rreq->i_size); |
| 314 | netfs_cancel_read(subreq, ret); |
| 315 | break; |
| 316 | } |
| 317 | subreq->len = len; |
| 318 | |
| 319 | netfs_stat(&netfs_n_rh_download); |
| 320 | if (rreq->netfs_ops->prepare_read) { |
| 321 | ret = rreq->netfs_ops->prepare_read(subreq); |
| 322 | if (ret < 0) { |
| 323 | netfs_cancel_read(subreq, ret); |
| 324 | break; |
| 325 | } |
| 326 | trace_netfs_sreq(subreq, netfs_sreq_trace_prepare); |
| 327 | } |
| 328 | goto issue; |
| 329 | } |
| 330 | |
| 331 | fill_with_zeroes: |
| 332 | if (source == NETFS_FILL_WITH_ZEROES) { |
| 333 | subreq->source = NETFS_FILL_WITH_ZEROES; |
| 334 | trace_netfs_sreq(subreq, netfs_sreq_trace_submit); |
| 335 | netfs_stat(&netfs_n_rh_zero); |
| 336 | goto issue; |
| 337 | } |
| 338 | |
| 339 | if (source == NETFS_READ_FROM_CACHE) { |
| 340 | trace_netfs_sreq(subreq, netfs_sreq_trace_submit); |
| 341 | goto issue; |
| 342 | } |
| 343 | |
| 344 | pr_err("Unexpected read source %u\n", source); |
| 345 | WARN_ON_ONCE(1); |
| 346 | netfs_cancel_read(subreq, ret); |
| 347 | break; |
| 348 | |
| 349 | issue: |
| 350 | slice = netfs_prepare_read_iterator(subreq); |
| 351 | if (slice < 0) { |
| 352 | ret = slice; |
| 353 | netfs_cancel_read(subreq, ret); |
| 354 | break; |
| 355 | } |
| 356 | start += slice; |
| 357 | size -= slice; |
| 358 | if (size <= 0) { |
| 359 | smp_wmb(); /* Write lists before ALL_QUEUED. */ |
| 360 | set_bit(NETFS_RREQ_ALL_QUEUED, &rreq->flags); |
| 361 | } |
| 362 | |
| 363 | if (fq) { |
| 364 | /* See if the cache indicated this should be cached. */ |
| 365 | bool copy = test_bit(NETFS_SREQ_COPY_TO_CACHE, &subreq->flags); |
| 366 | |
| 367 | netfs_mark_copy_to_cache(rreq, &fq, &slot, &offset, slice, copy); |
| 368 | } |
| 369 | |
| 370 | netfs_issue_read(rreq, subreq); |
| 371 | netfs_maybe_bulk_drop_ra_refs(rreq); |
| 372 | |
| 373 | if (test_bit(NETFS_RREQ_PAUSE, &rreq->flags)) |
| 374 | netfs_wait_for_paused_read(rreq); |
| 375 | if (test_bit(NETFS_RREQ_FAILED, &rreq->flags)) |
| 376 | break; |
| 377 | cond_resched(); |
| 378 | } while (size > 0); |
| 379 | |
| 380 | if (unlikely(size > 0)) { |
| 381 | smp_wmb(); /* Write lists before ALL_QUEUED. */ |
| 382 | set_bit(NETFS_RREQ_ALL_QUEUED, &rreq->flags); |
| 383 | netfs_wake_collector(rreq); |
| 384 | } |
| 385 | |
| 386 | /* Defer error return as we may need to wait for outstanding I/O. */ |
| 387 | cmpxchg(&rreq->error, 0, ret); |
| 388 | } |
| 389 | |
| 390 | /** |
| 391 | * netfs_readahead - Helper to manage a read request |
| 392 | * @ractl: The description of the readahead request |
| 393 | * |
| 394 | * Fulfil a readahead request by drawing data from the cache if possible, or |
| 395 | * the netfs if not. Space beyond the EOF is zero-filled. Multiple I/O |
| 396 | * requests from different sources will get munged together. If necessary, the |
| 397 | * readahead window can be expanded in either direction to a more convenient |
| 398 | * alighment for RPC efficiency or to make storage in the cache feasible. |
| 399 | * |
| 400 | * The calling netfs must initialise a netfs context contiguous to the vfs |
| 401 | * inode before calling this. |
| 402 | * |
| 403 | * This is usable whether or not caching is enabled. |
| 404 | */ |
| 405 | void netfs_readahead(struct readahead_control *ractl) |
| 406 | { |
| 407 | struct netfs_io_request *rreq; |
| 408 | struct netfs_inode *ictx = netfs_inode(ractl->mapping->host); |
| 409 | ssize_t added; |
| 410 | uoff_t start = readahead_pos(ractl); |
| 411 | size_t size = readahead_length(ractl); |
| 412 | int ret; |
| 413 | |
| 414 | rreq = netfs_alloc_request(ractl->mapping, ractl->file, start, size, |
| 415 | NETFS_READAHEAD); |
| 416 | if (IS_ERR(rreq)) |
| 417 | return; |
| 418 | |
| 419 | __set_bit(NETFS_RREQ_OFFLOAD_COLLECTION, &rreq->flags); |
| 420 | |
| 421 | ret = netfs_begin_cache_read(rreq, ictx); |
| 422 | if (ret == -ENOMEM || ret == -EINTR || ret == -ERESTARTSYS) |
| 423 | goto cleanup_free; |
| 424 | |
| 425 | netfs_stat(&netfs_n_rh_readahead); |
| 426 | trace_netfs_read(rreq, readahead_pos(ractl), readahead_length(ractl), |
| 427 | netfs_read_trace_readahead); |
| 428 | |
| 429 | netfs_rreq_expand(rreq, ractl); |
| 430 | |
| 431 | /* Load the folios to be read into a bvecq chain. Note that this |
| 432 | * acquires a ref on each folio that we will need to release later - |
| 433 | * but we don't want to do that until after we've started the I/O. |
| 434 | */ |
| 435 | added = rolling_buffer_bulk_load_from_ra(&rreq->buffer, ractl, |
| 436 | rreq->debug_id, rreq->gfp); |
| 437 | if (added < 0) { |
| 438 | ret = added; |
| 439 | goto cleanup_free; |
| 440 | } |
| 441 | __set_bit(NETFS_RREQ_NEED_PUT_RA_REFS, &rreq->flags); |
| 442 | |
| 443 | rreq->submitted = rreq->start + added; |
| 444 | rreq->cleaned_to = rreq->start; |
| 445 | netfs_read_set_unlock_at(rreq); |
| 446 | |
| 447 | netfs_read_to_pagecache(rreq); |
| 448 | netfs_maybe_bulk_drop_ra_refs(rreq); |
| 449 | return netfs_put_request(rreq, netfs_rreq_trace_put_return); |
| 450 | |
| 451 | cleanup_free: |
| 452 | return netfs_put_failed_request(rreq); |
| 453 | } |
| 454 | EXPORT_SYMBOL(netfs_readahead); |
| 455 | |
| 456 | /* |
| 457 | * Create a rolling buffer with a single occupying folio. |
| 458 | */ |
| 459 | static int netfs_create_singular_buffer(struct netfs_io_request *rreq, struct folio *folio, |
| 460 | unsigned int rollbuf_flags) |
| 461 | { |
| 462 | ssize_t added; |
| 463 | |
| 464 | if (rolling_buffer_init(&rreq->buffer, rreq->debug_id, ITER_DEST, rreq->gfp) < 0) |
| 465 | return -ENOMEM; |
| 466 | |
| 467 | added = rolling_buffer_append(&rreq->buffer, folio, rollbuf_flags, rreq->gfp); |
| 468 | if (added < 0) |
| 469 | return added; |
| 470 | rreq->submitted = rreq->start + added; |
| 471 | rreq->progress_at = added; |
| 472 | return 0; |
| 473 | } |
| 474 | |
| 475 | /* |
| 476 | * Read into gaps in a folio partially filled by a streaming write. |
| 477 | */ |
| 478 | static int netfs_read_gaps(struct file *file, struct folio *folio) |
| 479 | { |
| 480 | struct netfs_io_request *rreq; |
| 481 | struct address_space *mapping = folio->mapping; |
| 482 | struct netfs_group *group = netfs_folio_group(folio); |
| 483 | struct netfs_folio *finfo = netfs_folio_info(folio); |
| 484 | struct netfs_inode *ctx = netfs_inode(mapping->host); |
| 485 | struct bio_vec *bvec = NULL; |
| 486 | unsigned int from = finfo->dirty_offset; |
| 487 | unsigned int to = from + finfo->dirty_len; |
| 488 | unsigned int off = 0; |
| 489 | size_t flen = folio_size(folio); |
| 490 | size_t nr_bvec = flen / PAGE_SIZE + 2; |
| 491 | size_t part; |
| 492 | int ret, i = 0, sink_from = -1, sink_to = -1; |
| 493 | |
| 494 | _enter("%lx", folio->index); |
| 495 | |
| 496 | rreq = netfs_alloc_request(mapping, file, folio_pos(folio), flen, NETFS_READ_GAPS); |
| 497 | if (IS_ERR(rreq)) { |
| 498 | ret = PTR_ERR(rreq); |
| 499 | goto alloc_error; |
| 500 | } |
| 501 | |
| 502 | ret = netfs_begin_cache_read(rreq, ctx); |
| 503 | if (ret == -ENOMEM || ret == -EINTR || ret == -ERESTARTSYS) |
| 504 | goto discard; |
| 505 | |
| 506 | netfs_stat(&netfs_n_rh_read_folio); |
| 507 | trace_netfs_read(rreq, rreq->start, rreq->len, netfs_read_trace_read_gaps); |
| 508 | |
| 509 | /* Fiddle the buffer so that a gap at the beginning and/or a gap at the |
| 510 | * end get copied to, but the middle is discarded. |
| 511 | */ |
| 512 | ret = -ENOMEM; |
| 513 | bvec = kmalloc_objs(*bvec, nr_bvec); |
| 514 | if (!bvec) |
| 515 | goto discard; |
| 516 | |
| 517 | trace_netfs_folio(folio, netfs_folio_trace_read_gaps); |
| 518 | |
| 519 | if (from > 0) { |
| 520 | bvec_set_folio(&bvec[i++], folio, from, 0); |
| 521 | off = from; |
| 522 | } |
| 523 | sink_from = i; |
| 524 | while (off < to) { |
| 525 | struct folio *sink = folio_alloc(GFP_KERNEL, 0); |
| 526 | |
| 527 | if (!sink) |
| 528 | goto discard; |
| 529 | part = min_t(size_t, to - off, PAGE_SIZE); |
| 530 | bvec_set_folio(&bvec[i], sink, part, 0); |
| 531 | off += part; |
| 532 | sink_to = i; |
| 533 | i++; |
| 534 | } |
| 535 | if (to < flen) |
| 536 | bvec_set_folio(&bvec[i++], folio, flen - to, to); |
| 537 | iov_iter_bvec(&rreq->buffer.iter, ITER_DEST, bvec, i, rreq->len); |
| 538 | rreq->submitted = rreq->start + flen; |
| 539 | |
| 540 | netfs_read_to_pagecache(rreq); |
| 541 | |
| 542 | ret = netfs_wait_for_read(rreq); |
| 543 | if (ret >= 0) { |
| 544 | if (ret < flen) { |
| 545 | struct iov_iter iter; |
| 546 | |
| 547 | iov_iter_bvec(&iter, ITER_DEST, bvec, i, flen); |
| 548 | iov_iter_advance(&iter, ret); |
| 549 | iov_iter_zero(flen - ret, &iter); |
| 550 | } |
| 551 | if (group) |
| 552 | folio_change_private(folio, group); |
| 553 | else |
| 554 | folio_detach_private(folio); |
| 555 | kfree(finfo); |
| 556 | trace_netfs_folio(folio, netfs_folio_trace_filled_gaps); |
| 557 | flush_dcache_folio(folio); |
| 558 | folio_mark_uptodate(folio); |
| 559 | } |
| 560 | |
| 561 | if (sink_to >= 0) |
| 562 | for (; sink_from <= sink_to; sink_from++) |
| 563 | folio_put(bvec_folio(&bvec[sink_from])); |
| 564 | kfree(bvec); |
| 565 | folio_unlock(folio); |
| 566 | netfs_put_request(rreq, netfs_rreq_trace_put_return); |
| 567 | return ret < 0 ? ret : 0; |
| 568 | |
| 569 | discard: |
| 570 | netfs_put_failed_request(rreq); |
| 571 | alloc_error: |
| 572 | folio_unlock(folio); |
| 573 | if (sink_to >= 0) |
| 574 | for (; sink_from <= sink_to; sink_from++) |
| 575 | folio_put(bvec_folio(&bvec[sink_from])); |
| 576 | kfree(bvec); |
| 577 | return ret; |
| 578 | } |
| 579 | |
| 580 | /** |
| 581 | * netfs_read_folio - Helper to manage a read_folio request |
| 582 | * @file: The file to read from |
| 583 | * @folio: The folio to read |
| 584 | * |
| 585 | * Fulfil a read_folio request by drawing data from the cache if |
| 586 | * possible, or the netfs if not. Space beyond the EOF is zero-filled. |
| 587 | * Multiple I/O requests from different sources will get munged together. |
| 588 | * |
| 589 | * The calling netfs must initialise a netfs context contiguous to the vfs |
| 590 | * inode before calling this. |
| 591 | * |
| 592 | * This is usable whether or not caching is enabled. |
| 593 | */ |
| 594 | int netfs_read_folio(struct file *file, struct folio *folio) |
| 595 | { |
| 596 | struct address_space *mapping = folio->mapping; |
| 597 | struct netfs_io_request *rreq; |
| 598 | struct netfs_inode *ctx = netfs_inode(mapping->host); |
| 599 | int ret; |
| 600 | |
| 601 | folio_wait_writeback(folio); |
| 602 | |
| 603 | if (folio_test_dirty(folio)) |
| 604 | return netfs_read_gaps(file, folio); |
| 605 | |
| 606 | _enter("%lx", folio->index); |
| 607 | |
| 608 | rreq = netfs_alloc_request(mapping, file, |
| 609 | folio_pos(folio), folio_size(folio), |
| 610 | NETFS_READPAGE); |
| 611 | if (IS_ERR(rreq)) { |
| 612 | ret = PTR_ERR(rreq); |
| 613 | goto alloc_error; |
| 614 | } |
| 615 | |
| 616 | ret = netfs_begin_cache_read(rreq, ctx); |
| 617 | if (ret == -ENOMEM || ret == -EINTR || ret == -ERESTARTSYS) |
| 618 | goto discard; |
| 619 | |
| 620 | netfs_stat(&netfs_n_rh_read_folio); |
| 621 | trace_netfs_read(rreq, rreq->start, rreq->len, netfs_read_trace_readpage); |
| 622 | |
| 623 | /* Set up the output buffer */ |
| 624 | ret = netfs_create_singular_buffer(rreq, folio, 0); |
| 625 | if (ret < 0) |
| 626 | goto discard; |
| 627 | |
| 628 | netfs_read_to_pagecache(rreq); |
| 629 | ret = netfs_wait_for_read(rreq); |
| 630 | netfs_put_request(rreq, netfs_rreq_trace_put_return); |
| 631 | return ret < 0 ? ret : 0; |
| 632 | |
| 633 | discard: |
| 634 | netfs_put_failed_request(rreq); |
| 635 | alloc_error: |
| 636 | folio_unlock(folio); |
| 637 | return ret; |
| 638 | } |
| 639 | EXPORT_SYMBOL(netfs_read_folio); |
| 640 | |
| 641 | /* |
| 642 | * Prepare a folio for writing without reading first |
| 643 | * @folio: The folio being prepared |
| 644 | * @pos: starting position for the write |
| 645 | * @len: length of write |
| 646 | * @always_fill: T if the folio should always be completely filled/cleared |
| 647 | * |
| 648 | * In some cases, write_begin doesn't need to read at all: |
| 649 | * - full folio write |
| 650 | * - write that lies in a folio that is completely beyond EOF |
| 651 | * - write that covers the folio from start to EOF or beyond it |
| 652 | * |
| 653 | * If any of these criteria are met, then zero out the unwritten parts |
| 654 | * of the folio and return true. Otherwise, return false. |
| 655 | */ |
| 656 | static bool netfs_skip_folio_read(struct folio *folio, loff_t pos, size_t len, |
| 657 | bool always_fill) |
| 658 | { |
| 659 | struct inode *inode = folio_inode(folio); |
| 660 | loff_t i_size = i_size_read(inode); |
| 661 | size_t offset = offset_in_folio(folio, pos); |
| 662 | size_t plen = folio_size(folio); |
| 663 | |
| 664 | if (unlikely(always_fill)) { |
| 665 | if (pos - offset + len <= i_size) |
| 666 | return false; /* Page entirely before EOF */ |
| 667 | folio_zero_segment(folio, 0, plen); |
| 668 | folio_mark_uptodate(folio); |
| 669 | return true; |
| 670 | } |
| 671 | |
| 672 | /* Full folio write */ |
| 673 | if (offset == 0 && len >= plen) |
| 674 | return true; |
| 675 | |
| 676 | /* Page entirely beyond the end of the file */ |
| 677 | if (pos - offset >= i_size) |
| 678 | goto zero_out; |
| 679 | |
| 680 | /* Write that covers from the start of the folio to EOF or beyond */ |
| 681 | if (offset == 0 && (pos + len) >= i_size) |
| 682 | goto zero_out; |
| 683 | |
| 684 | return false; |
| 685 | zero_out: |
| 686 | folio_zero_segments(folio, 0, offset, offset + len, plen); |
| 687 | return true; |
| 688 | } |
| 689 | |
| 690 | /** |
| 691 | * netfs_write_begin - Helper to prepare for writing [DEPRECATED] |
| 692 | * @ctx: The netfs context |
| 693 | * @file: The file to read from |
| 694 | * @mapping: The mapping to read from |
| 695 | * @pos: File position at which the write will begin |
| 696 | * @len: The length of the write (may extend beyond the end of the folio chosen) |
| 697 | * @_folio: Where to put the resultant folio |
| 698 | * @_fsdata: Place for the netfs to store a cookie |
| 699 | * |
| 700 | * Pre-read data for a write-begin request by drawing data from the cache if |
| 701 | * possible, or the netfs if not. Space beyond the EOF is zero-filled. |
| 702 | * Multiple I/O requests from different sources will get munged together. |
| 703 | * |
| 704 | * The calling netfs must provide a table of operations, only one of which, |
| 705 | * issue_read, is mandatory. |
| 706 | * |
| 707 | * The check_write_begin() operation can be provided to check for and flush |
| 708 | * conflicting writes once the folio is grabbed and locked. It is passed a |
| 709 | * pointer to the fsdata cookie that gets returned to the VM to be passed to |
| 710 | * write_end. It is permitted to sleep. It should return 0 if the request |
| 711 | * should go ahead or it may return an error. It may also unlock and put the |
| 712 | * folio, provided it sets ``*foliop`` to NULL, in which case a return of 0 |
| 713 | * will cause the folio to be re-got and the process to be retried. |
| 714 | * |
| 715 | * The calling netfs must initialise a netfs context contiguous to the vfs |
| 716 | * inode before calling this. |
| 717 | * |
| 718 | * This is usable whether or not caching is enabled. |
| 719 | * |
| 720 | * Note that this should be considered deprecated and netfs_perform_write() |
| 721 | * used instead. |
| 722 | */ |
| 723 | int netfs_write_begin(struct netfs_inode *ctx, |
| 724 | struct file *file, struct address_space *mapping, |
| 725 | loff_t pos, unsigned int len, struct folio **_folio, |
| 726 | void **_fsdata) |
| 727 | { |
| 728 | struct netfs_io_request *rreq; |
| 729 | struct folio *folio; |
| 730 | pgoff_t index = pos >> PAGE_SHIFT; |
| 731 | int ret; |
| 732 | |
| 733 | retry: |
| 734 | folio = __filemap_get_folio(mapping, index, FGP_WRITEBEGIN, |
| 735 | mapping_gfp_mask(mapping)); |
| 736 | if (IS_ERR(folio)) |
| 737 | return PTR_ERR(folio); |
| 738 | |
| 739 | if (ctx->ops->check_write_begin) { |
| 740 | /* Allow the netfs (eg. ceph) to flush conflicts. */ |
| 741 | ret = ctx->ops->check_write_begin(file, pos, len, &folio, _fsdata); |
| 742 | if (ret < 0) { |
| 743 | trace_netfs_failure(NULL, NULL, ret, netfs_fail_check_write_begin); |
| 744 | goto error; |
| 745 | } |
| 746 | if (!folio) |
| 747 | goto retry; |
| 748 | } |
| 749 | |
| 750 | if (folio_test_uptodate(folio)) |
| 751 | goto have_folio; |
| 752 | |
| 753 | /* If the folio is beyond the EOF, we want to clear it - unless it's |
| 754 | * within the cache granule containing the EOF, in which case we need |
| 755 | * to preload the granule. |
| 756 | */ |
| 757 | if (!netfs_is_cache_maybe_enabled(ctx) && |
| 758 | netfs_skip_folio_read(folio, pos, len, false)) { |
| 759 | netfs_stat(&netfs_n_rh_write_zskip); |
| 760 | goto have_folio_no_wait; |
| 761 | } |
| 762 | |
| 763 | rreq = netfs_alloc_request(mapping, file, |
| 764 | folio_pos(folio), folio_size(folio), |
| 765 | NETFS_READ_FOR_WRITE); |
| 766 | if (IS_ERR(rreq)) { |
| 767 | ret = PTR_ERR(rreq); |
| 768 | goto error; |
| 769 | } |
| 770 | rreq->no_unlock_folio = folio; |
| 771 | __set_bit(NETFS_RREQ_NO_UNLOCK_FOLIO, &rreq->flags); |
| 772 | |
| 773 | ret = netfs_begin_cache_read(rreq, ctx); |
| 774 | if (ret == -ENOMEM || ret == -EINTR || ret == -ERESTARTSYS) |
| 775 | goto error_put; |
| 776 | |
| 777 | netfs_stat(&netfs_n_rh_write_begin); |
| 778 | trace_netfs_read(rreq, pos, len, netfs_read_trace_write_begin); |
| 779 | |
| 780 | /* Set up the output buffer */ |
| 781 | ret = netfs_create_singular_buffer(rreq, folio, 0); |
| 782 | if (ret < 0) |
| 783 | goto error_put; |
| 784 | |
| 785 | netfs_read_to_pagecache(rreq); |
| 786 | ret = netfs_wait_for_read(rreq); |
| 787 | netfs_put_request(rreq, netfs_rreq_trace_put_return); |
| 788 | if (ret < 0) |
| 789 | goto error; |
| 790 | |
| 791 | have_folio: |
| 792 | ret = folio_wait_private_2_killable(folio); |
| 793 | if (ret < 0) |
| 794 | goto error; |
| 795 | have_folio_no_wait: |
| 796 | *_folio = folio; |
| 797 | _leave(" = 0"); |
| 798 | return 0; |
| 799 | |
| 800 | error_put: |
| 801 | netfs_put_failed_request(rreq); |
| 802 | error: |
| 803 | if (folio) { |
| 804 | folio_unlock(folio); |
| 805 | folio_put(folio); |
| 806 | } |
| 807 | _leave(" = %d", ret); |
| 808 | return ret; |
| 809 | } |
| 810 | EXPORT_SYMBOL(netfs_write_begin); |
| 811 | |
| 812 | /* |
| 813 | * Preload the data into a folio we're proposing to write into. |
| 814 | */ |
| 815 | int netfs_prefetch_for_write(struct file *file, struct folio *folio, |
| 816 | size_t offset, size_t len) |
| 817 | { |
| 818 | struct netfs_io_request *rreq; |
| 819 | struct address_space *mapping = folio->mapping; |
| 820 | struct netfs_inode *ctx = netfs_inode(mapping->host); |
| 821 | unsigned long long start = folio_pos(folio); |
| 822 | size_t flen = folio_size(folio); |
| 823 | int ret; |
| 824 | |
| 825 | _enter("%zx @%llx", flen, start); |
| 826 | |
| 827 | ret = -ENOMEM; |
| 828 | |
| 829 | rreq = netfs_alloc_request(mapping, file, start, flen, |
| 830 | NETFS_READ_FOR_WRITE); |
| 831 | if (IS_ERR(rreq)) { |
| 832 | ret = PTR_ERR(rreq); |
| 833 | goto error; |
| 834 | } |
| 835 | |
| 836 | rreq->no_unlock_folio = folio; |
| 837 | __set_bit(NETFS_RREQ_NO_UNLOCK_FOLIO, &rreq->flags); |
| 838 | ret = netfs_begin_cache_read(rreq, ctx); |
| 839 | if (ret == -ENOMEM || ret == -EINTR || ret == -ERESTARTSYS) |
| 840 | goto error_put; |
| 841 | |
| 842 | netfs_stat(&netfs_n_rh_write_begin); |
| 843 | trace_netfs_read(rreq, start, flen, netfs_read_trace_prefetch_for_write); |
| 844 | |
| 845 | /* Set up the output buffer */ |
| 846 | ret = netfs_create_singular_buffer(rreq, folio, NETFS_ROLLBUF_PAGECACHE_MARK); |
| 847 | if (ret < 0) |
| 848 | goto error_put; |
| 849 | |
| 850 | netfs_read_to_pagecache(rreq); |
| 851 | ret = netfs_wait_for_read(rreq); |
| 852 | netfs_put_request(rreq, netfs_rreq_trace_put_return); |
| 853 | return ret < 0 ? ret : 0; |
| 854 | |
| 855 | error_put: |
| 856 | netfs_put_failed_request(rreq); |
| 857 | error: |
| 858 | _leave(" = %d", ret); |
| 859 | return ret; |
| 860 | } |
| 861 | |
| 862 | /** |
| 863 | * netfs_buffered_read_iter - Filesystem buffered I/O read routine |
| 864 | * @iocb: kernel I/O control block |
| 865 | * @iter: destination for the data read |
| 866 | * |
| 867 | * This is the ->read_iter() routine for all filesystems that can use the page |
| 868 | * cache directly. |
| 869 | * |
| 870 | * The IOCB_NOWAIT flag in iocb->ki_flags indicates that -EAGAIN shall be |
| 871 | * returned when no data can be read without waiting for I/O requests to |
| 872 | * complete; it doesn't prevent readahead. |
| 873 | * |
| 874 | * The IOCB_NOIO flag in iocb->ki_flags indicates that no new I/O requests |
| 875 | * shall be made for the read or for readahead. When no data can be read, |
| 876 | * -EAGAIN shall be returned. When readahead would be triggered, a partial, |
| 877 | * possibly empty read shall be returned. |
| 878 | * |
| 879 | * Return: |
| 880 | * * number of bytes copied, even for partial reads |
| 881 | * * negative error code (or 0 if IOCB_NOIO) if nothing was read |
| 882 | */ |
| 883 | ssize_t netfs_buffered_read_iter(struct kiocb *iocb, struct iov_iter *iter) |
| 884 | { |
| 885 | struct inode *inode = file_inode(iocb->ki_filp); |
| 886 | struct netfs_inode *ictx = netfs_inode(inode); |
| 887 | ssize_t ret; |
| 888 | |
| 889 | if (WARN_ON_ONCE((iocb->ki_flags & IOCB_DIRECT) || |
| 890 | test_bit(NETFS_ICTX_UNBUFFERED, &ictx->flags))) |
| 891 | return -EINVAL; |
| 892 | |
| 893 | ret = netfs_start_io_read(inode); |
| 894 | if (ret == 0) { |
| 895 | ret = filemap_read(iocb, iter, 0); |
| 896 | netfs_end_io_read(inode); |
| 897 | } |
| 898 | return ret; |
| 899 | } |
| 900 | EXPORT_SYMBOL(netfs_buffered_read_iter); |
| 901 | |
| 902 | /** |
| 903 | * netfs_file_read_iter - Generic filesystem read routine |
| 904 | * @iocb: kernel I/O control block |
| 905 | * @iter: destination for the data read |
| 906 | * |
| 907 | * This is the ->read_iter() routine for all filesystems that can use the page |
| 908 | * cache directly. |
| 909 | * |
| 910 | * The IOCB_NOWAIT flag in iocb->ki_flags indicates that -EAGAIN shall be |
| 911 | * returned when no data can be read without waiting for I/O requests to |
| 912 | * complete; it doesn't prevent readahead. |
| 913 | * |
| 914 | * The IOCB_NOIO flag in iocb->ki_flags indicates that no new I/O requests |
| 915 | * shall be made for the read or for readahead. When no data can be read, |
| 916 | * -EAGAIN shall be returned. When readahead would be triggered, a partial, |
| 917 | * possibly empty read shall be returned. |
| 918 | * |
| 919 | * Return: |
| 920 | * * number of bytes copied, even for partial reads |
| 921 | * * negative error code (or 0 if IOCB_NOIO) if nothing was read |
| 922 | */ |
| 923 | ssize_t netfs_file_read_iter(struct kiocb *iocb, struct iov_iter *iter) |
| 924 | { |
| 925 | struct netfs_inode *ictx = netfs_inode(iocb->ki_filp->f_mapping->host); |
| 926 | |
| 927 | if ((iocb->ki_flags & IOCB_DIRECT) || |
| 928 | test_bit(NETFS_ICTX_UNBUFFERED, &ictx->flags)) |
| 929 | return netfs_unbuffered_read_iter(iocb, iter); |
| 930 | |
| 931 | return netfs_buffered_read_iter(iocb, iter); |
| 932 | } |
| 933 | EXPORT_SYMBOL(netfs_file_read_iter); |