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