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_read_subrequest *, bool);
33 static void netfs_put_subrequest(struct netfs_read_subrequest *subreq,
36 if (refcount_dec_and_test(&subreq->usage))
37 __netfs_put_subrequest(subreq, was_async);
40 static struct netfs_read_request *netfs_alloc_read_request(
41 const struct netfs_read_request_ops *ops, void *netfs_priv,
44 static atomic_t debug_ids;
45 struct netfs_read_request *rreq;
47 rreq = kzalloc(sizeof(struct netfs_read_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 ops->init_rreq(rreq, file);
59 netfs_stat(&netfs_n_rh_rreq);
65 static void netfs_get_read_request(struct netfs_read_request *rreq)
67 refcount_inc(&rreq->usage);
70 static void netfs_rreq_clear_subreqs(struct netfs_read_request *rreq,
73 struct netfs_read_subrequest *subreq;
75 while (!list_empty(&rreq->subrequests)) {
76 subreq = list_first_entry(&rreq->subrequests,
77 struct netfs_read_subrequest, rreq_link);
78 list_del(&subreq->rreq_link);
79 netfs_put_subrequest(subreq, was_async);
83 static void netfs_free_read_request(struct work_struct *work)
85 struct netfs_read_request *rreq =
86 container_of(work, struct netfs_read_request, work);
87 netfs_rreq_clear_subreqs(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_read_request(struct netfs_read_request *rreq, bool was_async)
99 if (refcount_dec_and_test(&rreq->usage)) {
101 rreq->work.func = netfs_free_read_request;
102 if (!queue_work(system_unbound_wq, &rreq->work))
105 netfs_free_read_request(&rreq->work);
111 * Allocate and partially initialise an I/O request structure.
113 static struct netfs_read_subrequest *netfs_alloc_subrequest(
114 struct netfs_read_request *rreq)
116 struct netfs_read_subrequest *subreq;
118 subreq = kzalloc(sizeof(struct netfs_read_subrequest), GFP_KERNEL);
120 INIT_LIST_HEAD(&subreq->rreq_link);
121 refcount_set(&subreq->usage, 2);
123 netfs_get_read_request(rreq);
124 netfs_stat(&netfs_n_rh_sreq);
130 static void netfs_get_read_subrequest(struct netfs_read_subrequest *subreq)
132 refcount_inc(&subreq->usage);
135 static void __netfs_put_subrequest(struct netfs_read_subrequest *subreq,
138 struct netfs_read_request *rreq = subreq->rreq;
140 trace_netfs_sreq(subreq, netfs_sreq_trace_free);
142 netfs_stat_d(&netfs_n_rh_sreq);
143 netfs_put_read_request(rreq, was_async);
147 * Clear the unread part of an I/O request.
149 static void netfs_clear_unread(struct netfs_read_subrequest *subreq)
151 struct iov_iter iter;
153 iov_iter_xarray(&iter, WRITE, &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_read_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_read_request *rreq,
172 struct netfs_read_subrequest *subreq,
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, seek_data,
184 netfs_cache_read_terminated, subreq);
188 * Fill a subrequest region with zeroes.
190 static void netfs_fill_with_zeroes(struct netfs_read_request *rreq,
191 struct netfs_read_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_read_request *rreq,
215 struct netfs_read_subrequest *subreq)
217 netfs_stat(&netfs_n_rh_download);
218 rreq->netfs_ops->issue_op(subreq);
222 * Release those waiting.
224 static void netfs_rreq_completed(struct netfs_read_request *rreq, bool was_async)
226 trace_netfs_rreq(rreq, netfs_rreq_trace_done);
227 netfs_rreq_clear_subreqs(rreq, was_async);
228 netfs_put_read_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 pages 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_read_request *rreq,
240 struct netfs_read_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, page, (subreq->start + subreq->len - 1) / PAGE_SIZE) {
251 /* We might have multiple writes from the same huge
252 * page, but we mustn't unlock a page more than once.
254 if (have_unlocked && page->index <= unlocked)
256 unlocked = page->index;
257 end_page_fscache(page);
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_read_subrequest *subreq = priv;
270 struct netfs_read_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_wr_ops to 0, the ref belongs to us. */
283 if (atomic_dec_and_test(&rreq->nr_wr_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_read_request *rreq)
295 struct netfs_cache_resources *cres = &rreq->cache_resources;
296 struct netfs_read_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_wr_ops);
307 list_for_each_entry_safe(subreq, p, &rreq->subrequests, rreq_link) {
308 if (!test_bit(NETFS_SREQ_WRITE_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_wr_ops);
337 netfs_stat(&netfs_n_rh_write);
338 netfs_get_read_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_wr_ops to 0, the usage ref belongs to us. */
345 if (atomic_dec_and_test(&rreq->nr_wr_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_read_request *rreq =
352 container_of(work, struct netfs_read_request, work);
354 netfs_rreq_do_write_to_cache(rreq);
357 static void netfs_rreq_write_to_cache(struct netfs_read_request *rreq,
361 rreq->work.func = netfs_rreq_write_to_cache_work;
362 if (!queue_work(system_unbound_wq, &rreq->work))
365 netfs_rreq_do_write_to_cache(rreq);
370 * Unlock the pages in a read operation. We need to set PG_fscache on any
371 * pages we're going to write back before we unlock them.
373 static void netfs_rreq_unlock(struct netfs_read_request *rreq)
375 struct netfs_read_subrequest *subreq;
377 unsigned int iopos, account = 0;
378 pgoff_t start_page = rreq->start / PAGE_SIZE;
379 pgoff_t last_page = ((rreq->start + rreq->len) / PAGE_SIZE) - 1;
380 bool subreq_failed = false;
383 XA_STATE(xas, &rreq->mapping->i_pages, start_page);
385 if (test_bit(NETFS_RREQ_FAILED, &rreq->flags)) {
386 __clear_bit(NETFS_RREQ_WRITE_TO_CACHE, &rreq->flags);
387 list_for_each_entry(subreq, &rreq->subrequests, rreq_link) {
388 __clear_bit(NETFS_SREQ_WRITE_TO_CACHE, &subreq->flags);
392 /* Walk through the pagecache and the I/O request lists simultaneously.
393 * We may have a mixture of cached and uncached sections and we only
394 * really want to write out the uncached sections. This is slightly
395 * complicated by the possibility that we might have huge pages with a
398 subreq = list_first_entry(&rreq->subrequests,
399 struct netfs_read_subrequest, rreq_link);
401 subreq_failed = (subreq->error < 0);
403 trace_netfs_rreq(rreq, netfs_rreq_trace_unlock);
406 xas_for_each(&xas, page, last_page) {
407 unsigned int pgpos = (page->index - start_page) * PAGE_SIZE;
408 unsigned int pgend = pgpos + thp_size(page);
409 bool pg_failed = false;
416 if (test_bit(NETFS_SREQ_WRITE_TO_CACHE, &subreq->flags))
417 set_page_fscache(page);
418 pg_failed |= subreq_failed;
419 if (pgend < iopos + subreq->len)
422 account += subreq->transferred;
423 iopos += subreq->len;
424 if (!list_is_last(&subreq->rreq_link, &rreq->subrequests)) {
425 subreq = list_next_entry(subreq, rreq_link);
426 subreq_failed = (subreq->error < 0);
429 subreq_failed = false;
436 for (i = 0; i < thp_nr_pages(page); i++)
437 flush_dcache_page(page);
438 SetPageUptodate(page);
441 if (!test_bit(NETFS_RREQ_DONT_UNLOCK_PAGES, &rreq->flags)) {
442 if (page->index == rreq->no_unlock_page &&
443 test_bit(NETFS_RREQ_NO_UNLOCK_PAGE, &rreq->flags))
451 task_io_account_read(account);
452 if (rreq->netfs_ops->done)
453 rreq->netfs_ops->done(rreq);
457 * Handle a short read.
459 static void netfs_rreq_short_read(struct netfs_read_request *rreq,
460 struct netfs_read_subrequest *subreq)
462 __clear_bit(NETFS_SREQ_SHORT_READ, &subreq->flags);
463 __set_bit(NETFS_SREQ_SEEK_DATA_READ, &subreq->flags);
465 netfs_stat(&netfs_n_rh_short_read);
466 trace_netfs_sreq(subreq, netfs_sreq_trace_resubmit_short);
468 netfs_get_read_subrequest(subreq);
469 atomic_inc(&rreq->nr_rd_ops);
470 if (subreq->source == NETFS_READ_FROM_CACHE)
471 netfs_read_from_cache(rreq, subreq, true);
473 netfs_read_from_server(rreq, subreq);
477 * Resubmit any short or failed operations. Returns true if we got the rreq
480 static bool netfs_rreq_perform_resubmissions(struct netfs_read_request *rreq)
482 struct netfs_read_subrequest *subreq;
484 WARN_ON(in_interrupt());
486 trace_netfs_rreq(rreq, netfs_rreq_trace_resubmit);
488 /* We don't want terminating submissions trying to wake us up whilst
489 * we're still going through the list.
491 atomic_inc(&rreq->nr_rd_ops);
493 __clear_bit(NETFS_RREQ_INCOMPLETE_IO, &rreq->flags);
494 list_for_each_entry(subreq, &rreq->subrequests, rreq_link) {
496 if (subreq->source != NETFS_READ_FROM_CACHE)
498 subreq->source = NETFS_DOWNLOAD_FROM_SERVER;
500 netfs_stat(&netfs_n_rh_download_instead);
501 trace_netfs_sreq(subreq, netfs_sreq_trace_download_instead);
502 netfs_get_read_subrequest(subreq);
503 atomic_inc(&rreq->nr_rd_ops);
504 netfs_read_from_server(rreq, subreq);
505 } else if (test_bit(NETFS_SREQ_SHORT_READ, &subreq->flags)) {
506 netfs_rreq_short_read(rreq, subreq);
510 /* If we decrement nr_rd_ops to 0, the usage ref belongs to us. */
511 if (atomic_dec_and_test(&rreq->nr_rd_ops))
514 wake_up_var(&rreq->nr_rd_ops);
519 * Check to see if the data read is still valid.
521 static void netfs_rreq_is_still_valid(struct netfs_read_request *rreq)
523 struct netfs_read_subrequest *subreq;
525 if (!rreq->netfs_ops->is_still_valid ||
526 rreq->netfs_ops->is_still_valid(rreq))
529 list_for_each_entry(subreq, &rreq->subrequests, rreq_link) {
530 if (subreq->source == NETFS_READ_FROM_CACHE) {
531 subreq->error = -ESTALE;
532 __set_bit(NETFS_RREQ_INCOMPLETE_IO, &rreq->flags);
538 * Assess the state of a read request and decide what to do next.
540 * Note that we could be in an ordinary kernel thread, on a workqueue or in
541 * softirq context at this point. We inherit a ref from the caller.
543 static void netfs_rreq_assess(struct netfs_read_request *rreq, bool was_async)
545 trace_netfs_rreq(rreq, netfs_rreq_trace_assess);
548 netfs_rreq_is_still_valid(rreq);
550 if (!test_bit(NETFS_RREQ_FAILED, &rreq->flags) &&
551 test_bit(NETFS_RREQ_INCOMPLETE_IO, &rreq->flags)) {
552 if (netfs_rreq_perform_resubmissions(rreq))
557 netfs_rreq_unlock(rreq);
559 clear_bit_unlock(NETFS_RREQ_IN_PROGRESS, &rreq->flags);
560 wake_up_bit(&rreq->flags, NETFS_RREQ_IN_PROGRESS);
562 if (test_bit(NETFS_RREQ_WRITE_TO_CACHE, &rreq->flags))
563 return netfs_rreq_write_to_cache(rreq, was_async);
565 netfs_rreq_completed(rreq, was_async);
568 static void netfs_rreq_work(struct work_struct *work)
570 struct netfs_read_request *rreq =
571 container_of(work, struct netfs_read_request, work);
572 netfs_rreq_assess(rreq, false);
576 * Handle the completion of all outstanding I/O operations on a read request.
577 * We inherit a ref from the caller.
579 static void netfs_rreq_terminated(struct netfs_read_request *rreq,
582 if (test_bit(NETFS_RREQ_INCOMPLETE_IO, &rreq->flags) &&
584 if (!queue_work(system_unbound_wq, &rreq->work))
587 netfs_rreq_assess(rreq, was_async);
592 * netfs_subreq_terminated - Note the termination of an I/O operation.
593 * @subreq: The I/O request that has terminated.
594 * @transferred_or_error: The amount of data transferred or an error code.
595 * @was_async: The termination was asynchronous
597 * This tells the read helper that a contributory I/O operation has terminated,
598 * one way or another, and that it should integrate the results.
600 * The caller indicates in @transferred_or_error the outcome of the operation,
601 * supplying a positive value to indicate the number of bytes transferred, 0 to
602 * indicate a failure to transfer anything that should be retried or a negative
603 * error code. The helper will look after reissuing I/O operations as
604 * appropriate and writing downloaded data to the cache.
606 * If @was_async is true, the caller might be running in softirq or interrupt
607 * context and we can't sleep.
609 void netfs_subreq_terminated(struct netfs_read_subrequest *subreq,
610 ssize_t transferred_or_error,
613 struct netfs_read_request *rreq = subreq->rreq;
616 _enter("[%u]{%llx,%lx},%zd",
617 subreq->debug_index, subreq->start, subreq->flags,
618 transferred_or_error);
620 switch (subreq->source) {
621 case NETFS_READ_FROM_CACHE:
622 netfs_stat(&netfs_n_rh_read_done);
624 case NETFS_DOWNLOAD_FROM_SERVER:
625 netfs_stat(&netfs_n_rh_download_done);
631 if (IS_ERR_VALUE(transferred_or_error)) {
632 subreq->error = transferred_or_error;
633 trace_netfs_failure(rreq, subreq, transferred_or_error,
638 if (WARN(transferred_or_error > subreq->len - subreq->transferred,
639 "Subreq overread: R%x[%x] %zd > %zu - %zu",
640 rreq->debug_id, subreq->debug_index,
641 transferred_or_error, subreq->len, subreq->transferred))
642 transferred_or_error = subreq->len - subreq->transferred;
645 subreq->transferred += transferred_or_error;
646 if (subreq->transferred < subreq->len)
650 __clear_bit(NETFS_SREQ_NO_PROGRESS, &subreq->flags);
651 if (test_bit(NETFS_SREQ_WRITE_TO_CACHE, &subreq->flags))
652 set_bit(NETFS_RREQ_WRITE_TO_CACHE, &rreq->flags);
655 trace_netfs_sreq(subreq, netfs_sreq_trace_terminated);
657 /* If we decrement nr_rd_ops to 0, the ref belongs to us. */
658 u = atomic_dec_return(&rreq->nr_rd_ops);
660 netfs_rreq_terminated(rreq, was_async);
662 wake_up_var(&rreq->nr_rd_ops);
664 netfs_put_subrequest(subreq, was_async);
668 if (test_bit(NETFS_SREQ_CLEAR_TAIL, &subreq->flags)) {
669 netfs_clear_unread(subreq);
670 subreq->transferred = subreq->len;
674 if (transferred_or_error == 0) {
675 if (__test_and_set_bit(NETFS_SREQ_NO_PROGRESS, &subreq->flags)) {
676 subreq->error = -ENODATA;
680 __clear_bit(NETFS_SREQ_NO_PROGRESS, &subreq->flags);
683 __set_bit(NETFS_SREQ_SHORT_READ, &subreq->flags);
684 set_bit(NETFS_RREQ_INCOMPLETE_IO, &rreq->flags);
688 if (subreq->source == NETFS_READ_FROM_CACHE) {
689 netfs_stat(&netfs_n_rh_read_failed);
690 set_bit(NETFS_RREQ_INCOMPLETE_IO, &rreq->flags);
692 netfs_stat(&netfs_n_rh_download_failed);
693 set_bit(NETFS_RREQ_FAILED, &rreq->flags);
694 rreq->error = subreq->error;
698 EXPORT_SYMBOL(netfs_subreq_terminated);
700 static enum netfs_read_source netfs_cache_prepare_read(struct netfs_read_subrequest *subreq,
703 struct netfs_read_request *rreq = subreq->rreq;
704 struct netfs_cache_resources *cres = &rreq->cache_resources;
707 return cres->ops->prepare_read(subreq, i_size);
708 if (subreq->start >= rreq->i_size)
709 return NETFS_FILL_WITH_ZEROES;
710 return NETFS_DOWNLOAD_FROM_SERVER;
714 * Work out what sort of subrequest the next one will be.
716 static enum netfs_read_source
717 netfs_rreq_prepare_read(struct netfs_read_request *rreq,
718 struct netfs_read_subrequest *subreq)
720 enum netfs_read_source source;
722 _enter("%llx-%llx,%llx", subreq->start, subreq->start + subreq->len, rreq->i_size);
724 source = netfs_cache_prepare_read(subreq, rreq->i_size);
725 if (source == NETFS_INVALID_READ)
728 if (source == NETFS_DOWNLOAD_FROM_SERVER) {
729 /* Call out to the netfs to let it shrink the request to fit
730 * its own I/O sizes and boundaries. If it shinks it here, it
731 * will be called again to make simultaneous calls; if it wants
732 * to make serial calls, it can indicate a short read and then
733 * we will call it again.
735 if (subreq->len > rreq->i_size - subreq->start)
736 subreq->len = rreq->i_size - subreq->start;
738 if (rreq->netfs_ops->clamp_length &&
739 !rreq->netfs_ops->clamp_length(subreq)) {
740 source = NETFS_INVALID_READ;
745 if (WARN_ON(subreq->len == 0))
746 source = NETFS_INVALID_READ;
749 subreq->source = source;
750 trace_netfs_sreq(subreq, netfs_sreq_trace_prepare);
755 * Slice off a piece of a read request and submit an I/O request for it.
757 static bool netfs_rreq_submit_slice(struct netfs_read_request *rreq,
758 unsigned int *_debug_index)
760 struct netfs_read_subrequest *subreq;
761 enum netfs_read_source source;
763 subreq = netfs_alloc_subrequest(rreq);
767 subreq->debug_index = (*_debug_index)++;
768 subreq->start = rreq->start + rreq->submitted;
769 subreq->len = rreq->len - rreq->submitted;
771 _debug("slice %llx,%zx,%zx", subreq->start, subreq->len, rreq->submitted);
772 list_add_tail(&subreq->rreq_link, &rreq->subrequests);
774 /* Call out to the cache to find out what it can do with the remaining
775 * subset. It tells us in subreq->flags what it decided should be done
776 * and adjusts subreq->len down if the subset crosses a cache boundary.
778 * Then when we hand the subset, it can choose to take a subset of that
779 * (the starts must coincide), in which case, we go around the loop
780 * again and ask it to download the next piece.
782 source = netfs_rreq_prepare_read(rreq, subreq);
783 if (source == NETFS_INVALID_READ)
786 atomic_inc(&rreq->nr_rd_ops);
788 rreq->submitted += subreq->len;
790 trace_netfs_sreq(subreq, netfs_sreq_trace_submit);
792 case NETFS_FILL_WITH_ZEROES:
793 netfs_fill_with_zeroes(rreq, subreq);
795 case NETFS_DOWNLOAD_FROM_SERVER:
796 netfs_read_from_server(rreq, subreq);
798 case NETFS_READ_FROM_CACHE:
799 netfs_read_from_cache(rreq, subreq, false);
808 rreq->error = subreq->error;
809 netfs_put_subrequest(subreq, false);
813 static void netfs_cache_expand_readahead(struct netfs_read_request *rreq,
814 loff_t *_start, size_t *_len, loff_t i_size)
816 struct netfs_cache_resources *cres = &rreq->cache_resources;
818 if (cres->ops && cres->ops->expand_readahead)
819 cres->ops->expand_readahead(cres, _start, _len, i_size);
822 static void netfs_rreq_expand(struct netfs_read_request *rreq,
823 struct readahead_control *ractl)
825 /* Give the cache a chance to change the request parameters. The
826 * resultant request must contain the original region.
828 netfs_cache_expand_readahead(rreq, &rreq->start, &rreq->len, rreq->i_size);
830 /* Give the netfs a chance to change the request parameters. The
831 * resultant request must contain the original region.
833 if (rreq->netfs_ops->expand_readahead)
834 rreq->netfs_ops->expand_readahead(rreq);
836 /* Expand the request if the cache wants it to start earlier. Note
837 * that the expansion may get further extended if the VM wishes to
838 * insert THPs and the preferred start and/or end wind up in the middle
841 * If this is the case, however, the THP size should be an integer
842 * multiple of the cache granule size, so we get a whole number of
843 * granules to deal with.
845 if (rreq->start != readahead_pos(ractl) ||
846 rreq->len != readahead_length(ractl)) {
847 readahead_expand(ractl, rreq->start, rreq->len);
848 rreq->start = readahead_pos(ractl);
849 rreq->len = readahead_length(ractl);
851 trace_netfs_read(rreq, readahead_pos(ractl), readahead_length(ractl),
852 netfs_read_trace_expanded);
857 * netfs_readahead - Helper to manage a read request
858 * @ractl: The description of the readahead request
859 * @ops: The network filesystem's operations for the helper to use
860 * @netfs_priv: Private netfs data to be retained in the request
862 * Fulfil a readahead request by drawing data from the cache if possible, or
863 * the netfs if not. Space beyond the EOF is zero-filled. Multiple I/O
864 * requests from different sources will get munged together. If necessary, the
865 * readahead window can be expanded in either direction to a more convenient
866 * alighment for RPC efficiency or to make storage in the cache feasible.
868 * The calling netfs must provide a table of operations, only one of which,
869 * issue_op, is mandatory. It may also be passed a private token, which will
870 * be retained in rreq->netfs_priv and will be cleaned up by ops->cleanup().
872 * This is usable whether or not caching is enabled.
874 void netfs_readahead(struct readahead_control *ractl,
875 const struct netfs_read_request_ops *ops,
878 struct netfs_read_request *rreq;
880 unsigned int debug_index = 0;
883 _enter("%lx,%x", readahead_index(ractl), readahead_count(ractl));
885 if (readahead_count(ractl) == 0)
888 rreq = netfs_alloc_read_request(ops, netfs_priv, ractl->file);
891 rreq->mapping = ractl->mapping;
892 rreq->start = readahead_pos(ractl);
893 rreq->len = readahead_length(ractl);
895 if (ops->begin_cache_operation) {
896 ret = ops->begin_cache_operation(rreq);
897 if (ret == -ENOMEM || ret == -EINTR || ret == -ERESTARTSYS)
901 netfs_stat(&netfs_n_rh_readahead);
902 trace_netfs_read(rreq, readahead_pos(ractl), readahead_length(ractl),
903 netfs_read_trace_readahead);
905 netfs_rreq_expand(rreq, ractl);
907 atomic_set(&rreq->nr_rd_ops, 1);
909 if (!netfs_rreq_submit_slice(rreq, &debug_index))
912 } while (rreq->submitted < rreq->len);
914 /* Drop the refs on the pages here rather than in the cache or
915 * filesystem. The locks will be dropped in netfs_rreq_unlock().
917 while ((page = readahead_page(ractl)))
920 /* If we decrement nr_rd_ops to 0, the ref belongs to us. */
921 if (atomic_dec_and_test(&rreq->nr_rd_ops))
922 netfs_rreq_assess(rreq, false);
926 netfs_put_read_request(rreq, false);
930 ops->cleanup(ractl->mapping, netfs_priv);
933 EXPORT_SYMBOL(netfs_readahead);
936 * netfs_readpage - Helper to manage a readpage request
937 * @file: The file to read from
938 * @page: The page to read
939 * @ops: The network filesystem's operations for the helper to use
940 * @netfs_priv: Private netfs data to be retained in the request
942 * Fulfil a readpage request by drawing data from the cache if possible, or the
943 * netfs if not. Space beyond the EOF is zero-filled. Multiple I/O requests
944 * from different sources will get munged together.
946 * The calling netfs must provide a table of operations, only one of which,
947 * issue_op, is mandatory. It may also be passed a private token, which will
948 * be retained in rreq->netfs_priv and will be cleaned up by ops->cleanup().
950 * This is usable whether or not caching is enabled.
952 int netfs_readpage(struct file *file,
954 const struct netfs_read_request_ops *ops,
957 struct netfs_read_request *rreq;
958 unsigned int debug_index = 0;
961 _enter("%lx", page_index(page));
963 rreq = netfs_alloc_read_request(ops, netfs_priv, file);
966 ops->cleanup(netfs_priv, page_file_mapping(page));
970 rreq->mapping = page_file_mapping(page);
971 rreq->start = page_file_offset(page);
972 rreq->len = thp_size(page);
974 if (ops->begin_cache_operation) {
975 ret = ops->begin_cache_operation(rreq);
976 if (ret == -ENOMEM || ret == -EINTR || ret == -ERESTARTSYS) {
982 netfs_stat(&netfs_n_rh_readpage);
983 trace_netfs_read(rreq, rreq->start, rreq->len, netfs_read_trace_readpage);
985 netfs_get_read_request(rreq);
987 atomic_set(&rreq->nr_rd_ops, 1);
989 if (!netfs_rreq_submit_slice(rreq, &debug_index))
992 } while (rreq->submitted < rreq->len);
994 /* Keep nr_rd_ops incremented so that the ref always belongs to us, and
995 * the service code isn't punted off to a random thread pool to
999 wait_var_event(&rreq->nr_rd_ops, atomic_read(&rreq->nr_rd_ops) == 1);
1000 netfs_rreq_assess(rreq, false);
1001 } while (test_bit(NETFS_RREQ_IN_PROGRESS, &rreq->flags));
1004 if (ret == 0 && rreq->submitted < rreq->len) {
1005 trace_netfs_failure(rreq, NULL, ret, netfs_fail_short_readpage);
1009 netfs_put_read_request(rreq, false);
1012 EXPORT_SYMBOL(netfs_readpage);
1015 * netfs_skip_page_read - prep a page for writing without reading first
1016 * @page: page being prepared
1017 * @pos: starting position for the write
1018 * @len: length of write
1020 * In some cases, write_begin doesn't need to read at all:
1022 * - write that lies in a page that is completely beyond EOF
1023 * - write that covers the the page from start to EOF or beyond it
1025 * If any of these criteria are met, then zero out the unwritten parts
1026 * of the page and return true. Otherwise, return false.
1028 static bool netfs_skip_page_read(struct page *page, loff_t pos, size_t len)
1030 struct inode *inode = page->mapping->host;
1031 loff_t i_size = i_size_read(inode);
1032 size_t offset = offset_in_thp(page, pos);
1034 /* Full page write */
1035 if (offset == 0 && len >= thp_size(page))
1038 /* pos beyond last page in the file */
1039 if (pos - offset >= i_size)
1042 /* Write that covers from the start of the page to EOF or beyond */
1043 if (offset == 0 && (pos + len) >= i_size)
1048 zero_user_segments(page, 0, offset, offset + len, thp_size(page));
1053 * netfs_write_begin - Helper to prepare for writing
1054 * @file: The file to read from
1055 * @mapping: The mapping to read from
1056 * @pos: File position at which the write will begin
1057 * @len: The length of the write (may extend beyond the end of the page chosen)
1058 * @flags: AOP_* flags
1059 * @_page: Where to put the resultant page
1060 * @_fsdata: Place for the netfs to store a cookie
1061 * @ops: The network filesystem's operations for the helper to use
1062 * @netfs_priv: Private netfs data to be retained in the request
1064 * Pre-read data for a write-begin request by drawing data from the cache if
1065 * possible, or the netfs if not. Space beyond the EOF is zero-filled.
1066 * Multiple I/O requests from different sources will get munged together. If
1067 * necessary, the readahead window can be expanded in either direction to a
1068 * more convenient alighment for RPC efficiency or to make storage in the cache
1071 * The calling netfs must provide a table of operations, only one of which,
1072 * issue_op, is mandatory.
1074 * The check_write_begin() operation can be provided to check for and flush
1075 * conflicting writes once the page is grabbed and locked. It is passed a
1076 * pointer to the fsdata cookie that gets returned to the VM to be passed to
1077 * write_end. It is permitted to sleep. It should return 0 if the request
1078 * should go ahead; unlock the page and return -EAGAIN to cause the page to be
1079 * regot; or return an error.
1081 * This is usable whether or not caching is enabled.
1083 int netfs_write_begin(struct file *file, struct address_space *mapping,
1084 loff_t pos, unsigned int len, unsigned int flags,
1085 struct page **_page, void **_fsdata,
1086 const struct netfs_read_request_ops *ops,
1089 struct netfs_read_request *rreq;
1090 struct page *page, *xpage;
1091 struct inode *inode = file_inode(file);
1092 unsigned int debug_index = 0;
1093 pgoff_t index = pos >> PAGE_SHIFT;
1096 DEFINE_READAHEAD(ractl, file, NULL, mapping, index);
1099 page = grab_cache_page_write_begin(mapping, index, flags);
1103 if (ops->check_write_begin) {
1104 /* Allow the netfs (eg. ceph) to flush conflicts. */
1105 ret = ops->check_write_begin(file, pos, len, page, _fsdata);
1107 trace_netfs_failure(NULL, NULL, ret, netfs_fail_check_write_begin);
1114 if (PageUptodate(page))
1117 /* If the page is beyond the EOF, we want to clear it - unless it's
1118 * within the cache granule containing the EOF, in which case we need
1119 * to preload the granule.
1121 if (!ops->is_cache_enabled(inode) &&
1122 netfs_skip_page_read(page, pos, len)) {
1123 netfs_stat(&netfs_n_rh_write_zskip);
1124 goto have_page_no_wait;
1128 rreq = netfs_alloc_read_request(ops, netfs_priv, file);
1131 rreq->mapping = page->mapping;
1132 rreq->start = page_offset(page);
1133 rreq->len = thp_size(page);
1134 rreq->no_unlock_page = page->index;
1135 __set_bit(NETFS_RREQ_NO_UNLOCK_PAGE, &rreq->flags);
1138 if (ops->begin_cache_operation) {
1139 ret = ops->begin_cache_operation(rreq);
1140 if (ret == -ENOMEM || ret == -EINTR || ret == -ERESTARTSYS)
1144 netfs_stat(&netfs_n_rh_write_begin);
1145 trace_netfs_read(rreq, pos, len, netfs_read_trace_write_begin);
1147 /* Expand the request to meet caching requirements and download
1150 ractl._nr_pages = thp_nr_pages(page);
1151 netfs_rreq_expand(rreq, &ractl);
1152 netfs_get_read_request(rreq);
1154 /* We hold the page locks, so we can drop the references */
1155 while ((xpage = readahead_page(&ractl)))
1159 atomic_set(&rreq->nr_rd_ops, 1);
1161 if (!netfs_rreq_submit_slice(rreq, &debug_index))
1164 } while (rreq->submitted < rreq->len);
1166 /* Keep nr_rd_ops incremented so that the ref always belongs to us, and
1167 * the service code isn't punted off to a random thread pool to
1171 wait_var_event(&rreq->nr_rd_ops, atomic_read(&rreq->nr_rd_ops) == 1);
1172 netfs_rreq_assess(rreq, false);
1173 if (!test_bit(NETFS_RREQ_IN_PROGRESS, &rreq->flags))
1179 if (ret == 0 && rreq->submitted < rreq->len) {
1180 trace_netfs_failure(rreq, NULL, ret, netfs_fail_short_write_begin);
1183 netfs_put_read_request(rreq, false);
1188 ret = wait_on_page_fscache_killable(page);
1193 ops->cleanup(netfs_priv, mapping);
1199 netfs_put_read_request(rreq, false);
1204 ops->cleanup(netfs_priv, mapping);
1205 _leave(" = %d", ret);
1208 EXPORT_SYMBOL(netfs_write_begin);