xref: /linux/fs/netfs/read_collect.c (revision dd05add7b2b6004c5fa3a1f276f56d8668f86b65)
1 // SPDX-License-Identifier: GPL-2.0-only
2 /* Network filesystem read subrequest result collection, assessment and
3  * retrying.
4  *
5  * Copyright (C) 2024 Red Hat, Inc. All Rights Reserved.
6  * Written by David Howells (dhowells@redhat.com)
7  */
8 
9 #include <linux/export.h>
10 #include <linux/fs.h>
11 #include <linux/mm.h>
12 #include <linux/pagemap.h>
13 #include <linux/slab.h>
14 #include <linux/task_io_accounting_ops.h>
15 #include "internal.h"
16 
17 /* Notes made in the collector */
18 #define HIT_PENDING	0x01	/* A front op was still pending */
19 #define MADE_PROGRESS	0x04	/* Made progress cleaning up a stream or the folio set */
20 #define BUFFERED	0x08	/* The pagecache needs cleaning up */
21 #define NEED_RETRY	0x10	/* A front op requests retrying */
22 #define ABANDON_SREQ	0x80	/* Need to abandon untransferred part of subrequest */
23 
24 /*
25  * Clear the unread part of an I/O request.
26  */
27 static void netfs_clear_unread(struct netfs_io_subrequest *subreq)
28 {
29 	netfs_reset_iter(subreq);
30 	WARN_ON_ONCE(subreq->len - subreq->transferred != iov_iter_count(&subreq->io_iter));
31 	iov_iter_zero(iov_iter_count(&subreq->io_iter), &subreq->io_iter);
32 	if (subreq->start + subreq->transferred >= subreq->rreq->i_size)
33 		__set_bit(NETFS_SREQ_HIT_EOF, &subreq->flags);
34 }
35 
36 static void netfs_clear_unread_dio(struct netfs_io_subrequest *subreq)
37 {
38 	uoff_t pos = subreq->start + subreq->transferred;
39 	struct netfs_io_request *rreq = subreq->rreq;
40 	size_t fill;
41 
42 	if (pos >= rreq->i_size)
43 		return;
44 
45 	fill = min_t(uoff_t, rreq->i_size - pos,
46 		     subreq->len - subreq->transferred);
47 
48 	netfs_reset_iter(subreq);
49 	subreq->transferred += iov_iter_zero(fill, &subreq->io_iter);
50 }
51 
52 /*
53  * Cancel the copy-to-cache mark on a folio.
54  */
55 void netfs_cancel_copy_to_cache(struct netfs_io_request *rreq, struct folio *folio)
56 {
57 	if (!test_bit(NETFS_RREQ_USE_PGPRIV2, &rreq->flags)) {
58 		if (folio_get_private(folio) == NETFS_FOLIO_COPY_TO_CACHE) {
59 			folio_detach_private(folio);
60 			trace_netfs_folio(folio, netfs_folio_trace_cancel_copy);
61 		} else if (netfs_folio_group(folio) == NETFS_FOLIO_COPY_TO_CACHE)  {
62 			struct netfs_folio *finfo = netfs_folio_info(folio);
63 
64 			finfo->netfs_group = NULL;
65 			trace_netfs_folio(folio, netfs_folio_trace_cancel_copy);
66 		}
67 	} else {
68 		// TODO: Use of PG_private_2 is deprecated.
69 		if (folio_test_private_2(folio)) {
70 			folio_end_private_2(folio);
71 			trace_netfs_folio(folio, netfs_folio_trace_cancel_copy);
72 		}
73 	}
74 }
75 
76 /*
77  * Flush, mark and unlock a folio that's now completely read.  If we want to
78  * cache the folio, we set the group to NETFS_FOLIO_COPY_TO_CACHE, mark it
79  * dirty and let writeback handle it.
80  */
81 static void netfs_unlock_read_folio(struct netfs_io_request *rreq,
82 				    struct folio_queue *folioq,
83 				    int slot)
84 {
85 	struct netfs_folio *finfo;
86 	struct folio *folio = folioq_folio(folioq, slot);
87 
88 	if (unlikely(folio_pos(folio) < rreq->abandon_to)) {
89 		trace_netfs_folio(folio, netfs_folio_trace_abandon);
90 		netfs_cancel_copy_to_cache(rreq, folio);
91 		goto just_unlock;
92 	}
93 
94 	flush_dcache_folio(folio);
95 	folio_mark_uptodate(folio);
96 
97 	if (unlikely(test_bit(NETFS_RREQ_CANCEL_CACHING, &rreq->flags)))
98 		netfs_cancel_copy_to_cache(rreq, folio);
99 
100 	if (!test_bit(NETFS_RREQ_USE_PGPRIV2, &rreq->flags)) {
101 		if (netfs_folio_group(folio) == NETFS_FOLIO_COPY_TO_CACHE)  {
102 			trace_netfs_folio(folio, netfs_folio_trace_sched_copy);
103 			folio_mark_dirty(folio);
104 		} else {
105 			finfo = netfs_folio_info(folio);
106 			if (finfo) {
107 				trace_netfs_folio(folio, netfs_folio_trace_filled_gaps);
108 				if (finfo->netfs_group)
109 					folio_change_private(folio, finfo->netfs_group);
110 				else
111 					folio_detach_private(folio);
112 				kfree(finfo);
113 			}
114 			trace_netfs_folio(folio, netfs_folio_trace_read_done);
115 		}
116 
117 		folioq_clear(folioq, slot);
118 	} else {
119 		// TODO: Use of PG_private_2 is deprecated.
120 		if (folio_test_private_2(folio))
121 			netfs_pgpriv2_copy_to_cache(rreq, folio);
122 	}
123 
124 just_unlock:
125 	if (folio == rreq->no_unlock_folio &&
126 	    test_bit(NETFS_RREQ_NO_UNLOCK_FOLIO, &rreq->flags)) {
127 		_debug("no unlock");
128 	} else {
129 		trace_netfs_folio(folio, netfs_folio_trace_read_unlock);
130 		folio_unlock(folio);
131 	}
132 
133 	folioq_clear(folioq, slot);
134 }
135 
136 /*
137  * Determine how much to gather before unlocking more folios.
138  */
139 void netfs_read_set_unlock_at(struct netfs_io_request *rreq)
140 {
141 	struct folio_queue *folioq = rreq->buffer.tail;
142 	unsigned int slot = rreq->buffer.first_tail_slot;
143 	size_t cleaned_to = rreq->cleaned_to - rreq->start;
144 	size_t progress_at = cleaned_to;
145 	size_t minimum = 256 * 1024;
146 
147 	while (progress_at < rreq->len) {
148 		if (slot >= folioq_count(folioq)) {
149 			folioq = folioq->next;
150 			if (!folioq)
151 				break;
152 			slot = 0;
153 		}
154 
155 		progress_at += folioq_folio_size(folioq, slot);
156 		if (progress_at - cleaned_to >= minimum)
157 			break;
158 		slot++;
159 	}
160 
161 	WRITE_ONCE(rreq->progress_at, progress_at);
162 	trace_netfs_read_progress_at(rreq);
163 }
164 
165 /*
166  * Unlock any folios we've finished with.
167  */
168 static void netfs_read_unlock_folios(struct netfs_io_request *rreq,
169 				     unsigned int *notes)
170 {
171 	struct folio_queue *folioq = rreq->buffer.tail;
172 	unsigned long long collected_to = rreq->collected_to;
173 	unsigned int slot = rreq->buffer.first_tail_slot;
174 
175 	if (rreq->cleaned_to >= rreq->collected_to)
176 		return;
177 
178 	// TODO: Begin decryption
179 
180 	if (slot >= folioq_nr_slots(folioq)) {
181 		folioq = rolling_buffer_delete_spent(&rreq->buffer);
182 		if (!folioq) {
183 			WRITE_ONCE(rreq->progress_at, rreq->len);
184 			return;
185 		}
186 		slot = 0;
187 	}
188 
189 	/* We have to wait for readahead refs to have been released before we
190 	 * can unlock any folios as the ref-dropper walks i_pages and the only
191 	 * thing preventing these folios from being removed is the folio lock.
192 	 */
193 	if (test_bit(NETFS_RREQ_NEED_PUT_RA_REFS, &rreq->flags))
194 		netfs_wait_for_put_ra_refs(rreq);
195 
196 	for (;;) {
197 		struct folio *folio;
198 		unsigned long long fpos, fend;
199 		size_t fsize;
200 
201 		folio = folioq_folio(folioq, slot);
202 		if (WARN_ONCE(!folio_test_locked(folio),
203 			      "R=%08x: folio %lx is not locked\n",
204 			      rreq->debug_id, folio->index))
205 			trace_netfs_folio(folio, netfs_folio_trace_not_locked);
206 
207 		fsize = folioq_folio_size(folioq, slot);
208 		fpos = folio_pos(folio);
209 		fend = fpos + fsize;
210 
211 		trace_netfs_collect_folio(rreq, folio, fend, collected_to);
212 
213 		/* Unlock any folio we've transferred all of. */
214 		if (collected_to < fend)
215 			break;
216 
217 		netfs_unlock_read_folio(rreq, folioq, slot);
218 		WRITE_ONCE(rreq->cleaned_to, fpos + fsize);
219 		*notes |= MADE_PROGRESS;
220 
221 		/* Clean up the head folioq.  If we clear an entire folioq, then
222 		 * we can get rid of it provided it's not also the tail folioq
223 		 * being filled by the issuer.
224 		 */
225 		folioq_clear(folioq, slot);
226 		slot++;
227 		if (slot >= folioq_nr_slots(folioq)) {
228 			folioq = rolling_buffer_delete_spent(&rreq->buffer);
229 			if (!folioq)
230 				goto done;
231 			slot = 0;
232 			trace_netfs_folioq(folioq, netfs_trace_folioq_read_progress);
233 		}
234 
235 		if (fpos + fsize >= collected_to)
236 			break;
237 	}
238 
239 	rreq->buffer.tail = folioq;
240 done:
241 	rreq->buffer.first_tail_slot = slot;
242 
243 	netfs_read_set_unlock_at(rreq);
244 }
245 
246 /*
247  * Collect and assess the results of various read subrequests.  We may need to
248  * retry some of the results.
249  *
250  * Note that we have a sequence of subrequests, which may be drawing on
251  * different sources and may or may not be the same size or starting position
252  * and may not even correspond in boundary alignment.
253  */
254 static void netfs_collect_read_results(struct netfs_io_request *rreq)
255 {
256 	struct netfs_io_subrequest *front, *remove;
257 	struct netfs_io_stream *stream = &rreq->io_streams[0];
258 	unsigned int notes;
259 
260 	_enter("%llx-%llx", rreq->start, rreq->start + rreq->len);
261 	trace_netfs_rreq(rreq, netfs_rreq_trace_collect);
262 	trace_netfs_collect(rreq);
263 
264 reassess:
265 	if (rreq->origin == NETFS_READAHEAD ||
266 	    rreq->origin == NETFS_READPAGE ||
267 	    rreq->origin == NETFS_READ_FOR_WRITE)
268 		notes = BUFFERED;
269 	else
270 		notes = 0;
271 
272 	/* Remove completed subrequests from the front of the stream and
273 	 * advance the completion point.  We stop when we hit something that's
274 	 * in progress.  The issuer thread may be adding stuff to the tail
275 	 * whilst we're doing this.
276 	 */
277 	front = list_first_entry_or_null_acquire(&stream->subrequests,
278 						 struct netfs_io_subrequest, rreq_link);
279 	/* Read first subreq pointer before IN_PROGRESS flag. */
280 
281 	while (front) {
282 		size_t transferred;
283 
284 		trace_netfs_collect_sreq(rreq, front);
285 		_debug("sreq [%x] %llx %zx/%zx",
286 		       front->debug_index, front->start, front->transferred, front->len);
287 
288 		if (stream->collected_to < front->start) {
289 			trace_netfs_collect_gap(rreq, stream, front->start, 'F');
290 			stream->collected_to = front->start;
291 		}
292 
293 		if (netfs_check_subreq_in_progress(front))
294 			notes |= HIT_PENDING;
295 		smp_rmb(); /* Read counters after IN_PROGRESS flag. */
296 		transferred = READ_ONCE(front->transferred);
297 
298 		/* If we can now collect the next folio, do so.  We don't want
299 		 * to defer this as we have to decide whether we need to copy
300 		 * to the cache or not, and that may differ between adjacent
301 		 * subreqs.
302 		 */
303 		if (notes & BUFFERED) {
304 			uoff_t unlock_at = rreq->start + rreq->progress_at;
305 
306 			/* Clear the tail of a short read. */
307 			if (!(notes & HIT_PENDING) &&
308 			    front->error == 0 &&
309 			    transferred < front->len &&
310 			    (test_bit(NETFS_SREQ_HIT_EOF, &front->flags) ||
311 			     test_bit(NETFS_SREQ_CLEAR_TAIL, &front->flags))) {
312 				netfs_clear_unread(front);
313 				transferred = front->transferred = front->len;
314 				trace_netfs_sreq(front, netfs_sreq_trace_clear);
315 			}
316 
317 			stream->collected_to = front->start + transferred;
318 			rreq->collected_to = stream->collected_to;
319 
320 			if (test_bit(NETFS_SREQ_FAILED, &front->flags)) {
321 				rreq->abandon_to = front->start + front->len;
322 				front->transferred = front->len;
323 				transferred = front->len;
324 				trace_netfs_rreq(rreq, netfs_rreq_trace_set_abandon);
325 			}
326 			if (front->start + transferred >= unlock_at ||
327 			    test_bit(NETFS_SREQ_HIT_EOF, &front->flags))
328 				netfs_read_unlock_folios(rreq, &notes);
329 		} else {
330 			if (!(notes & HIT_PENDING) &&
331 			    front->error == 0 &&
332 			    transferred < front->len &&
333 			    test_bit(NETFS_SREQ_CLEAR_TAIL, &front->flags)) {
334 				netfs_clear_unread_dio(front);
335 				transferred = front->transferred;
336 				trace_netfs_sreq(front, netfs_sreq_trace_clear);
337 			}
338 			stream->collected_to = front->start + transferred;
339 			rreq->collected_to = stream->collected_to;
340 		}
341 
342 		/* Stall if the front is still undergoing I/O. */
343 		if (notes & HIT_PENDING)
344 			break;
345 
346 		if (test_bit(NETFS_SREQ_FAILED, &front->flags)) {
347 			if (!stream->failed) {
348 				stream->error = front->error;
349 				rreq->error = front->error;
350 				set_bit(NETFS_RREQ_FAILED, &rreq->flags);
351 				stream->failed = true;
352 			}
353 			notes |= MADE_PROGRESS | ABANDON_SREQ;
354 		} else if (test_bit(NETFS_SREQ_NEED_RETRY, &front->flags)) {
355 			stream->need_retry = true;
356 			notes |= NEED_RETRY | MADE_PROGRESS;
357 			break;
358 		} else if (test_bit(NETFS_RREQ_SHORT_TRANSFER, &rreq->flags)) {
359 			notes |= MADE_PROGRESS;
360 		} else {
361 			if (!stream->failed) {
362 				stream->transferred += transferred;
363 				stream->transferred_valid = true;
364 			}
365 			if (front->transferred < front->len)
366 				set_bit(NETFS_RREQ_SHORT_TRANSFER, &rreq->flags);
367 			notes |= MADE_PROGRESS;
368 		}
369 
370 		/* Remove if completely consumed. */
371 		stream->source = front->source;
372 		spin_lock(&rreq->lock);
373 
374 		remove = front;
375 		trace_netfs_sreq(front,
376 				 notes & ABANDON_SREQ ?
377 				 netfs_sreq_trace_abandoned : netfs_sreq_trace_consumed);
378 		list_del_init(&front->rreq_link);
379 		front = list_first_entry_or_null(&stream->subrequests,
380 						 struct netfs_io_subrequest, rreq_link);
381 		spin_unlock(&rreq->lock);
382 		netfs_put_subrequest(remove,
383 				     notes & ABANDON_SREQ ?
384 				     netfs_sreq_trace_put_abandon :
385 				     netfs_sreq_trace_put_done);
386 	}
387 
388 	trace_netfs_collect_stream(rreq, stream);
389 	trace_netfs_collect_state(rreq, rreq->collected_to, notes);
390 
391 	if (!(notes & BUFFERED))
392 		rreq->cleaned_to = rreq->collected_to;
393 
394 	if (notes & NEED_RETRY)
395 		goto need_retry;
396 	if (notes & MADE_PROGRESS) {
397 		netfs_wake_rreq_flag(rreq, NETFS_RREQ_PAUSE, netfs_rreq_trace_unpause);
398 		//cond_resched();
399 		goto reassess;
400 	}
401 
402 out:
403 	_leave(" = %x", notes);
404 	return;
405 
406 need_retry:
407 	/* Okay...  We're going to have to retry parts of the stream.  Note
408 	 * that any partially completed op will have had any wholly transferred
409 	 * folios removed from it.
410 	 */
411 	_debug("retry");
412 	netfs_retry_reads(rreq);
413 	goto out;
414 }
415 
416 /*
417  * Do page flushing and suchlike after DIO.
418  */
419 static void netfs_rreq_assess_dio(struct netfs_io_request *rreq)
420 {
421 	unsigned int i;
422 
423 	if (rreq->origin == NETFS_UNBUFFERED_READ ||
424 	    rreq->origin == NETFS_DIO_READ) {
425 		for (i = 0; i < rreq->direct_bv_count; i++) {
426 			flush_dcache_page(rreq->direct_bv[i].bv_page);
427 			// TODO: cifs marks pages in the destination buffer
428 			// dirty under some circumstances after a read.  Do we
429 			// need to do that too?
430 			set_page_dirty(rreq->direct_bv[i].bv_page);
431 		}
432 	}
433 
434 	if (rreq->iocb) {
435 		rreq->iocb->ki_pos += rreq->transferred;
436 		if (rreq->iocb->ki_complete) {
437 			trace_netfs_rreq(rreq, netfs_rreq_trace_ki_complete);
438 			rreq->iocb->ki_complete(
439 				rreq->iocb, rreq->error ? rreq->error : rreq->transferred);
440 		}
441 	}
442 	if (rreq->netfs_ops->done)
443 		rreq->netfs_ops->done(rreq);
444 	if (rreq->origin == NETFS_UNBUFFERED_READ ||
445 	    rreq->origin == NETFS_DIO_READ)
446 		inode_dio_end(rreq->inode);
447 }
448 
449 /*
450  * Do processing after reading a monolithic single object.
451  */
452 static void netfs_rreq_assess_single(struct netfs_io_request *rreq)
453 {
454 	struct netfs_io_stream *stream = &rreq->io_streams[0];
455 
456 	if (!rreq->error && stream->source == NETFS_DOWNLOAD_FROM_SERVER &&
457 	    fscache_resources_valid(&rreq->cache_resources)) {
458 		trace_netfs_rreq(rreq, netfs_rreq_trace_dirty);
459 		netfs_single_mark_inode_dirty(rreq->inode);
460 	}
461 
462 	/* To do DIO, the cache has to round the size up, so we need to undo
463 	 * the rounding.
464 	 */
465 	rreq->transferred = min(rreq->transferred, rreq->i_size);
466 
467 	if (rreq->iocb) {
468 		rreq->iocb->ki_pos += rreq->transferred;
469 		if (rreq->iocb->ki_complete) {
470 			trace_netfs_rreq(rreq, netfs_rreq_trace_ki_complete);
471 			rreq->iocb->ki_complete(
472 				rreq->iocb, rreq->error ? rreq->error : rreq->transferred);
473 		}
474 	}
475 	if (rreq->netfs_ops->done)
476 		rreq->netfs_ops->done(rreq);
477 }
478 
479 /*
480  * Perform the collection of subrequests and folios.
481  *
482  * Note that we're in normal kernel thread context at this point, possibly
483  * running on a workqueue.
484  */
485 bool netfs_read_collection(struct netfs_io_request *rreq)
486 {
487 	struct netfs_io_stream *stream = &rreq->io_streams[0];
488 
489 	netfs_collect_read_results(rreq);
490 
491 	/* We're done when the app thread has finished posting subreqs and the
492 	 * queue is empty.
493 	 */
494 	if (!test_bit(NETFS_RREQ_ALL_QUEUED, &rreq->flags))
495 		return false;
496 	smp_rmb(); /* Read ALL_QUEUED before subreq lists. */
497 
498 	if (!list_empty(&stream->subrequests))
499 		return false;
500 
501 	/* Okay, declare that all I/O is complete. */
502 	rreq->transferred = stream->transferred;
503 	trace_netfs_rreq(rreq, netfs_rreq_trace_complete);
504 
505 	//netfs_rreq_is_still_valid(rreq);
506 
507 	switch (rreq->origin) {
508 	case NETFS_UNBUFFERED_READ:
509 	case NETFS_DIO_READ:
510 	case NETFS_READ_GAPS:
511 		netfs_rreq_assess_dio(rreq);
512 		break;
513 	case NETFS_READ_SINGLE:
514 		netfs_rreq_assess_single(rreq);
515 		break;
516 	default:
517 		break;
518 	}
519 	task_io_account_read(rreq->transferred);
520 
521 	netfs_wake_rreq_flag(rreq, NETFS_RREQ_IN_PROGRESS, netfs_rreq_trace_wake_ip);
522 	/* As we cleared NETFS_RREQ_IN_PROGRESS, we acquired its ref. */
523 
524 	trace_netfs_rreq(rreq, netfs_rreq_trace_done);
525 	netfs_clear_subrequests(rreq);
526 	netfs_unlock_abandoned_read_pages(rreq);
527 	if (unlikely(rreq->copy_to_cache))
528 		netfs_pgpriv2_end_copy_to_cache(rreq);
529 	return true;
530 }
531 
532 void netfs_read_collection_worker(struct work_struct *work)
533 {
534 	struct netfs_io_request *rreq = container_of(work, struct netfs_io_request, work);
535 
536 	netfs_see_request(rreq, netfs_rreq_trace_see_work);
537 	if (netfs_check_rreq_in_progress(rreq)) {
538 		if (netfs_read_collection(rreq))
539 			/* Drop the ref from the IN_PROGRESS flag. */
540 			netfs_put_request(rreq, netfs_rreq_trace_put_work_ip);
541 		else
542 			netfs_see_request(rreq, netfs_rreq_trace_see_work_complete);
543 	}
544 }
545 
546 /**
547  * netfs_read_subreq_progress - Note progress of a read operation.
548  * @subreq: The read request that has terminated.
549  *
550  * This tells the read side of netfs lib that a contributory I/O operation has
551  * made some progress and that it may be possible to unlock some folios.
552  *
553  * Before calling, the filesystem should update subreq->transferred to track
554  * the amount of data copied into the output buffer.
555  */
556 void netfs_read_subreq_progress(struct netfs_io_subrequest *subreq)
557 {
558 	struct netfs_io_request *rreq = subreq->rreq;
559 	struct netfs_io_stream *stream = &rreq->io_streams[subreq->stream_nr];
560 	size_t progress_at = READ_ONCE(rreq->progress_at);
561 	uoff_t update_at = rreq->start + progress_at;
562 	uoff_t transferred_to = subreq->start + subreq->transferred;
563 
564 	/* If we are at the head of the queue, wake up the collector,
565 	 * getting a ref to it if we were the ones to do so.
566 	 */
567 	if (progress_at < rreq->len &&
568 	    transferred_to >= update_at &&
569 	    (rreq->origin == NETFS_READAHEAD ||
570 	     rreq->origin == NETFS_READPAGE ||
571 	     rreq->origin == NETFS_READ_FOR_WRITE) &&
572 	    list_is_first(&subreq->rreq_link, &stream->subrequests)
573 	    ) {
574 		trace_netfs_sreq(subreq, netfs_sreq_trace_progress);
575 		__set_bit(NETFS_SREQ_MADE_PROGRESS, &subreq->flags);
576 		netfs_wake_collector(rreq);
577 	}
578 }
579 EXPORT_SYMBOL(netfs_read_subreq_progress);
580 
581 /**
582  * netfs_read_subreq_terminated - Note the termination of an I/O operation.
583  * @subreq: The I/O request that has terminated.
584  *
585  * This tells the read helper that a contributory I/O operation has terminated,
586  * one way or another, and that it should integrate the results.
587  *
588  * The caller indicates the outcome of the operation through @subreq->error,
589  * supplying 0 to indicate a successful or retryable transfer (if
590  * NETFS_SREQ_NEED_RETRY is set) or a negative error code.  The helper will
591  * look after reissuing I/O operations as appropriate and writing downloaded
592  * data to the cache.
593  *
594  * Before calling, the filesystem should update subreq->transferred to track
595  * the amount of data copied into the output buffer.
596  */
597 void netfs_read_subreq_terminated(struct netfs_io_subrequest *subreq)
598 {
599 	struct netfs_io_request *rreq = subreq->rreq;
600 
601 	switch (subreq->source) {
602 	case NETFS_READ_FROM_CACHE:
603 		netfs_stat(&netfs_n_rh_read_done);
604 		break;
605 	case NETFS_DOWNLOAD_FROM_SERVER:
606 		netfs_stat(&netfs_n_rh_download_done);
607 		break;
608 	default:
609 		break;
610 	}
611 
612 	/* Deal with retry requests, short reads and errors.  If we retry
613 	 * but don't make progress, we abandon the attempt.
614 	 */
615 	if (!subreq->error && subreq->transferred < subreq->len) {
616 		if (test_bit(NETFS_SREQ_HIT_EOF, &subreq->flags)) {
617 			trace_netfs_sreq(subreq, netfs_sreq_trace_hit_eof);
618 		} else if (test_bit(NETFS_SREQ_CLEAR_TAIL, &subreq->flags)) {
619 			trace_netfs_sreq(subreq, netfs_sreq_trace_need_clear);
620 		} else if (test_bit(NETFS_SREQ_NEED_RETRY, &subreq->flags)) {
621 			trace_netfs_sreq(subreq, netfs_sreq_trace_need_retry);
622 		} else if (test_bit(NETFS_SREQ_MADE_PROGRESS, &subreq->flags)) {
623 			__set_bit(NETFS_SREQ_NEED_RETRY, &subreq->flags);
624 			trace_netfs_sreq(subreq, netfs_sreq_trace_partial_read);
625 		} else {
626 			__set_bit(NETFS_SREQ_FAILED, &subreq->flags);
627 			subreq->error = -ENODATA;
628 			trace_netfs_sreq(subreq, netfs_sreq_trace_short);
629 		}
630 	}
631 
632 	/* If need retry is set, error should not matter unless we hit too many
633 	 * retries. Pause the generation of new subreqs
634 	 */
635 	if (test_bit(NETFS_SREQ_NEED_RETRY, &subreq->flags)) {
636 		trace_netfs_rreq(rreq, netfs_rreq_trace_set_pause);
637 		set_bit(NETFS_RREQ_PAUSE, &rreq->flags);
638 		goto skip_error_checks;
639 	}
640 
641 	if (unlikely(subreq->error < 0)) {
642 		trace_netfs_failure(rreq, subreq, subreq->error, netfs_fail_read);
643 		if (subreq->source == NETFS_READ_FROM_CACHE) {
644 			netfs_stat(&netfs_n_rh_read_failed);
645 			__set_bit(NETFS_SREQ_NEED_RETRY, &subreq->flags);
646 		} else {
647 			netfs_stat(&netfs_n_rh_download_failed);
648 			__set_bit(NETFS_SREQ_FAILED, &subreq->flags);
649 		}
650 		trace_netfs_rreq(rreq, netfs_rreq_trace_set_pause);
651 		set_bit(NETFS_RREQ_PAUSE, &rreq->flags);
652 	}
653 
654 skip_error_checks:
655 	trace_netfs_sreq(subreq, netfs_sreq_trace_terminated);
656 	netfs_subreq_clear_in_progress(subreq);
657 	netfs_put_subrequest(subreq, netfs_sreq_trace_put_terminated);
658 }
659 EXPORT_SYMBOL(netfs_read_subreq_terminated);
660 
661 /*
662  * Cancel a read subrequest due to preparation failure.
663  */
664 void netfs_cancel_read(struct netfs_io_subrequest *subreq, int error)
665 {
666 	trace_netfs_sreq(subreq, netfs_sreq_trace_cancel);
667 	subreq->error = error;
668 	__set_bit(NETFS_SREQ_FAILED, &subreq->flags);
669 	netfs_read_subreq_terminated(subreq);
670 }
671 
672 /*
673  * Handle termination of a read from the cache.
674  */
675 void netfs_cache_read_terminated(void *priv, ssize_t transferred_or_error)
676 {
677 	struct netfs_io_subrequest *subreq = priv;
678 
679 	if (transferred_or_error > 0) {
680 		subreq->error = 0;
681 		if (transferred_or_error > 0) {
682 			subreq->transferred += transferred_or_error;
683 			__set_bit(NETFS_SREQ_MADE_PROGRESS, &subreq->flags);
684 		}
685 	} else {
686 		subreq->error = transferred_or_error;
687 	}
688 	netfs_read_subreq_terminated(subreq);
689 }
690