xref: /openbmc/linux/fs/netfs/io.c (revision 7ae9fb1b7ecbb5d85d07857943f677fd1a559b18)
13be01750SDavid Howells // SPDX-License-Identifier: GPL-2.0-or-later
23be01750SDavid Howells /* Network filesystem high-level read support.
33be01750SDavid Howells  *
43be01750SDavid Howells  * Copyright (C) 2021 Red Hat, Inc. All Rights Reserved.
53be01750SDavid Howells  * Written by David Howells (dhowells@redhat.com)
63be01750SDavid Howells  */
73be01750SDavid Howells 
83be01750SDavid Howells #include <linux/module.h>
93be01750SDavid Howells #include <linux/export.h>
103be01750SDavid Howells #include <linux/fs.h>
113be01750SDavid Howells #include <linux/mm.h>
123be01750SDavid Howells #include <linux/pagemap.h>
133be01750SDavid Howells #include <linux/slab.h>
143be01750SDavid Howells #include <linux/uio.h>
153be01750SDavid Howells #include <linux/sched/mm.h>
163be01750SDavid Howells #include <linux/task_io_accounting_ops.h>
173be01750SDavid Howells #include "internal.h"
183be01750SDavid Howells 
193be01750SDavid Howells /*
203be01750SDavid Howells  * Clear the unread part of an I/O request.
213be01750SDavid Howells  */
netfs_clear_unread(struct netfs_io_subrequest * subreq)223be01750SDavid Howells static void netfs_clear_unread(struct netfs_io_subrequest *subreq)
233be01750SDavid Howells {
243be01750SDavid Howells 	struct iov_iter iter;
253be01750SDavid Howells 
26*de4eda9dSAl Viro 	iov_iter_xarray(&iter, ITER_DEST, &subreq->rreq->mapping->i_pages,
273be01750SDavid Howells 			subreq->start + subreq->transferred,
283be01750SDavid Howells 			subreq->len   - subreq->transferred);
293be01750SDavid Howells 	iov_iter_zero(iov_iter_count(&iter), &iter);
303be01750SDavid Howells }
313be01750SDavid Howells 
netfs_cache_read_terminated(void * priv,ssize_t transferred_or_error,bool was_async)323be01750SDavid Howells static void netfs_cache_read_terminated(void *priv, ssize_t transferred_or_error,
333be01750SDavid Howells 					bool was_async)
343be01750SDavid Howells {
353be01750SDavid Howells 	struct netfs_io_subrequest *subreq = priv;
363be01750SDavid Howells 
373be01750SDavid Howells 	netfs_subreq_terminated(subreq, transferred_or_error, was_async);
383be01750SDavid Howells }
393be01750SDavid Howells 
403be01750SDavid Howells /*
413be01750SDavid Howells  * Issue a read against the cache.
423be01750SDavid Howells  * - Eats the caller's ref on subreq.
433be01750SDavid Howells  */
netfs_read_from_cache(struct netfs_io_request * rreq,struct netfs_io_subrequest * subreq,enum netfs_read_from_hole read_hole)443be01750SDavid Howells static void netfs_read_from_cache(struct netfs_io_request *rreq,
453be01750SDavid Howells 				  struct netfs_io_subrequest *subreq,
463be01750SDavid Howells 				  enum netfs_read_from_hole read_hole)
473be01750SDavid Howells {
483be01750SDavid Howells 	struct netfs_cache_resources *cres = &rreq->cache_resources;
493be01750SDavid Howells 	struct iov_iter iter;
503be01750SDavid Howells 
513be01750SDavid Howells 	netfs_stat(&netfs_n_rh_read);
52*de4eda9dSAl Viro 	iov_iter_xarray(&iter, ITER_DEST, &rreq->mapping->i_pages,
533be01750SDavid Howells 			subreq->start + subreq->transferred,
543be01750SDavid Howells 			subreq->len   - subreq->transferred);
553be01750SDavid Howells 
563be01750SDavid Howells 	cres->ops->read(cres, subreq->start, &iter, read_hole,
573be01750SDavid Howells 			netfs_cache_read_terminated, subreq);
583be01750SDavid Howells }
593be01750SDavid Howells 
603be01750SDavid Howells /*
613be01750SDavid Howells  * Fill a subrequest region with zeroes.
623be01750SDavid Howells  */
netfs_fill_with_zeroes(struct netfs_io_request * rreq,struct netfs_io_subrequest * subreq)633be01750SDavid Howells static void netfs_fill_with_zeroes(struct netfs_io_request *rreq,
643be01750SDavid Howells 				   struct netfs_io_subrequest *subreq)
653be01750SDavid Howells {
663be01750SDavid Howells 	netfs_stat(&netfs_n_rh_zero);
673be01750SDavid Howells 	__set_bit(NETFS_SREQ_CLEAR_TAIL, &subreq->flags);
683be01750SDavid Howells 	netfs_subreq_terminated(subreq, 0, false);
693be01750SDavid Howells }
703be01750SDavid Howells 
713be01750SDavid Howells /*
723be01750SDavid Howells  * Ask the netfs to issue a read request to the server for us.
733be01750SDavid Howells  *
743be01750SDavid Howells  * The netfs is expected to read from subreq->pos + subreq->transferred to
753be01750SDavid Howells  * subreq->pos + subreq->len - 1.  It may not backtrack and write data into the
763be01750SDavid Howells  * buffer prior to the transferred point as it might clobber dirty data
773be01750SDavid Howells  * obtained from the cache.
783be01750SDavid Howells  *
793be01750SDavid Howells  * Alternatively, the netfs is allowed to indicate one of two things:
803be01750SDavid Howells  *
813be01750SDavid Howells  * - NETFS_SREQ_SHORT_READ: A short read - it will get called again to try and
823be01750SDavid Howells  *   make progress.
833be01750SDavid Howells  *
843be01750SDavid Howells  * - NETFS_SREQ_CLEAR_TAIL: A short read - the rest of the buffer will be
853be01750SDavid Howells  *   cleared.
863be01750SDavid Howells  */
netfs_read_from_server(struct netfs_io_request * rreq,struct netfs_io_subrequest * subreq)873be01750SDavid Howells static void netfs_read_from_server(struct netfs_io_request *rreq,
883be01750SDavid Howells 				   struct netfs_io_subrequest *subreq)
893be01750SDavid Howells {
903be01750SDavid Howells 	netfs_stat(&netfs_n_rh_download);
913be01750SDavid Howells 	rreq->netfs_ops->issue_read(subreq);
923be01750SDavid Howells }
933be01750SDavid Howells 
943be01750SDavid Howells /*
953be01750SDavid Howells  * Release those waiting.
963be01750SDavid Howells  */
netfs_rreq_completed(struct netfs_io_request * rreq,bool was_async)973be01750SDavid Howells static void netfs_rreq_completed(struct netfs_io_request *rreq, bool was_async)
983be01750SDavid Howells {
993be01750SDavid Howells 	trace_netfs_rreq(rreq, netfs_rreq_trace_done);
1003be01750SDavid Howells 	netfs_clear_subrequests(rreq, was_async);
1013be01750SDavid Howells 	netfs_put_request(rreq, was_async, netfs_rreq_trace_put_complete);
1023be01750SDavid Howells }
1033be01750SDavid Howells 
1043be01750SDavid Howells /*
1053be01750SDavid Howells  * Deal with the completion of writing the data to the cache.  We have to clear
1063be01750SDavid Howells  * the PG_fscache bits on the folios involved and release the caller's ref.
1073be01750SDavid Howells  *
1083be01750SDavid Howells  * May be called in softirq mode and we inherit a ref from the caller.
1093be01750SDavid Howells  */
netfs_rreq_unmark_after_write(struct netfs_io_request * rreq,bool was_async)1103be01750SDavid Howells static void netfs_rreq_unmark_after_write(struct netfs_io_request *rreq,
1113be01750SDavid Howells 					  bool was_async)
1123be01750SDavid Howells {
1133be01750SDavid Howells 	struct netfs_io_subrequest *subreq;
1143be01750SDavid Howells 	struct folio *folio;
1153be01750SDavid Howells 	pgoff_t unlocked = 0;
1163be01750SDavid Howells 	bool have_unlocked = false;
1173be01750SDavid Howells 
1183be01750SDavid Howells 	rcu_read_lock();
1193be01750SDavid Howells 
1203be01750SDavid Howells 	list_for_each_entry(subreq, &rreq->subrequests, rreq_link) {
1213be01750SDavid Howells 		XA_STATE(xas, &rreq->mapping->i_pages, subreq->start / PAGE_SIZE);
1223be01750SDavid Howells 
1233be01750SDavid Howells 		xas_for_each(&xas, folio, (subreq->start + subreq->len - 1) / PAGE_SIZE) {
1247e043a80SDavid Howells 			if (xas_retry(&xas, folio))
1257e043a80SDavid Howells 				continue;
1267e043a80SDavid Howells 
1273be01750SDavid Howells 			/* We might have multiple writes from the same huge
1283be01750SDavid Howells 			 * folio, but we mustn't unlock a folio more than once.
1293be01750SDavid Howells 			 */
1303be01750SDavid Howells 			if (have_unlocked && folio_index(folio) <= unlocked)
1313be01750SDavid Howells 				continue;
1323be01750SDavid Howells 			unlocked = folio_index(folio);
1333be01750SDavid Howells 			folio_end_fscache(folio);
1343be01750SDavid Howells 			have_unlocked = true;
1353be01750SDavid Howells 		}
1363be01750SDavid Howells 	}
1373be01750SDavid Howells 
1383be01750SDavid Howells 	rcu_read_unlock();
1393be01750SDavid Howells 	netfs_rreq_completed(rreq, was_async);
1403be01750SDavid Howells }
1413be01750SDavid Howells 
netfs_rreq_copy_terminated(void * priv,ssize_t transferred_or_error,bool was_async)1423be01750SDavid Howells static void netfs_rreq_copy_terminated(void *priv, ssize_t transferred_or_error,
1433be01750SDavid Howells 				       bool was_async)
1443be01750SDavid Howells {
1453be01750SDavid Howells 	struct netfs_io_subrequest *subreq = priv;
1463be01750SDavid Howells 	struct netfs_io_request *rreq = subreq->rreq;
1473be01750SDavid Howells 
1483be01750SDavid Howells 	if (IS_ERR_VALUE(transferred_or_error)) {
1493be01750SDavid Howells 		netfs_stat(&netfs_n_rh_write_failed);
1503be01750SDavid Howells 		trace_netfs_failure(rreq, subreq, transferred_or_error,
1513be01750SDavid Howells 				    netfs_fail_copy_to_cache);
1523be01750SDavid Howells 	} else {
1533be01750SDavid Howells 		netfs_stat(&netfs_n_rh_write_done);
1543be01750SDavid Howells 	}
1553be01750SDavid Howells 
1563be01750SDavid Howells 	trace_netfs_sreq(subreq, netfs_sreq_trace_write_term);
1573be01750SDavid Howells 
1583be01750SDavid Howells 	/* If we decrement nr_copy_ops to 0, the ref belongs to us. */
1593be01750SDavid Howells 	if (atomic_dec_and_test(&rreq->nr_copy_ops))
1603be01750SDavid Howells 		netfs_rreq_unmark_after_write(rreq, was_async);
1613be01750SDavid Howells 
1623be01750SDavid Howells 	netfs_put_subrequest(subreq, was_async, netfs_sreq_trace_put_terminated);
1633be01750SDavid Howells }
1643be01750SDavid Howells 
1653be01750SDavid Howells /*
1663be01750SDavid Howells  * Perform any outstanding writes to the cache.  We inherit a ref from the
1673be01750SDavid Howells  * caller.
1683be01750SDavid Howells  */
netfs_rreq_do_write_to_cache(struct netfs_io_request * rreq)1693be01750SDavid Howells static void netfs_rreq_do_write_to_cache(struct netfs_io_request *rreq)
1703be01750SDavid Howells {
1713be01750SDavid Howells 	struct netfs_cache_resources *cres = &rreq->cache_resources;
1723be01750SDavid Howells 	struct netfs_io_subrequest *subreq, *next, *p;
1733be01750SDavid Howells 	struct iov_iter iter;
1743be01750SDavid Howells 	int ret;
1753be01750SDavid Howells 
1763be01750SDavid Howells 	trace_netfs_rreq(rreq, netfs_rreq_trace_copy);
1773be01750SDavid Howells 
1783be01750SDavid Howells 	/* We don't want terminating writes trying to wake us up whilst we're
1793be01750SDavid Howells 	 * still going through the list.
1803be01750SDavid Howells 	 */
1813be01750SDavid Howells 	atomic_inc(&rreq->nr_copy_ops);
1823be01750SDavid Howells 
1833be01750SDavid Howells 	list_for_each_entry_safe(subreq, p, &rreq->subrequests, rreq_link) {
1843be01750SDavid Howells 		if (!test_bit(NETFS_SREQ_COPY_TO_CACHE, &subreq->flags)) {
1853be01750SDavid Howells 			list_del_init(&subreq->rreq_link);
1863be01750SDavid Howells 			netfs_put_subrequest(subreq, false,
1873be01750SDavid Howells 					     netfs_sreq_trace_put_no_copy);
1883be01750SDavid Howells 		}
1893be01750SDavid Howells 	}
1903be01750SDavid Howells 
1913be01750SDavid Howells 	list_for_each_entry(subreq, &rreq->subrequests, rreq_link) {
1923be01750SDavid Howells 		/* Amalgamate adjacent writes */
1933be01750SDavid Howells 		while (!list_is_last(&subreq->rreq_link, &rreq->subrequests)) {
1943be01750SDavid Howells 			next = list_next_entry(subreq, rreq_link);
1953be01750SDavid Howells 			if (next->start != subreq->start + subreq->len)
1963be01750SDavid Howells 				break;
1973be01750SDavid Howells 			subreq->len += next->len;
1983be01750SDavid Howells 			list_del_init(&next->rreq_link);
1993be01750SDavid Howells 			netfs_put_subrequest(next, false,
2003be01750SDavid Howells 					     netfs_sreq_trace_put_merged);
2013be01750SDavid Howells 		}
2023be01750SDavid Howells 
2033be01750SDavid Howells 		ret = cres->ops->prepare_write(cres, &subreq->start, &subreq->len,
2043be01750SDavid Howells 					       rreq->i_size, true);
2053be01750SDavid Howells 		if (ret < 0) {
2063be01750SDavid Howells 			trace_netfs_failure(rreq, subreq, ret, netfs_fail_prepare_write);
2073be01750SDavid Howells 			trace_netfs_sreq(subreq, netfs_sreq_trace_write_skip);
2083be01750SDavid Howells 			continue;
2093be01750SDavid Howells 		}
2103be01750SDavid Howells 
211*de4eda9dSAl Viro 		iov_iter_xarray(&iter, ITER_SOURCE, &rreq->mapping->i_pages,
2123be01750SDavid Howells 				subreq->start, subreq->len);
2133be01750SDavid Howells 
2143be01750SDavid Howells 		atomic_inc(&rreq->nr_copy_ops);
2153be01750SDavid Howells 		netfs_stat(&netfs_n_rh_write);
2163be01750SDavid Howells 		netfs_get_subrequest(subreq, netfs_sreq_trace_get_copy_to_cache);
2173be01750SDavid Howells 		trace_netfs_sreq(subreq, netfs_sreq_trace_write);
2183be01750SDavid Howells 		cres->ops->write(cres, subreq->start, &iter,
2193be01750SDavid Howells 				 netfs_rreq_copy_terminated, subreq);
2203be01750SDavid Howells 	}
2213be01750SDavid Howells 
2223be01750SDavid Howells 	/* If we decrement nr_copy_ops to 0, the usage ref belongs to us. */
2233be01750SDavid Howells 	if (atomic_dec_and_test(&rreq->nr_copy_ops))
2243be01750SDavid Howells 		netfs_rreq_unmark_after_write(rreq, false);
2253be01750SDavid Howells }
2263be01750SDavid Howells 
netfs_rreq_write_to_cache_work(struct work_struct * work)2273be01750SDavid Howells static void netfs_rreq_write_to_cache_work(struct work_struct *work)
2283be01750SDavid Howells {
2293be01750SDavid Howells 	struct netfs_io_request *rreq =
2303be01750SDavid Howells 		container_of(work, struct netfs_io_request, work);
2313be01750SDavid Howells 
2323be01750SDavid Howells 	netfs_rreq_do_write_to_cache(rreq);
2333be01750SDavid Howells }
2343be01750SDavid Howells 
netfs_rreq_write_to_cache(struct netfs_io_request * rreq)2353be01750SDavid Howells static void netfs_rreq_write_to_cache(struct netfs_io_request *rreq)
2363be01750SDavid Howells {
2373be01750SDavid Howells 	rreq->work.func = netfs_rreq_write_to_cache_work;
2383be01750SDavid Howells 	if (!queue_work(system_unbound_wq, &rreq->work))
2393be01750SDavid Howells 		BUG();
2403be01750SDavid Howells }
2413be01750SDavid Howells 
2423be01750SDavid Howells /*
2433be01750SDavid Howells  * Handle a short read.
2443be01750SDavid Howells  */
netfs_rreq_short_read(struct netfs_io_request * rreq,struct netfs_io_subrequest * subreq)2453be01750SDavid Howells static void netfs_rreq_short_read(struct netfs_io_request *rreq,
2463be01750SDavid Howells 				  struct netfs_io_subrequest *subreq)
2473be01750SDavid Howells {
2483be01750SDavid Howells 	__clear_bit(NETFS_SREQ_SHORT_IO, &subreq->flags);
2493be01750SDavid Howells 	__set_bit(NETFS_SREQ_SEEK_DATA_READ, &subreq->flags);
2503be01750SDavid Howells 
2513be01750SDavid Howells 	netfs_stat(&netfs_n_rh_short_read);
2523be01750SDavid Howells 	trace_netfs_sreq(subreq, netfs_sreq_trace_resubmit_short);
2533be01750SDavid Howells 
2543be01750SDavid Howells 	netfs_get_subrequest(subreq, netfs_sreq_trace_get_short_read);
2553be01750SDavid Howells 	atomic_inc(&rreq->nr_outstanding);
2563be01750SDavid Howells 	if (subreq->source == NETFS_READ_FROM_CACHE)
2573be01750SDavid Howells 		netfs_read_from_cache(rreq, subreq, NETFS_READ_HOLE_CLEAR);
2583be01750SDavid Howells 	else
2593be01750SDavid Howells 		netfs_read_from_server(rreq, subreq);
2603be01750SDavid Howells }
2613be01750SDavid Howells 
2623be01750SDavid Howells /*
2633be01750SDavid Howells  * Resubmit any short or failed operations.  Returns true if we got the rreq
2643be01750SDavid Howells  * ref back.
2653be01750SDavid Howells  */
netfs_rreq_perform_resubmissions(struct netfs_io_request * rreq)2663be01750SDavid Howells static bool netfs_rreq_perform_resubmissions(struct netfs_io_request *rreq)
2673be01750SDavid Howells {
2683be01750SDavid Howells 	struct netfs_io_subrequest *subreq;
2693be01750SDavid Howells 
2703be01750SDavid Howells 	WARN_ON(in_interrupt());
2713be01750SDavid Howells 
2723be01750SDavid Howells 	trace_netfs_rreq(rreq, netfs_rreq_trace_resubmit);
2733be01750SDavid Howells 
2743be01750SDavid Howells 	/* We don't want terminating submissions trying to wake us up whilst
2753be01750SDavid Howells 	 * we're still going through the list.
2763be01750SDavid Howells 	 */
2773be01750SDavid Howells 	atomic_inc(&rreq->nr_outstanding);
2783be01750SDavid Howells 
2793be01750SDavid Howells 	__clear_bit(NETFS_RREQ_INCOMPLETE_IO, &rreq->flags);
2803be01750SDavid Howells 	list_for_each_entry(subreq, &rreq->subrequests, rreq_link) {
2813be01750SDavid Howells 		if (subreq->error) {
2823be01750SDavid Howells 			if (subreq->source != NETFS_READ_FROM_CACHE)
2833be01750SDavid Howells 				break;
2843be01750SDavid Howells 			subreq->source = NETFS_DOWNLOAD_FROM_SERVER;
2853be01750SDavid Howells 			subreq->error = 0;
2863be01750SDavid Howells 			netfs_stat(&netfs_n_rh_download_instead);
2873be01750SDavid Howells 			trace_netfs_sreq(subreq, netfs_sreq_trace_download_instead);
2883be01750SDavid Howells 			netfs_get_subrequest(subreq, netfs_sreq_trace_get_resubmit);
2893be01750SDavid Howells 			atomic_inc(&rreq->nr_outstanding);
2903be01750SDavid Howells 			netfs_read_from_server(rreq, subreq);
2913be01750SDavid Howells 		} else if (test_bit(NETFS_SREQ_SHORT_IO, &subreq->flags)) {
2923be01750SDavid Howells 			netfs_rreq_short_read(rreq, subreq);
2933be01750SDavid Howells 		}
2943be01750SDavid Howells 	}
2953be01750SDavid Howells 
2963be01750SDavid Howells 	/* If we decrement nr_outstanding to 0, the usage ref belongs to us. */
2973be01750SDavid Howells 	if (atomic_dec_and_test(&rreq->nr_outstanding))
2983be01750SDavid Howells 		return true;
2993be01750SDavid Howells 
3003be01750SDavid Howells 	wake_up_var(&rreq->nr_outstanding);
3013be01750SDavid Howells 	return false;
3023be01750SDavid Howells }
3033be01750SDavid Howells 
3043be01750SDavid Howells /*
3053be01750SDavid Howells  * Check to see if the data read is still valid.
3063be01750SDavid Howells  */
netfs_rreq_is_still_valid(struct netfs_io_request * rreq)3073be01750SDavid Howells static void netfs_rreq_is_still_valid(struct netfs_io_request *rreq)
3083be01750SDavid Howells {
3093be01750SDavid Howells 	struct netfs_io_subrequest *subreq;
3103be01750SDavid Howells 
3113be01750SDavid Howells 	if (!rreq->netfs_ops->is_still_valid ||
3123be01750SDavid Howells 	    rreq->netfs_ops->is_still_valid(rreq))
3133be01750SDavid Howells 		return;
3143be01750SDavid Howells 
3153be01750SDavid Howells 	list_for_each_entry(subreq, &rreq->subrequests, rreq_link) {
3163be01750SDavid Howells 		if (subreq->source == NETFS_READ_FROM_CACHE) {
3173be01750SDavid Howells 			subreq->error = -ESTALE;
3183be01750SDavid Howells 			__set_bit(NETFS_RREQ_INCOMPLETE_IO, &rreq->flags);
3193be01750SDavid Howells 		}
3203be01750SDavid Howells 	}
3213be01750SDavid Howells }
3223be01750SDavid Howells 
3233be01750SDavid Howells /*
3243be01750SDavid Howells  * Assess the state of a read request and decide what to do next.
3253be01750SDavid Howells  *
3263be01750SDavid Howells  * Note that we could be in an ordinary kernel thread, on a workqueue or in
3273be01750SDavid Howells  * softirq context at this point.  We inherit a ref from the caller.
3283be01750SDavid Howells  */
netfs_rreq_assess(struct netfs_io_request * rreq,bool was_async)3293be01750SDavid Howells static void netfs_rreq_assess(struct netfs_io_request *rreq, bool was_async)
3303be01750SDavid Howells {
3313be01750SDavid Howells 	trace_netfs_rreq(rreq, netfs_rreq_trace_assess);
3323be01750SDavid Howells 
3333be01750SDavid Howells again:
3343be01750SDavid Howells 	netfs_rreq_is_still_valid(rreq);
3353be01750SDavid Howells 
3363be01750SDavid Howells 	if (!test_bit(NETFS_RREQ_FAILED, &rreq->flags) &&
3373be01750SDavid Howells 	    test_bit(NETFS_RREQ_INCOMPLETE_IO, &rreq->flags)) {
3383be01750SDavid Howells 		if (netfs_rreq_perform_resubmissions(rreq))
3393be01750SDavid Howells 			goto again;
3403be01750SDavid Howells 		return;
3413be01750SDavid Howells 	}
3423be01750SDavid Howells 
3433be01750SDavid Howells 	netfs_rreq_unlock_folios(rreq);
3443be01750SDavid Howells 
3453be01750SDavid Howells 	clear_bit_unlock(NETFS_RREQ_IN_PROGRESS, &rreq->flags);
3463be01750SDavid Howells 	wake_up_bit(&rreq->flags, NETFS_RREQ_IN_PROGRESS);
3473be01750SDavid Howells 
3483be01750SDavid Howells 	if (test_bit(NETFS_RREQ_COPY_TO_CACHE, &rreq->flags))
3493be01750SDavid Howells 		return netfs_rreq_write_to_cache(rreq);
3503be01750SDavid Howells 
3513be01750SDavid Howells 	netfs_rreq_completed(rreq, was_async);
3523be01750SDavid Howells }
3533be01750SDavid Howells 
netfs_rreq_work(struct work_struct * work)3543be01750SDavid Howells static void netfs_rreq_work(struct work_struct *work)
3553be01750SDavid Howells {
3563be01750SDavid Howells 	struct netfs_io_request *rreq =
3573be01750SDavid Howells 		container_of(work, struct netfs_io_request, work);
3583be01750SDavid Howells 	netfs_rreq_assess(rreq, false);
3593be01750SDavid Howells }
3603be01750SDavid Howells 
3613be01750SDavid Howells /*
3623be01750SDavid Howells  * Handle the completion of all outstanding I/O operations on a read request.
3633be01750SDavid Howells  * We inherit a ref from the caller.
3643be01750SDavid Howells  */
netfs_rreq_terminated(struct netfs_io_request * rreq,bool was_async)3653be01750SDavid Howells static void netfs_rreq_terminated(struct netfs_io_request *rreq,
3663be01750SDavid Howells 				  bool was_async)
3673be01750SDavid Howells {
3683be01750SDavid Howells 	if (test_bit(NETFS_RREQ_INCOMPLETE_IO, &rreq->flags) &&
3693be01750SDavid Howells 	    was_async) {
3703be01750SDavid Howells 		if (!queue_work(system_unbound_wq, &rreq->work))
3713be01750SDavid Howells 			BUG();
3723be01750SDavid Howells 	} else {
3733be01750SDavid Howells 		netfs_rreq_assess(rreq, was_async);
3743be01750SDavid Howells 	}
3753be01750SDavid Howells }
3763be01750SDavid Howells 
3773be01750SDavid Howells /**
3783be01750SDavid Howells  * netfs_subreq_terminated - Note the termination of an I/O operation.
3793be01750SDavid Howells  * @subreq: The I/O request that has terminated.
3803be01750SDavid Howells  * @transferred_or_error: The amount of data transferred or an error code.
3813be01750SDavid Howells  * @was_async: The termination was asynchronous
3823be01750SDavid Howells  *
3833be01750SDavid Howells  * This tells the read helper that a contributory I/O operation has terminated,
3843be01750SDavid Howells  * one way or another, and that it should integrate the results.
3853be01750SDavid Howells  *
3863be01750SDavid Howells  * The caller indicates in @transferred_or_error the outcome of the operation,
3873be01750SDavid Howells  * supplying a positive value to indicate the number of bytes transferred, 0 to
3883be01750SDavid Howells  * indicate a failure to transfer anything that should be retried or a negative
3893be01750SDavid Howells  * error code.  The helper will look after reissuing I/O operations as
3903be01750SDavid Howells  * appropriate and writing downloaded data to the cache.
3913be01750SDavid Howells  *
3923be01750SDavid Howells  * If @was_async is true, the caller might be running in softirq or interrupt
3933be01750SDavid Howells  * context and we can't sleep.
3943be01750SDavid Howells  */
netfs_subreq_terminated(struct netfs_io_subrequest * subreq,ssize_t transferred_or_error,bool was_async)3953be01750SDavid Howells void netfs_subreq_terminated(struct netfs_io_subrequest *subreq,
3963be01750SDavid Howells 			     ssize_t transferred_or_error,
3973be01750SDavid Howells 			     bool was_async)
3983be01750SDavid Howells {
3993be01750SDavid Howells 	struct netfs_io_request *rreq = subreq->rreq;
4003be01750SDavid Howells 	int u;
4013be01750SDavid Howells 
4023be01750SDavid Howells 	_enter("[%u]{%llx,%lx},%zd",
4033be01750SDavid Howells 	       subreq->debug_index, subreq->start, subreq->flags,
4043be01750SDavid Howells 	       transferred_or_error);
4053be01750SDavid Howells 
4063be01750SDavid Howells 	switch (subreq->source) {
4073be01750SDavid Howells 	case NETFS_READ_FROM_CACHE:
4083be01750SDavid Howells 		netfs_stat(&netfs_n_rh_read_done);
4093be01750SDavid Howells 		break;
4103be01750SDavid Howells 	case NETFS_DOWNLOAD_FROM_SERVER:
4113be01750SDavid Howells 		netfs_stat(&netfs_n_rh_download_done);
4123be01750SDavid Howells 		break;
4133be01750SDavid Howells 	default:
4143be01750SDavid Howells 		break;
4153be01750SDavid Howells 	}
4163be01750SDavid Howells 
4173be01750SDavid Howells 	if (IS_ERR_VALUE(transferred_or_error)) {
4183be01750SDavid Howells 		subreq->error = transferred_or_error;
4193be01750SDavid Howells 		trace_netfs_failure(rreq, subreq, transferred_or_error,
4203be01750SDavid Howells 				    netfs_fail_read);
4213be01750SDavid Howells 		goto failed;
4223be01750SDavid Howells 	}
4233be01750SDavid Howells 
4243be01750SDavid Howells 	if (WARN(transferred_or_error > subreq->len - subreq->transferred,
4253be01750SDavid Howells 		 "Subreq overread: R%x[%x] %zd > %zu - %zu",
4263be01750SDavid Howells 		 rreq->debug_id, subreq->debug_index,
4273be01750SDavid Howells 		 transferred_or_error, subreq->len, subreq->transferred))
4283be01750SDavid Howells 		transferred_or_error = subreq->len - subreq->transferred;
4293be01750SDavid Howells 
4303be01750SDavid Howells 	subreq->error = 0;
4313be01750SDavid Howells 	subreq->transferred += transferred_or_error;
4323be01750SDavid Howells 	if (subreq->transferred < subreq->len)
4333be01750SDavid Howells 		goto incomplete;
4343be01750SDavid Howells 
4353be01750SDavid Howells complete:
4363be01750SDavid Howells 	__clear_bit(NETFS_SREQ_NO_PROGRESS, &subreq->flags);
4373be01750SDavid Howells 	if (test_bit(NETFS_SREQ_COPY_TO_CACHE, &subreq->flags))
4383be01750SDavid Howells 		set_bit(NETFS_RREQ_COPY_TO_CACHE, &rreq->flags);
4393be01750SDavid Howells 
4403be01750SDavid Howells out:
4413be01750SDavid Howells 	trace_netfs_sreq(subreq, netfs_sreq_trace_terminated);
4423be01750SDavid Howells 
4433be01750SDavid Howells 	/* If we decrement nr_outstanding to 0, the ref belongs to us. */
4443be01750SDavid Howells 	u = atomic_dec_return(&rreq->nr_outstanding);
4453be01750SDavid Howells 	if (u == 0)
4463be01750SDavid Howells 		netfs_rreq_terminated(rreq, was_async);
4473be01750SDavid Howells 	else if (u == 1)
4483be01750SDavid Howells 		wake_up_var(&rreq->nr_outstanding);
4493be01750SDavid Howells 
4503be01750SDavid Howells 	netfs_put_subrequest(subreq, was_async, netfs_sreq_trace_put_terminated);
4513be01750SDavid Howells 	return;
4523be01750SDavid Howells 
4533be01750SDavid Howells incomplete:
4543be01750SDavid Howells 	if (test_bit(NETFS_SREQ_CLEAR_TAIL, &subreq->flags)) {
4553be01750SDavid Howells 		netfs_clear_unread(subreq);
4563be01750SDavid Howells 		subreq->transferred = subreq->len;
4573be01750SDavid Howells 		goto complete;
4583be01750SDavid Howells 	}
4593be01750SDavid Howells 
4603be01750SDavid Howells 	if (transferred_or_error == 0) {
4613be01750SDavid Howells 		if (__test_and_set_bit(NETFS_SREQ_NO_PROGRESS, &subreq->flags)) {
4623be01750SDavid Howells 			subreq->error = -ENODATA;
4633be01750SDavid Howells 			goto failed;
4643be01750SDavid Howells 		}
4653be01750SDavid Howells 	} else {
4663be01750SDavid Howells 		__clear_bit(NETFS_SREQ_NO_PROGRESS, &subreq->flags);
4673be01750SDavid Howells 	}
4683be01750SDavid Howells 
4693be01750SDavid Howells 	__set_bit(NETFS_SREQ_SHORT_IO, &subreq->flags);
4703be01750SDavid Howells 	set_bit(NETFS_RREQ_INCOMPLETE_IO, &rreq->flags);
4713be01750SDavid Howells 	goto out;
4723be01750SDavid Howells 
4733be01750SDavid Howells failed:
4743be01750SDavid Howells 	if (subreq->source == NETFS_READ_FROM_CACHE) {
4753be01750SDavid Howells 		netfs_stat(&netfs_n_rh_read_failed);
4763be01750SDavid Howells 		set_bit(NETFS_RREQ_INCOMPLETE_IO, &rreq->flags);
4773be01750SDavid Howells 	} else {
4783be01750SDavid Howells 		netfs_stat(&netfs_n_rh_download_failed);
4793be01750SDavid Howells 		set_bit(NETFS_RREQ_FAILED, &rreq->flags);
4803be01750SDavid Howells 		rreq->error = subreq->error;
4813be01750SDavid Howells 	}
4823be01750SDavid Howells 	goto out;
4833be01750SDavid Howells }
4843be01750SDavid Howells EXPORT_SYMBOL(netfs_subreq_terminated);
4853be01750SDavid Howells 
netfs_cache_prepare_read(struct netfs_io_subrequest * subreq,loff_t i_size)4863be01750SDavid Howells static enum netfs_io_source netfs_cache_prepare_read(struct netfs_io_subrequest *subreq,
4873be01750SDavid Howells 						       loff_t i_size)
4883be01750SDavid Howells {
4893be01750SDavid Howells 	struct netfs_io_request *rreq = subreq->rreq;
4903be01750SDavid Howells 	struct netfs_cache_resources *cres = &rreq->cache_resources;
4913be01750SDavid Howells 
4923be01750SDavid Howells 	if (cres->ops)
4933be01750SDavid Howells 		return cres->ops->prepare_read(subreq, i_size);
4943be01750SDavid Howells 	if (subreq->start >= rreq->i_size)
4953be01750SDavid Howells 		return NETFS_FILL_WITH_ZEROES;
4963be01750SDavid Howells 	return NETFS_DOWNLOAD_FROM_SERVER;
4973be01750SDavid Howells }
4983be01750SDavid Howells 
4993be01750SDavid Howells /*
5003be01750SDavid Howells  * Work out what sort of subrequest the next one will be.
5013be01750SDavid Howells  */
5023be01750SDavid Howells static enum netfs_io_source
netfs_rreq_prepare_read(struct netfs_io_request * rreq,struct netfs_io_subrequest * subreq)5033be01750SDavid Howells netfs_rreq_prepare_read(struct netfs_io_request *rreq,
5043be01750SDavid Howells 			struct netfs_io_subrequest *subreq)
5053be01750SDavid Howells {
5063be01750SDavid Howells 	enum netfs_io_source source;
5073be01750SDavid Howells 
5083be01750SDavid Howells 	_enter("%llx-%llx,%llx", subreq->start, subreq->start + subreq->len, rreq->i_size);
5093be01750SDavid Howells 
5103be01750SDavid Howells 	source = netfs_cache_prepare_read(subreq, rreq->i_size);
5113be01750SDavid Howells 	if (source == NETFS_INVALID_READ)
5123be01750SDavid Howells 		goto out;
5133be01750SDavid Howells 
5143be01750SDavid Howells 	if (source == NETFS_DOWNLOAD_FROM_SERVER) {
5153be01750SDavid Howells 		/* Call out to the netfs to let it shrink the request to fit
5163be01750SDavid Howells 		 * its own I/O sizes and boundaries.  If it shinks it here, it
5173be01750SDavid Howells 		 * will be called again to make simultaneous calls; if it wants
5183be01750SDavid Howells 		 * to make serial calls, it can indicate a short read and then
5193be01750SDavid Howells 		 * we will call it again.
5203be01750SDavid Howells 		 */
5213be01750SDavid Howells 		if (subreq->len > rreq->i_size - subreq->start)
5223be01750SDavid Howells 			subreq->len = rreq->i_size - subreq->start;
5233be01750SDavid Howells 
5243be01750SDavid Howells 		if (rreq->netfs_ops->clamp_length &&
5253be01750SDavid Howells 		    !rreq->netfs_ops->clamp_length(subreq)) {
5263be01750SDavid Howells 			source = NETFS_INVALID_READ;
5273be01750SDavid Howells 			goto out;
5283be01750SDavid Howells 		}
5293be01750SDavid Howells 	}
5303be01750SDavid Howells 
5313be01750SDavid Howells 	if (WARN_ON(subreq->len == 0))
5323be01750SDavid Howells 		source = NETFS_INVALID_READ;
5333be01750SDavid Howells 
5343be01750SDavid Howells out:
5353be01750SDavid Howells 	subreq->source = source;
5363be01750SDavid Howells 	trace_netfs_sreq(subreq, netfs_sreq_trace_prepare);
5373be01750SDavid Howells 	return source;
5383be01750SDavid Howells }
5393be01750SDavid Howells 
5403be01750SDavid Howells /*
5413be01750SDavid Howells  * Slice off a piece of a read request and submit an I/O request for it.
5423be01750SDavid Howells  */
netfs_rreq_submit_slice(struct netfs_io_request * rreq,unsigned int * _debug_index)5433be01750SDavid Howells static bool netfs_rreq_submit_slice(struct netfs_io_request *rreq,
5443be01750SDavid Howells 				    unsigned int *_debug_index)
5453be01750SDavid Howells {
5463be01750SDavid Howells 	struct netfs_io_subrequest *subreq;
5473be01750SDavid Howells 	enum netfs_io_source source;
5483be01750SDavid Howells 
5493be01750SDavid Howells 	subreq = netfs_alloc_subrequest(rreq);
5503be01750SDavid Howells 	if (!subreq)
5513be01750SDavid Howells 		return false;
5523be01750SDavid Howells 
5533be01750SDavid Howells 	subreq->debug_index	= (*_debug_index)++;
5543be01750SDavid Howells 	subreq->start		= rreq->start + rreq->submitted;
5553be01750SDavid Howells 	subreq->len		= rreq->len   - rreq->submitted;
5563be01750SDavid Howells 
5573be01750SDavid Howells 	_debug("slice %llx,%zx,%zx", subreq->start, subreq->len, rreq->submitted);
5583be01750SDavid Howells 	list_add_tail(&subreq->rreq_link, &rreq->subrequests);
5593be01750SDavid Howells 
5603be01750SDavid Howells 	/* Call out to the cache to find out what it can do with the remaining
5613be01750SDavid Howells 	 * subset.  It tells us in subreq->flags what it decided should be done
5623be01750SDavid Howells 	 * and adjusts subreq->len down if the subset crosses a cache boundary.
5633be01750SDavid Howells 	 *
5643be01750SDavid Howells 	 * Then when we hand the subset, it can choose to take a subset of that
5653be01750SDavid Howells 	 * (the starts must coincide), in which case, we go around the loop
5663be01750SDavid Howells 	 * again and ask it to download the next piece.
5673be01750SDavid Howells 	 */
5683be01750SDavid Howells 	source = netfs_rreq_prepare_read(rreq, subreq);
5693be01750SDavid Howells 	if (source == NETFS_INVALID_READ)
5703be01750SDavid Howells 		goto subreq_failed;
5713be01750SDavid Howells 
5723be01750SDavid Howells 	atomic_inc(&rreq->nr_outstanding);
5733be01750SDavid Howells 
5743be01750SDavid Howells 	rreq->submitted += subreq->len;
5753be01750SDavid Howells 
5763be01750SDavid Howells 	trace_netfs_sreq(subreq, netfs_sreq_trace_submit);
5773be01750SDavid Howells 	switch (source) {
5783be01750SDavid Howells 	case NETFS_FILL_WITH_ZEROES:
5793be01750SDavid Howells 		netfs_fill_with_zeroes(rreq, subreq);
5803be01750SDavid Howells 		break;
5813be01750SDavid Howells 	case NETFS_DOWNLOAD_FROM_SERVER:
5823be01750SDavid Howells 		netfs_read_from_server(rreq, subreq);
5833be01750SDavid Howells 		break;
5843be01750SDavid Howells 	case NETFS_READ_FROM_CACHE:
5853be01750SDavid Howells 		netfs_read_from_cache(rreq, subreq, NETFS_READ_HOLE_IGNORE);
5863be01750SDavid Howells 		break;
5873be01750SDavid Howells 	default:
5883be01750SDavid Howells 		BUG();
5893be01750SDavid Howells 	}
5903be01750SDavid Howells 
5913be01750SDavid Howells 	return true;
5923be01750SDavid Howells 
5933be01750SDavid Howells subreq_failed:
5943be01750SDavid Howells 	rreq->error = subreq->error;
5953be01750SDavid Howells 	netfs_put_subrequest(subreq, false, netfs_sreq_trace_put_failed);
5963be01750SDavid Howells 	return false;
5973be01750SDavid Howells }
5983be01750SDavid Howells 
5993be01750SDavid Howells /*
6003be01750SDavid Howells  * Begin the process of reading in a chunk of data, where that data may be
6013be01750SDavid Howells  * stitched together from multiple sources, including multiple servers and the
6023be01750SDavid Howells  * local cache.
6033be01750SDavid Howells  */
netfs_begin_read(struct netfs_io_request * rreq,bool sync)6043be01750SDavid Howells int netfs_begin_read(struct netfs_io_request *rreq, bool sync)
6053be01750SDavid Howells {
6063be01750SDavid Howells 	unsigned int debug_index = 0;
6073be01750SDavid Howells 	int ret;
6083be01750SDavid Howells 
6093be01750SDavid Howells 	_enter("R=%x %llx-%llx",
6103be01750SDavid Howells 	       rreq->debug_id, rreq->start, rreq->start + rreq->len - 1);
6113be01750SDavid Howells 
6123be01750SDavid Howells 	if (rreq->len == 0) {
6133be01750SDavid Howells 		pr_err("Zero-sized read [R=%x]\n", rreq->debug_id);
6143be01750SDavid Howells 		netfs_put_request(rreq, false, netfs_rreq_trace_put_zero_len);
6153be01750SDavid Howells 		return -EIO;
6163be01750SDavid Howells 	}
6173be01750SDavid Howells 
6183be01750SDavid Howells 	INIT_WORK(&rreq->work, netfs_rreq_work);
6193be01750SDavid Howells 
6203be01750SDavid Howells 	if (sync)
6213be01750SDavid Howells 		netfs_get_request(rreq, netfs_rreq_trace_get_hold);
6223be01750SDavid Howells 
6233be01750SDavid Howells 	/* Chop the read into slices according to what the cache and the netfs
6243be01750SDavid Howells 	 * want and submit each one.
6253be01750SDavid Howells 	 */
6263be01750SDavid Howells 	atomic_set(&rreq->nr_outstanding, 1);
6273be01750SDavid Howells 	do {
6283be01750SDavid Howells 		if (!netfs_rreq_submit_slice(rreq, &debug_index))
6293be01750SDavid Howells 			break;
6303be01750SDavid Howells 
6313be01750SDavid Howells 	} while (rreq->submitted < rreq->len);
6323be01750SDavid Howells 
6333be01750SDavid Howells 	if (sync) {
6343be01750SDavid Howells 		/* Keep nr_outstanding incremented so that the ref always belongs to
6353be01750SDavid Howells 		 * us, and the service code isn't punted off to a random thread pool to
6363be01750SDavid Howells 		 * process.
6373be01750SDavid Howells 		 */
6383be01750SDavid Howells 		for (;;) {
6393be01750SDavid Howells 			wait_var_event(&rreq->nr_outstanding,
6403be01750SDavid Howells 				       atomic_read(&rreq->nr_outstanding) == 1);
6413be01750SDavid Howells 			netfs_rreq_assess(rreq, false);
6423be01750SDavid Howells 			if (!test_bit(NETFS_RREQ_IN_PROGRESS, &rreq->flags))
6433be01750SDavid Howells 				break;
6443be01750SDavid Howells 			cond_resched();
6453be01750SDavid Howells 		}
6463be01750SDavid Howells 
6473be01750SDavid Howells 		ret = rreq->error;
6483be01750SDavid Howells 		if (ret == 0 && rreq->submitted < rreq->len) {
6493be01750SDavid Howells 			trace_netfs_failure(rreq, NULL, ret, netfs_fail_short_read);
6503be01750SDavid Howells 			ret = -EIO;
6513be01750SDavid Howells 		}
6523be01750SDavid Howells 		netfs_put_request(rreq, false, netfs_rreq_trace_put_hold);
6533be01750SDavid Howells 	} else {
6543be01750SDavid Howells 		/* If we decrement nr_outstanding to 0, the ref belongs to us. */
6553be01750SDavid Howells 		if (atomic_dec_and_test(&rreq->nr_outstanding))
6563be01750SDavid Howells 			netfs_rreq_assess(rreq, false);
6573be01750SDavid Howells 		ret = 0;
6583be01750SDavid Howells 	}
6593be01750SDavid Howells 	return ret;
6603be01750SDavid Howells }
661