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