read_retry.c 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /* Network filesystem read subrequest retrying.
  3. *
  4. * Copyright (C) 2024 Red Hat, Inc. All Rights Reserved.
  5. * Written by David Howells (dhowells@redhat.com)
  6. */
  7. #include <linux/fs.h>
  8. #include <linux/slab.h>
  9. #include "internal.h"
  10. static void netfs_reissue_read(struct netfs_io_request *rreq,
  11. struct netfs_io_subrequest *subreq)
  12. {
  13. struct iov_iter *io_iter = &subreq->io_iter;
  14. if (iov_iter_is_folioq(io_iter)) {
  15. subreq->curr_folioq = (struct folio_queue *)io_iter->folioq;
  16. subreq->curr_folioq_slot = io_iter->folioq_slot;
  17. subreq->curr_folio_order = subreq->curr_folioq->orders[subreq->curr_folioq_slot];
  18. }
  19. atomic_inc(&rreq->nr_outstanding);
  20. __set_bit(NETFS_SREQ_IN_PROGRESS, &subreq->flags);
  21. netfs_get_subrequest(subreq, netfs_sreq_trace_get_resubmit);
  22. subreq->rreq->netfs_ops->issue_read(subreq);
  23. }
  24. /*
  25. * Go through the list of failed/short reads, retrying all retryable ones. We
  26. * need to switch failed cache reads to network downloads.
  27. */
  28. static void netfs_retry_read_subrequests(struct netfs_io_request *rreq)
  29. {
  30. struct netfs_io_subrequest *subreq;
  31. struct netfs_io_stream *stream0 = &rreq->io_streams[0];
  32. LIST_HEAD(sublist);
  33. LIST_HEAD(queue);
  34. _enter("R=%x", rreq->debug_id);
  35. if (list_empty(&rreq->subrequests))
  36. return;
  37. if (rreq->netfs_ops->retry_request)
  38. rreq->netfs_ops->retry_request(rreq, NULL);
  39. /* If there's no renegotiation to do, just resend each retryable subreq
  40. * up to the first permanently failed one.
  41. */
  42. if (!rreq->netfs_ops->prepare_read &&
  43. !rreq->cache_resources.ops) {
  44. struct netfs_io_subrequest *subreq;
  45. list_for_each_entry(subreq, &rreq->subrequests, rreq_link) {
  46. if (test_bit(NETFS_SREQ_FAILED, &subreq->flags))
  47. break;
  48. if (__test_and_clear_bit(NETFS_SREQ_NEED_RETRY, &subreq->flags)) {
  49. netfs_reset_iter(subreq);
  50. netfs_reissue_read(rreq, subreq);
  51. }
  52. }
  53. return;
  54. }
  55. /* Okay, we need to renegotiate all the download requests and flip any
  56. * failed cache reads over to being download requests and negotiate
  57. * those also. All fully successful subreqs have been removed from the
  58. * list and any spare data from those has been donated.
  59. *
  60. * What we do is decant the list and rebuild it one subreq at a time so
  61. * that we don't end up with donations jumping over a gap we're busy
  62. * populating with smaller subrequests. In the event that the subreq
  63. * we just launched finishes before we insert the next subreq, it'll
  64. * fill in rreq->prev_donated instead.
  65. * Note: Alternatively, we could split the tail subrequest right before
  66. * we reissue it and fix up the donations under lock.
  67. */
  68. list_splice_init(&rreq->subrequests, &queue);
  69. do {
  70. struct netfs_io_subrequest *from;
  71. struct iov_iter source;
  72. unsigned long long start, len;
  73. size_t part, deferred_next_donated = 0;
  74. bool boundary = false;
  75. /* Go through the subreqs and find the next span of contiguous
  76. * buffer that we then rejig (cifs, for example, needs the
  77. * rsize renegotiating) and reissue.
  78. */
  79. from = list_first_entry(&queue, struct netfs_io_subrequest, rreq_link);
  80. list_move_tail(&from->rreq_link, &sublist);
  81. start = from->start + from->transferred;
  82. len = from->len - from->transferred;
  83. _debug("from R=%08x[%x] s=%llx ctl=%zx/%zx/%zx",
  84. rreq->debug_id, from->debug_index,
  85. from->start, from->consumed, from->transferred, from->len);
  86. if (test_bit(NETFS_SREQ_FAILED, &from->flags) ||
  87. !test_bit(NETFS_SREQ_NEED_RETRY, &from->flags))
  88. goto abandon;
  89. deferred_next_donated = from->next_donated;
  90. while ((subreq = list_first_entry_or_null(
  91. &queue, struct netfs_io_subrequest, rreq_link))) {
  92. if (subreq->start != start + len ||
  93. subreq->transferred > 0 ||
  94. !test_bit(NETFS_SREQ_NEED_RETRY, &subreq->flags))
  95. break;
  96. list_move_tail(&subreq->rreq_link, &sublist);
  97. len += subreq->len;
  98. deferred_next_donated = subreq->next_donated;
  99. if (test_bit(NETFS_SREQ_BOUNDARY, &subreq->flags))
  100. break;
  101. }
  102. _debug(" - range: %llx-%llx %llx", start, start + len - 1, len);
  103. /* Determine the set of buffers we're going to use. Each
  104. * subreq gets a subset of a single overall contiguous buffer.
  105. */
  106. netfs_reset_iter(from);
  107. source = from->io_iter;
  108. source.count = len;
  109. /* Work through the sublist. */
  110. while ((subreq = list_first_entry_or_null(
  111. &sublist, struct netfs_io_subrequest, rreq_link))) {
  112. list_del(&subreq->rreq_link);
  113. subreq->source = NETFS_DOWNLOAD_FROM_SERVER;
  114. subreq->start = start - subreq->transferred;
  115. subreq->len = len + subreq->transferred;
  116. stream0->sreq_max_len = subreq->len;
  117. __clear_bit(NETFS_SREQ_NEED_RETRY, &subreq->flags);
  118. __set_bit(NETFS_SREQ_RETRYING, &subreq->flags);
  119. spin_lock_bh(&rreq->lock);
  120. list_add_tail(&subreq->rreq_link, &rreq->subrequests);
  121. subreq->prev_donated += rreq->prev_donated;
  122. rreq->prev_donated = 0;
  123. trace_netfs_sreq(subreq, netfs_sreq_trace_retry);
  124. spin_unlock_bh(&rreq->lock);
  125. BUG_ON(!len);
  126. /* Renegotiate max_len (rsize) */
  127. if (rreq->netfs_ops->prepare_read &&
  128. rreq->netfs_ops->prepare_read(subreq) < 0) {
  129. trace_netfs_sreq(subreq, netfs_sreq_trace_reprep_failed);
  130. __set_bit(NETFS_SREQ_FAILED, &subreq->flags);
  131. }
  132. part = umin(len, stream0->sreq_max_len);
  133. if (unlikely(rreq->io_streams[0].sreq_max_segs))
  134. part = netfs_limit_iter(&source, 0, part, stream0->sreq_max_segs);
  135. subreq->len = subreq->transferred + part;
  136. subreq->io_iter = source;
  137. iov_iter_truncate(&subreq->io_iter, part);
  138. iov_iter_advance(&source, part);
  139. len -= part;
  140. start += part;
  141. if (!len) {
  142. if (boundary)
  143. __set_bit(NETFS_SREQ_BOUNDARY, &subreq->flags);
  144. subreq->next_donated = deferred_next_donated;
  145. } else {
  146. __clear_bit(NETFS_SREQ_BOUNDARY, &subreq->flags);
  147. subreq->next_donated = 0;
  148. }
  149. netfs_reissue_read(rreq, subreq);
  150. if (!len)
  151. break;
  152. /* If we ran out of subrequests, allocate another. */
  153. if (list_empty(&sublist)) {
  154. subreq = netfs_alloc_subrequest(rreq);
  155. if (!subreq)
  156. goto abandon;
  157. subreq->source = NETFS_DOWNLOAD_FROM_SERVER;
  158. subreq->start = start;
  159. /* We get two refs, but need just one. */
  160. netfs_put_subrequest(subreq, false, netfs_sreq_trace_new);
  161. trace_netfs_sreq(subreq, netfs_sreq_trace_split);
  162. list_add_tail(&subreq->rreq_link, &sublist);
  163. }
  164. }
  165. /* If we managed to use fewer subreqs, we can discard the
  166. * excess.
  167. */
  168. while ((subreq = list_first_entry_or_null(
  169. &sublist, struct netfs_io_subrequest, rreq_link))) {
  170. trace_netfs_sreq(subreq, netfs_sreq_trace_discard);
  171. list_del(&subreq->rreq_link);
  172. netfs_put_subrequest(subreq, false, netfs_sreq_trace_put_done);
  173. }
  174. } while (!list_empty(&queue));
  175. return;
  176. /* If we hit ENOMEM, fail all remaining subrequests */
  177. abandon:
  178. list_splice_init(&sublist, &queue);
  179. list_for_each_entry(subreq, &queue, rreq_link) {
  180. if (!subreq->error)
  181. subreq->error = -ENOMEM;
  182. __clear_bit(NETFS_SREQ_FAILED, &subreq->flags);
  183. __clear_bit(NETFS_SREQ_NEED_RETRY, &subreq->flags);
  184. __clear_bit(NETFS_SREQ_RETRYING, &subreq->flags);
  185. }
  186. spin_lock_bh(&rreq->lock);
  187. list_splice_tail_init(&queue, &rreq->subrequests);
  188. spin_unlock_bh(&rreq->lock);
  189. }
  190. /*
  191. * Retry reads.
  192. */
  193. void netfs_retry_reads(struct netfs_io_request *rreq)
  194. {
  195. trace_netfs_rreq(rreq, netfs_rreq_trace_resubmit);
  196. atomic_inc(&rreq->nr_outstanding);
  197. netfs_retry_read_subrequests(rreq);
  198. if (atomic_dec_and_test(&rreq->nr_outstanding))
  199. netfs_rreq_terminated(rreq, false);
  200. }
  201. /*
  202. * Unlock any the pages that haven't been unlocked yet due to abandoned
  203. * subrequests.
  204. */
  205. void netfs_unlock_abandoned_read_pages(struct netfs_io_request *rreq)
  206. {
  207. struct folio_queue *p;
  208. for (p = rreq->buffer; p; p = p->next) {
  209. for (int slot = 0; slot < folioq_count(p); slot++) {
  210. struct folio *folio = folioq_folio(p, slot);
  211. if (folio && !folioq_is_marked2(p, slot)) {
  212. trace_netfs_folio(folio, netfs_folio_trace_abandon);
  213. folio_unlock(folio);
  214. }
  215. }
  216. }
  217. }