1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /* Network filesystem high-level buffered read support.
4 * Copyright (C) 2021 Red Hat, Inc. All Rights Reserved.
5 * Written by David Howells (dhowells@redhat.com)
8 #include <linux/export.h>
9 #include <linux/task_io_accounting_ops.h>
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
)
17 struct netfs_cache_resources
*cres
= &rreq
->cache_resources
;
19 if (cres
->ops
&& cres
->ops
->expand_readahead
)
20 cres
->ops
->expand_readahead(cres
, _start
, _len
, i_size
);
23 static void netfs_rreq_expand(struct netfs_io_request
*rreq
,
24 struct readahead_control
*ractl
)
26 /* Give the cache a chance to change the request parameters. The
27 * resultant request must contain the original region.
29 netfs_cache_expand_readahead(rreq
, &rreq
->start
, &rreq
->len
, rreq
->i_size
);
31 /* Give the netfs a chance to change the request parameters. The
32 * resultant request must contain the original region.
34 if (rreq
->netfs_ops
->expand_readahead
)
35 rreq
->netfs_ops
->expand_readahead(rreq
);
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
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.
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
);
52 trace_netfs_read(rreq
, readahead_pos(ractl
), readahead_length(ractl
),
53 netfs_read_trace_expanded
);
58 * Begin an operation, and fetch the stored zero point value from the cookie if
61 static int netfs_begin_cache_read(struct netfs_io_request
*rreq
, struct netfs_inode
*ctx
)
63 return fscache_begin_read_operation(&rreq
->cache_resources
, netfs_i_cookie(ctx
));
67 * Decant the list of folios to read into a rolling buffer.
69 static size_t netfs_load_buffer_from_ra(struct netfs_io_request
*rreq
,
70 struct folio_queue
*folioq
,
71 struct folio_batch
*put_batch
)
73 unsigned int order
, nr
;
76 nr
= __readahead_batch(rreq
->ractl
, (struct page
**)folioq
->vec
.folios
,
77 ARRAY_SIZE(folioq
->vec
.folios
));
79 for (int i
= 0; i
< nr
; i
++) {
80 struct folio
*folio
= folioq_folio(folioq
, i
);
82 trace_netfs_folio(folio
, netfs_folio_trace_read
);
83 order
= folio_order(folio
);
84 folioq
->orders
[i
] = order
;
85 size
+= PAGE_SIZE
<< order
;
87 if (!folio_batch_add(put_batch
, folio
))
88 folio_batch_release(put_batch
);
91 for (int i
= nr
; i
< folioq_nr_slots(folioq
); i
++)
92 folioq_clear(folioq
, i
);
98 * netfs_prepare_read_iterator - Prepare the subreq iterator for I/O
99 * @subreq: The subrequest to be set up
101 * Prepare the I/O iterator representing the read buffer on a subrequest for
102 * the filesystem to use for I/O (it can be passed directly to a socket). This
103 * is intended to be called from the ->issue_read() method once the filesystem
104 * has trimmed the request to the size it wants.
106 * Returns the limited size if successful and -ENOMEM if insufficient memory
109 * [!] NOTE: This must be run in the same thread as ->issue_read() was called
110 * in as we access the readahead_control struct.
112 static ssize_t
netfs_prepare_read_iterator(struct netfs_io_subrequest
*subreq
)
114 struct netfs_io_request
*rreq
= subreq
->rreq
;
115 size_t rsize
= subreq
->len
;
117 if (subreq
->source
== NETFS_DOWNLOAD_FROM_SERVER
)
118 rsize
= umin(rsize
, rreq
->io_streams
[0].sreq_max_len
);
121 /* If we don't have sufficient folios in the rolling buffer,
122 * extract a folioq's worth from the readahead region at a time
123 * into the buffer. Note that this acquires a ref on each page
124 * that we will need to release later - but we don't want to do
125 * that until after we've started the I/O.
127 struct folio_batch put_batch
;
129 folio_batch_init(&put_batch
);
130 while (rreq
->submitted
< subreq
->start
+ rsize
) {
131 struct folio_queue
*tail
= rreq
->buffer_tail
, *new;
134 new = kmalloc(sizeof(*new), GFP_NOFS
);
137 netfs_stat(&netfs_n_folioq
);
141 rreq
->buffer_tail
= new;
142 added
= netfs_load_buffer_from_ra(rreq
, new, &put_batch
);
143 rreq
->iter
.count
+= added
;
144 rreq
->submitted
+= added
;
146 folio_batch_release(&put_batch
);
150 if (unlikely(rreq
->io_streams
[0].sreq_max_segs
)) {
151 size_t limit
= netfs_limit_iter(&rreq
->iter
, 0, rsize
,
152 rreq
->io_streams
[0].sreq_max_segs
);
156 trace_netfs_sreq(subreq
, netfs_sreq_trace_limited
);
160 subreq
->io_iter
= rreq
->iter
;
162 if (iov_iter_is_folioq(&subreq
->io_iter
)) {
163 if (subreq
->io_iter
.folioq_slot
>= folioq_nr_slots(subreq
->io_iter
.folioq
)) {
164 subreq
->io_iter
.folioq
= subreq
->io_iter
.folioq
->next
;
165 subreq
->io_iter
.folioq_slot
= 0;
167 subreq
->curr_folioq
= (struct folio_queue
*)subreq
->io_iter
.folioq
;
168 subreq
->curr_folioq_slot
= subreq
->io_iter
.folioq_slot
;
169 subreq
->curr_folio_order
= subreq
->curr_folioq
->orders
[subreq
->curr_folioq_slot
];
172 iov_iter_truncate(&subreq
->io_iter
, subreq
->len
);
173 iov_iter_advance(&rreq
->iter
, subreq
->len
);
177 static enum netfs_io_source
netfs_cache_prepare_read(struct netfs_io_request
*rreq
,
178 struct netfs_io_subrequest
*subreq
,
181 struct netfs_cache_resources
*cres
= &rreq
->cache_resources
;
184 return NETFS_DOWNLOAD_FROM_SERVER
;
185 return cres
->ops
->prepare_read(subreq
, i_size
);
188 static void netfs_cache_read_terminated(void *priv
, ssize_t transferred_or_error
,
191 struct netfs_io_subrequest
*subreq
= priv
;
193 if (transferred_or_error
< 0) {
194 netfs_read_subreq_terminated(subreq
, transferred_or_error
, was_async
);
198 if (transferred_or_error
> 0)
199 subreq
->transferred
+= transferred_or_error
;
200 netfs_read_subreq_terminated(subreq
, 0, was_async
);
204 * Issue a read against the cache.
205 * - Eats the caller's ref on subreq.
207 static void netfs_read_cache_to_pagecache(struct netfs_io_request
*rreq
,
208 struct netfs_io_subrequest
*subreq
)
210 struct netfs_cache_resources
*cres
= &rreq
->cache_resources
;
212 netfs_stat(&netfs_n_rh_read
);
213 cres
->ops
->read(cres
, subreq
->start
, &subreq
->io_iter
, NETFS_READ_HOLE_IGNORE
,
214 netfs_cache_read_terminated
, subreq
);
218 * Perform a read to the pagecache from a series of sources of different types,
219 * slicing up the region to be read according to available cache blocks and
222 static void netfs_read_to_pagecache(struct netfs_io_request
*rreq
)
224 struct netfs_inode
*ictx
= netfs_inode(rreq
->inode
);
225 unsigned long long start
= rreq
->start
;
226 ssize_t size
= rreq
->len
;
229 atomic_inc(&rreq
->nr_outstanding
);
232 struct netfs_io_subrequest
*subreq
;
233 enum netfs_io_source source
= NETFS_DOWNLOAD_FROM_SERVER
;
236 subreq
= netfs_alloc_subrequest(rreq
);
242 subreq
->start
= start
;
245 atomic_inc(&rreq
->nr_outstanding
);
246 spin_lock_bh(&rreq
->lock
);
247 list_add_tail(&subreq
->rreq_link
, &rreq
->subrequests
);
248 subreq
->prev_donated
= rreq
->prev_donated
;
249 rreq
->prev_donated
= 0;
250 trace_netfs_sreq(subreq
, netfs_sreq_trace_added
);
251 spin_unlock_bh(&rreq
->lock
);
253 source
= netfs_cache_prepare_read(rreq
, subreq
, rreq
->i_size
);
254 subreq
->source
= source
;
255 if (source
== NETFS_DOWNLOAD_FROM_SERVER
) {
256 unsigned long long zp
= umin(ictx
->zero_point
, rreq
->i_size
);
257 size_t len
= subreq
->len
;
259 if (subreq
->start
>= zp
) {
260 subreq
->source
= source
= NETFS_FILL_WITH_ZEROES
;
261 goto fill_with_zeroes
;
264 if (len
> zp
- subreq
->start
)
265 len
= zp
- subreq
->start
;
267 pr_err("ZERO-LEN READ: R=%08x[%x] l=%zx/%zx s=%llx z=%llx i=%llx",
268 rreq
->debug_id
, subreq
->debug_index
,
270 subreq
->start
, ictx
->zero_point
, rreq
->i_size
);
275 netfs_stat(&netfs_n_rh_download
);
276 if (rreq
->netfs_ops
->prepare_read
) {
277 ret
= rreq
->netfs_ops
->prepare_read(subreq
);
279 atomic_dec(&rreq
->nr_outstanding
);
280 netfs_put_subrequest(subreq
, false,
281 netfs_sreq_trace_put_cancel
);
284 trace_netfs_sreq(subreq
, netfs_sreq_trace_prepare
);
287 slice
= netfs_prepare_read_iterator(subreq
);
289 atomic_dec(&rreq
->nr_outstanding
);
290 netfs_put_subrequest(subreq
, false, netfs_sreq_trace_put_cancel
);
295 rreq
->netfs_ops
->issue_read(subreq
);
300 if (source
== NETFS_FILL_WITH_ZEROES
) {
301 subreq
->source
= NETFS_FILL_WITH_ZEROES
;
302 trace_netfs_sreq(subreq
, netfs_sreq_trace_submit
);
303 netfs_stat(&netfs_n_rh_zero
);
304 slice
= netfs_prepare_read_iterator(subreq
);
305 __set_bit(NETFS_SREQ_CLEAR_TAIL
, &subreq
->flags
);
306 netfs_read_subreq_terminated(subreq
, 0, false);
310 if (source
== NETFS_READ_FROM_CACHE
) {
311 trace_netfs_sreq(subreq
, netfs_sreq_trace_submit
);
312 slice
= netfs_prepare_read_iterator(subreq
);
313 netfs_read_cache_to_pagecache(rreq
, subreq
);
317 pr_err("Unexpected read source %u\n", source
);
327 if (atomic_dec_and_test(&rreq
->nr_outstanding
))
328 netfs_rreq_terminated(rreq
, false);
330 /* Defer error return as we may need to wait for outstanding I/O. */
331 cmpxchg(&rreq
->error
, 0, ret
);
335 * Wait for the read operation to complete, successfully or otherwise.
337 static int netfs_wait_for_read(struct netfs_io_request
*rreq
)
341 trace_netfs_rreq(rreq
, netfs_rreq_trace_wait_ip
);
342 wait_on_bit(&rreq
->flags
, NETFS_RREQ_IN_PROGRESS
, TASK_UNINTERRUPTIBLE
);
344 if (ret
== 0 && rreq
->submitted
< rreq
->len
) {
345 trace_netfs_failure(rreq
, NULL
, ret
, netfs_fail_short_read
);
353 * Set up the initial folioq of buffer folios in the rolling buffer and set the
354 * iterator to refer to it.
356 static int netfs_prime_buffer(struct netfs_io_request
*rreq
)
358 struct folio_queue
*folioq
;
359 struct folio_batch put_batch
;
362 folioq
= kmalloc(sizeof(*folioq
), GFP_KERNEL
);
365 netfs_stat(&netfs_n_folioq
);
367 rreq
->buffer
= folioq
;
368 rreq
->buffer_tail
= folioq
;
369 rreq
->submitted
= rreq
->start
;
370 iov_iter_folio_queue(&rreq
->iter
, ITER_DEST
, folioq
, 0, 0, 0);
372 folio_batch_init(&put_batch
);
373 added
= netfs_load_buffer_from_ra(rreq
, folioq
, &put_batch
);
374 folio_batch_release(&put_batch
);
375 rreq
->iter
.count
+= added
;
376 rreq
->submitted
+= added
;
381 * netfs_readahead - Helper to manage a read request
382 * @ractl: The description of the readahead request
384 * Fulfil a readahead request by drawing data from the cache if possible, or
385 * the netfs if not. Space beyond the EOF is zero-filled. Multiple I/O
386 * requests from different sources will get munged together. If necessary, the
387 * readahead window can be expanded in either direction to a more convenient
388 * alighment for RPC efficiency or to make storage in the cache feasible.
390 * The calling netfs must initialise a netfs context contiguous to the vfs
391 * inode before calling this.
393 * This is usable whether or not caching is enabled.
395 void netfs_readahead(struct readahead_control
*ractl
)
397 struct netfs_io_request
*rreq
;
398 struct netfs_inode
*ictx
= netfs_inode(ractl
->mapping
->host
);
399 unsigned long long start
= readahead_pos(ractl
);
400 size_t size
= readahead_length(ractl
);
403 rreq
= netfs_alloc_request(ractl
->mapping
, ractl
->file
, start
, size
,
408 ret
= netfs_begin_cache_read(rreq
, ictx
);
409 if (ret
== -ENOMEM
|| ret
== -EINTR
|| ret
== -ERESTARTSYS
)
412 netfs_stat(&netfs_n_rh_readahead
);
413 trace_netfs_read(rreq
, readahead_pos(ractl
), readahead_length(ractl
),
414 netfs_read_trace_readahead
);
416 netfs_rreq_expand(rreq
, ractl
);
419 if (netfs_prime_buffer(rreq
) < 0)
421 netfs_read_to_pagecache(rreq
);
423 netfs_put_request(rreq
, true, netfs_rreq_trace_put_return
);
427 netfs_put_request(rreq
, false, netfs_rreq_trace_put_failed
);
430 EXPORT_SYMBOL(netfs_readahead
);
433 * Create a rolling buffer with a single occupying folio.
435 static int netfs_create_singular_buffer(struct netfs_io_request
*rreq
, struct folio
*folio
)
437 struct folio_queue
*folioq
;
439 folioq
= kmalloc(sizeof(*folioq
), GFP_KERNEL
);
443 netfs_stat(&netfs_n_folioq
);
445 folioq_append(folioq
, folio
);
446 BUG_ON(folioq_folio(folioq
, 0) != folio
);
447 BUG_ON(folioq_folio_order(folioq
, 0) != folio_order(folio
));
448 rreq
->buffer
= folioq
;
449 rreq
->buffer_tail
= folioq
;
450 rreq
->submitted
= rreq
->start
+ rreq
->len
;
451 iov_iter_folio_queue(&rreq
->iter
, ITER_DEST
, folioq
, 0, 0, rreq
->len
);
452 rreq
->ractl
= (struct readahead_control
*)1UL;
457 * Read into gaps in a folio partially filled by a streaming write.
459 static int netfs_read_gaps(struct file
*file
, struct folio
*folio
)
461 struct netfs_io_request
*rreq
;
462 struct address_space
*mapping
= folio
->mapping
;
463 struct netfs_folio
*finfo
= netfs_folio_info(folio
);
464 struct netfs_inode
*ctx
= netfs_inode(mapping
->host
);
465 struct folio
*sink
= NULL
;
466 struct bio_vec
*bvec
;
467 unsigned int from
= finfo
->dirty_offset
;
468 unsigned int to
= from
+ finfo
->dirty_len
;
469 unsigned int off
= 0, i
= 0;
470 size_t flen
= folio_size(folio
);
471 size_t nr_bvec
= flen
/ PAGE_SIZE
+ 2;
475 _enter("%lx", folio
->index
);
477 rreq
= netfs_alloc_request(mapping
, file
, folio_pos(folio
), flen
, NETFS_READ_GAPS
);
483 ret
= netfs_begin_cache_read(rreq
, ctx
);
484 if (ret
== -ENOMEM
|| ret
== -EINTR
|| ret
== -ERESTARTSYS
)
487 netfs_stat(&netfs_n_rh_read_folio
);
488 trace_netfs_read(rreq
, rreq
->start
, rreq
->len
, netfs_read_trace_read_gaps
);
490 /* Fiddle the buffer so that a gap at the beginning and/or a gap at the
491 * end get copied to, but the middle is discarded.
494 bvec
= kmalloc_array(nr_bvec
, sizeof(*bvec
), GFP_KERNEL
);
498 sink
= folio_alloc(GFP_KERNEL
, 0);
504 trace_netfs_folio(folio
, netfs_folio_trace_read_gaps
);
506 rreq
->direct_bv
= bvec
;
507 rreq
->direct_bv_count
= nr_bvec
;
509 bvec_set_folio(&bvec
[i
++], folio
, from
, 0);
513 part
= min_t(size_t, to
- off
, PAGE_SIZE
);
514 bvec_set_folio(&bvec
[i
++], sink
, part
, 0);
518 bvec_set_folio(&bvec
[i
++], folio
, flen
- to
, to
);
519 iov_iter_bvec(&rreq
->iter
, ITER_DEST
, bvec
, i
, rreq
->len
);
520 rreq
->submitted
= rreq
->start
+ flen
;
522 netfs_read_to_pagecache(rreq
);
527 ret
= netfs_wait_for_read(rreq
);
529 flush_dcache_folio(folio
);
530 folio_mark_uptodate(folio
);
533 netfs_put_request(rreq
, false, netfs_rreq_trace_put_return
);
534 return ret
< 0 ? ret
: 0;
537 netfs_put_request(rreq
, false, netfs_rreq_trace_put_discard
);
544 * netfs_read_folio - Helper to manage a read_folio request
545 * @file: The file to read from
546 * @folio: The folio to read
548 * Fulfil a read_folio request by drawing data from the cache if
549 * possible, or the netfs if not. Space beyond the EOF is zero-filled.
550 * Multiple I/O requests from different sources will get munged together.
552 * The calling netfs must initialise a netfs context contiguous to the vfs
553 * inode before calling this.
555 * This is usable whether or not caching is enabled.
557 int netfs_read_folio(struct file
*file
, struct folio
*folio
)
559 struct address_space
*mapping
= folio
->mapping
;
560 struct netfs_io_request
*rreq
;
561 struct netfs_inode
*ctx
= netfs_inode(mapping
->host
);
564 if (folio_test_dirty(folio
)) {
565 trace_netfs_folio(folio
, netfs_folio_trace_read_gaps
);
566 return netfs_read_gaps(file
, folio
);
569 _enter("%lx", folio
->index
);
571 rreq
= netfs_alloc_request(mapping
, file
,
572 folio_pos(folio
), folio_size(folio
),
579 ret
= netfs_begin_cache_read(rreq
, ctx
);
580 if (ret
== -ENOMEM
|| ret
== -EINTR
|| ret
== -ERESTARTSYS
)
583 netfs_stat(&netfs_n_rh_read_folio
);
584 trace_netfs_read(rreq
, rreq
->start
, rreq
->len
, netfs_read_trace_readpage
);
586 /* Set up the output buffer */
587 ret
= netfs_create_singular_buffer(rreq
, folio
);
591 netfs_read_to_pagecache(rreq
);
592 ret
= netfs_wait_for_read(rreq
);
593 netfs_put_request(rreq
, false, netfs_rreq_trace_put_return
);
594 return ret
< 0 ? ret
: 0;
597 netfs_put_request(rreq
, false, netfs_rreq_trace_put_discard
);
602 EXPORT_SYMBOL(netfs_read_folio
);
605 * Prepare a folio for writing without reading first
606 * @folio: The folio being prepared
607 * @pos: starting position for the write
608 * @len: length of write
609 * @always_fill: T if the folio should always be completely filled/cleared
611 * In some cases, write_begin doesn't need to read at all:
613 * - write that lies in a folio that is completely beyond EOF
614 * - write that covers the folio from start to EOF or beyond it
616 * If any of these criteria are met, then zero out the unwritten parts
617 * of the folio and return true. Otherwise, return false.
619 static bool netfs_skip_folio_read(struct folio
*folio
, loff_t pos
, size_t len
,
622 struct inode
*inode
= folio_inode(folio
);
623 loff_t i_size
= i_size_read(inode
);
624 size_t offset
= offset_in_folio(folio
, pos
);
625 size_t plen
= folio_size(folio
);
627 if (unlikely(always_fill
)) {
628 if (pos
- offset
+ len
<= i_size
)
629 return false; /* Page entirely before EOF */
630 folio_zero_segment(folio
, 0, plen
);
631 folio_mark_uptodate(folio
);
635 /* Full folio write */
636 if (offset
== 0 && len
>= plen
)
639 /* Page entirely beyond the end of the file */
640 if (pos
- offset
>= i_size
)
643 /* Write that covers from the start of the folio to EOF or beyond */
644 if (offset
== 0 && (pos
+ len
) >= i_size
)
649 folio_zero_segments(folio
, 0, offset
, offset
+ len
, plen
);
654 * netfs_write_begin - Helper to prepare for writing [DEPRECATED]
655 * @ctx: The netfs context
656 * @file: The file to read from
657 * @mapping: The mapping to read from
658 * @pos: File position at which the write will begin
659 * @len: The length of the write (may extend beyond the end of the folio chosen)
660 * @_folio: Where to put the resultant folio
661 * @_fsdata: Place for the netfs to store a cookie
663 * Pre-read data for a write-begin request by drawing data from the cache if
664 * possible, or the netfs if not. Space beyond the EOF is zero-filled.
665 * Multiple I/O requests from different sources will get munged together.
667 * The calling netfs must provide a table of operations, only one of which,
668 * issue_read, is mandatory.
670 * The check_write_begin() operation can be provided to check for and flush
671 * conflicting writes once the folio is grabbed and locked. It is passed a
672 * pointer to the fsdata cookie that gets returned to the VM to be passed to
673 * write_end. It is permitted to sleep. It should return 0 if the request
674 * should go ahead or it may return an error. It may also unlock and put the
675 * folio, provided it sets ``*foliop`` to NULL, in which case a return of 0
676 * will cause the folio to be re-got and the process to be retried.
678 * The calling netfs must initialise a netfs context contiguous to the vfs
679 * inode before calling this.
681 * This is usable whether or not caching is enabled.
683 * Note that this should be considered deprecated and netfs_perform_write()
686 int netfs_write_begin(struct netfs_inode
*ctx
,
687 struct file
*file
, struct address_space
*mapping
,
688 loff_t pos
, unsigned int len
, struct folio
**_folio
,
691 struct netfs_io_request
*rreq
;
693 pgoff_t index
= pos
>> PAGE_SHIFT
;
697 folio
= __filemap_get_folio(mapping
, index
, FGP_WRITEBEGIN
,
698 mapping_gfp_mask(mapping
));
700 return PTR_ERR(folio
);
702 if (ctx
->ops
->check_write_begin
) {
703 /* Allow the netfs (eg. ceph) to flush conflicts. */
704 ret
= ctx
->ops
->check_write_begin(file
, pos
, len
, &folio
, _fsdata
);
706 trace_netfs_failure(NULL
, NULL
, ret
, netfs_fail_check_write_begin
);
713 if (folio_test_uptodate(folio
))
716 /* If the folio is beyond the EOF, we want to clear it - unless it's
717 * within the cache granule containing the EOF, in which case we need
718 * to preload the granule.
720 if (!netfs_is_cache_enabled(ctx
) &&
721 netfs_skip_folio_read(folio
, pos
, len
, false)) {
722 netfs_stat(&netfs_n_rh_write_zskip
);
723 goto have_folio_no_wait
;
726 rreq
= netfs_alloc_request(mapping
, file
,
727 folio_pos(folio
), folio_size(folio
),
728 NETFS_READ_FOR_WRITE
);
733 rreq
->no_unlock_folio
= folio
->index
;
734 __set_bit(NETFS_RREQ_NO_UNLOCK_FOLIO
, &rreq
->flags
);
736 ret
= netfs_begin_cache_read(rreq
, ctx
);
737 if (ret
== -ENOMEM
|| ret
== -EINTR
|| ret
== -ERESTARTSYS
)
740 netfs_stat(&netfs_n_rh_write_begin
);
741 trace_netfs_read(rreq
, pos
, len
, netfs_read_trace_write_begin
);
743 /* Set up the output buffer */
744 ret
= netfs_create_singular_buffer(rreq
, folio
);
748 netfs_read_to_pagecache(rreq
);
749 ret
= netfs_wait_for_read(rreq
);
752 netfs_put_request(rreq
, false, netfs_rreq_trace_put_return
);
755 ret
= folio_wait_private_2_killable(folio
);
764 netfs_put_request(rreq
, false, netfs_rreq_trace_put_failed
);
770 _leave(" = %d", ret
);
773 EXPORT_SYMBOL(netfs_write_begin
);
776 * Preload the data into a folio we're proposing to write into.
778 int netfs_prefetch_for_write(struct file
*file
, struct folio
*folio
,
779 size_t offset
, size_t len
)
781 struct netfs_io_request
*rreq
;
782 struct address_space
*mapping
= folio
->mapping
;
783 struct netfs_inode
*ctx
= netfs_inode(mapping
->host
);
784 unsigned long long start
= folio_pos(folio
);
785 size_t flen
= folio_size(folio
);
788 _enter("%zx @%llx", flen
, start
);
792 rreq
= netfs_alloc_request(mapping
, file
, start
, flen
,
793 NETFS_READ_FOR_WRITE
);
799 rreq
->no_unlock_folio
= folio
->index
;
800 __set_bit(NETFS_RREQ_NO_UNLOCK_FOLIO
, &rreq
->flags
);
801 ret
= netfs_begin_cache_read(rreq
, ctx
);
802 if (ret
== -ENOMEM
|| ret
== -EINTR
|| ret
== -ERESTARTSYS
)
805 netfs_stat(&netfs_n_rh_write_begin
);
806 trace_netfs_read(rreq
, start
, flen
, netfs_read_trace_prefetch_for_write
);
808 /* Set up the output buffer */
809 ret
= netfs_create_singular_buffer(rreq
, folio
);
813 folioq_mark2(rreq
->buffer
, 0);
814 netfs_read_to_pagecache(rreq
);
815 ret
= netfs_wait_for_read(rreq
);
816 netfs_put_request(rreq
, false, netfs_rreq_trace_put_return
);
820 netfs_put_request(rreq
, false, netfs_rreq_trace_put_discard
);
822 _leave(" = %d", ret
);
827 * netfs_buffered_read_iter - Filesystem buffered I/O read routine
828 * @iocb: kernel I/O control block
829 * @iter: destination for the data read
831 * This is the ->read_iter() routine for all filesystems that can use the page
834 * The IOCB_NOWAIT flag in iocb->ki_flags indicates that -EAGAIN shall be
835 * returned when no data can be read without waiting for I/O requests to
836 * complete; it doesn't prevent readahead.
838 * The IOCB_NOIO flag in iocb->ki_flags indicates that no new I/O requests
839 * shall be made for the read or for readahead. When no data can be read,
840 * -EAGAIN shall be returned. When readahead would be triggered, a partial,
841 * possibly empty read shall be returned.
844 * * number of bytes copied, even for partial reads
845 * * negative error code (or 0 if IOCB_NOIO) if nothing was read
847 ssize_t
netfs_buffered_read_iter(struct kiocb
*iocb
, struct iov_iter
*iter
)
849 struct inode
*inode
= file_inode(iocb
->ki_filp
);
850 struct netfs_inode
*ictx
= netfs_inode(inode
);
853 if (WARN_ON_ONCE((iocb
->ki_flags
& IOCB_DIRECT
) ||
854 test_bit(NETFS_ICTX_UNBUFFERED
, &ictx
->flags
)))
857 ret
= netfs_start_io_read(inode
);
859 ret
= filemap_read(iocb
, iter
, 0);
860 netfs_end_io_read(inode
);
864 EXPORT_SYMBOL(netfs_buffered_read_iter
);
867 * netfs_file_read_iter - Generic filesystem read routine
868 * @iocb: kernel I/O control block
869 * @iter: destination for the data read
871 * This is the ->read_iter() routine for all filesystems that can use the page
874 * The IOCB_NOWAIT flag in iocb->ki_flags indicates that -EAGAIN shall be
875 * returned when no data can be read without waiting for I/O requests to
876 * complete; it doesn't prevent readahead.
878 * The IOCB_NOIO flag in iocb->ki_flags indicates that no new I/O requests
879 * shall be made for the read or for readahead. When no data can be read,
880 * -EAGAIN shall be returned. When readahead would be triggered, a partial,
881 * possibly empty read shall be returned.
884 * * number of bytes copied, even for partial reads
885 * * negative error code (or 0 if IOCB_NOIO) if nothing was read
887 ssize_t
netfs_file_read_iter(struct kiocb
*iocb
, struct iov_iter
*iter
)
889 struct netfs_inode
*ictx
= netfs_inode(iocb
->ki_filp
->f_mapping
->host
);
891 if ((iocb
->ki_flags
& IOCB_DIRECT
) ||
892 test_bit(NETFS_ICTX_UNBUFFERED
, &ictx
->flags
))
893 return netfs_unbuffered_read_iter(iocb
, iter
);
895 return netfs_buffered_read_iter(iocb
, iter
);
897 EXPORT_SYMBOL(netfs_file_read_iter
);