]> Git Repo - linux.git/blob - fs/netfs/io.c
netfs: Fix missing iterator reset on retry of short read
[linux.git] / fs / netfs / io.c
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /* Network filesystem high-level read support.
3  *
4  * Copyright (C) 2021 Red Hat, Inc. All Rights Reserved.
5  * Written by David Howells ([email protected])
6  */
7
8 #include <linux/module.h>
9 #include <linux/export.h>
10 #include <linux/fs.h>
11 #include <linux/mm.h>
12 #include <linux/pagemap.h>
13 #include <linux/slab.h>
14 #include <linux/uio.h>
15 #include <linux/sched/mm.h>
16 #include <linux/task_io_accounting_ops.h>
17 #include "internal.h"
18
19 /*
20  * Clear the unread part of an I/O request.
21  */
22 static void netfs_clear_unread(struct netfs_io_subrequest *subreq)
23 {
24         iov_iter_zero(iov_iter_count(&subreq->io_iter), &subreq->io_iter);
25 }
26
27 static void netfs_cache_read_terminated(void *priv, ssize_t transferred_or_error,
28                                         bool was_async)
29 {
30         struct netfs_io_subrequest *subreq = priv;
31
32         netfs_subreq_terminated(subreq, transferred_or_error, was_async);
33 }
34
35 /*
36  * Issue a read against the cache.
37  * - Eats the caller's ref on subreq.
38  */
39 static void netfs_read_from_cache(struct netfs_io_request *rreq,
40                                   struct netfs_io_subrequest *subreq,
41                                   enum netfs_read_from_hole read_hole)
42 {
43         struct netfs_cache_resources *cres = &rreq->cache_resources;
44
45         netfs_stat(&netfs_n_rh_read);
46         cres->ops->read(cres, subreq->start, &subreq->io_iter, read_hole,
47                         netfs_cache_read_terminated, subreq);
48 }
49
50 /*
51  * Fill a subrequest region with zeroes.
52  */
53 static void netfs_fill_with_zeroes(struct netfs_io_request *rreq,
54                                    struct netfs_io_subrequest *subreq)
55 {
56         netfs_stat(&netfs_n_rh_zero);
57         __set_bit(NETFS_SREQ_CLEAR_TAIL, &subreq->flags);
58         netfs_subreq_terminated(subreq, 0, false);
59 }
60
61 /*
62  * Ask the netfs to issue a read request to the server for us.
63  *
64  * The netfs is expected to read from subreq->pos + subreq->transferred to
65  * subreq->pos + subreq->len - 1.  It may not backtrack and write data into the
66  * buffer prior to the transferred point as it might clobber dirty data
67  * obtained from the cache.
68  *
69  * Alternatively, the netfs is allowed to indicate one of two things:
70  *
71  * - NETFS_SREQ_SHORT_READ: A short read - it will get called again to try and
72  *   make progress.
73  *
74  * - NETFS_SREQ_CLEAR_TAIL: A short read - the rest of the buffer will be
75  *   cleared.
76  */
77 static void netfs_read_from_server(struct netfs_io_request *rreq,
78                                    struct netfs_io_subrequest *subreq)
79 {
80         netfs_stat(&netfs_n_rh_download);
81
82         if (rreq->origin != NETFS_DIO_READ &&
83             iov_iter_count(&subreq->io_iter) != subreq->len - subreq->transferred)
84                 pr_warn("R=%08x[%u] ITER PRE-MISMATCH %zx != %zx-%zx %lx\n",
85                         rreq->debug_id, subreq->debug_index,
86                         iov_iter_count(&subreq->io_iter), subreq->len,
87                         subreq->transferred, subreq->flags);
88         rreq->netfs_ops->issue_read(subreq);
89 }
90
91 /*
92  * Release those waiting.
93  */
94 static void netfs_rreq_completed(struct netfs_io_request *rreq, bool was_async)
95 {
96         trace_netfs_rreq(rreq, netfs_rreq_trace_done);
97         netfs_clear_subrequests(rreq, was_async);
98         netfs_put_request(rreq, was_async, netfs_rreq_trace_put_complete);
99 }
100
101 /*
102  * [DEPRECATED] Deal with the completion of writing the data to the cache.  We
103  * have to clear the PG_fscache bits on the folios involved and release the
104  * caller's ref.
105  *
106  * May be called in softirq mode and we inherit a ref from the caller.
107  */
108 static void netfs_rreq_unmark_after_write(struct netfs_io_request *rreq,
109                                           bool was_async)
110 {
111         struct netfs_io_subrequest *subreq;
112         struct folio *folio;
113         pgoff_t unlocked = 0;
114         bool have_unlocked = false;
115
116         rcu_read_lock();
117
118         list_for_each_entry(subreq, &rreq->subrequests, rreq_link) {
119                 XA_STATE(xas, &rreq->mapping->i_pages, subreq->start / PAGE_SIZE);
120
121                 xas_for_each(&xas, folio, (subreq->start + subreq->len - 1) / PAGE_SIZE) {
122                         if (xas_retry(&xas, folio))
123                                 continue;
124
125                         /* We might have multiple writes from the same huge
126                          * folio, but we mustn't unlock a folio more than once.
127                          */
128                         if (have_unlocked && folio->index <= unlocked)
129                                 continue;
130                         unlocked = folio_next_index(folio) - 1;
131                         trace_netfs_folio(folio, netfs_folio_trace_end_copy);
132                         folio_end_private_2(folio);
133                         have_unlocked = true;
134                 }
135         }
136
137         rcu_read_unlock();
138         netfs_rreq_completed(rreq, was_async);
139 }
140
141 static void netfs_rreq_copy_terminated(void *priv, ssize_t transferred_or_error,
142                                        bool was_async) /* [DEPRECATED] */
143 {
144         struct netfs_io_subrequest *subreq = priv;
145         struct netfs_io_request *rreq = subreq->rreq;
146
147         if (IS_ERR_VALUE(transferred_or_error)) {
148                 netfs_stat(&netfs_n_rh_write_failed);
149                 trace_netfs_failure(rreq, subreq, transferred_or_error,
150                                     netfs_fail_copy_to_cache);
151         } else {
152                 netfs_stat(&netfs_n_rh_write_done);
153         }
154
155         trace_netfs_sreq(subreq, netfs_sreq_trace_write_term);
156
157         /* If we decrement nr_copy_ops to 0, the ref belongs to us. */
158         if (atomic_dec_and_test(&rreq->nr_copy_ops))
159                 netfs_rreq_unmark_after_write(rreq, was_async);
160
161         netfs_put_subrequest(subreq, was_async, netfs_sreq_trace_put_terminated);
162 }
163
164 /*
165  * [DEPRECATED] Perform any outstanding writes to the cache.  We inherit a ref
166  * from the caller.
167  */
168 static void netfs_rreq_do_write_to_cache(struct netfs_io_request *rreq)
169 {
170         struct netfs_cache_resources *cres = &rreq->cache_resources;
171         struct netfs_io_subrequest *subreq, *next, *p;
172         struct iov_iter iter;
173         int ret;
174
175         trace_netfs_rreq(rreq, netfs_rreq_trace_copy);
176
177         /* We don't want terminating writes trying to wake us up whilst we're
178          * still going through the list.
179          */
180         atomic_inc(&rreq->nr_copy_ops);
181
182         list_for_each_entry_safe(subreq, p, &rreq->subrequests, rreq_link) {
183                 if (!test_bit(NETFS_SREQ_COPY_TO_CACHE, &subreq->flags)) {
184                         list_del_init(&subreq->rreq_link);
185                         netfs_put_subrequest(subreq, false,
186                                              netfs_sreq_trace_put_no_copy);
187                 }
188         }
189
190         list_for_each_entry(subreq, &rreq->subrequests, rreq_link) {
191                 /* Amalgamate adjacent writes */
192                 while (!list_is_last(&subreq->rreq_link, &rreq->subrequests)) {
193                         next = list_next_entry(subreq, rreq_link);
194                         if (next->start != subreq->start + subreq->len)
195                                 break;
196                         subreq->len += next->len;
197                         list_del_init(&next->rreq_link);
198                         netfs_put_subrequest(next, false,
199                                              netfs_sreq_trace_put_merged);
200                 }
201
202                 ret = cres->ops->prepare_write(cres, &subreq->start, &subreq->len,
203                                                subreq->len, rreq->i_size, true);
204                 if (ret < 0) {
205                         trace_netfs_failure(rreq, subreq, ret, netfs_fail_prepare_write);
206                         trace_netfs_sreq(subreq, netfs_sreq_trace_write_skip);
207                         continue;
208                 }
209
210                 iov_iter_xarray(&iter, ITER_SOURCE, &rreq->mapping->i_pages,
211                                 subreq->start, subreq->len);
212
213                 atomic_inc(&rreq->nr_copy_ops);
214                 netfs_stat(&netfs_n_rh_write);
215                 netfs_get_subrequest(subreq, netfs_sreq_trace_get_copy_to_cache);
216                 trace_netfs_sreq(subreq, netfs_sreq_trace_write);
217                 cres->ops->write(cres, subreq->start, &iter,
218                                  netfs_rreq_copy_terminated, subreq);
219         }
220
221         /* If we decrement nr_copy_ops to 0, the usage ref belongs to us. */
222         if (atomic_dec_and_test(&rreq->nr_copy_ops))
223                 netfs_rreq_unmark_after_write(rreq, false);
224 }
225
226 static void netfs_rreq_write_to_cache_work(struct work_struct *work) /* [DEPRECATED] */
227 {
228         struct netfs_io_request *rreq =
229                 container_of(work, struct netfs_io_request, work);
230
231         netfs_rreq_do_write_to_cache(rreq);
232 }
233
234 static void netfs_rreq_write_to_cache(struct netfs_io_request *rreq) /* [DEPRECATED] */
235 {
236         rreq->work.func = netfs_rreq_write_to_cache_work;
237         if (!queue_work(system_unbound_wq, &rreq->work))
238                 BUG();
239 }
240
241 /*
242  * Handle a short read.
243  */
244 static void netfs_rreq_short_read(struct netfs_io_request *rreq,
245                                   struct netfs_io_subrequest *subreq)
246 {
247         __clear_bit(NETFS_SREQ_SHORT_IO, &subreq->flags);
248         __set_bit(NETFS_SREQ_SEEK_DATA_READ, &subreq->flags);
249
250         netfs_stat(&netfs_n_rh_short_read);
251         trace_netfs_sreq(subreq, netfs_sreq_trace_resubmit_short);
252
253         netfs_get_subrequest(subreq, netfs_sreq_trace_get_short_read);
254         atomic_inc(&rreq->nr_outstanding);
255         if (subreq->source == NETFS_READ_FROM_CACHE)
256                 netfs_read_from_cache(rreq, subreq, NETFS_READ_HOLE_CLEAR);
257         else
258                 netfs_read_from_server(rreq, subreq);
259 }
260
261 /*
262  * Reset the subrequest iterator prior to resubmission.
263  */
264 static void netfs_reset_subreq_iter(struct netfs_io_request *rreq,
265                                     struct netfs_io_subrequest *subreq)
266 {
267         size_t remaining = subreq->len - subreq->transferred;
268         size_t count = iov_iter_count(&subreq->io_iter);
269
270         if (count == remaining)
271                 return;
272
273         _debug("R=%08x[%u] ITER RESUB-MISMATCH %zx != %zx-%zx-%llx %x\n",
274                rreq->debug_id, subreq->debug_index,
275                iov_iter_count(&subreq->io_iter), subreq->transferred,
276                subreq->len, rreq->i_size,
277                subreq->io_iter.iter_type);
278
279         if (count < remaining)
280                 iov_iter_revert(&subreq->io_iter, remaining - count);
281         else
282                 iov_iter_advance(&subreq->io_iter, count - remaining);
283 }
284
285 /*
286  * Resubmit any short or failed operations.  Returns true if we got the rreq
287  * ref back.
288  */
289 static bool netfs_rreq_perform_resubmissions(struct netfs_io_request *rreq)
290 {
291         struct netfs_io_subrequest *subreq;
292
293         WARN_ON(in_interrupt());
294
295         trace_netfs_rreq(rreq, netfs_rreq_trace_resubmit);
296
297         /* We don't want terminating submissions trying to wake us up whilst
298          * we're still going through the list.
299          */
300         atomic_inc(&rreq->nr_outstanding);
301
302         __clear_bit(NETFS_RREQ_INCOMPLETE_IO, &rreq->flags);
303         list_for_each_entry(subreq, &rreq->subrequests, rreq_link) {
304                 if (subreq->error) {
305                         if (subreq->source != NETFS_READ_FROM_CACHE)
306                                 break;
307                         subreq->source = NETFS_DOWNLOAD_FROM_SERVER;
308                         subreq->error = 0;
309                         netfs_stat(&netfs_n_rh_download_instead);
310                         trace_netfs_sreq(subreq, netfs_sreq_trace_download_instead);
311                         netfs_get_subrequest(subreq, netfs_sreq_trace_get_resubmit);
312                         atomic_inc(&rreq->nr_outstanding);
313                         netfs_reset_subreq_iter(rreq, subreq);
314                         netfs_read_from_server(rreq, subreq);
315                 } else if (test_bit(NETFS_SREQ_SHORT_IO, &subreq->flags)) {
316                         netfs_reset_subreq_iter(rreq, subreq);
317                         netfs_rreq_short_read(rreq, subreq);
318                 }
319         }
320
321         /* If we decrement nr_outstanding to 0, the usage ref belongs to us. */
322         if (atomic_dec_and_test(&rreq->nr_outstanding))
323                 return true;
324
325         wake_up_var(&rreq->nr_outstanding);
326         return false;
327 }
328
329 /*
330  * Check to see if the data read is still valid.
331  */
332 static void netfs_rreq_is_still_valid(struct netfs_io_request *rreq)
333 {
334         struct netfs_io_subrequest *subreq;
335
336         if (!rreq->netfs_ops->is_still_valid ||
337             rreq->netfs_ops->is_still_valid(rreq))
338                 return;
339
340         list_for_each_entry(subreq, &rreq->subrequests, rreq_link) {
341                 if (subreq->source == NETFS_READ_FROM_CACHE) {
342                         subreq->error = -ESTALE;
343                         __set_bit(NETFS_RREQ_INCOMPLETE_IO, &rreq->flags);
344                 }
345         }
346 }
347
348 /*
349  * Determine how much we can admit to having read from a DIO read.
350  */
351 static void netfs_rreq_assess_dio(struct netfs_io_request *rreq)
352 {
353         struct netfs_io_subrequest *subreq;
354         unsigned int i;
355         size_t transferred = 0;
356
357         for (i = 0; i < rreq->direct_bv_count; i++) {
358                 flush_dcache_page(rreq->direct_bv[i].bv_page);
359                 // TODO: cifs marks pages in the destination buffer
360                 // dirty under some circumstances after a read.  Do we
361                 // need to do that too?
362                 set_page_dirty(rreq->direct_bv[i].bv_page);
363         }
364
365         list_for_each_entry(subreq, &rreq->subrequests, rreq_link) {
366                 if (subreq->error || subreq->transferred == 0)
367                         break;
368                 transferred += subreq->transferred;
369                 if (subreq->transferred < subreq->len)
370                         break;
371         }
372
373         for (i = 0; i < rreq->direct_bv_count; i++)
374                 flush_dcache_page(rreq->direct_bv[i].bv_page);
375
376         rreq->transferred = transferred;
377         task_io_account_read(transferred);
378
379         if (rreq->iocb) {
380                 rreq->iocb->ki_pos += transferred;
381                 if (rreq->iocb->ki_complete)
382                         rreq->iocb->ki_complete(
383                                 rreq->iocb, rreq->error ? rreq->error : transferred);
384         }
385         if (rreq->netfs_ops->done)
386                 rreq->netfs_ops->done(rreq);
387         inode_dio_end(rreq->inode);
388 }
389
390 /*
391  * Assess the state of a read request and decide what to do next.
392  *
393  * Note that we could be in an ordinary kernel thread, on a workqueue or in
394  * softirq context at this point.  We inherit a ref from the caller.
395  */
396 static void netfs_rreq_assess(struct netfs_io_request *rreq, bool was_async)
397 {
398         trace_netfs_rreq(rreq, netfs_rreq_trace_assess);
399
400 again:
401         netfs_rreq_is_still_valid(rreq);
402
403         if (!test_bit(NETFS_RREQ_FAILED, &rreq->flags) &&
404             test_bit(NETFS_RREQ_INCOMPLETE_IO, &rreq->flags)) {
405                 if (netfs_rreq_perform_resubmissions(rreq))
406                         goto again;
407                 return;
408         }
409
410         if (rreq->origin != NETFS_DIO_READ)
411                 netfs_rreq_unlock_folios(rreq);
412         else
413                 netfs_rreq_assess_dio(rreq);
414
415         trace_netfs_rreq(rreq, netfs_rreq_trace_wake_ip);
416         clear_bit_unlock(NETFS_RREQ_IN_PROGRESS, &rreq->flags);
417         wake_up_bit(&rreq->flags, NETFS_RREQ_IN_PROGRESS);
418
419         if (test_bit(NETFS_RREQ_COPY_TO_CACHE, &rreq->flags) &&
420             test_bit(NETFS_RREQ_USE_PGPRIV2, &rreq->flags))
421                 return netfs_rreq_write_to_cache(rreq);
422
423         netfs_rreq_completed(rreq, was_async);
424 }
425
426 static void netfs_rreq_work(struct work_struct *work)
427 {
428         struct netfs_io_request *rreq =
429                 container_of(work, struct netfs_io_request, work);
430         netfs_rreq_assess(rreq, false);
431 }
432
433 /*
434  * Handle the completion of all outstanding I/O operations on a read request.
435  * We inherit a ref from the caller.
436  */
437 static void netfs_rreq_terminated(struct netfs_io_request *rreq,
438                                   bool was_async)
439 {
440         if (test_bit(NETFS_RREQ_INCOMPLETE_IO, &rreq->flags) &&
441             was_async) {
442                 if (!queue_work(system_unbound_wq, &rreq->work))
443                         BUG();
444         } else {
445                 netfs_rreq_assess(rreq, was_async);
446         }
447 }
448
449 /**
450  * netfs_subreq_terminated - Note the termination of an I/O operation.
451  * @subreq: The I/O request that has terminated.
452  * @transferred_or_error: The amount of data transferred or an error code.
453  * @was_async: The termination was asynchronous
454  *
455  * This tells the read helper that a contributory I/O operation has terminated,
456  * one way or another, and that it should integrate the results.
457  *
458  * The caller indicates in @transferred_or_error the outcome of the operation,
459  * supplying a positive value to indicate the number of bytes transferred, 0 to
460  * indicate a failure to transfer anything that should be retried or a negative
461  * error code.  The helper will look after reissuing I/O operations as
462  * appropriate and writing downloaded data to the cache.
463  *
464  * If @was_async is true, the caller might be running in softirq or interrupt
465  * context and we can't sleep.
466  */
467 void netfs_subreq_terminated(struct netfs_io_subrequest *subreq,
468                              ssize_t transferred_or_error,
469                              bool was_async)
470 {
471         struct netfs_io_request *rreq = subreq->rreq;
472         int u;
473
474         _enter("R=%x[%x]{%llx,%lx},%zd",
475                rreq->debug_id, subreq->debug_index,
476                subreq->start, subreq->flags, transferred_or_error);
477
478         switch (subreq->source) {
479         case NETFS_READ_FROM_CACHE:
480                 netfs_stat(&netfs_n_rh_read_done);
481                 break;
482         case NETFS_DOWNLOAD_FROM_SERVER:
483                 netfs_stat(&netfs_n_rh_download_done);
484                 break;
485         default:
486                 break;
487         }
488
489         if (IS_ERR_VALUE(transferred_or_error)) {
490                 subreq->error = transferred_or_error;
491                 trace_netfs_failure(rreq, subreq, transferred_or_error,
492                                     netfs_fail_read);
493                 goto failed;
494         }
495
496         if (WARN(transferred_or_error > subreq->len - subreq->transferred,
497                  "Subreq overread: R%x[%x] %zd > %zu - %zu",
498                  rreq->debug_id, subreq->debug_index,
499                  transferred_or_error, subreq->len, subreq->transferred))
500                 transferred_or_error = subreq->len - subreq->transferred;
501
502         subreq->error = 0;
503         subreq->transferred += transferred_or_error;
504         if (subreq->transferred < subreq->len)
505                 goto incomplete;
506
507 complete:
508         __clear_bit(NETFS_SREQ_NO_PROGRESS, &subreq->flags);
509         if (test_bit(NETFS_SREQ_COPY_TO_CACHE, &subreq->flags))
510                 set_bit(NETFS_RREQ_COPY_TO_CACHE, &rreq->flags);
511
512 out:
513         trace_netfs_sreq(subreq, netfs_sreq_trace_terminated);
514
515         /* If we decrement nr_outstanding to 0, the ref belongs to us. */
516         u = atomic_dec_return(&rreq->nr_outstanding);
517         if (u == 0)
518                 netfs_rreq_terminated(rreq, was_async);
519         else if (u == 1)
520                 wake_up_var(&rreq->nr_outstanding);
521
522         netfs_put_subrequest(subreq, was_async, netfs_sreq_trace_put_terminated);
523         return;
524
525 incomplete:
526         if (test_bit(NETFS_SREQ_CLEAR_TAIL, &subreq->flags)) {
527                 netfs_clear_unread(subreq);
528                 subreq->transferred = subreq->len;
529                 goto complete;
530         }
531
532         if (transferred_or_error == 0) {
533                 if (__test_and_set_bit(NETFS_SREQ_NO_PROGRESS, &subreq->flags)) {
534                         if (rreq->origin != NETFS_DIO_READ)
535                                 subreq->error = -ENODATA;
536                         goto failed;
537                 }
538         } else {
539                 __clear_bit(NETFS_SREQ_NO_PROGRESS, &subreq->flags);
540         }
541
542         __set_bit(NETFS_SREQ_SHORT_IO, &subreq->flags);
543         set_bit(NETFS_RREQ_INCOMPLETE_IO, &rreq->flags);
544         goto out;
545
546 failed:
547         if (subreq->source == NETFS_READ_FROM_CACHE) {
548                 netfs_stat(&netfs_n_rh_read_failed);
549                 set_bit(NETFS_RREQ_INCOMPLETE_IO, &rreq->flags);
550         } else {
551                 netfs_stat(&netfs_n_rh_download_failed);
552                 set_bit(NETFS_RREQ_FAILED, &rreq->flags);
553                 rreq->error = subreq->error;
554         }
555         goto out;
556 }
557 EXPORT_SYMBOL(netfs_subreq_terminated);
558
559 static enum netfs_io_source netfs_cache_prepare_read(struct netfs_io_subrequest *subreq,
560                                                        loff_t i_size)
561 {
562         struct netfs_io_request *rreq = subreq->rreq;
563         struct netfs_cache_resources *cres = &rreq->cache_resources;
564
565         if (cres->ops)
566                 return cres->ops->prepare_read(subreq, i_size);
567         if (subreq->start >= rreq->i_size)
568                 return NETFS_FILL_WITH_ZEROES;
569         return NETFS_DOWNLOAD_FROM_SERVER;
570 }
571
572 /*
573  * Work out what sort of subrequest the next one will be.
574  */
575 static enum netfs_io_source
576 netfs_rreq_prepare_read(struct netfs_io_request *rreq,
577                         struct netfs_io_subrequest *subreq,
578                         struct iov_iter *io_iter)
579 {
580         enum netfs_io_source source = NETFS_DOWNLOAD_FROM_SERVER;
581         struct netfs_inode *ictx = netfs_inode(rreq->inode);
582         size_t lsize;
583
584         _enter("%llx-%llx,%llx", subreq->start, subreq->start + subreq->len, rreq->i_size);
585
586         if (rreq->origin != NETFS_DIO_READ) {
587                 source = netfs_cache_prepare_read(subreq, rreq->i_size);
588                 if (source == NETFS_INVALID_READ)
589                         goto out;
590         }
591
592         if (source == NETFS_DOWNLOAD_FROM_SERVER) {
593                 /* Call out to the netfs to let it shrink the request to fit
594                  * its own I/O sizes and boundaries.  If it shinks it here, it
595                  * will be called again to make simultaneous calls; if it wants
596                  * to make serial calls, it can indicate a short read and then
597                  * we will call it again.
598                  */
599                 if (rreq->origin != NETFS_DIO_READ) {
600                         if (subreq->start >= ictx->zero_point) {
601                                 source = NETFS_FILL_WITH_ZEROES;
602                                 goto set;
603                         }
604                         if (subreq->len > ictx->zero_point - subreq->start)
605                                 subreq->len = ictx->zero_point - subreq->start;
606
607                         /* We limit buffered reads to the EOF, but let the
608                          * server deal with larger-than-EOF DIO/unbuffered
609                          * reads.
610                          */
611                         if (subreq->len > rreq->i_size - subreq->start)
612                                 subreq->len = rreq->i_size - subreq->start;
613                 }
614                 if (rreq->rsize && subreq->len > rreq->rsize)
615                         subreq->len = rreq->rsize;
616
617                 if (rreq->netfs_ops->clamp_length &&
618                     !rreq->netfs_ops->clamp_length(subreq)) {
619                         source = NETFS_INVALID_READ;
620                         goto out;
621                 }
622
623                 if (subreq->max_nr_segs) {
624                         lsize = netfs_limit_iter(io_iter, 0, subreq->len,
625                                                  subreq->max_nr_segs);
626                         if (subreq->len > lsize) {
627                                 subreq->len = lsize;
628                                 trace_netfs_sreq(subreq, netfs_sreq_trace_limited);
629                         }
630                 }
631         }
632
633 set:
634         if (subreq->len > rreq->len)
635                 pr_warn("R=%08x[%u] SREQ>RREQ %zx > %llx\n",
636                         rreq->debug_id, subreq->debug_index,
637                         subreq->len, rreq->len);
638
639         if (WARN_ON(subreq->len == 0)) {
640                 source = NETFS_INVALID_READ;
641                 goto out;
642         }
643
644         subreq->source = source;
645         trace_netfs_sreq(subreq, netfs_sreq_trace_prepare);
646
647         subreq->io_iter = *io_iter;
648         iov_iter_truncate(&subreq->io_iter, subreq->len);
649         iov_iter_advance(io_iter, subreq->len);
650 out:
651         subreq->source = source;
652         trace_netfs_sreq(subreq, netfs_sreq_trace_prepare);
653         return source;
654 }
655
656 /*
657  * Slice off a piece of a read request and submit an I/O request for it.
658  */
659 static bool netfs_rreq_submit_slice(struct netfs_io_request *rreq,
660                                     struct iov_iter *io_iter)
661 {
662         struct netfs_io_subrequest *subreq;
663         enum netfs_io_source source;
664
665         subreq = netfs_alloc_subrequest(rreq);
666         if (!subreq)
667                 return false;
668
669         subreq->start           = rreq->start + rreq->submitted;
670         subreq->len             = io_iter->count;
671
672         _debug("slice %llx,%zx,%llx", subreq->start, subreq->len, rreq->submitted);
673         list_add_tail(&subreq->rreq_link, &rreq->subrequests);
674
675         /* Call out to the cache to find out what it can do with the remaining
676          * subset.  It tells us in subreq->flags what it decided should be done
677          * and adjusts subreq->len down if the subset crosses a cache boundary.
678          *
679          * Then when we hand the subset, it can choose to take a subset of that
680          * (the starts must coincide), in which case, we go around the loop
681          * again and ask it to download the next piece.
682          */
683         source = netfs_rreq_prepare_read(rreq, subreq, io_iter);
684         if (source == NETFS_INVALID_READ)
685                 goto subreq_failed;
686
687         atomic_inc(&rreq->nr_outstanding);
688
689         rreq->submitted += subreq->len;
690
691         trace_netfs_sreq(subreq, netfs_sreq_trace_submit);
692         switch (source) {
693         case NETFS_FILL_WITH_ZEROES:
694                 netfs_fill_with_zeroes(rreq, subreq);
695                 break;
696         case NETFS_DOWNLOAD_FROM_SERVER:
697                 netfs_read_from_server(rreq, subreq);
698                 break;
699         case NETFS_READ_FROM_CACHE:
700                 netfs_read_from_cache(rreq, subreq, NETFS_READ_HOLE_IGNORE);
701                 break;
702         default:
703                 BUG();
704         }
705
706         return true;
707
708 subreq_failed:
709         rreq->error = subreq->error;
710         netfs_put_subrequest(subreq, false, netfs_sreq_trace_put_failed);
711         return false;
712 }
713
714 /*
715  * Begin the process of reading in a chunk of data, where that data may be
716  * stitched together from multiple sources, including multiple servers and the
717  * local cache.
718  */
719 int netfs_begin_read(struct netfs_io_request *rreq, bool sync)
720 {
721         struct iov_iter io_iter;
722         int ret;
723
724         _enter("R=%x %llx-%llx",
725                rreq->debug_id, rreq->start, rreq->start + rreq->len - 1);
726
727         if (rreq->len == 0) {
728                 pr_err("Zero-sized read [R=%x]\n", rreq->debug_id);
729                 return -EIO;
730         }
731
732         if (rreq->origin == NETFS_DIO_READ)
733                 inode_dio_begin(rreq->inode);
734
735         // TODO: Use bounce buffer if requested
736         rreq->io_iter = rreq->iter;
737
738         INIT_WORK(&rreq->work, netfs_rreq_work);
739
740         /* Chop the read into slices according to what the cache and the netfs
741          * want and submit each one.
742          */
743         netfs_get_request(rreq, netfs_rreq_trace_get_for_outstanding);
744         atomic_set(&rreq->nr_outstanding, 1);
745         io_iter = rreq->io_iter;
746         do {
747                 _debug("submit %llx + %llx >= %llx",
748                        rreq->start, rreq->submitted, rreq->i_size);
749                 if (!netfs_rreq_submit_slice(rreq, &io_iter))
750                         break;
751                 if (test_bit(NETFS_SREQ_NO_PROGRESS, &rreq->flags))
752                         break;
753                 if (test_bit(NETFS_RREQ_BLOCKED, &rreq->flags) &&
754                     test_bit(NETFS_RREQ_NONBLOCK, &rreq->flags))
755                         break;
756
757         } while (rreq->submitted < rreq->len);
758
759         if (!rreq->submitted) {
760                 netfs_put_request(rreq, false, netfs_rreq_trace_put_no_submit);
761                 if (rreq->origin == NETFS_DIO_READ)
762                         inode_dio_end(rreq->inode);
763                 ret = 0;
764                 goto out;
765         }
766
767         if (sync) {
768                 /* Keep nr_outstanding incremented so that the ref always
769                  * belongs to us, and the service code isn't punted off to a
770                  * random thread pool to process.  Note that this might start
771                  * further work, such as writing to the cache.
772                  */
773                 wait_var_event(&rreq->nr_outstanding,
774                                atomic_read(&rreq->nr_outstanding) == 1);
775                 if (atomic_dec_and_test(&rreq->nr_outstanding))
776                         netfs_rreq_assess(rreq, false);
777
778                 trace_netfs_rreq(rreq, netfs_rreq_trace_wait_ip);
779                 wait_on_bit(&rreq->flags, NETFS_RREQ_IN_PROGRESS,
780                             TASK_UNINTERRUPTIBLE);
781
782                 ret = rreq->error;
783                 if (ret == 0 && rreq->submitted < rreq->len &&
784                     rreq->origin != NETFS_DIO_READ) {
785                         trace_netfs_failure(rreq, NULL, ret, netfs_fail_short_read);
786                         ret = -EIO;
787                 }
788         } else {
789                 /* If we decrement nr_outstanding to 0, the ref belongs to us. */
790                 if (atomic_dec_and_test(&rreq->nr_outstanding))
791                         netfs_rreq_assess(rreq, false);
792                 ret = -EIOCBQUEUED;
793         }
794
795 out:
796         return ret;
797 }
This page took 0.076143 seconds and 4 git commands to generate.