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 folio *sink = NULL;
486 struct bio_vec *bvec;
487 unsigned int from = finfo->dirty_offset;
488 unsigned int to = from + finfo->dirty_len;
489 unsigned int off = 0, i = 0;
490 size_t flen = folio_size(folio);
491 size_t nr_bvec = flen / PAGE_SIZE + 2;
492 size_t part;
493 int ret;
494
495 _enter("%lx", folio->index);
496
497 rreq = netfs_alloc_request(mapping, file, folio_pos(folio), flen, NETFS_READ_GAPS);
498 if (IS_ERR(rreq)) {
499 ret = PTR_ERR(rreq);
500 goto alloc_error;
501 }
502
503 ret = netfs_begin_cache_read(rreq, ctx);
504 if (ret == -ENOMEM || ret == -EINTR || ret == -ERESTARTSYS)
505 goto discard;
506
507 netfs_stat(&netfs_n_rh_read_folio);
508 trace_netfs_read(rreq, rreq->start, rreq->len, netfs_read_trace_read_gaps);
509
510 /* Fiddle the buffer so that a gap at the beginning and/or a gap at the
511 * end get copied to, but the middle is discarded.
512 */
513 ret = -ENOMEM;
514 bvec = kmalloc_objs(*bvec, nr_bvec);
515 if (!bvec)
516 goto discard;
517
518 sink = folio_alloc(GFP_KERNEL, 0);
519 if (!sink) {
520 kfree(bvec);
521 goto discard;
522 }
523
524 trace_netfs_folio(folio, netfs_folio_trace_read_gaps);
525
526 rreq->direct_bv = bvec;
527 rreq->direct_bv_count = nr_bvec;
528 if (from > 0) {
529 bvec_set_folio(&bvec[i++], folio, from, 0);
530 off = from;
531 }
532 while (off < to) {
533 part = min_t(size_t, to - off, PAGE_SIZE);
534 bvec_set_folio(&bvec[i++], sink, part, 0);
535 off += part;
536 }
537 if (to < flen)
538 bvec_set_folio(&bvec[i++], folio, flen - to, to);
539 iov_iter_bvec(&rreq->buffer.iter, ITER_DEST, bvec, i, rreq->len);
540 rreq->submitted = rreq->start + flen;
541
542 netfs_read_to_pagecache(rreq);
543
544 ret = netfs_wait_for_read(rreq);
545 if (ret >= 0) {
546 if (group)
547 folio_change_private(folio, group);
548 else
549 folio_detach_private(folio);
550 kfree(finfo);
551 trace_netfs_folio(folio, netfs_folio_trace_filled_gaps);
552 flush_dcache_folio(folio);
553 folio_mark_uptodate(folio);
554 }
555
556 if (sink)
557 folio_put(sink);
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 return ret;
567 }
568
569 /**
570 * netfs_read_folio - Helper to manage a read_folio request
571 * @file: The file to read from
572 * @folio: The folio to read
573 *
574 * Fulfil a read_folio request by drawing data from the cache if
575 * possible, or the netfs if not. Space beyond the EOF is zero-filled.
576 * Multiple I/O requests from different sources will get munged together.
577 *
578 * The calling netfs must initialise a netfs context contiguous to the vfs
579 * inode before calling this.
580 *
581 * This is usable whether or not caching is enabled.
582 */
netfs_read_folio(struct file * file,struct folio * folio)583 int netfs_read_folio(struct file *file, struct folio *folio)
584 {
585 struct address_space *mapping = folio->mapping;
586 struct netfs_io_request *rreq;
587 struct netfs_inode *ctx = netfs_inode(mapping->host);
588 int ret;
589
590 folio_wait_writeback(folio);
591
592 if (folio_test_dirty(folio))
593 return netfs_read_gaps(file, folio);
594
595 _enter("%lx", folio->index);
596
597 rreq = netfs_alloc_request(mapping, file,
598 folio_pos(folio), folio_size(folio),
599 NETFS_READPAGE);
600 if (IS_ERR(rreq)) {
601 ret = PTR_ERR(rreq);
602 goto alloc_error;
603 }
604
605 ret = netfs_begin_cache_read(rreq, ctx);
606 if (ret == -ENOMEM || ret == -EINTR || ret == -ERESTARTSYS)
607 goto discard;
608
609 netfs_stat(&netfs_n_rh_read_folio);
610 trace_netfs_read(rreq, rreq->start, rreq->len, netfs_read_trace_readpage);
611
612 /* Set up the output buffer */
613 ret = netfs_create_singular_buffer(rreq, folio, 0);
614 if (ret < 0)
615 goto discard;
616
617 netfs_read_to_pagecache(rreq);
618 ret = netfs_wait_for_read(rreq);
619 netfs_put_request(rreq, netfs_rreq_trace_put_return);
620 return ret < 0 ? ret : 0;
621
622 discard:
623 netfs_put_failed_request(rreq);
624 alloc_error:
625 folio_unlock(folio);
626 return ret;
627 }
628 EXPORT_SYMBOL(netfs_read_folio);
629
630 /*
631 * Prepare a folio for writing without reading first
632 * @folio: The folio being prepared
633 * @pos: starting position for the write
634 * @len: length of write
635 * @always_fill: T if the folio should always be completely filled/cleared
636 *
637 * In some cases, write_begin doesn't need to read at all:
638 * - full folio write
639 * - write that lies in a folio that is completely beyond EOF
640 * - write that covers the folio from start to EOF or beyond it
641 *
642 * If any of these criteria are met, then zero out the unwritten parts
643 * of the folio and return true. Otherwise, return false.
644 */
netfs_skip_folio_read(struct folio * folio,loff_t pos,size_t len,bool always_fill)645 static bool netfs_skip_folio_read(struct folio *folio, loff_t pos, size_t len,
646 bool always_fill)
647 {
648 struct inode *inode = folio_inode(folio);
649 loff_t i_size = i_size_read(inode);
650 size_t offset = offset_in_folio(folio, pos);
651 size_t plen = folio_size(folio);
652
653 if (unlikely(always_fill)) {
654 if (pos - offset + len <= i_size)
655 return false; /* Page entirely before EOF */
656 folio_zero_segment(folio, 0, plen);
657 folio_mark_uptodate(folio);
658 return true;
659 }
660
661 /* Full folio write */
662 if (offset == 0 && len >= plen)
663 return true;
664
665 /* Page entirely beyond the end of the file */
666 if (pos - offset >= i_size)
667 goto zero_out;
668
669 /* Write that covers from the start of the folio to EOF or beyond */
670 if (offset == 0 && (pos + len) >= i_size)
671 goto zero_out;
672
673 return false;
674 zero_out:
675 folio_zero_segments(folio, 0, offset, offset + len, plen);
676 return true;
677 }
678
679 /**
680 * netfs_write_begin - Helper to prepare for writing [DEPRECATED]
681 * @ctx: The netfs context
682 * @file: The file to read from
683 * @mapping: The mapping to read from
684 * @pos: File position at which the write will begin
685 * @len: The length of the write (may extend beyond the end of the folio chosen)
686 * @_folio: Where to put the resultant folio
687 * @_fsdata: Place for the netfs to store a cookie
688 *
689 * Pre-read data for a write-begin request by drawing data from the cache if
690 * possible, or the netfs if not. Space beyond the EOF is zero-filled.
691 * Multiple I/O requests from different sources will get munged together.
692 *
693 * The calling netfs must provide a table of operations, only one of which,
694 * issue_read, is mandatory.
695 *
696 * The check_write_begin() operation can be provided to check for and flush
697 * conflicting writes once the folio is grabbed and locked. It is passed a
698 * pointer to the fsdata cookie that gets returned to the VM to be passed to
699 * write_end. It is permitted to sleep. It should return 0 if the request
700 * should go ahead or it may return an error. It may also unlock and put the
701 * folio, provided it sets ``*foliop`` to NULL, in which case a return of 0
702 * will cause the folio to be re-got and the process to be retried.
703 *
704 * The calling netfs must initialise a netfs context contiguous to the vfs
705 * inode before calling this.
706 *
707 * This is usable whether or not caching is enabled.
708 *
709 * Note that this should be considered deprecated and netfs_perform_write()
710 * used instead.
711 */
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)712 int netfs_write_begin(struct netfs_inode *ctx,
713 struct file *file, struct address_space *mapping,
714 loff_t pos, unsigned int len, struct folio **_folio,
715 void **_fsdata)
716 {
717 struct netfs_io_request *rreq;
718 struct folio *folio;
719 pgoff_t index = pos >> PAGE_SHIFT;
720 int ret;
721
722 retry:
723 folio = __filemap_get_folio(mapping, index, FGP_WRITEBEGIN,
724 mapping_gfp_mask(mapping));
725 if (IS_ERR(folio))
726 return PTR_ERR(folio);
727
728 if (ctx->ops->check_write_begin) {
729 /* Allow the netfs (eg. ceph) to flush conflicts. */
730 ret = ctx->ops->check_write_begin(file, pos, len, &folio, _fsdata);
731 if (ret < 0) {
732 trace_netfs_failure(NULL, NULL, ret, netfs_fail_check_write_begin);
733 goto error;
734 }
735 if (!folio)
736 goto retry;
737 }
738
739 if (folio_test_uptodate(folio))
740 goto have_folio;
741
742 /* If the folio is beyond the EOF, we want to clear it - unless it's
743 * within the cache granule containing the EOF, in which case we need
744 * to preload the granule.
745 */
746 if (!netfs_is_cache_maybe_enabled(ctx) &&
747 netfs_skip_folio_read(folio, pos, len, false)) {
748 netfs_stat(&netfs_n_rh_write_zskip);
749 goto have_folio_no_wait;
750 }
751
752 rreq = netfs_alloc_request(mapping, file,
753 folio_pos(folio), folio_size(folio),
754 NETFS_READ_FOR_WRITE);
755 if (IS_ERR(rreq)) {
756 ret = PTR_ERR(rreq);
757 goto error;
758 }
759 rreq->no_unlock_folio = folio;
760 __set_bit(NETFS_RREQ_NO_UNLOCK_FOLIO, &rreq->flags);
761
762 ret = netfs_begin_cache_read(rreq, ctx);
763 if (ret == -ENOMEM || ret == -EINTR || ret == -ERESTARTSYS)
764 goto error_put;
765
766 netfs_stat(&netfs_n_rh_write_begin);
767 trace_netfs_read(rreq, pos, len, netfs_read_trace_write_begin);
768
769 /* Set up the output buffer */
770 ret = netfs_create_singular_buffer(rreq, folio, 0);
771 if (ret < 0)
772 goto error_put;
773
774 netfs_read_to_pagecache(rreq);
775 ret = netfs_wait_for_read(rreq);
776 netfs_put_request(rreq, netfs_rreq_trace_put_return);
777 if (ret < 0)
778 goto error;
779
780 have_folio:
781 ret = folio_wait_private_2_killable(folio);
782 if (ret < 0)
783 goto error;
784 have_folio_no_wait:
785 *_folio = folio;
786 _leave(" = 0");
787 return 0;
788
789 error_put:
790 netfs_put_failed_request(rreq);
791 error:
792 if (folio) {
793 folio_unlock(folio);
794 folio_put(folio);
795 }
796 _leave(" = %d", ret);
797 return ret;
798 }
799 EXPORT_SYMBOL(netfs_write_begin);
800
801 /*
802 * Preload the data into a folio we're proposing to write into.
803 */
netfs_prefetch_for_write(struct file * file,struct folio * folio,size_t offset,size_t len)804 int netfs_prefetch_for_write(struct file *file, struct folio *folio,
805 size_t offset, size_t len)
806 {
807 struct netfs_io_request *rreq;
808 struct address_space *mapping = folio->mapping;
809 struct netfs_inode *ctx = netfs_inode(mapping->host);
810 unsigned long long start = folio_pos(folio);
811 size_t flen = folio_size(folio);
812 int ret;
813
814 _enter("%zx @%llx", flen, start);
815
816 ret = -ENOMEM;
817
818 rreq = netfs_alloc_request(mapping, file, start, flen,
819 NETFS_READ_FOR_WRITE);
820 if (IS_ERR(rreq)) {
821 ret = PTR_ERR(rreq);
822 goto error;
823 }
824
825 rreq->no_unlock_folio = folio;
826 __set_bit(NETFS_RREQ_NO_UNLOCK_FOLIO, &rreq->flags);
827 ret = netfs_begin_cache_read(rreq, ctx);
828 if (ret == -ENOMEM || ret == -EINTR || ret == -ERESTARTSYS)
829 goto error_put;
830
831 netfs_stat(&netfs_n_rh_write_begin);
832 trace_netfs_read(rreq, start, flen, netfs_read_trace_prefetch_for_write);
833
834 /* Set up the output buffer */
835 ret = netfs_create_singular_buffer(rreq, folio, NETFS_ROLLBUF_PAGECACHE_MARK);
836 if (ret < 0)
837 goto error_put;
838
839 netfs_read_to_pagecache(rreq);
840 ret = netfs_wait_for_read(rreq);
841 netfs_put_request(rreq, netfs_rreq_trace_put_return);
842 return ret < 0 ? ret : 0;
843
844 error_put:
845 netfs_put_failed_request(rreq);
846 error:
847 _leave(" = %d", ret);
848 return ret;
849 }
850
851 /**
852 * netfs_buffered_read_iter - Filesystem buffered I/O read routine
853 * @iocb: kernel I/O control block
854 * @iter: destination for the data read
855 *
856 * This is the ->read_iter() routine for all filesystems that can use the page
857 * cache directly.
858 *
859 * The IOCB_NOWAIT flag in iocb->ki_flags indicates that -EAGAIN shall be
860 * returned when no data can be read without waiting for I/O requests to
861 * complete; it doesn't prevent readahead.
862 *
863 * The IOCB_NOIO flag in iocb->ki_flags indicates that no new I/O requests
864 * shall be made for the read or for readahead. When no data can be read,
865 * -EAGAIN shall be returned. When readahead would be triggered, a partial,
866 * possibly empty read shall be returned.
867 *
868 * Return:
869 * * number of bytes copied, even for partial reads
870 * * negative error code (or 0 if IOCB_NOIO) if nothing was read
871 */
netfs_buffered_read_iter(struct kiocb * iocb,struct iov_iter * iter)872 ssize_t netfs_buffered_read_iter(struct kiocb *iocb, struct iov_iter *iter)
873 {
874 struct inode *inode = file_inode(iocb->ki_filp);
875 struct netfs_inode *ictx = netfs_inode(inode);
876 ssize_t ret;
877
878 if (WARN_ON_ONCE((iocb->ki_flags & IOCB_DIRECT) ||
879 test_bit(NETFS_ICTX_UNBUFFERED, &ictx->flags)))
880 return -EINVAL;
881
882 ret = netfs_start_io_read(inode);
883 if (ret == 0) {
884 ret = filemap_read(iocb, iter, 0);
885 netfs_end_io_read(inode);
886 }
887 return ret;
888 }
889 EXPORT_SYMBOL(netfs_buffered_read_iter);
890
891 /**
892 * netfs_file_read_iter - Generic filesystem read routine
893 * @iocb: kernel I/O control block
894 * @iter: destination for the data read
895 *
896 * This is the ->read_iter() routine for all filesystems that can use the page
897 * cache directly.
898 *
899 * The IOCB_NOWAIT flag in iocb->ki_flags indicates that -EAGAIN shall be
900 * returned when no data can be read without waiting for I/O requests to
901 * complete; it doesn't prevent readahead.
902 *
903 * The IOCB_NOIO flag in iocb->ki_flags indicates that no new I/O requests
904 * shall be made for the read or for readahead. When no data can be read,
905 * -EAGAIN shall be returned. When readahead would be triggered, a partial,
906 * possibly empty read shall be returned.
907 *
908 * Return:
909 * * number of bytes copied, even for partial reads
910 * * negative error code (or 0 if IOCB_NOIO) if nothing was read
911 */
netfs_file_read_iter(struct kiocb * iocb,struct iov_iter * iter)912 ssize_t netfs_file_read_iter(struct kiocb *iocb, struct iov_iter *iter)
913 {
914 struct netfs_inode *ictx = netfs_inode(iocb->ki_filp->f_mapping->host);
915
916 if ((iocb->ki_flags & IOCB_DIRECT) ||
917 test_bit(NETFS_ICTX_UNBUFFERED, &ictx->flags))
918 return netfs_unbuffered_read_iter(iocb, iter);
919
920 return netfs_buffered_read_iter(iocb, iter);
921 }
922 EXPORT_SYMBOL(netfs_file_read_iter);
923