1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /* Network filesystem high-level read support.
4 * Copyright (C) 2021 Red Hat, Inc. All Rights Reserved.
5 * Written by David Howells (dhowells@redhat.com)
8 #include <linux/module.h>
9 #include <linux/export.h>
12 #include <linux/pagemap.h>
13 #include <linux/slab.h>
14 #include <linux/uio.h>
15 #include <linux/sched/mm.h>
16 #include <linux/task_io_accounting_ops.h>
17 #include <linux/netfs.h>
19 #define CREATE_TRACE_POINTS
20 #include <trace/events/netfs.h>
22 MODULE_DESCRIPTION("Network fs support");
23 MODULE_AUTHOR("Red Hat, Inc.");
24 MODULE_LICENSE("GPL");
27 module_param_named(debug, netfs_debug, uint, S_IWUSR | S_IRUGO);
28 MODULE_PARM_DESC(netfs_debug, "Netfs support debugging mask");
30 static void netfs_rreq_work(struct work_struct *);
31 static void __netfs_put_subrequest(struct netfs_io_subrequest *, bool);
33 static void netfs_put_subrequest(struct netfs_io_subrequest *subreq,
36 if (refcount_dec_and_test(&subreq->usage))
37 __netfs_put_subrequest(subreq, was_async);
40 static struct netfs_io_request *netfs_alloc_request(
41 const struct netfs_request_ops *ops, void *netfs_priv,
44 static atomic_t debug_ids;
45 struct netfs_io_request *rreq;
47 rreq = kzalloc(sizeof(struct netfs_io_request), GFP_KERNEL);
49 rreq->netfs_ops = ops;
50 rreq->netfs_priv = netfs_priv;
51 rreq->inode = file_inode(file);
52 rreq->i_size = i_size_read(rreq->inode);
53 rreq->debug_id = atomic_inc_return(&debug_ids);
54 INIT_LIST_HEAD(&rreq->subrequests);
55 INIT_WORK(&rreq->work, netfs_rreq_work);
56 refcount_set(&rreq->usage, 1);
57 __set_bit(NETFS_RREQ_IN_PROGRESS, &rreq->flags);
58 if (ops->init_request)
59 ops->init_request(rreq, file);
60 netfs_stat(&netfs_n_rh_rreq);
66 static void netfs_get_request(struct netfs_io_request *rreq)
68 refcount_inc(&rreq->usage);
71 static void netfs_clear_subrequests(struct netfs_io_request *rreq, bool was_async)
73 struct netfs_io_subrequest *subreq;
75 while (!list_empty(&rreq->subrequests)) {
76 subreq = list_first_entry(&rreq->subrequests,
77 struct netfs_io_subrequest, rreq_link);
78 list_del(&subreq->rreq_link);
79 netfs_put_subrequest(subreq, was_async);
83 static void netfs_free_request(struct work_struct *work)
85 struct netfs_io_request *rreq =
86 container_of(work, struct netfs_io_request, work);
87 netfs_clear_subrequests(rreq, false);
89 rreq->netfs_ops->cleanup(rreq->mapping, rreq->netfs_priv);
90 trace_netfs_rreq(rreq, netfs_rreq_trace_free);
91 if (rreq->cache_resources.ops)
92 rreq->cache_resources.ops->end_operation(&rreq->cache_resources);
94 netfs_stat_d(&netfs_n_rh_rreq);
97 static void netfs_put_request(struct netfs_io_request *rreq, bool was_async)
99 if (refcount_dec_and_test(&rreq->usage)) {
101 rreq->work.func = netfs_free_request;
102 if (!queue_work(system_unbound_wq, &rreq->work))
105 netfs_free_request(&rreq->work);
111 * Allocate and partially initialise an I/O request structure.
113 static struct netfs_io_subrequest *netfs_alloc_subrequest(
114 struct netfs_io_request *rreq)
116 struct netfs_io_subrequest *subreq;
118 subreq = kzalloc(sizeof(struct netfs_io_subrequest), GFP_KERNEL);
120 INIT_LIST_HEAD(&subreq->rreq_link);
121 refcount_set(&subreq->usage, 2);
123 netfs_get_request(rreq);
124 netfs_stat(&netfs_n_rh_sreq);
130 static void netfs_get_subrequest(struct netfs_io_subrequest *subreq)
132 refcount_inc(&subreq->usage);
135 static void __netfs_put_subrequest(struct netfs_io_subrequest *subreq,
138 struct netfs_io_request *rreq = subreq->rreq;
140 trace_netfs_sreq(subreq, netfs_sreq_trace_free);
142 netfs_stat_d(&netfs_n_rh_sreq);
143 netfs_put_request(rreq, was_async);
147 * Clear the unread part of an I/O request.
149 static void netfs_clear_unread(struct netfs_io_subrequest *subreq)
151 struct iov_iter iter;
153 iov_iter_xarray(&iter, READ, &subreq->rreq->mapping->i_pages,
154 subreq->start + subreq->transferred,
155 subreq->len - subreq->transferred);
156 iov_iter_zero(iov_iter_count(&iter), &iter);
159 static void netfs_cache_read_terminated(void *priv, ssize_t transferred_or_error,
162 struct netfs_io_subrequest *subreq = priv;
164 netfs_subreq_terminated(subreq, transferred_or_error, was_async);
168 * Issue a read against the cache.
169 * - Eats the caller's ref on subreq.
171 static void netfs_read_from_cache(struct netfs_io_request *rreq,
172 struct netfs_io_subrequest *subreq,
173 enum netfs_read_from_hole read_hole)
175 struct netfs_cache_resources *cres = &rreq->cache_resources;
176 struct iov_iter iter;
178 netfs_stat(&netfs_n_rh_read);
179 iov_iter_xarray(&iter, READ, &rreq->mapping->i_pages,
180 subreq->start + subreq->transferred,
181 subreq->len - subreq->transferred);
183 cres->ops->read(cres, subreq->start, &iter, read_hole,
184 netfs_cache_read_terminated, subreq);
188 * Fill a subrequest region with zeroes.
190 static void netfs_fill_with_zeroes(struct netfs_io_request *rreq,
191 struct netfs_io_subrequest *subreq)
193 netfs_stat(&netfs_n_rh_zero);
194 __set_bit(NETFS_SREQ_CLEAR_TAIL, &subreq->flags);
195 netfs_subreq_terminated(subreq, 0, false);
199 * Ask the netfs to issue a read request to the server for us.
201 * The netfs is expected to read from subreq->pos + subreq->transferred to
202 * subreq->pos + subreq->len - 1. It may not backtrack and write data into the
203 * buffer prior to the transferred point as it might clobber dirty data
204 * obtained from the cache.
206 * Alternatively, the netfs is allowed to indicate one of two things:
208 * - NETFS_SREQ_SHORT_READ: A short read - it will get called again to try and
211 * - NETFS_SREQ_CLEAR_TAIL: A short read - the rest of the buffer will be
214 static void netfs_read_from_server(struct netfs_io_request *rreq,
215 struct netfs_io_subrequest *subreq)
217 netfs_stat(&netfs_n_rh_download);
218 rreq->netfs_ops->issue_read(subreq);
222 * Release those waiting.
224 static void netfs_rreq_completed(struct netfs_io_request *rreq, bool was_async)
226 trace_netfs_rreq(rreq, netfs_rreq_trace_done);
227 netfs_clear_subrequests(rreq, was_async);
228 netfs_put_request(rreq, was_async);
232 * Deal with the completion of writing the data to the cache. We have to clear
233 * the PG_fscache bits on the folios involved and release the caller's ref.
235 * May be called in softirq mode and we inherit a ref from the caller.
237 static void netfs_rreq_unmark_after_write(struct netfs_io_request *rreq,
240 struct netfs_io_subrequest *subreq;
242 pgoff_t unlocked = 0;
243 bool have_unlocked = false;
247 list_for_each_entry(subreq, &rreq->subrequests, rreq_link) {
248 XA_STATE(xas, &rreq->mapping->i_pages, subreq->start / PAGE_SIZE);
250 xas_for_each(&xas, folio, (subreq->start + subreq->len - 1) / PAGE_SIZE) {
251 /* We might have multiple writes from the same huge
252 * folio, but we mustn't unlock a folio more than once.
254 if (have_unlocked && folio_index(folio) <= unlocked)
256 unlocked = folio_index(folio);
257 folio_end_fscache(folio);
258 have_unlocked = true;
263 netfs_rreq_completed(rreq, was_async);
266 static void netfs_rreq_copy_terminated(void *priv, ssize_t transferred_or_error,
269 struct netfs_io_subrequest *subreq = priv;
270 struct netfs_io_request *rreq = subreq->rreq;
272 if (IS_ERR_VALUE(transferred_or_error)) {
273 netfs_stat(&netfs_n_rh_write_failed);
274 trace_netfs_failure(rreq, subreq, transferred_or_error,
275 netfs_fail_copy_to_cache);
277 netfs_stat(&netfs_n_rh_write_done);
280 trace_netfs_sreq(subreq, netfs_sreq_trace_write_term);
282 /* If we decrement nr_copy_ops to 0, the ref belongs to us. */
283 if (atomic_dec_and_test(&rreq->nr_copy_ops))
284 netfs_rreq_unmark_after_write(rreq, was_async);
286 netfs_put_subrequest(subreq, was_async);
290 * Perform any outstanding writes to the cache. We inherit a ref from the
293 static void netfs_rreq_do_write_to_cache(struct netfs_io_request *rreq)
295 struct netfs_cache_resources *cres = &rreq->cache_resources;
296 struct netfs_io_subrequest *subreq, *next, *p;
297 struct iov_iter iter;
300 trace_netfs_rreq(rreq, netfs_rreq_trace_write);
302 /* We don't want terminating writes trying to wake us up whilst we're
303 * still going through the list.
305 atomic_inc(&rreq->nr_copy_ops);
307 list_for_each_entry_safe(subreq, p, &rreq->subrequests, rreq_link) {
308 if (!test_bit(NETFS_SREQ_COPY_TO_CACHE, &subreq->flags)) {
309 list_del_init(&subreq->rreq_link);
310 netfs_put_subrequest(subreq, false);
314 list_for_each_entry(subreq, &rreq->subrequests, rreq_link) {
315 /* Amalgamate adjacent writes */
316 while (!list_is_last(&subreq->rreq_link, &rreq->subrequests)) {
317 next = list_next_entry(subreq, rreq_link);
318 if (next->start != subreq->start + subreq->len)
320 subreq->len += next->len;
321 list_del_init(&next->rreq_link);
322 netfs_put_subrequest(next, false);
325 ret = cres->ops->prepare_write(cres, &subreq->start, &subreq->len,
328 trace_netfs_failure(rreq, subreq, ret, netfs_fail_prepare_write);
329 trace_netfs_sreq(subreq, netfs_sreq_trace_write_skip);
333 iov_iter_xarray(&iter, WRITE, &rreq->mapping->i_pages,
334 subreq->start, subreq->len);
336 atomic_inc(&rreq->nr_copy_ops);
337 netfs_stat(&netfs_n_rh_write);
338 netfs_get_subrequest(subreq);
339 trace_netfs_sreq(subreq, netfs_sreq_trace_write);
340 cres->ops->write(cres, subreq->start, &iter,
341 netfs_rreq_copy_terminated, subreq);
344 /* If we decrement nr_copy_ops to 0, the usage ref belongs to us. */
345 if (atomic_dec_and_test(&rreq->nr_copy_ops))
346 netfs_rreq_unmark_after_write(rreq, false);
349 static void netfs_rreq_write_to_cache_work(struct work_struct *work)
351 struct netfs_io_request *rreq =
352 container_of(work, struct netfs_io_request, work);
354 netfs_rreq_do_write_to_cache(rreq);
357 static void netfs_rreq_write_to_cache(struct netfs_io_request *rreq)
359 rreq->work.func = netfs_rreq_write_to_cache_work;
360 if (!queue_work(system_unbound_wq, &rreq->work))
365 * Unlock the folios in a read operation. We need to set PG_fscache on any
366 * folios we're going to write back before we unlock them.
368 static void netfs_rreq_unlock(struct netfs_io_request *rreq)
370 struct netfs_io_subrequest *subreq;
372 unsigned int iopos, account = 0;
373 pgoff_t start_page = rreq->start / PAGE_SIZE;
374 pgoff_t last_page = ((rreq->start + rreq->len) / PAGE_SIZE) - 1;
375 bool subreq_failed = false;
377 XA_STATE(xas, &rreq->mapping->i_pages, start_page);
379 if (test_bit(NETFS_RREQ_FAILED, &rreq->flags)) {
380 __clear_bit(NETFS_RREQ_COPY_TO_CACHE, &rreq->flags);
381 list_for_each_entry(subreq, &rreq->subrequests, rreq_link) {
382 __clear_bit(NETFS_SREQ_COPY_TO_CACHE, &subreq->flags);
386 /* Walk through the pagecache and the I/O request lists simultaneously.
387 * We may have a mixture of cached and uncached sections and we only
388 * really want to write out the uncached sections. This is slightly
389 * complicated by the possibility that we might have huge pages with a
392 subreq = list_first_entry(&rreq->subrequests,
393 struct netfs_io_subrequest, rreq_link);
395 subreq_failed = (subreq->error < 0);
397 trace_netfs_rreq(rreq, netfs_rreq_trace_unlock);
400 xas_for_each(&xas, folio, last_page) {
401 unsigned int pgpos = (folio_index(folio) - start_page) * PAGE_SIZE;
402 unsigned int pgend = pgpos + folio_size(folio);
403 bool pg_failed = false;
410 if (test_bit(NETFS_SREQ_COPY_TO_CACHE, &subreq->flags))
411 folio_start_fscache(folio);
412 pg_failed |= subreq_failed;
413 if (pgend < iopos + subreq->len)
416 account += subreq->transferred;
417 iopos += subreq->len;
418 if (!list_is_last(&subreq->rreq_link, &rreq->subrequests)) {
419 subreq = list_next_entry(subreq, rreq_link);
420 subreq_failed = (subreq->error < 0);
423 subreq_failed = false;
430 flush_dcache_folio(folio);
431 folio_mark_uptodate(folio);
434 if (!test_bit(NETFS_RREQ_DONT_UNLOCK_FOLIOS, &rreq->flags)) {
435 if (folio_index(folio) == rreq->no_unlock_folio &&
436 test_bit(NETFS_RREQ_NO_UNLOCK_FOLIO, &rreq->flags))
444 task_io_account_read(account);
445 if (rreq->netfs_ops->done)
446 rreq->netfs_ops->done(rreq);
450 * Handle a short read.
452 static void netfs_rreq_short_read(struct netfs_io_request *rreq,
453 struct netfs_io_subrequest *subreq)
455 __clear_bit(NETFS_SREQ_SHORT_IO, &subreq->flags);
456 __set_bit(NETFS_SREQ_SEEK_DATA_READ, &subreq->flags);
458 netfs_stat(&netfs_n_rh_short_read);
459 trace_netfs_sreq(subreq, netfs_sreq_trace_resubmit_short);
461 netfs_get_subrequest(subreq);
462 atomic_inc(&rreq->nr_outstanding);
463 if (subreq->source == NETFS_READ_FROM_CACHE)
464 netfs_read_from_cache(rreq, subreq, NETFS_READ_HOLE_CLEAR);
466 netfs_read_from_server(rreq, subreq);
470 * Resubmit any short or failed operations. Returns true if we got the rreq
473 static bool netfs_rreq_perform_resubmissions(struct netfs_io_request *rreq)
475 struct netfs_io_subrequest *subreq;
477 WARN_ON(in_interrupt());
479 trace_netfs_rreq(rreq, netfs_rreq_trace_resubmit);
481 /* We don't want terminating submissions trying to wake us up whilst
482 * we're still going through the list.
484 atomic_inc(&rreq->nr_outstanding);
486 __clear_bit(NETFS_RREQ_INCOMPLETE_IO, &rreq->flags);
487 list_for_each_entry(subreq, &rreq->subrequests, rreq_link) {
489 if (subreq->source != NETFS_READ_FROM_CACHE)
491 subreq->source = NETFS_DOWNLOAD_FROM_SERVER;
493 netfs_stat(&netfs_n_rh_download_instead);
494 trace_netfs_sreq(subreq, netfs_sreq_trace_download_instead);
495 netfs_get_subrequest(subreq);
496 atomic_inc(&rreq->nr_outstanding);
497 netfs_read_from_server(rreq, subreq);
498 } else if (test_bit(NETFS_SREQ_SHORT_IO, &subreq->flags)) {
499 netfs_rreq_short_read(rreq, subreq);
503 /* If we decrement nr_outstanding to 0, the usage ref belongs to us. */
504 if (atomic_dec_and_test(&rreq->nr_outstanding))
507 wake_up_var(&rreq->nr_outstanding);
512 * Check to see if the data read is still valid.
514 static void netfs_rreq_is_still_valid(struct netfs_io_request *rreq)
516 struct netfs_io_subrequest *subreq;
518 if (!rreq->netfs_ops->is_still_valid ||
519 rreq->netfs_ops->is_still_valid(rreq))
522 list_for_each_entry(subreq, &rreq->subrequests, rreq_link) {
523 if (subreq->source == NETFS_READ_FROM_CACHE) {
524 subreq->error = -ESTALE;
525 __set_bit(NETFS_RREQ_INCOMPLETE_IO, &rreq->flags);
531 * Assess the state of a read request and decide what to do next.
533 * Note that we could be in an ordinary kernel thread, on a workqueue or in
534 * softirq context at this point. We inherit a ref from the caller.
536 static void netfs_rreq_assess(struct netfs_io_request *rreq, bool was_async)
538 trace_netfs_rreq(rreq, netfs_rreq_trace_assess);
541 netfs_rreq_is_still_valid(rreq);
543 if (!test_bit(NETFS_RREQ_FAILED, &rreq->flags) &&
544 test_bit(NETFS_RREQ_INCOMPLETE_IO, &rreq->flags)) {
545 if (netfs_rreq_perform_resubmissions(rreq))
550 netfs_rreq_unlock(rreq);
552 clear_bit_unlock(NETFS_RREQ_IN_PROGRESS, &rreq->flags);
553 wake_up_bit(&rreq->flags, NETFS_RREQ_IN_PROGRESS);
555 if (test_bit(NETFS_RREQ_COPY_TO_CACHE, &rreq->flags))
556 return netfs_rreq_write_to_cache(rreq);
558 netfs_rreq_completed(rreq, was_async);
561 static void netfs_rreq_work(struct work_struct *work)
563 struct netfs_io_request *rreq =
564 container_of(work, struct netfs_io_request, work);
565 netfs_rreq_assess(rreq, false);
569 * Handle the completion of all outstanding I/O operations on a read request.
570 * We inherit a ref from the caller.
572 static void netfs_rreq_terminated(struct netfs_io_request *rreq,
575 if (test_bit(NETFS_RREQ_INCOMPLETE_IO, &rreq->flags) &&
577 if (!queue_work(system_unbound_wq, &rreq->work))
580 netfs_rreq_assess(rreq, was_async);
585 * netfs_subreq_terminated - Note the termination of an I/O operation.
586 * @subreq: The I/O request that has terminated.
587 * @transferred_or_error: The amount of data transferred or an error code.
588 * @was_async: The termination was asynchronous
590 * This tells the read helper that a contributory I/O operation has terminated,
591 * one way or another, and that it should integrate the results.
593 * The caller indicates in @transferred_or_error the outcome of the operation,
594 * supplying a positive value to indicate the number of bytes transferred, 0 to
595 * indicate a failure to transfer anything that should be retried or a negative
596 * error code. The helper will look after reissuing I/O operations as
597 * appropriate and writing downloaded data to the cache.
599 * If @was_async is true, the caller might be running in softirq or interrupt
600 * context and we can't sleep.
602 void netfs_subreq_terminated(struct netfs_io_subrequest *subreq,
603 ssize_t transferred_or_error,
606 struct netfs_io_request *rreq = subreq->rreq;
609 _enter("[%u]{%llx,%lx},%zd",
610 subreq->debug_index, subreq->start, subreq->flags,
611 transferred_or_error);
613 switch (subreq->source) {
614 case NETFS_READ_FROM_CACHE:
615 netfs_stat(&netfs_n_rh_read_done);
617 case NETFS_DOWNLOAD_FROM_SERVER:
618 netfs_stat(&netfs_n_rh_download_done);
624 if (IS_ERR_VALUE(transferred_or_error)) {
625 subreq->error = transferred_or_error;
626 trace_netfs_failure(rreq, subreq, transferred_or_error,
631 if (WARN(transferred_or_error > subreq->len - subreq->transferred,
632 "Subreq overread: R%x[%x] %zd > %zu - %zu",
633 rreq->debug_id, subreq->debug_index,
634 transferred_or_error, subreq->len, subreq->transferred))
635 transferred_or_error = subreq->len - subreq->transferred;
638 subreq->transferred += transferred_or_error;
639 if (subreq->transferred < subreq->len)
643 __clear_bit(NETFS_SREQ_NO_PROGRESS, &subreq->flags);
644 if (test_bit(NETFS_SREQ_COPY_TO_CACHE, &subreq->flags))
645 set_bit(NETFS_RREQ_COPY_TO_CACHE, &rreq->flags);
648 trace_netfs_sreq(subreq, netfs_sreq_trace_terminated);
650 /* If we decrement nr_outstanding to 0, the ref belongs to us. */
651 u = atomic_dec_return(&rreq->nr_outstanding);
653 netfs_rreq_terminated(rreq, was_async);
655 wake_up_var(&rreq->nr_outstanding);
657 netfs_put_subrequest(subreq, was_async);
661 if (test_bit(NETFS_SREQ_CLEAR_TAIL, &subreq->flags)) {
662 netfs_clear_unread(subreq);
663 subreq->transferred = subreq->len;
667 if (transferred_or_error == 0) {
668 if (__test_and_set_bit(NETFS_SREQ_NO_PROGRESS, &subreq->flags)) {
669 subreq->error = -ENODATA;
673 __clear_bit(NETFS_SREQ_NO_PROGRESS, &subreq->flags);
676 __set_bit(NETFS_SREQ_SHORT_IO, &subreq->flags);
677 set_bit(NETFS_RREQ_INCOMPLETE_IO, &rreq->flags);
681 if (subreq->source == NETFS_READ_FROM_CACHE) {
682 netfs_stat(&netfs_n_rh_read_failed);
683 set_bit(NETFS_RREQ_INCOMPLETE_IO, &rreq->flags);
685 netfs_stat(&netfs_n_rh_download_failed);
686 set_bit(NETFS_RREQ_FAILED, &rreq->flags);
687 rreq->error = subreq->error;
691 EXPORT_SYMBOL(netfs_subreq_terminated);
693 static enum netfs_io_source netfs_cache_prepare_read(struct netfs_io_subrequest *subreq,
696 struct netfs_io_request *rreq = subreq->rreq;
697 struct netfs_cache_resources *cres = &rreq->cache_resources;
700 return cres->ops->prepare_read(subreq, i_size);
701 if (subreq->start >= rreq->i_size)
702 return NETFS_FILL_WITH_ZEROES;
703 return NETFS_DOWNLOAD_FROM_SERVER;
707 * Work out what sort of subrequest the next one will be.
709 static enum netfs_io_source
710 netfs_rreq_prepare_read(struct netfs_io_request *rreq,
711 struct netfs_io_subrequest *subreq)
713 enum netfs_io_source source;
715 _enter("%llx-%llx,%llx", subreq->start, subreq->start + subreq->len, rreq->i_size);
717 source = netfs_cache_prepare_read(subreq, rreq->i_size);
718 if (source == NETFS_INVALID_READ)
721 if (source == NETFS_DOWNLOAD_FROM_SERVER) {
722 /* Call out to the netfs to let it shrink the request to fit
723 * its own I/O sizes and boundaries. If it shinks it here, it
724 * will be called again to make simultaneous calls; if it wants
725 * to make serial calls, it can indicate a short read and then
726 * we will call it again.
728 if (subreq->len > rreq->i_size - subreq->start)
729 subreq->len = rreq->i_size - subreq->start;
731 if (rreq->netfs_ops->clamp_length &&
732 !rreq->netfs_ops->clamp_length(subreq)) {
733 source = NETFS_INVALID_READ;
738 if (WARN_ON(subreq->len == 0))
739 source = NETFS_INVALID_READ;
742 subreq->source = source;
743 trace_netfs_sreq(subreq, netfs_sreq_trace_prepare);
748 * Slice off a piece of a read request and submit an I/O request for it.
750 static bool netfs_rreq_submit_slice(struct netfs_io_request *rreq,
751 unsigned int *_debug_index)
753 struct netfs_io_subrequest *subreq;
754 enum netfs_io_source source;
756 subreq = netfs_alloc_subrequest(rreq);
760 subreq->debug_index = (*_debug_index)++;
761 subreq->start = rreq->start + rreq->submitted;
762 subreq->len = rreq->len - rreq->submitted;
764 _debug("slice %llx,%zx,%zx", subreq->start, subreq->len, rreq->submitted);
765 list_add_tail(&subreq->rreq_link, &rreq->subrequests);
767 /* Call out to the cache to find out what it can do with the remaining
768 * subset. It tells us in subreq->flags what it decided should be done
769 * and adjusts subreq->len down if the subset crosses a cache boundary.
771 * Then when we hand the subset, it can choose to take a subset of that
772 * (the starts must coincide), in which case, we go around the loop
773 * again and ask it to download the next piece.
775 source = netfs_rreq_prepare_read(rreq, subreq);
776 if (source == NETFS_INVALID_READ)
779 atomic_inc(&rreq->nr_outstanding);
781 rreq->submitted += subreq->len;
783 trace_netfs_sreq(subreq, netfs_sreq_trace_submit);
785 case NETFS_FILL_WITH_ZEROES:
786 netfs_fill_with_zeroes(rreq, subreq);
788 case NETFS_DOWNLOAD_FROM_SERVER:
789 netfs_read_from_server(rreq, subreq);
791 case NETFS_READ_FROM_CACHE:
792 netfs_read_from_cache(rreq, subreq, NETFS_READ_HOLE_IGNORE);
801 rreq->error = subreq->error;
802 netfs_put_subrequest(subreq, false);
806 static void netfs_cache_expand_readahead(struct netfs_io_request *rreq,
807 loff_t *_start, size_t *_len, loff_t i_size)
809 struct netfs_cache_resources *cres = &rreq->cache_resources;
811 if (cres->ops && cres->ops->expand_readahead)
812 cres->ops->expand_readahead(cres, _start, _len, i_size);
815 static void netfs_rreq_expand(struct netfs_io_request *rreq,
816 struct readahead_control *ractl)
818 /* Give the cache a chance to change the request parameters. The
819 * resultant request must contain the original region.
821 netfs_cache_expand_readahead(rreq, &rreq->start, &rreq->len, rreq->i_size);
823 /* Give the netfs a chance to change the request parameters. The
824 * resultant request must contain the original region.
826 if (rreq->netfs_ops->expand_readahead)
827 rreq->netfs_ops->expand_readahead(rreq);
829 /* Expand the request if the cache wants it to start earlier. Note
830 * that the expansion may get further extended if the VM wishes to
831 * insert THPs and the preferred start and/or end wind up in the middle
834 * If this is the case, however, the THP size should be an integer
835 * multiple of the cache granule size, so we get a whole number of
836 * granules to deal with.
838 if (rreq->start != readahead_pos(ractl) ||
839 rreq->len != readahead_length(ractl)) {
840 readahead_expand(ractl, rreq->start, rreq->len);
841 rreq->start = readahead_pos(ractl);
842 rreq->len = readahead_length(ractl);
844 trace_netfs_read(rreq, readahead_pos(ractl), readahead_length(ractl),
845 netfs_read_trace_expanded);
850 * netfs_readahead - Helper to manage a read request
851 * @ractl: The description of the readahead request
852 * @ops: The network filesystem's operations for the helper to use
853 * @netfs_priv: Private netfs data to be retained in the request
855 * Fulfil a readahead request by drawing data from the cache if possible, or
856 * the netfs if not. Space beyond the EOF is zero-filled. Multiple I/O
857 * requests from different sources will get munged together. If necessary, the
858 * readahead window can be expanded in either direction to a more convenient
859 * alighment for RPC efficiency or to make storage in the cache feasible.
861 * The calling netfs must provide a table of operations, only one of which,
862 * issue_op, is mandatory. It may also be passed a private token, which will
863 * be retained in rreq->netfs_priv and will be cleaned up by ops->cleanup().
865 * This is usable whether or not caching is enabled.
867 void netfs_readahead(struct readahead_control *ractl,
868 const struct netfs_request_ops *ops,
871 struct netfs_io_request *rreq;
872 unsigned int debug_index = 0;
875 _enter("%lx,%x", readahead_index(ractl), readahead_count(ractl));
877 if (readahead_count(ractl) == 0)
880 rreq = netfs_alloc_request(ops, netfs_priv, ractl->file);
883 rreq->mapping = ractl->mapping;
884 rreq->start = readahead_pos(ractl);
885 rreq->len = readahead_length(ractl);
887 if (ops->begin_cache_operation) {
888 ret = ops->begin_cache_operation(rreq);
889 if (ret == -ENOMEM || ret == -EINTR || ret == -ERESTARTSYS)
893 netfs_stat(&netfs_n_rh_readahead);
894 trace_netfs_read(rreq, readahead_pos(ractl), readahead_length(ractl),
895 netfs_read_trace_readahead);
897 netfs_rreq_expand(rreq, ractl);
899 atomic_set(&rreq->nr_outstanding, 1);
901 if (!netfs_rreq_submit_slice(rreq, &debug_index))
904 } while (rreq->submitted < rreq->len);
906 /* Drop the refs on the folios here rather than in the cache or
907 * filesystem. The locks will be dropped in netfs_rreq_unlock().
909 while (readahead_folio(ractl))
912 /* If we decrement nr_outstanding to 0, the ref belongs to us. */
913 if (atomic_dec_and_test(&rreq->nr_outstanding))
914 netfs_rreq_assess(rreq, false);
918 netfs_put_request(rreq, false);
922 ops->cleanup(ractl->mapping, netfs_priv);
925 EXPORT_SYMBOL(netfs_readahead);
928 * netfs_readpage - Helper to manage a readpage request
929 * @file: The file to read from
930 * @folio: The folio to read
931 * @ops: The network filesystem's operations for the helper to use
932 * @netfs_priv: Private netfs data to be retained in the request
934 * Fulfil a readpage request by drawing data from the cache if possible, or the
935 * netfs if not. Space beyond the EOF is zero-filled. Multiple I/O requests
936 * from different sources will get munged together.
938 * The calling netfs must provide a table of operations, only one of which,
939 * issue_op, is mandatory. It may also be passed a private token, which will
940 * be retained in rreq->netfs_priv and will be cleaned up by ops->cleanup().
942 * This is usable whether or not caching is enabled.
944 int netfs_readpage(struct file *file,
946 const struct netfs_request_ops *ops,
949 struct netfs_io_request *rreq;
950 unsigned int debug_index = 0;
953 _enter("%lx", folio_index(folio));
955 rreq = netfs_alloc_request(ops, netfs_priv, file);
958 ops->cleanup(folio_file_mapping(folio), netfs_priv);
962 rreq->mapping = folio_file_mapping(folio);
963 rreq->start = folio_file_pos(folio);
964 rreq->len = folio_size(folio);
966 if (ops->begin_cache_operation) {
967 ret = ops->begin_cache_operation(rreq);
968 if (ret == -ENOMEM || ret == -EINTR || ret == -ERESTARTSYS) {
974 netfs_stat(&netfs_n_rh_readpage);
975 trace_netfs_read(rreq, rreq->start, rreq->len, netfs_read_trace_readpage);
977 netfs_get_request(rreq);
979 atomic_set(&rreq->nr_outstanding, 1);
981 if (!netfs_rreq_submit_slice(rreq, &debug_index))
984 } while (rreq->submitted < rreq->len);
986 /* Keep nr_outstanding incremented so that the ref always belongs to us, and
987 * the service code isn't punted off to a random thread pool to
991 wait_var_event(&rreq->nr_outstanding,
992 atomic_read(&rreq->nr_outstanding) == 1);
993 netfs_rreq_assess(rreq, false);
994 } while (test_bit(NETFS_RREQ_IN_PROGRESS, &rreq->flags));
997 if (ret == 0 && rreq->submitted < rreq->len) {
998 trace_netfs_failure(rreq, NULL, ret, netfs_fail_short_readpage);
1002 netfs_put_request(rreq, false);
1005 EXPORT_SYMBOL(netfs_readpage);
1008 * Prepare a folio for writing without reading first
1009 * @folio: The folio being prepared
1010 * @pos: starting position for the write
1011 * @len: length of write
1013 * In some cases, write_begin doesn't need to read at all:
1014 * - full folio write
1015 * - write that lies in a folio that is completely beyond EOF
1016 * - write that covers the folio from start to EOF or beyond it
1018 * If any of these criteria are met, then zero out the unwritten parts
1019 * of the folio and return true. Otherwise, return false.
1021 static bool netfs_skip_folio_read(struct folio *folio, loff_t pos, size_t len)
1023 struct inode *inode = folio_inode(folio);
1024 loff_t i_size = i_size_read(inode);
1025 size_t offset = offset_in_folio(folio, pos);
1027 /* Full folio write */
1028 if (offset == 0 && len >= folio_size(folio))
1031 /* pos beyond last folio in the file */
1032 if (pos - offset >= i_size)
1035 /* Write that covers from the start of the folio to EOF or beyond */
1036 if (offset == 0 && (pos + len) >= i_size)
1041 zero_user_segments(&folio->page, 0, offset, offset + len, folio_size(folio));
1046 * netfs_write_begin - Helper to prepare for writing
1047 * @file: The file to read from
1048 * @mapping: The mapping to read from
1049 * @pos: File position at which the write will begin
1050 * @len: The length of the write (may extend beyond the end of the folio chosen)
1051 * @aop_flags: AOP_* flags
1052 * @_folio: Where to put the resultant folio
1053 * @_fsdata: Place for the netfs to store a cookie
1054 * @ops: The network filesystem's operations for the helper to use
1055 * @netfs_priv: Private netfs data to be retained in the request
1057 * Pre-read data for a write-begin request by drawing data from the cache if
1058 * possible, or the netfs if not. Space beyond the EOF is zero-filled.
1059 * Multiple I/O requests from different sources will get munged together. If
1060 * necessary, the readahead window can be expanded in either direction to a
1061 * more convenient alighment for RPC efficiency or to make storage in the cache
1064 * The calling netfs must provide a table of operations, only one of which,
1065 * issue_op, is mandatory.
1067 * The check_write_begin() operation can be provided to check for and flush
1068 * conflicting writes once the folio is grabbed and locked. It is passed a
1069 * pointer to the fsdata cookie that gets returned to the VM to be passed to
1070 * write_end. It is permitted to sleep. It should return 0 if the request
1071 * should go ahead; unlock the folio and return -EAGAIN to cause the folio to
1072 * be regot; or return an error.
1074 * This is usable whether or not caching is enabled.
1076 int netfs_write_begin(struct file *file, struct address_space *mapping,
1077 loff_t pos, unsigned int len, unsigned int aop_flags,
1078 struct folio **_folio, void **_fsdata,
1079 const struct netfs_request_ops *ops,
1082 struct netfs_io_request *rreq;
1083 struct folio *folio;
1084 struct inode *inode = file_inode(file);
1085 unsigned int debug_index = 0, fgp_flags;
1086 pgoff_t index = pos >> PAGE_SHIFT;
1089 DEFINE_READAHEAD(ractl, file, NULL, mapping, index);
1092 fgp_flags = FGP_LOCK | FGP_WRITE | FGP_CREAT | FGP_STABLE;
1093 if (aop_flags & AOP_FLAG_NOFS)
1094 fgp_flags |= FGP_NOFS;
1095 folio = __filemap_get_folio(mapping, index, fgp_flags,
1096 mapping_gfp_mask(mapping));
1100 if (ops->check_write_begin) {
1101 /* Allow the netfs (eg. ceph) to flush conflicts. */
1102 ret = ops->check_write_begin(file, pos, len, folio, _fsdata);
1104 trace_netfs_failure(NULL, NULL, ret, netfs_fail_check_write_begin);
1111 if (folio_test_uptodate(folio))
1114 /* If the page is beyond the EOF, we want to clear it - unless it's
1115 * within the cache granule containing the EOF, in which case we need
1116 * to preload the granule.
1118 if (!ops->is_cache_enabled(inode) &&
1119 netfs_skip_folio_read(folio, pos, len)) {
1120 netfs_stat(&netfs_n_rh_write_zskip);
1121 goto have_folio_no_wait;
1125 rreq = netfs_alloc_request(ops, netfs_priv, file);
1128 rreq->mapping = folio_file_mapping(folio);
1129 rreq->start = folio_file_pos(folio);
1130 rreq->len = folio_size(folio);
1131 rreq->no_unlock_folio = folio_index(folio);
1132 __set_bit(NETFS_RREQ_NO_UNLOCK_FOLIO, &rreq->flags);
1135 if (ops->begin_cache_operation) {
1136 ret = ops->begin_cache_operation(rreq);
1137 if (ret == -ENOMEM || ret == -EINTR || ret == -ERESTARTSYS)
1141 netfs_stat(&netfs_n_rh_write_begin);
1142 trace_netfs_read(rreq, pos, len, netfs_read_trace_write_begin);
1144 /* Expand the request to meet caching requirements and download
1147 ractl._nr_pages = folio_nr_pages(folio);
1148 netfs_rreq_expand(rreq, &ractl);
1149 netfs_get_request(rreq);
1151 /* We hold the folio locks, so we can drop the references */
1153 while (readahead_folio(&ractl))
1156 atomic_set(&rreq->nr_outstanding, 1);
1158 if (!netfs_rreq_submit_slice(rreq, &debug_index))
1161 } while (rreq->submitted < rreq->len);
1163 /* Keep nr_outstanding incremented so that the ref always belongs to
1164 * us, and the service code isn't punted off to a random thread pool to
1168 wait_var_event(&rreq->nr_outstanding,
1169 atomic_read(&rreq->nr_outstanding) == 1);
1170 netfs_rreq_assess(rreq, false);
1171 if (!test_bit(NETFS_RREQ_IN_PROGRESS, &rreq->flags))
1177 if (ret == 0 && rreq->submitted < rreq->len) {
1178 trace_netfs_failure(rreq, NULL, ret, netfs_fail_short_write_begin);
1181 netfs_put_request(rreq, false);
1186 ret = folio_wait_fscache_killable(folio);
1191 ops->cleanup(mapping, netfs_priv);
1197 netfs_put_request(rreq, false);
1199 folio_unlock(folio);
1202 ops->cleanup(mapping, netfs_priv);
1203 _leave(" = %d", ret);
1206 EXPORT_SYMBOL(netfs_write_begin);